summaryrefslogtreecommitdiff
path: root/dev
diff options
context:
space:
mode:
Diffstat (limited to 'dev')
-rw-r--r--dev/lsp/lsp_client.zig39
1 files changed, 33 insertions, 6 deletions
diff --git a/dev/lsp/lsp_client.zig b/dev/lsp/lsp_client.zig
index a53ff57f..b76be78c 100644
--- a/dev/lsp/lsp_client.zig
+++ b/dev/lsp/lsp_client.zig
@@ -50,6 +50,7 @@ const std = @import("std");
const builtin = @import("builtin");
const libc = std.c;
const lsp = @import("core").lsp;
+const SymbolKind = @import("core").syntax.SymbolKind;
const Scip = @import("Scip.zig");
extern "c" fn execv(path: [*:0]const u8, argv: [*:null]const ?[*:0]const u8) c_int;
@@ -1002,7 +1003,11 @@ fn run(c: *Conn, io: std.Io, si: usize, arena: std.mem.Allocator, req: lsp.Req,
.document_symbols => {
const b = try docParams(arena, uri.items);
const result = (try call(c, io, arena, "textDocument/documentSymbol", b.items, deadline)) orelse return;
- try walkSymbols(&cx, c.caps.enc, uri.items, result, 0);
+ // Types, functions, fields, then the rest, each in the
+ // server's order (UX DESIGN N, the Symbols picker's).
+ var found: std.ArrayList(Symbol) = .empty;
+ try walkSymbols(arena, &found, uri.items, result, "", 0);
+ for (0..4) |group| for (found.items) |sym| if (sym.kind.group() == group) try emitRange(&cx, c.caps.enc, sym.uri, sym.range, sym.note);
},
.workspace_symbols => {
var b: std.ArrayList(u8) = .empty;
@@ -1106,20 +1111,42 @@ fn locations(cx: *Cx, enc: Enc, result: std.json.Value) Err!void {
}
}
-/// DocumentSymbol[] nests (`children`), SymbolInformation[] is flat.
-fn walkSymbols(cx: *Cx, enc: Enc, uri: []const u8, node: std.json.Value, depth: u8) Err!void {
+/// A symbol a server listed: its row's place and words, `t Shape.area`.
+const Symbol = struct { kind: SymbolKind, uri: []const u8, range: ?std.json.Value, note: []const u8 };
+
+/// DocumentSymbol[] nests (`children`), SymbolInformation[] is flat: each
+/// its kind's mark and its name qualified by the ones it is in (the
+/// nesting, or SymbolInformation's `containerName`), into `out`.
+fn walkSymbols(arena: std.mem.Allocator, out: *std.ArrayList(Symbol), uri: []const u8, node: std.json.Value, qualifier: []const u8, depth: u8) Err!void {
if (depth > 8) return;
for (items(node)) |sym| {
const name = str(get(sym, "name")) orelse continue;
+ const kind = symbolKind(num(get(sym, "kind")) orelse 0);
+ const container = if (qualifier.len > 0) qualifier else str(get(sym, "containerName")) orelse "";
+ const qualified = if (container.len > 0) try std.fmt.allocPrint(arena, "{s}.{s}", .{ container, name }) else name;
+ const note = try std.fmt.allocPrint(arena, "{s} {s}", .{ kind.mark(), qualified });
if (get(get(sym, "location"), "range")) |r| {
- try emitRange(cx, enc, str(get(get(sym, "location"), "uri")) orelse uri, r, name);
+ try out.append(arena, .{ .kind = kind, .uri = str(get(get(sym, "location"), "uri")) orelse uri, .range = r, .note = note });
} else {
- try emitRange(cx, enc, uri, get(sym, "selectionRange") orelse get(sym, "range"), name);
+ try out.append(arena, .{ .kind = kind, .uri = uri, .range = get(sym, "selectionRange") orelse get(sym, "range"), .note = note });
}
- if (get(sym, "children")) |kids| try walkSymbols(cx, enc, uri, kids, depth + 1);
+ if (get(sym, "children")) |kids| try walkSymbols(arena, out, uri, kids, qualified, depth + 1);
}
}
+/// LSP's SymbolKind as the Symbols picker's (UX DESIGN N2).
+fn symbolKind(lsp_kind: i64) SymbolKind {
+ return switch (lsp_kind) {
+ 5, 10, 11, 23, 26 => .type,
+ 6, 9, 12, 25 => .function,
+ 7, 8, 22 => .field,
+ 14 => .constant,
+ 13, 15...21, 24 => .variable,
+ 1...4 => .module,
+ else => .other,
+ };
+}
+
/// Pull when the server does (LSP 3.17), the push store otherwise. The store
/// path waits briefly for a publish that postdates the didChange we just
/// sent, so `]d` right after an edit sees the new truth, not the old one.