From e1316abe754b98991ad1c28e76129d99634a62b3 Mon Sep 17 00:00:00 2001 From: LordMZTE Date: Thu, 18 Jan 2024 18:23:20 +0100 Subject: [PATCH] mzte-nv: enable inlay hints --- mzte-nv/conf/lua/pluginconf/p-lspconf.fnl | 5 +++ mzte-nv/src/main.zig | 2 ++ mzte-nv/src/modules/lsp.zig | 40 +++++++++++++++++++++++ 3 files changed, 47 insertions(+) create mode 100644 mzte-nv/src/modules/lsp.zig diff --git a/mzte-nv/conf/lua/pluginconf/p-lspconf.fnl b/mzte-nv/conf/lua/pluginconf/p-lspconf.fnl index d62ced8..3fbb12d 100644 --- a/mzte-nv/conf/lua/pluginconf/p-lspconf.fnl +++ b/mzte-nv/conf/lua/pluginconf/p-lspconf.fnl @@ -1,3 +1,4 @@ +(local mztenv (require :mzte_nv)) (local lspc (require :lspconfig)) (local lsp-configs (require :lspconfig.configs)) @@ -5,6 +6,10 @@ (var args args) (when (not args) (set args {})) + (tset args :on_attach (if args.on_attach + `(fn [client# bufnr#] (mztenv.lsp.onAttach client# bufnr#) + (,args.on_attach client# bufnr#)) + `mztenv.lsp.onAttach)) (tset args :capabilities `caps) `((. lspc ,conf :setup) ,args)) diff --git a/mzte-nv/src/main.zig b/mzte-nv/src/main.zig index 0f340d1..83ee129 100644 --- a/mzte-nv/src/main.zig +++ b/mzte-nv/src/main.zig @@ -13,6 +13,7 @@ const modules = struct { const cpbuf = @import("modules/cpbuf.zig"); const fennel = @import("modules/fennel.zig"); const jdtls = @import("modules/jdtls.zig"); + const lsp = @import("modules/lsp.zig"); const telescope = @import("modules/telescope.zig"); const tsn_actions = @import("modules/tsn_actions.zig"); const utils = @import("modules/utils.zig"); @@ -87,6 +88,7 @@ export fn luaopen_mzte_nv(l_: ?*c.lua_State) c_int { .cpbuf = modules.cpbuf, .fennel = modules.fennel, .jdtls = modules.jdtls, + .lsp = modules.lsp, .telescope = modules.telescope, .tsn_actions = modules.tsn_actions, .utils = modules.utils, diff --git a/mzte-nv/src/modules/lsp.zig b/mzte-nv/src/modules/lsp.zig new file mode 100644 index 0000000..543c106 --- /dev/null +++ b/mzte-nv/src/modules/lsp.zig @@ -0,0 +1,40 @@ +//! Module for nvim-lsp related utilities +const std = @import("std"); +const ser = @import("../ser.zig"); +const ffi = @import("../ffi.zig"); +const c = ffi.c; + +pub fn luaPush(l: *c.lua_State) void { + ser.luaPushAny(l, .{ + .onAttach = ffi.luaFunc(lOnAttach), + }); +} + +fn lOnAttach(l: *c.lua_State) !c_int { + c.luaL_checkany(l, 1); + const bufnr = c.luaL_checknumber(l, 2); + + const has_inlay_hints = hints: { + c.lua_getfield(l, 1, "server_capabilities"); + defer c.lua_pop(l, 1); + if (c.lua_isnil(l, -1)) break :hints false; + c.lua_getfield(l, -1, "inlayHintProvider"); + defer c.lua_pop(l, 1); + break :hints c.lua_toboolean(l, -1) != 0; + }; + + if (has_inlay_hints) { + c.lua_getglobal(l, "vim"); + defer c.lua_pop(l, 1); + c.lua_getfield(l, -1, "lsp"); + defer c.lua_pop(l, 1); + c.lua_getfield(l, -1, "inlay_hint"); + defer c.lua_pop(l, 1); + c.lua_getfield(l, -1, "enable"); + c.lua_pushnumber(l, bufnr); + c.lua_pushboolean(l, 1); + c.lua_call(l, 2, 0); + } + + return 0; +}