nix-community / nixvim

Configure Neovim with Nix! [maintainer=@GaetanLepage, @traxys, @mattsturgeon]
https://nix-community.github.io/nixvim
MIT License
1.39k stars 210 forks source link

Using autocomplete from html LSP #1414

Open b-m-f opened 2 months ago

b-m-f commented 2 months ago

According to https://www.andersevenrud.net/neovim.github.io/lsp/configurations/html/ it is necessary to setup up the vscode-langservers-extracted with

--Enable (broadcasting) snippet capability for completion
local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities.textDocument.completion.completionItem.snippetSupport = true

require'lspconfig'.html.setup {
  capabilities = capabilities,
}

In order to get auto-completion working for HTML.

Is there a possibility to add an option for this on https://nix-community.github.io/nixvim/plugins/lsp/servers/html/index.html .

Maybe there is also another way to do this in my user config? Unfortunately I have not been able to get this properly setup.

GaetanLepage commented 2 months ago

The following should work:

extraConfigLuaPre = ''
  local hmtl_capabilities = vim.lsp.protocol.make_client_capabilities()
  html_capabilities.textDocument.completion.completionItem.snippetSupport = true
'';

lsp = {
  enable = true;
  servers.html = {
    enable = true;
    extraOptions.capabilities.__raw = "html_capabilities";
  };
};

Could you please try it and tell me if it works ?

b-m-f commented 2 months ago

Hi, thanks for the quick response!

Unfortunately it still does not seem to work. Maybe it has something to do with the luasnip setup mentioned in lspconfig.

Here is where I currently at:

{ pkgs, ... }:
let
in
{
  home.packages = [
    pkgs.go
    pkgs.gcc
    pkgs.gnumake
    pkgs.cmake
    pkgs.lldb
    pkgs.nodejs
    pkgs.rustc
    pkgs.cargo
    pkgs.rustfmt
    pkgs.xsel
  ];

  programs.nixvim = {
    enable = true;
    extraPlugins = [
    ];
    extraPackages = [
    ];
    extraConfigLuaPre = ''
      require ("luasnip.loaders.from_vscode").lazy_load()
      local html_capabilities = vim.lsp.protocol.make_client_capabilities()
      html_capabilities.textDocument.completion.completionItem.snippetSupport = true
    '';
    opts = {
      number = true; # Show line numbers
      relativenumber = false; # Show relative line numbers
      shiftwidth = 2; # tab creates 2 spaces
      showcmd = true;
    };
    globals = {
      mapleader = " ";
      tmux_navigator_no_mappings = 1;
    };
    extraFiles = {
      "lua/luasnip-helpers.lua" = (builtins.readFile ./neovim/snippets/helpers.lua);
      "lua/nvim-tree-functions.lua" = (builtins.readFile ./neovim/lua/nvim-tree.lua);
    };
    colorschemes.nord.enable = true;
    # check https://github.com/nix-community/nixvim/blob/main/lib/keymap-helpers.nix#
    keymaps = [
      {
        key = ";";
        action = ":";
        options = {
          desc = "Rebind ; to trigger command mode as well";
        };
      }
      {
        key = "c";
        mode = "v";
        action = "<Nop>";
        options = {
          desc = "Removes search highlighting";
        };
      }
      {
        key = "<leader>fo";
        mode = "n";
        action = ":lua vim.lsp.buf.format()<CR>";
        options = {
          silent = true;
          desc = "Format file";
        };
      }
      {
        key = "<leader>p";
        mode = "n";
        action = ":e#<CR>";
        options = {
          silent = true;
          desc = "Go to last file";
        };
      }
      {
        key = "<leader>fb";
        mode = "n";
        action = ":lua require(\"nvim-tree.api\").tree.toggle()<CR>";
        options = {
          silent = true;
          desc = "Toggle nvim-tree";
        };
      }
      {
        key = "<leader>t";
        mode = "n";
        action = ":lua require(\"nvim-tree-functions\").tree_actions_menu()<CR>";
        options = {
          silent = true;
          desc = "Open nvim-tree editing commands";
        };
      }
      {
        key = "<leader>-";
        mode = "n";
        action = ":split<CR>";
        options = {
          silent = true;
          desc = "Split window vertically";
        };
      }
      {
        key = "<leader>\\";
        mode = "n";
        action = ":vsplit<CR>";
        options = {
          silent = true;
          desc = "Split window horizontally";
        };
      }
      {
        key = "<leader>h";
        mode = "n";
        action = "<cmd>wincmd h <cr>";
        options = {
          silent = true;
          desc = "Move into window on the left";
        };
      }
      {
        key = "<leader>j";
        mode = "n";
        action = "<cmd>wincmd j <cr>";
        options = {
          silent = true;
          desc = "Move into window below";
        };
      }
      {
        key = "<leader>k";
        mode = "n";
        action = "<cmd>wincmd k <cr>";
        options = {
          silent = true;
          desc = "Move into window above";
        };
      }
      {
        key = "<leader>l";
        mode = "n";
        action = "<cmd>wincmd l <cr>";
        options = {
          silent = true;
          desc = "Move into window on the right";
        };
      }
      {
        key = "<leader>/";
        mode = "n";
        action = ":noh<CR>";
        options = {
          silent = true;
          desc = "Remove search highlighting";
        };
      }
      {
        key = "<leader>fh";
        mode = "n";
        action = "*N";
        options = {
          silent = true;
          desc = "Hightlight all occurences of word under cursor";
        };
      }
      {
        key = "<leader>lc";
        mode = "n";
        action = ":VimtexCompile <CR>";
        options = {
          silent = true;
          desc = "Start latex compilation";
        };
      }
    ];
    clipboard = {
      register = "unnamedplus";
    };
    plugins = {
      ## Misc
      markdown-preview = {
        enable = true;
      };

      ## Navigation
      which-key = {
        enable = true;
      };
      telescope = {
        enable = true;
        extensions = {
          fzf-native.enable = true;
        };
        keymaps = {
          "<leader>ff" = {
            action = "find_files";
            desc = "Find Files";
          };
          "<leader>fg" = {
            action = "live_grep";
            desc = "Grep in files";
          };
          "/" = {
            action = "current_buffer_fuzzy_find";
            desc = "Find in File";
          };
        };
      };
      nvim-tree = {
        enable = true;
        hijackCursor = true;
        hijackNetrw = true;
        hijackUnnamedBufferWhenOpening = true;
        ignoreBufferOnSetup = true;
        updateFocusedFile.enable = true;
      };
      treesitter = {
        enable = true;
        disabledLanguages = [ "latex" "tex" ];
      };
      illuminate = {
        enable = true;
      };
      todo-comments = {
        enable = true;
        keymaps = {
          todoTelescope = {
            key = "<leader>tf";
          };
        };
      };
      ## Code Editing
      gitgutter.enable = true;
      comment = {
        enable = true;
        settings = {
          toggler = {
            line = "<leader>cc";
            block = "<leader>cb";
          };
          opleader = {
            line = "<leader>cc";
            block = "<leader>cb";
          };

        };
      };
      nvim-autopairs = {
        enable = true;
      };
      surround = {
        enable = true;
      };
      luasnip = {
        enable = true;
        extraConfig = {
          update_events = "TextChanged,TextChangedI";
          enable_autosnippets = true;
          store_selection_keys = "<Tab>";
        };
        fromLua = [{ paths = ./neovim/snippets/lua; }];
      };
      cmp = {
        enable = true;
    autoEnableSources = false;
        settings = {
          completion = {
            autocomplete = [ "require('cmp.types').cmp.TriggerEvent.TextChanged" ];
          };
          mapping = {
            __raw = ''
              cmp.mapping.preset.insert({
                ['<C-b>'] = cmp.mapping.scroll_docs(-4),
                ['<C-f>'] = cmp.mapping.scroll_docs(4),
                ['<C-Space>'] = cmp.mapping.complete(),
                ['<C-e>'] = cmp.mapping.abort(),
                ['<CR>'] = cmp.mapping.confirm({ select = true }),
              })
            '';
          };
          sources.__raw = ''
              cmp.config.sources({
                { name = 'nvim_lsp' },
                { name = 'cmdline' },
                { name = 'path' },
                { name = 'luasnip' },
                { name = 'buffer' },
              })
          '';
        };
      };
      cmp-nvim-lsp = {
        enable = true;
      };
      cmp-buffer = {
        enable = true;
      };
      cmp-path = {
        enable = true;
      };
      cmp-cmdline = {
        enable = true;
      };
      cmp_luasnip = {
        enable = true;
      };

      ## Languages
      vimtex = {
        # enable = if pkgs.hostPlatform.config == "aarch64-apple-darwin" then false else true;
        enable = true;
        settings = {
          view_method = "zathura_simple";
        };
      };
      lsp = {
        enable = true;
        servers = {
          rust-analyzer = {
            enable = true;
            settings = {
              checkOnSave = true;
            };
            installRustc = false;
            installCargo = false;
          };
          bashls.enable = true;
          gopls.enable = true;
          jsonls.enable = true;
          nixd = {
            enable = true;
            settings = {
              formatting.command = "nixpkgs-fmt";
            };
          };
          lua-ls = {
            enable = true;
            settings.diagnostics.globals = [
              "t"
              "s"
              "f"
              "i"
              "d"
              "fmta"
              "vim"
              "rep"
            ];

          };
          html = {
            enable = true;
            autostart = true;
            filetypes = [ "html" "htmldjango" ];
            extraOptions.capabilities.__raw = "html_capabilities";
          };

          clangd.enable = true;
          cssls.enable = true;

        };
        keymaps = {
          diagnostic = {
            "gn" = { action = "goto_next"; desc = "Go to next diagnostic warning"; };
            "gb" = { action = "goto_prev"; desc = "Go to previous diagnostic warning"; };
            "<leader>e" = { action = "open_float"; desc = "Open error/warning inside a float"; };
          };
          lspBuf = {
            "<leader>g" = { action = "hover"; desc = "Show info about word under cursor"; };
            gr = { action = "references"; desc = "List all references"; };
            gd = { action = "definition"; desc = "Go to definition"; };
            gi = { action = "implementation"; desc = "List all implementations"; };
            gt = { action = "type_definition"; desc = "Jump to type definition"; };
          };
        };
      };
      crates-nvim.enable = true;
    };
  };
}

Since I could not find a better place I am also loading the snippets in extraConfigLuaPre.