From b163be51d36f3ac2c47c6da47052cbb971855b48 Mon Sep 17 00:00:00 2001 From: mlugg Date: Thu, 5 Jan 2023 18:50:02 +0000 Subject: [PATCH] Fix crash when using nvim-lspconfig (#884) Apparently, nvim reports its code action kinds using both the actual strings (e.g. "refactor.extract") and the enumeration names (e.g. "RefactorExtract"). I don't know why this is done - possibly an attempt at compatibility with non-compliant server implementations? Regardless, this was causing a crash on init (when tres tried to parse an initializaiton message), which is easily fixed by just supporting those enumeration values. Resolves: #867 --- src/lsp.zig | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/lsp.zig b/src/lsp.zig index 9585783..2b7fbcf 100644 --- a/src/lsp.zig +++ b/src/lsp.zig @@ -708,7 +708,29 @@ pub const CodeActionKind = enum { _ = maybe_allocator; if (json_value != .String) return error.InvalidEnumTag; if (json_value.String.len == 0) return .empty; - return std.meta.stringToEnum(@This(), json_value.String) orelse return error.InvalidEnumTag; + if (std.meta.stringToEnum(@This(), json_value.String)) |val| return val; + + // Some clients (nvim) may report these by the enumeration names rather than the + // actual strings, so let's check those names here + const aliases = [_]struct { []const u8, CodeActionKind }{ + .{ "Empty", .empty }, + .{ "QuickFix", .quickfix }, + .{ "Refactor", .refactor }, + .{ "RefactorExtract", .@"refactor.extract" }, + .{ "RefactorInline", .@"refactor.inline" }, + .{ "RefactorRewrite", .@"refactor.rewrite" }, + .{ "Source", .source }, + .{ "SourceOrganizeImports", .@"source.organizeImports" }, + .{ "SourceFixAll", .@"source.fixAll" }, + }; + + for (aliases) |alias| { + if (std.mem.eql(u8, json_value.String, alias[0])) { + return alias[1]; + } + } + + return error.InvalidEnumTag; } };