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
This commit is contained in:
mlugg 2023-01-05 18:50:02 +00:00 committed by GitHub
parent 20baa592eb
commit b163be51d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -708,7 +708,29 @@ pub const CodeActionKind = enum {
_ = maybe_allocator; _ = maybe_allocator;
if (json_value != .String) return error.InvalidEnumTag; if (json_value != .String) return error.InvalidEnumTag;
if (json_value.String.len == 0) return .empty; 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;
} }
}; };