From 7879eb2f8dc5d951e357de904e4e299996c8f94d Mon Sep 17 00:00:00 2001 From: LordMZTE Date: Thu, 16 Nov 2023 19:52:51 +0100 Subject: [PATCH] mzte-nv: improve telescope UI --- mzte-nv/conf/lua/pluginconf/p-telescope.fnl | 22 ++++--- mzte-nv/src/main.zig | 2 + mzte-nv/src/modules/telescope.zig | 66 +++++++++++++++++++++ 3 files changed, 82 insertions(+), 8 deletions(-) create mode 100644 mzte-nv/src/modules/telescope.zig diff --git a/mzte-nv/conf/lua/pluginconf/p-telescope.fnl b/mzte-nv/conf/lua/pluginconf/p-telescope.fnl index 05aed1f..fe6de19 100644 --- a/mzte-nv/conf/lua/pluginconf/p-telescope.fnl +++ b/mzte-nv/conf/lua/pluginconf/p-telescope.fnl @@ -1,5 +1,6 @@ -(local (telescope builtin ext themes) - (values (require :telescope) (require :telescope.builtin) +(local (mztenv telescope utils builtin ext themes) + (values (require :mzte_nv) (require :telescope) + (require :telescope.utils) (require :telescope.builtin) (. (require :telescope._extensions) :manager) (require :telescope.themes))) @@ -10,7 +11,8 @@ :--line-number :--column :--smart-case - :--hidden]} + :--hidden] + :path_display [:truncate]} :pickers {:find_files {:find_command [:fd :--type :f @@ -19,7 +21,11 @@ (telescope.load_extension :harpoon) -(let [mopt (. (require :mzte_nv) :utils :map_opt)] +(set utils.transform_path + (mztenv.telescope.makePathTransformer utils.transform_path)) + +(let [mopt mztenv.utils.map_opt + lsp-opts {:fname_width 80 :show_line false}] (macro nmap [map action opt] `(vim.keymap.set :n ,map #(,action (themes.get_ivy ,opt)) mopt)) ;; resume search @@ -28,10 +34,10 @@ (nmap :ff builtin.find_files) (nmap :fg builtin.live_grep) ;; LSP mappings - (nmap :gd builtin.lsp_definitions) - (nmap :gi builtin.lsp_implementations) - (nmap :gr builtin.lsp_references) - (nmap :gs builtin.lsp_dynamic_workspace_symbols) + (nmap :gd builtin.lsp_definitions lsp-opts) + (nmap :gi builtin.lsp_implementations lsp-opts) + (nmap :gr builtin.lsp_references lsp-opts) + (nmap :gs builtin.lsp_dynamic_workspace_symbols lsp-opts) (nmap :gp builtin.diagnostics {:bufnr 0}) (nmap :gP builtin.diagnostics) ;; harpoon diff --git a/mzte-nv/src/main.zig b/mzte-nv/src/main.zig index 51da461..0f340d1 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 telescope = @import("modules/telescope.zig"); const tsn_actions = @import("modules/tsn_actions.zig"); const utils = @import("modules/utils.zig"); }; @@ -86,6 +87,7 @@ export fn luaopen_mzte_nv(l_: ?*c.lua_State) c_int { .cpbuf = modules.cpbuf, .fennel = modules.fennel, .jdtls = modules.jdtls, + .telescope = modules.telescope, .tsn_actions = modules.tsn_actions, .utils = modules.utils, }); diff --git a/mzte-nv/src/modules/telescope.zig b/mzte-nv/src/modules/telescope.zig new file mode 100644 index 0000000..1bb26de --- /dev/null +++ b/mzte-nv/src/modules/telescope.zig @@ -0,0 +1,66 @@ +//! Module for telescope.nvim. +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, .{ + .makePathTransformer = ffi.luaFunc(lMakePathTransformer), + }); +} + +fn lMakePathTransformer(l: *c.lua_State) !c_int { + c.luaL_checktype(l, 1, c.LUA_TFUNCTION); + + c.lua_pushvalue(l, 1); + c.lua_pushcclosure(l, ffi.luaFunc(lPathTransformerClosure), 1); + return 1; +} + +fn lPathTransformerClosure(l: *c.lua_State) !c_int { + c.luaL_checkany(l, 1); + const path = ffi.luaCheckstring(l, 2); + + // push the delegate function + c.lua_pushvalue(l, c.lua_upvalueindex(1)); + // push the opts parameter + c.lua_pushvalue(l, 1); + + const prefix = "jdt://contents/"; + if (std.mem.startsWith(u8, path, prefix)) { + var buf: [1024 * 4]u8 = undefined; + var fbs = std.io.fixedBufferStream(&buf); + if (transformJdtlsURI(path, fbs.writer())) { + ffi.luaPushString(l, fbs.getWritten()); + } else |e| { + std.log.err("transforming JDTLS URI: {}", .{e}); + ffi.luaPushString(l, path); + } + } else { + ffi.luaPushString(l, path); + } + + c.lua_call(l, 2, 1); + return 1; +} + +fn transformJdtlsURI(uri_str: []const u8, writer: anytype) !void { + // We do a full-on URI parse here because JDTLS often appends parameters and other garbage data. + const uri = try std.Uri.parse(uri_str); + var fname_iter = std.fs.path.ComponentIterator(.posix, u8).init(uri.path) catch + unreachable; // this can only error on windows lol + + _ = fname_iter.next() orelse return error.InvalidPath; // name of the JAR + + const package = (fname_iter.next() orelse return error.InvalidPath).name; + const classfile = (fname_iter.next() orelse return error.InvalidPath).name; + + if (std.mem.endsWith(u8, classfile, ".class")) { + try writer.writeAll(classfile[0 .. classfile.len - ".class".len]); + } else { + try writer.writeAll(classfile); + } + + try writer.print(" ({s})", .{package}); +}