Skip to content

Commit

Permalink
docs: optimize the neovim testing environment (#449)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruixi-rebirth authored Apr 23, 2024
1 parent 25ef401 commit a8d7322
Show file tree
Hide file tree
Showing 5 changed files with 279 additions and 56 deletions.
9 changes: 9 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@
pkgs.nixpkgs-fmt
(import ./nixd/docs/editors/nvim-lsp.nix { inherit pkgs; })
];
shellHook = ''
echo -e "\033[1;34mEntering the nvim test environment...\033[0m"
mkdir -p /tmp/NixOS_Home-Manager
cp -r ./nixd/docs/examples/NixOS_Home-Manager/* /tmp/NixOS_Home-Manager/
cd /tmp/NixOS_Home-Manager
echo -e "\033[1;32mNow, you can edit the nix file by running the following command:\033[0m"
echo -e "\033[1;33m'nvim-lsp flake.nix'\033[0m"
echo -e "\033[1;34mEnvironment setup complete.\033[0m"
'';
};
devShells.vscodium = pkgs.mkShell {
nativeBuildInputs = [
Expand Down
16 changes: 2 additions & 14 deletions nixd/docs/editors/editors.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

### VSCodium

We provide a test environment with the *vscode-nix-ide* plugin, and the repository has some [configuration examples](/nixd/docs/examples) that you can try.
We provide a test environment with the _vscode-nix-ide_ plugin, and the repository has some [configuration examples](/nixd/docs/examples) that you can try.

Start up the test environment:

Expand All @@ -14,20 +14,8 @@ $ codium-test

### Neovim

You can run the following command to edit a *.nix file

```console
$ nix develop github:nix-community/nixd#nvim

$ nvim-lsp /tmp/test/default.nix

```

tip: If you want to configure lsp itself, see [configuration](/nixd/docs/user-guide.md#configuration), and the following tree-like directory

```console
# tree -a /tmp/test
/tmp/test/
├── default.nix
└── .nixd.json
$ nvim-lsp flake.nix
```
218 changes: 176 additions & 42 deletions nixd/docs/editors/nvim-lsp.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ let
packages.myPlugins.start = with pkgs.vimPlugins; [
(nvim-treesitter.withPlugins (parsers: [
parsers.nix
parsers.json
]))
friendly-snippets
luasnip
Expand All @@ -20,8 +19,14 @@ let
cmp-buffer
cmp_luasnip
cmp-path
null-ls-nvim
cmp-cmdline
none-ls-nvim
nvim-lspconfig
nord-nvim
noice-nvim
lualine-nvim
bufferline-nvim
lspsaga-nvim
];
};
};
Expand Down Expand Up @@ -59,6 +64,62 @@ let
vim.opt[k] = v
end
----------------
-- nord theme --
----------------
vim.g.nord_contrast = false
vim.g.nord_borders = true
vim.g.nord_disable_background = false
vim.g.nord_italic = true
vim.g.nord_uniform_diff_background = true
vim.g.nord_enable_sidebar_background = true
vim.g.nord_bold = true
vim.g.nord_cursorline_transparent = false
require("nord").set()
-----------------
-- About noice --
-----------------
require("noice").setup({
routes = {
{
filter = {
event = "msg_show",
any = {
{ find = "%d+L, %d+B" },
{ find = "; after #%d+" },
{ find = "; before #%d+" },
{ find = "%d fewer lines" },
{ find = "%d more lines" },
},
},
opts = { skip = true },
},
},
})
-------------------
-- About lualine --
-------------------
require("lualine").setup({
options = {
theme = "auto",
globalstatus = true,
},
})
----------------------
-- About bufferline --
----------------------
local highlights
highlights = require("nord").bufferline.highlights({
italic = true,
bold = true,
})
require("bufferline").setup({
highlights = highlights,
})
----------------------
-- About treesitter --
----------------------
Expand Down Expand Up @@ -181,9 +242,17 @@ let
native_menu = false,
},
})
cmp.setup.cmdline(":", {
mapping = cmp.mapping.preset.cmdline(),
sources = cmp.config.sources({
{ name = "path" },
}, {
{ name = "cmdline" },
}),
})
-------------------
-- About null-ls --
-- About none-ls --
-------------------
-- format(async)
local async_formatting = function(bufnr)
Expand Down Expand Up @@ -291,48 +360,113 @@ let
})
end
nvim_lsp.nixd.setup({
on_attach = on_attach(),
capabilities = capabilities,
on_attach = on_attach(),
capabilities = capabilities,
settings = {
nixd = {
nixpkgs = {
expr = "import <nixpkgs> { }",
},
formatting = {
command = { "nixpkgs-fmt" },
},
options = {
nixos = {
expr = '(builtins.getFlake "/tmp/NixOS_Home-Manager").nixosConfigurations.hostname.options',
},
home_manager = {
expr = '(builtins.getFlake "/tmp/NixOS_Home-Manager").homeConfigurations."user@hostname".options',
},
},
},
},
})
-- Global mappings.
-- See `:help vim.diagnostic.*` for documentation on any of the below functions
vim.keymap.set("n", "<space>e", vim.diagnostic.open_float)
vim.keymap.set("n", "[d", vim.diagnostic.goto_prev)
vim.keymap.set("n", "]d", vim.diagnostic.goto_next)
vim.keymap.set("n", "<space>q", vim.diagnostic.setloclist)
-- Use LspAttach autocommand to only map the following keys
-- after the language server attaches to the current buffer
vim.api.nvim_create_autocmd("LspAttach", {
group = vim.api.nvim_create_augroup("UserLspConfig", {}),
callback = function(ev)
-- Manual, triggered completion is provided by Nvim's builtin omnifunc. For autocompletion, a general purpose autocompletion plugin(.i.e nvim-cmp) is required
-- Enable completion triggered by <c-x><c-o>
vim.bo[ev.buf].omnifunc = "v:lua.vim.lsp.omnifunc"
-- Buffer local mappings.
-- See `:help vim.lsp.*` for documentation on any of the below functions
local opts = { buffer = ev.buf }
vim.keymap.set("n", "gD", vim.lsp.buf.declaration, opts)
vim.keymap.set("n", "gd", vim.lsp.buf.definition, opts)
vim.keymap.set("n", "K", vim.lsp.buf.hover, opts)
vim.keymap.set("n", "gi", vim.lsp.buf.implementation, opts)
vim.keymap.set("n", "<C-k>", vim.lsp.buf.signature_help, opts)
vim.keymap.set("n", "<space>wa", vim.lsp.buf.add_workspace_folder, opts)
vim.keymap.set("n", "<space>wr", vim.lsp.buf.remove_workspace_folder, opts)
vim.keymap.set("n", "<space>wl", function()
print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
end, opts)
vim.keymap.set("n", "<space>D", vim.lsp.buf.type_definition, opts)
vim.keymap.set("n", "<space>rn", vim.lsp.buf.rename, opts)
vim.keymap.set({ "n", "v" }, "<space>ca", vim.lsp.buf.code_action, opts)
vim.keymap.set("n", "gr", vim.lsp.buf.references, opts)
vim.keymap.set("n", "<space>f", function()
vim.lsp.buf.format({ async = true })
end, opts)
end,
-------------------
-- About lspsaga --
-------------------
local colors, kind
colors = { normal_bg = "#3b4252" }
require("lspsaga").setup({
ui = {
colors = colors,
kind = kind,
border = "single",
},
outline = {
win_width = 25,
},
})
vim.cmd([[ colorscheme nord ]])
local keymap = vim.keymap.set
-- Lsp finder
-- Find the symbol definition, implementation, reference.
-- If there is no implementation, it will hide.
-- When you use action in finder like open, vsplit, then you can use <C-t> to jump back.
keymap("n", "gh", "<cmd>Lspsaga lsp_finder<CR>", { silent = true, desc = "Lsp finder" })
-- Code action
keymap("n", "<leader>ca", "<cmd>Lspsaga code_action<CR>", { silent = true, desc = "Code action" })
keymap("v", "<leader>ca", "<cmd>Lspsaga code_action<CR>", { silent = true, desc = "Code action" })
-- Rename
keymap("n", "gr", "<cmd>Lspsaga rename<CR>", { silent = true, desc = "Rename" })
-- Rename word in whole project
keymap("n", "gr", "<cmd>Lspsaga rename ++project<CR>", { silent = true, desc = "Rename in project" })
-- Peek definition
keymap("n", "gD", "<cmd>Lspsaga peek_definition<CR>", { silent = true, desc = "Peek definition" })
-- Go to definition
keymap("n", "gd", "<cmd>Lspsaga goto_definition<CR>", { silent = true, desc = "Go to definition" })
-- Show line diagnostics
keymap("n", "<leader>sl", "<cmd>Lspsaga show_line_diagnostics<CR>", { silent = true, desc = "Show line diagnostics" })
-- Show cursor diagnostics
keymap(
"n",
"<leader>sc",
"<cmd>Lspsaga show_cursor_diagnostics<CR>",
{ silent = true, desc = "Show cursor diagnostic" }
)
-- Show buffer diagnostics
keymap("n", "<leader>sb", "<cmd>Lspsaga show_buf_diagnostics<CR>", { silent = true, desc = "Show buffer diagnostic" })
-- Diagnostic jump prev
keymap("n", "[e", "<cmd>Lspsaga diagnostic_jump_prev<CR>", { silent = true, desc = "Diagnostic jump prev" })
-- Diagnostic jump next
keymap("n", "]e", "<cmd>Lspsaga diagnostic_jump_next<CR>", { silent = true, desc = "Diagnostic jump next" })
-- Goto prev error
keymap("n", "[E", function()
require("lspsaga.diagnostic"):goto_prev({ severity = vim.diagnostic.severity.ERROR })
end, { silent = true, desc = "Goto prev error" })
-- Goto next error
keymap("n", "]E", function()
require("lspsaga.diagnostic"):goto_next({ severity = vim.diagnostic.severity.ERROR })
end, { silent = true, desc = "Goto next error" })
-- Toggle outline
keymap("n", "ss", "<cmd>Lspsaga outline<CR>", { silent = true, desc = "Toggle outline" })
-- Hover doc
keymap("n", "K", "<cmd>Lspsaga hover_doc ++keep<CR>", { silent = true, desc = "Hover doc" })
-- Incoming calls
keymap("n", "<Leader>ci", "<cmd>Lspsaga incoming_calls<CR>", { silent = true, desc = "Incoming calls" })
-- Outgoing calls
keymap("n", "<Leader>co", "<cmd>Lspsaga outgoing_calls<CR>", { silent = true, desc = "Outgoing calls" })
-- Float terminal
keymap("n", "<A-d>", "<cmd>Lspsaga term_toggle<CR>", { silent = true, desc = "Float terminal" })
keymap("t", "<A-d>", "<cmd>Lspsaga term_toggle<CR>", { silent = true, desc = "Float terminal" })
'';

in
Expand Down
48 changes: 48 additions & 0 deletions nixd/docs/examples/NixOS_Home-Manager/flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions nixd/docs/examples/NixOS_Home-Manager/flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
description = "A simple flake for NixOS and Home Manager";

inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
home-manager = {
url = "github:nix-community/home-manager";
inputs.nixpkgs.follows = "nixpkgs";
};
};

outputs = { self, nixpkgs, home-manager, ... }:
{
nixosConfigurations = {
hostname = nixpkgs.lib.nixosSystem {
system = "x86_64-linux";
modules = [
({ pkgs, ... }: {
networking.hostName = "hostname";
environment.systemPackages = with pkgs; [
nixd
];
})
];
};
};

homeConfigurations = {
"user@hostname" = home-manager.lib.homeManagerConfiguration {
pkgs = nixpkgs.legacyPackages.x86_64-linux;
modules = [
{
home.stateVersion = "24.05";
home.username = "user";
home.homeDirectory = "/home/user";
}
({ pkgs, ... }: {
wayland.windowManager.hyprland.enable = true;
})
];
};
};
};
}

0 comments on commit a8d7322

Please sign in to comment.