Skip to content

Commit

Permalink
feat: implement inlay virtual text for rust and go (#759)
Browse files Browse the repository at this point in the history
* feat: implement inlay virtual text for rust and go

Signed-off-by: 蔡略 <[email protected]>

* fix: go-nvim use lsp-inlayhints.nvim only

Signed-off-by: 蔡略 <[email protected]>

* refactor: move inlay-hints to `completion`

Signed-off-by: 蔡略 <[email protected]>

* feat: lua support

Signed-off-by: 蔡略 <[email protected]>

* chore(inlay-hints): remove `sumneko_lua`

* fix: set inlay-hints debug mode off

Signed-off-by: 蔡略 <[email protected]>

---------

Signed-off-by: 蔡略 <[email protected]>
Co-authored-by: Charles Chiu <[email protected]>
  • Loading branch information
ClSlaid and CharlesChiuGit authored May 23, 2023
1 parent abaa7c5 commit b434c79
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 13 deletions.
6 changes: 4 additions & 2 deletions lazy-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"LuaSnip": { "branch": "master", "commit": "fcdaa4313305fe20d928551134c1ec2266e7af2a" },
"accelerated-jk.nvim": { "branch": "main", "commit": "8fb5dad4ccc1811766cebf16b544038aeeb7806f" },
"alpha-nvim": { "branch": "main", "commit": "1838ae926e8d49fe5330d1498ee8289ae2c340bc" },
"auto-session": { "branch": "main", "commit": "571ecb873654554109f63eac3193b133aec2f90c" },
"auto-session": { "branch": "main", "commit": "a2ca98ceec8b62ae955159ef167b15dd4f28185b" },
"autoclose.nvim": { "branch": "main", "commit": "c4db42ffc0edbd244502be951c142df0c8a7e582" },
"better-escape.nvim": { "branch": "master", "commit": "7031dc734add47bb71c010e0551829fa5799375f" },
"bigfile.nvim": { "branch": "main", "commit": "a34e42616c20bfd52943ed5d6632bb28d22b057e" },
Expand All @@ -30,9 +30,12 @@
"friendly-snippets": { "branch": "main", "commit": "ef6547d2f586e08e071efeebac835e545f3015cc" },
"fzy-lua-native": { "branch": "master", "commit": "45148b3f70d244e8b8b5db60df447a2b7ac2de9d" },
"gitsigns.nvim": { "branch": "main", "commit": "c18b7ca0b5b50596722f3a1572eb9b8eb520c0f1" },
"go.nvim": { "branch": "master", "commit": "1a2530d3e9b29e9e1279a67d27304a05663537fd" },
"guihua.lua": { "branch": "master", "commit": "ab8b1f09603cc268770efd057115035dc6cfa83d" },
"hop.nvim": { "branch": "v2", "commit": "90db1b2c61b820e230599a04fedcd2679e64bd07" },
"indent-blankline.nvim": { "branch": "master", "commit": "86d1b71c5c26168c3a3a9ff5f69e833889a09c1d" },
"lazy.nvim": { "branch": "main", "commit": "6610b15dfd76f7992423916e2b87f031881d7b25" },
"lsp-inlayhints.nvim": { "branch": "anticonceal", "commit": "48b76c3c761fffc12ec2a94db7b371b6304e2067" },
"lsp_signature.nvim": { "branch": "master", "commit": "7a26ebaa7e36aa2aefa6c1994b2b866c96de32e6" },
"lspsaga.nvim": { "branch": "main", "commit": "01b9633aefd010f272d6c7e3d8293c44fcfe7696" },
"lualine.nvim": { "branch": "master", "commit": "05d78e9fd0cdfb4545974a5aa14b1be95a86e9c9" },
Expand Down Expand Up @@ -82,7 +85,6 @@
"vim-cool": { "branch": "master", "commit": "80536b9f2e23292708a64f2e7bcf5e596f9faf24" },
"vim-easy-align": { "branch": "master", "commit": "12dd6316974f71ce333e360c0260b4e1f81169c3" },
"vim-fugitive": { "branch": "master", "commit": "5f0d280b517cacb16f59316659966c7ca5e2bea2" },
"vim-go": { "branch": "master", "commit": "2a874910a242fd4a5da021ea32fb1cde3b69f79b" },
"vim-illuminate": { "branch": "master", "commit": "a2907275a6899c570d16e95b9db5fd921c167502" },
"vim-matchup": { "branch": "master", "commit": "b8eca3b588e41e0bb1b3ae200fae88183b91a76d" },
"which-key.nvim": { "branch": "main", "commit": "e271c28118998c93a14d189af3395812a1aa646c" },
Expand Down
3 changes: 3 additions & 0 deletions lua/core/event.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ vim.api.nvim_create_autocmd("LspAttach", {
group = vim.api.nvim_create_augroup("UserLspConfig", {}),
callback = function(event)
mapping.lsp(event.buf)
local inlay_hints = require("lsp-inlayhints")
local client = vim.lsp.get_client_by_id(event.data.client_id)
inlay_hints.on_attach(client, event.buf)
end,
})

Expand Down
47 changes: 47 additions & 0 deletions lua/modules/configs/completion/inlay-hints.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
return function()
-- listed below are the default values
local override = {
inlay_hints = {
parameter_hints = {
show = true,
},
type_hints = {
show = true,
},
label_formatter = function(tbl, kind, opts, client_name)
if kind == 2 and not opts.parameter_hints.show then
return ""
elseif not opts.type_hints.show then
return ""
end

return table.concat(tbl, ", ")
end,
virt_text_formatter = function(label, hint, opts, client_name)
if client_name == "lua_ls" then
if hint.kind == 2 then
hint.paddingLeft = false
else
hint.paddingRight = false
end
end

local vt = {}
vt[#vt + 1] = hint.paddingLeft and { " ", "None" } or nil
vt[#vt + 1] = { label, opts.highlight }
vt[#vt + 1] = hint.paddingRight and { " ", "None" } or nil

return vt
end,
only_current_line = false,
-- highlight group
highlight = "LspInlayHint",
-- highlight = "Comment",
-- virt_text priority
priority = 0,
},
enabled_at_startup = true,
debug_mode = false,
}
require("lsp-inlayhints").setup(override)
end
4 changes: 4 additions & 0 deletions lua/modules/configs/completion/servers/lua_ls.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
return {
settings = {
Lua = {
runtime = {
version = "LuaJIT",
},
diagnostics = {
globals = { "vim" },
disable = { "different-requires" },
Expand All @@ -14,6 +17,7 @@ return {
maxPreload = 100000,
preloadFileSize = 10000,
},
hint = { enable = true, setType = true },
format = { enable = false },
telemetry = { enable = false },
-- Do not override treesitter lua highlighting with lua_ls's highlighting
Expand Down
36 changes: 36 additions & 0 deletions lua/modules/configs/lang/go-nvim.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
return function()
require("go").setup({
lsp_keymaps = false,
dap_debug_keymap = false,
icons = false,
gofmt = "gopls",
goimport = "gopls",
lsp_gofumpt = "true",
lsp_inlay_hints = { enable = false },
run_in_floaterm = true,
trouble = true,
lsp_cfg = {
flags = { debounce_text_changes = 500 },
cmd = { "gopls", "-remote=auto" },
settings = {
gopls = {
usePlaceholders = true,
analyses = {
nilness = true,
shadow = true,
unusedparams = true,
unusewrites = true,
},
hints = {
assignVariableTypes = true,
compositeLiteralFields = true,
constantValues = true,
functionTypeParameters = true,
parameterNames = true,
rangeVariableTypes = true,
},
},
},
},
})
end
4 changes: 2 additions & 2 deletions lua/modules/configs/lang/rust-tools.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ return function()
inlay_hints = {
-- automatically set inlay hints (type hints)
-- default: true
auto = true,
auto = false,

-- Only show inlay hints for the current line
only_current_line = false,
Expand All @@ -46,7 +46,7 @@ return function()
-- default: "=>"
other_hints_prefix = "=> ",

-- whether to align to the lenght of the longest line in the file
-- whether to align to the length of the longest line in the file
max_len_align = false,

-- padding from the left if max_len_align is true
Expand Down
5 changes: 0 additions & 5 deletions lua/modules/configs/lang/vim-go.lua

This file was deleted.

7 changes: 7 additions & 0 deletions lua/modules/plugins/completion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ completion["neovim/nvim-lspconfig"] = {
"nvimdev/lspsaga.nvim",
config = require("completion.lspsaga"),
},
{
"lvimuser/lsp-inlayhints.nvim",
config = require("completion.inlay-hints"),
lazy = true,
branch = "anticonceal",
event = { "LspAttach" },
},
},
}
completion["jose-elias-alvarez/null-ls.nvim"] = {
Expand Down
14 changes: 10 additions & 4 deletions lua/modules/plugins/lang.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
local lang = {}

lang["fatih/vim-go"] = {
lang["ray-x/go.nvim"] = {
lazy = true,
ft = "go",
build = ":GoInstallBinaries",
config = require("lang.vim-go"),
dependencies = { -- optional packages
"ray-x/guihua.lua",
"neovim/nvim-lspconfig",
"nvim-treesitter/nvim-treesitter",
},
ft = { "go", "gomod" },
event = { "CmdlineEnter" },
build = ':lua require("go.install").update_all_sync()', -- if you need to install/update all binaries
config = require("lang.go-nvim"),
}
lang["simrat39/rust-tools.nvim"] = {
lazy = true,
Expand Down

0 comments on commit b434c79

Please sign in to comment.