Skip to content

Commit

Permalink
nix: Update nvim-lspconfig example config
Browse files Browse the repository at this point in the history
- Implement sync scrolling
- Attempt to automatically detect the user's virtual env
- Move nix config code into its own package - simplifying the
  top-level flake.
  • Loading branch information
alcarney committed Sep 14, 2023
1 parent cdb7473 commit 57b3eed
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 101 deletions.
37 changes: 37 additions & 0 deletions docs/lsp/editors/nvim-lspconfig/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
lib,
neovim,
stdenv,
python3Packages,
vimPlugins,
writeShellScriptBin,
writeText,
}:

let
paths = lib.makeBinPath [
neovim
(python3Packages.esbonio.overridePythonAttrs (_: { doCheck = false ;}))
];

pluginList = with vimPlugins; [
nvim-lspconfig
];

plugins = stdenv.mkDerivation {
name = "esbonio-nvim-plugins";
buildCommand = ''
mkdir -p $out/nvim/site/pack/plugins/start/
${lib.concatMapStringsSep "\n" (path: "ln -s ${path} $out/nvim/site/pack/plugins/start/") pluginList}
'';
};

initVim = builtins.readFile ./init.vim;
in

writeShellScriptBin "nvim" ''
export PATH=${paths}:$PATH
export XDG_CONFIG_DIRS=
export XDG_DATA_DIRS=${plugins.outPath}
nvim --clean --cmd 'source ${writeText "init.vim" initVim}' "$@"
''
67 changes: 66 additions & 1 deletion docs/lsp/editors/nvim-lspconfig/init.vim
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
" vim: et ts=2 sw=2
set expandtab
set tabstop=3
set softtabstop=3
Expand All @@ -7,25 +8,88 @@ let mapleader='<space>'

lua << EOF
local lspconfig = require('lspconfig')
local util = require('lspconfig.util')

local keymap_opts = { noremap = true, silent = true}
vim.keymap.set('n', '<space>e', vim.diagnostic.open_float, keymap_opts)
vim.keymap.set('n', '[d', vim.diagnostic.goto_prev, keymap_opts)
vim.keymap.set('n', ']d', vim.diagnostic.goto_next, keymap_opts)
vim.keymap.set('n', '<space>q', vim.diagnostic.setloclist, keymap_opts)

local on_attach = function(client, bufnr)
vim.lsp.set_log_level("info")

local function scroll_view(ev)
local esbonio = vim.lsp.get_active_clients({bufnr = 0, name = "esbonio"})[1]
local view = vim.fn.winsaveview()

local params = { line = view.topline }
esbonio.notify("view/scroll", params)
end

local function preview_file()
local params = {
command = "esbonio.server.previewFile",
arguments = {
{ uri = vim.uri_from_bufnr(0), show = false },
}
}
local result = vim.lsp.buf.execute_command(params)
print(vim.inspect(result))

-- Setup sync scrolling
local augroup = vim.api.nvim_create_augroup("EsbonioSyncScroll", { clear = true })
vim.api.nvim_create_autocmd({"WinScrolled"}, {
callback = scroll_view,
group = augroup,
buffer = 0,
})
end

-- Attempt to find a virtualenv that the server can use to build the docs.

local function find_venv()

-- If there is an active virtual env, use that
if vim.env.VIRTUAL_ENV then
return { vim.env.VIRTUAL_ENV .. "/bin/python" }
end

-- Search within the current git repo to see if we can find a virtual env to use.
local repo = util.find_git_ancestor(vim.fn.getcwd())
if not repo then
return nil
end

local candidates = vim.fs.find("pyvenv.cfg", { path = repo })
if #candidates == 0 then
return nil
end

return { vim.fn.resolve(candidates[1] .. "./../bin/python") }
end

lspconfig.esbonio.setup {
cmd = { 'esbonio' },
init_options = {
server = {
logLevel = 'debug',
completion = {
preferredInsertBehavior = 'insert'
}
}
},
settings = {
esbonio = {
sphinx = {
pythonCommand = find_venv(),
}
}
},
handlers = {
["editor/scroll"] = function(err, result, ctx, config)
vim.cmd('normal '.. result.line .. 'Gzt')
end
},
on_attach = function(client, bufnr)
vim.api.nvim_buf_set_option(bufnr, 'omnifunc', 'v:lua.vim.lsp.omnifunc')

Expand All @@ -35,6 +99,7 @@ lspconfig.esbonio.setup {
vim.keymap.set('n', 'gh', vim.lsp.buf.hover, bufopts)
vim.keymap.set('n', '<leader>ca', vim.lsp.buf.code_action, bufopts)

vim.api.nvim_create_user_command("EsbonioPreviewFile", preview_file, { desc = "Preview file" })
end
}
EOF
86 changes: 18 additions & 68 deletions flake.lock

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

48 changes: 17 additions & 31 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,32 @@

inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
esbonio.url = "path:lib/esbonio";
esbonio.inputs.nixpkgs.follows = "nixpkgs";
utils.url = "github:numtide/flake-utils";

lsp-devtools.url = "github:swyddfa/lsp-devtools";
lsp-devtools.inputs.nixpkgs.follows = "nixpkgs";
lsp-devtools.inputs.utils.follows = "utils";
};

outputs = { self, nixpkgs, esbonio, utils }:
outputs = { self, nixpkgs, lsp-devtools, utils }:

utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs { inherit system ; overlays = [ esbonio.overlays.default ];};

esbonio-lsp = pkgs.python3Packages.esbonio.overridePythonAttrs (_: { doCheck = false ; });
paths = pkgs.lib.makeBinPath [
pkgs.neovim
esbonio-lsp
];
pkgs = import nixpkgs { inherit system ; overlays = [ self.overlays.default ];};

pluginList = with pkgs.vimPlugins; [
nvim-lspconfig
];
plugins = pkgs.stdenv.mkDerivation {
name = "esbonio-nvim-plugins";
buildCommand = ''
mkdir -p $out/nvim/site/pack/plugins/start/
${pkgs.lib.concatMapStringsSep "\n" (path: "ln -s ${path} $out/nvim/site/pack/plugins/start/") pluginList }
'';
};
nvim-lspconfig = pkgs.callPackage ./docs/lsp/editors/nvim-lspconfig {};

initVim = builtins.readFile ./docs/lsp/editors/nvim-lspconfig/init.vim;
neovim = pkgs.writeShellScriptBin "nvim" ''
export PATH=${paths}:$PATH
export XDG_CONFIG_DIRS=
export XDG_DATA_DIRS=${plugins.outPath}
nvim --clean --cmd 'source ${pkgs.writeText "init.vim" initVim}' "$@"
'';
in {
apps.default = {
apps.nvim-lspconfig = {
type = "app";
program = "${neovim}/bin/nvim";
program = "${nvim-lspconfig}/bin/nvim";
};
}
);

}) // {
overlays.default = self: super:
nixpkgs.lib.composeManyExtensions [
lsp-devtools.overlays.default
(import ./lib/esbonio/nix/esbonio-overlay.nix)
] self super;
};
}
2 changes: 1 addition & 1 deletion lib/esbonio/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ dependencies = ["platformdirs", "docutils", "pygls>=1.0.0", "websockets"]
"Source Code" = "https://github.com/swyddfa/esbonio"

[project.scripts]
esbonio = "esbonio.server:main"
esbonio = "esbonio.server.cli:main"
esbonio-sphinx = "esbonio.lsp.sphinx.cli:main"

[project.optional-dependencies]
Expand Down

0 comments on commit 57b3eed

Please sign in to comment.