mzte-nv: improve telescope UI

This commit is contained in:
LordMZTE 2023-11-16 19:52:51 +01:00
parent 0ffdc78012
commit 7879eb2f8d
Signed by: LordMZTE
GPG key ID: B64802DC33A64FF6
3 changed files with 82 additions and 8 deletions

View file

@ -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

View file

@ -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,
});

View file

@ -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});
}