imc-trading / svlangserver

MIT License
97 stars 13 forks source link

Support Neovim client with nvim-lspconfig #22

Closed henry-hsieh closed 2 years ago

henry-hsieh commented 2 years ago

This commit add the support for Neovim built-in language server. To support various language servers, Neovim developers created nvim-lspconfig to initialize language servers for users. Unlike Coc.nvim, nvim-lspconfig doesn't provide project LSP configuration file. Therefore, I reuse the syntax of coc-settings.json and use the json parser to extract the settings of the configuration file.

The only modification of svlangserver is Neovim client detection. I choose .nvim as the configuration directory name, and .nvim/lspconfig.json as LSP configuration file.

To test the Neovim LSP client, the several things should be setup:

  1. Install nvim-lspconfig by any Vim plugin manager.
  2. Create {plugin_root_dir}/nvim-lspconfig/lua/lspconfig/server_configurations/svlangserver.lua and paste following code. Note: this step is not required by user after we submit the svlangserver default configuration to nvim-lspconfig.
    
    local util = require 'lspconfig.util'

local bin_name = 'svlangserver' local cmd = { bin_name }

if vim.fn.has 'win32' == 1 then cmd = { 'cmd.exe', '/C', bin_name } end

local function build_index() local params = { command = 'systemverilog.build_index', } vim.lsp.buf.execute_command(params) end

local function report_hierarchy() local params = { command = 'systemverilog.report_hierarchy', arguments = { vim.fn.expand('') }, } vim.lsp.buf.execute_command(params) end

return { default_config = { cmd = cmd, filetypes = { 'verilog', 'systemverilog' }, root_dir = function(fname) return util.root_pattern '.nvim'(fname) or util.find_git_ancestor(fname) or util.path.dirname(fname) end, single_file_support = true, settings = { systemverilog = { includeIndexing = {"*.{v,vh,sv,svh}","*/.{v,vh,sv,svh}"}, }, }, on_init = function(client) local json = "" for line in io.lines(client.config.root_dir .. "/.nvim/lspconfig.json") do json = json .. line end json = vim.json.decode(json) client.config.cmd = {json.languageserver.svlangserver.command} client.config.filetypes = json.languageserver.svlangserver.filetypes client.config.settings = json.languageserver.svlangserver.settings end, }, commands = { SvlangserverBuildIndex = { build_index, description = 'Instructs language server to rerun indexing', }, SvlangserverReportHierarchy = { report_hierarchy, description = 'Generates hierarchy for the given module', }, }, docs = { description = [[ https://github.com/imc-trading/svlangserver

svlangserver, a language server for systemverilog ]], }, }


3. Create .nvim/lspconfig.json
The syntax is identical to coc-settings.json.
4. Add `require'lspconfig'.svlangserver.setup{}` in `$HOME/.config/nvim/init.lua`.
5. Open any source verilog / systemverilog file of the project folder via Neovim.
6. Enter vim command `:LspInfo`. The LSP should be up now.
kkanhere commented 2 years ago

Thanks!

henry-hsieh commented 2 years ago

Great! I'll submit the svlangserver default settings to nvim-lspconfig.

henry-hsieh commented 2 years ago

Great! I'll submit the svlangserver default settings to nvim-lspconfig.

I put the pull request of nvim-lspconfig here.