2020-04-24 23:19:03 +01:00
|
|
|
const std = @import("std");
|
2020-05-07 16:29:40 +01:00
|
|
|
const build_options = @import("build_options");
|
|
|
|
|
2020-05-09 14:43:51 +01:00
|
|
|
const Config = @import("config.zig");
|
2020-05-14 00:10:41 +01:00
|
|
|
const DocumentStore = @import("document_store.zig");
|
2020-05-17 12:40:32 +01:00
|
|
|
const DebugAllocator = @import("debug_allocator.zig");
|
2020-05-17 15:50:13 +01:00
|
|
|
const readRequestHeader = @import("header.zig").readRequestHeader;
|
2020-05-07 16:29:40 +01:00
|
|
|
const data = @import("data/" ++ build_options.data_version ++ ".zig");
|
2020-06-29 23:34:21 +01:00
|
|
|
const requests = @import("requests.zig");
|
2020-04-27 21:38:35 +01:00
|
|
|
const types = @import("types.zig");
|
|
|
|
const analysis = @import("analysis.zig");
|
2020-05-19 20:09:00 +01:00
|
|
|
const URI = @import("uri.zig");
|
2020-07-05 23:32:14 +01:00
|
|
|
const references = @import("references.zig");
|
2020-06-27 01:16:14 +01:00
|
|
|
const rename = @import("rename.zig");
|
2020-07-03 00:31:28 +01:00
|
|
|
const offsets = @import("offsets.zig");
|
2020-04-24 23:19:03 +01:00
|
|
|
|
2020-08-14 11:41:34 +01:00
|
|
|
const logger = std.log.scoped(.main);
|
|
|
|
|
2020-06-26 12:29:59 +01:00
|
|
|
pub const log_level: std.log.Level = switch (std.builtin.mode) {
|
|
|
|
.Debug => .debug,
|
|
|
|
else => .notice,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub fn log(
|
|
|
|
comptime message_level: std.log.Level,
|
|
|
|
comptime scope: @Type(.EnumLiteral),
|
|
|
|
comptime format: []const u8,
|
2020-07-12 20:12:09 +01:00
|
|
|
args: anytype,
|
2020-06-26 12:29:59 +01:00
|
|
|
) void {
|
2020-06-29 23:34:21 +01:00
|
|
|
var arena = std.heap.ArenaAllocator.init(allocator);
|
|
|
|
defer arena.deinit();
|
|
|
|
|
|
|
|
var message = std.fmt.allocPrint(&arena.allocator, "[{}-{}] " ++ format, .{ @tagName(message_level), @tagName(scope) } ++ args) catch |err| {
|
2020-06-26 12:29:59 +01:00
|
|
|
std.debug.print("Failed to allocPrint message.", .{});
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
if (@enumToInt(message_level) <= @enumToInt(std.log.Level.notice)) {
|
|
|
|
const message_type: types.MessageType = switch (message_level) {
|
|
|
|
.info => .Log,
|
|
|
|
.notice => .Info,
|
|
|
|
.warn => .Warning,
|
|
|
|
.err => .Error,
|
|
|
|
else => .Error,
|
|
|
|
};
|
2020-06-29 23:34:21 +01:00
|
|
|
send(&arena, types.Notification{
|
2020-06-26 12:29:59 +01:00
|
|
|
.method = "window/showMessage",
|
2020-06-27 01:16:14 +01:00
|
|
|
.params = types.NotificationParams{
|
|
|
|
.ShowMessageParams = .{
|
|
|
|
.type = message_type,
|
|
|
|
.message = message,
|
|
|
|
},
|
2020-06-26 12:29:59 +01:00
|
|
|
},
|
|
|
|
}) catch |err| {
|
|
|
|
std.debug.print("Failed to send show message notification (error: {}).", .{err});
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
const message_type: types.MessageType = if (message_level == .debug)
|
|
|
|
.Log
|
|
|
|
else
|
|
|
|
.Info;
|
|
|
|
|
2020-06-29 23:34:21 +01:00
|
|
|
send(&arena, types.Notification{
|
2020-06-26 12:29:59 +01:00
|
|
|
.method = "window/logMessage",
|
2020-06-27 01:16:14 +01:00
|
|
|
.params = types.NotificationParams{
|
|
|
|
.LogMessageParams = .{
|
|
|
|
.type = message_type,
|
|
|
|
.message = message,
|
|
|
|
},
|
2020-06-26 12:29:59 +01:00
|
|
|
},
|
|
|
|
}) catch |err| {
|
|
|
|
std.debug.print("Failed to send show message notification (error: {}).", .{err});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-24 23:19:03 +01:00
|
|
|
// Code is largely based off of https://github.com/andersfr/zig-lsp/blob/master/server.zig
|
2020-05-17 18:26:02 +01:00
|
|
|
var stdout: std.io.BufferedOutStream(4096, std.fs.File.OutStream) = undefined;
|
2020-04-24 23:19:03 +01:00
|
|
|
var allocator: *std.mem.Allocator = undefined;
|
|
|
|
|
2020-05-14 00:10:41 +01:00
|
|
|
var document_store: DocumentStore = undefined;
|
2020-05-19 20:09:00 +01:00
|
|
|
var workspace_folder_configs: std.StringHashMap(?Config) = undefined;
|
2020-04-27 21:38:35 +01:00
|
|
|
|
2020-06-01 11:29:06 +01:00
|
|
|
const ClientCapabilities = struct {
|
|
|
|
supports_snippets: bool = false,
|
2020-06-12 15:42:41 +01:00
|
|
|
supports_semantic_tokens: bool = false,
|
|
|
|
hover_supports_md: bool = false,
|
|
|
|
completion_doc_supports_md: bool = false,
|
2020-06-16 16:49:31 +01:00
|
|
|
supports_workspace_folders: bool = false,
|
2020-06-01 11:29:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
var client_capabilities = ClientCapabilities{};
|
2020-07-03 09:34:42 +01:00
|
|
|
var offset_encoding = offsets.Encoding.utf16;
|
2020-06-01 11:29:06 +01:00
|
|
|
|
2020-07-03 10:00:00 +01:00
|
|
|
const initialize_capabilities =
|
2020-07-05 22:35:52 +01:00
|
|
|
\\"capabilities": {"signatureHelpProvider": {"triggerCharacters": ["(",","]},"textDocumentSync": 1,"renameProvider":true,"completionProvider": {"resolveProvider": false,"triggerCharacters": [".",":","@"]},"documentHighlightProvider": false,"hoverProvider": true,"codeActionProvider": false,"declarationProvider": true,"definitionProvider": true,"typeDefinitionProvider": true,"implementationProvider": false,"referencesProvider": true,"documentSymbolProvider": true,"colorProvider": false,"documentFormattingProvider": true,"documentRangeFormattingProvider": false,"foldingRangeProvider": false,"selectionRangeProvider": false,"workspaceSymbolProvider": false,"rangeProvider": false,"documentProvider": true,"workspace": {"workspaceFolders": {"supported": true,"changeNotifications": true}},"semanticTokensProvider": {"documentProvider": true,"legend": {"tokenTypes": ["namespace","type","struct","enum","union","parameter","variable","tagField","field","errorTag","function","keyword","comment","string","number","operator","builtin","label"],"tokenModifiers": ["definition","async","documentation", "generic"]}}}}}
|
2020-04-24 23:19:03 +01:00
|
|
|
;
|
|
|
|
|
2020-07-03 10:00:00 +01:00
|
|
|
const initialize_response = ",\"result\": {" ++ initialize_capabilities;
|
|
|
|
|
2020-05-14 22:16:40 +01:00
|
|
|
const not_implemented_response =
|
|
|
|
\\,"error":{"code":-32601,"message":"NotImplemented"}}
|
2020-04-24 23:19:03 +01:00
|
|
|
;
|
|
|
|
|
2020-05-14 22:16:40 +01:00
|
|
|
const null_result_response =
|
|
|
|
\\,"result":null}
|
2020-04-24 23:19:03 +01:00
|
|
|
;
|
2020-05-14 22:16:40 +01:00
|
|
|
const empty_result_response =
|
|
|
|
\\,"result":{}}
|
2020-04-24 23:19:03 +01:00
|
|
|
;
|
2020-05-14 22:16:40 +01:00
|
|
|
const empty_array_response =
|
|
|
|
\\,"result":[]}
|
2020-04-24 23:19:03 +01:00
|
|
|
;
|
2020-05-14 22:16:40 +01:00
|
|
|
const edit_not_applied_response =
|
|
|
|
\\,"result":{"applied":false,"failureReason":"feature not implemented"}}
|
2020-04-24 23:19:03 +01:00
|
|
|
;
|
2020-05-14 22:16:40 +01:00
|
|
|
const no_completions_response =
|
|
|
|
\\,"result":{"isIncomplete":false,"items":[]}}
|
2020-04-24 23:19:03 +01:00
|
|
|
;
|
2020-06-16 12:27:00 +01:00
|
|
|
const no_semantic_tokens_response =
|
|
|
|
\\,"result":{"data":[]}}
|
|
|
|
;
|
2020-04-24 23:19:03 +01:00
|
|
|
|
2020-04-27 21:38:35 +01:00
|
|
|
/// Sends a request or response
|
2020-07-12 20:12:09 +01:00
|
|
|
fn send(arena: *std.heap.ArenaAllocator, reqOrRes: anytype) !void {
|
2020-05-28 01:39:36 +01:00
|
|
|
var arr = std.ArrayList(u8).init(&arena.allocator);
|
2020-06-27 01:16:14 +01:00
|
|
|
try std.json.stringify(reqOrRes, .{}, arr.writer());
|
2020-04-27 21:38:35 +01:00
|
|
|
|
2020-06-29 23:34:21 +01:00
|
|
|
const stdout_stream = stdout.writer();
|
2020-05-28 01:39:36 +01:00
|
|
|
try stdout_stream.print("Content-Length: {}\r\n\r\n", .{arr.items.len});
|
|
|
|
try stdout_stream.writeAll(arr.items);
|
2020-05-17 18:26:02 +01:00
|
|
|
try stdout.flush();
|
2020-04-24 23:19:03 +01:00
|
|
|
}
|
|
|
|
|
2020-06-06 13:40:33 +01:00
|
|
|
fn respondGeneric(id: types.RequestId, response: []const u8) !void {
|
|
|
|
const id_len = switch (id) {
|
|
|
|
.Integer => |id_val| blk: {
|
|
|
|
if (id_val == 0) break :blk 1;
|
|
|
|
var digits: usize = 1;
|
|
|
|
var value = @divTrunc(id_val, 10);
|
|
|
|
while (value != 0) : (value = @divTrunc(value, 10)) {
|
|
|
|
digits += 1;
|
|
|
|
}
|
|
|
|
break :blk digits;
|
|
|
|
},
|
|
|
|
.String => |str_val| str_val.len + 2,
|
|
|
|
else => unreachable,
|
2020-04-24 23:19:03 +01:00
|
|
|
};
|
|
|
|
|
2020-06-06 13:40:33 +01:00
|
|
|
// Numbers of character that will be printed from this string: len - 1 brackets
|
|
|
|
const json_fmt = "{{\"jsonrpc\":\"2.0\",\"id\":";
|
2020-05-17 18:26:02 +01:00
|
|
|
|
|
|
|
const stdout_stream = stdout.outStream();
|
2020-06-10 18:48:40 +01:00
|
|
|
try stdout_stream.print("Content-Length: {}\r\n\r\n" ++ json_fmt, .{response.len + id_len + json_fmt.len - 1});
|
2020-06-06 13:40:33 +01:00
|
|
|
switch (id) {
|
|
|
|
.Integer => |int| try stdout_stream.print("{}", .{int}),
|
|
|
|
.String => |str| try stdout_stream.print("\"{}\"", .{str}),
|
|
|
|
else => unreachable,
|
|
|
|
}
|
|
|
|
|
2020-05-17 18:26:02 +01:00
|
|
|
try stdout_stream.writeAll(response);
|
|
|
|
try stdout.flush();
|
2020-05-08 00:53:00 +01:00
|
|
|
}
|
|
|
|
|
2020-06-09 04:21:55 +01:00
|
|
|
fn showMessage(@"type": types.MessageType, message: []const u8) !void {
|
|
|
|
try send(types.Notification{
|
|
|
|
.method = "window/showMessage",
|
|
|
|
.params = .{
|
|
|
|
.ShowMessageParams = .{
|
|
|
|
.@"type" = @"type",
|
2020-06-10 18:48:40 +01:00
|
|
|
.message = message,
|
2020-06-09 04:21:55 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-14 22:16:40 +01:00
|
|
|
// TODO: Is this correct or can we get a better end?
|
2020-05-08 16:01:34 +01:00
|
|
|
fn astLocationToRange(loc: std.zig.ast.Tree.Location) types.Range {
|
|
|
|
return .{
|
|
|
|
.start = .{
|
|
|
|
.line = @intCast(i64, loc.line),
|
|
|
|
.character = @intCast(i64, loc.column),
|
|
|
|
},
|
|
|
|
.end = .{
|
|
|
|
.line = @intCast(i64, loc.line),
|
|
|
|
.character = @intCast(i64, loc.column),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-06-29 23:34:21 +01:00
|
|
|
fn publishDiagnostics(arena: *std.heap.ArenaAllocator, handle: DocumentStore.Handle, config: Config) !void {
|
2020-05-24 17:00:21 +01:00
|
|
|
const tree = handle.tree;
|
2020-04-24 23:19:03 +01:00
|
|
|
|
2020-05-07 13:01:16 +01:00
|
|
|
var diagnostics = std.ArrayList(types.Diagnostic).init(&arena.allocator);
|
2020-04-24 23:19:03 +01:00
|
|
|
|
2020-05-23 23:21:02 +01:00
|
|
|
for (tree.errors) |*err| {
|
2020-05-09 02:02:29 +01:00
|
|
|
const loc = tree.tokenLocation(0, err.loc());
|
|
|
|
|
|
|
|
var mem_buffer: [256]u8 = undefined;
|
|
|
|
var fbs = std.io.fixedBufferStream(&mem_buffer);
|
|
|
|
try tree.renderError(err, fbs.outStream());
|
|
|
|
|
|
|
|
try diagnostics.append(.{
|
|
|
|
.range = astLocationToRange(loc),
|
|
|
|
.severity = .Error,
|
|
|
|
.code = @tagName(err.*),
|
|
|
|
.source = "zls",
|
|
|
|
.message = try std.mem.dupe(&arena.allocator, u8, fbs.getWritten()),
|
|
|
|
// .relatedInformation = undefined
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tree.errors.len == 0) {
|
2020-05-23 23:21:02 +01:00
|
|
|
for (tree.root_node.decls()) |decl| {
|
2020-07-16 15:56:22 +01:00
|
|
|
switch (decl.tag) {
|
2020-05-08 18:02:46 +01:00
|
|
|
.FnProto => blk: {
|
2020-05-04 03:17:19 +01:00
|
|
|
const func = decl.cast(std.zig.ast.Node.FnProto).?;
|
2020-09-01 06:53:52 +01:00
|
|
|
const is_extern = func.getExternExportInlineToken() != null;
|
2020-05-08 16:01:34 +01:00
|
|
|
if (is_extern)
|
2020-05-08 18:02:46 +01:00
|
|
|
break :blk;
|
2020-05-08 16:01:34 +01:00
|
|
|
|
2020-05-15 20:10:53 +01:00
|
|
|
if (config.warn_style) {
|
2020-09-01 06:53:52 +01:00
|
|
|
if (func.getNameToken()) |name_token| {
|
2020-05-15 20:10:53 +01:00
|
|
|
const loc = tree.tokenLocation(0, name_token);
|
|
|
|
|
2020-05-17 15:23:04 +01:00
|
|
|
const is_type_function = analysis.isTypeFunction(tree, func);
|
2020-05-15 20:10:53 +01:00
|
|
|
|
|
|
|
const func_name = tree.tokenSlice(name_token);
|
|
|
|
if (!is_type_function and !analysis.isCamelCase(func_name)) {
|
|
|
|
try diagnostics.append(.{
|
|
|
|
.range = astLocationToRange(loc),
|
|
|
|
.severity = .Information,
|
|
|
|
.code = "BadStyle",
|
|
|
|
.source = "zls",
|
|
|
|
.message = "Functions should be camelCase",
|
|
|
|
});
|
|
|
|
} else if (is_type_function and !analysis.isPascalCase(func_name)) {
|
|
|
|
try diagnostics.append(.{
|
|
|
|
.range = astLocationToRange(loc),
|
|
|
|
.severity = .Information,
|
|
|
|
.code = "BadStyle",
|
|
|
|
.source = "zls",
|
|
|
|
.message = "Type functions should be PascalCase",
|
|
|
|
});
|
|
|
|
}
|
2020-05-04 03:17:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2020-05-14 22:16:40 +01:00
|
|
|
else => {},
|
2020-05-04 03:17:19 +01:00
|
|
|
}
|
|
|
|
}
|
2020-04-24 23:19:03 +01:00
|
|
|
}
|
|
|
|
|
2020-06-29 23:34:21 +01:00
|
|
|
try send(arena, types.Notification{
|
2020-04-27 21:38:35 +01:00
|
|
|
.method = "textDocument/publishDiagnostics",
|
2020-05-07 13:29:53 +01:00
|
|
|
.params = .{
|
|
|
|
.PublishDiagnosticsParams = .{
|
2020-05-14 00:10:41 +01:00
|
|
|
.uri = handle.uri(),
|
2020-05-07 11:56:08 +01:00
|
|
|
.diagnostics = diagnostics.items,
|
2020-05-07 13:29:53 +01:00
|
|
|
},
|
2020-05-07 14:23:13 +01:00
|
|
|
},
|
2020-04-27 21:38:35 +01:00
|
|
|
});
|
|
|
|
}
|
2020-04-24 23:19:03 +01:00
|
|
|
|
2020-06-17 03:12:12 +01:00
|
|
|
fn typeToCompletion(
|
|
|
|
arena: *std.heap.ArenaAllocator,
|
|
|
|
list: *std.ArrayList(types.CompletionItem),
|
2020-07-07 21:26:12 +01:00
|
|
|
field_access: analysis.FieldAccessReturn,
|
2020-06-17 03:12:12 +01:00
|
|
|
orig_handle: *DocumentStore.Handle,
|
|
|
|
config: Config,
|
|
|
|
) error{OutOfMemory}!void {
|
2020-07-07 21:26:12 +01:00
|
|
|
const type_handle = field_access.original;
|
2020-06-17 03:12:12 +01:00
|
|
|
switch (type_handle.type.data) {
|
|
|
|
.slice => {
|
|
|
|
if (!type_handle.type.is_type_val) {
|
|
|
|
try list.append(.{
|
|
|
|
.label = "len",
|
|
|
|
.kind = .Field,
|
|
|
|
});
|
|
|
|
try list.append(.{
|
|
|
|
.label = "ptr",
|
|
|
|
.kind = .Field,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
.error_union => {},
|
2020-07-24 11:20:13 +01:00
|
|
|
.pointer => |n| {
|
|
|
|
if (config.operator_completions) {
|
|
|
|
try list.append(.{
|
|
|
|
.label = "*",
|
|
|
|
.kind = .Operator,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
try nodeToCompletion(
|
|
|
|
arena,
|
|
|
|
list,
|
|
|
|
.{ .node = n, .handle = type_handle.handle },
|
|
|
|
null,
|
|
|
|
orig_handle,
|
|
|
|
type_handle.type.is_type_val,
|
|
|
|
config,
|
|
|
|
);
|
|
|
|
},
|
2020-06-17 03:12:12 +01:00
|
|
|
.other => |n| try nodeToCompletion(
|
|
|
|
arena,
|
|
|
|
list,
|
|
|
|
.{ .node = n, .handle = type_handle.handle },
|
2020-07-07 21:26:12 +01:00
|
|
|
field_access.unwrapped,
|
2020-06-17 03:12:12 +01:00
|
|
|
orig_handle,
|
|
|
|
type_handle.type.is_type_val,
|
|
|
|
config,
|
|
|
|
),
|
2020-06-17 21:36:40 +01:00
|
|
|
.primitive => {},
|
2020-06-17 03:12:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-23 14:14:03 +01:00
|
|
|
fn nodeToCompletion(
|
2020-06-10 19:24:17 +01:00
|
|
|
arena: *std.heap.ArenaAllocator,
|
2020-05-23 14:14:03 +01:00
|
|
|
list: *std.ArrayList(types.CompletionItem),
|
2020-06-10 17:54:01 +01:00
|
|
|
node_handle: analysis.NodeWithHandle,
|
2020-07-07 21:26:12 +01:00
|
|
|
unwrapped: ?analysis.TypeWithHandle,
|
2020-06-10 18:48:40 +01:00
|
|
|
orig_handle: *DocumentStore.Handle,
|
2020-06-17 03:12:12 +01:00
|
|
|
is_type_val: bool,
|
2020-05-23 14:14:03 +01:00
|
|
|
config: Config,
|
|
|
|
) error{OutOfMemory}!void {
|
2020-06-10 18:48:40 +01:00
|
|
|
const node = node_handle.node;
|
|
|
|
const handle = node_handle.handle;
|
|
|
|
|
2020-06-12 15:42:41 +01:00
|
|
|
const doc_kind: types.MarkupKind = if (client_capabilities.completion_doc_supports_md) .Markdown else .PlainText;
|
|
|
|
const doc = if (try analysis.getDocComments(
|
|
|
|
list.allocator,
|
|
|
|
handle.tree,
|
|
|
|
node,
|
|
|
|
doc_kind,
|
|
|
|
)) |doc_comments|
|
2020-05-14 22:16:40 +01:00
|
|
|
types.MarkupContent{
|
2020-06-12 15:42:41 +01:00
|
|
|
.kind = doc_kind,
|
2020-05-14 22:16:40 +01:00
|
|
|
.value = doc_comments,
|
|
|
|
}
|
|
|
|
else
|
|
|
|
null;
|
2020-05-13 18:30:57 +01:00
|
|
|
|
2020-07-16 15:56:22 +01:00
|
|
|
if (node.tag == .ErrorSetDecl or node.tag == .Root or node.tag == .ContainerDecl) {
|
2020-06-17 03:12:12 +01:00
|
|
|
const context = DeclToCompletionContext{
|
|
|
|
.completions = list,
|
|
|
|
.config = &config,
|
|
|
|
.arena = arena,
|
|
|
|
.orig_handle = orig_handle,
|
|
|
|
};
|
|
|
|
try analysis.iterateSymbolsContainer(&document_store, arena, node_handle, orig_handle, declToCompletion, context, !is_type_val);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_type_val) return;
|
|
|
|
|
2020-07-16 15:56:22 +01:00
|
|
|
switch (node.tag) {
|
2020-05-13 18:30:57 +01:00
|
|
|
.FnProto => {
|
2020-05-17 15:23:04 +01:00
|
|
|
const func = node.cast(std.zig.ast.Node.FnProto).?;
|
2020-09-01 06:53:52 +01:00
|
|
|
if (func.getNameToken()) |name_token| {
|
2020-06-01 11:29:06 +01:00
|
|
|
const use_snippets = config.enable_snippets and client_capabilities.supports_snippets;
|
2020-06-06 00:44:43 +01:00
|
|
|
|
|
|
|
const insert_text = if (use_snippets) blk: {
|
2020-06-17 03:12:12 +01:00
|
|
|
// TODO Also check if we are dot accessing from a type val and dont skip in that case.
|
2020-06-10 20:05:11 +01:00
|
|
|
const skip_self_param = if (func.params_len > 0) param_check: {
|
|
|
|
const in_container = analysis.innermostContainer(handle, handle.tree.token_locs[func.firstToken()].start);
|
2020-06-17 03:12:12 +01:00
|
|
|
|
2020-06-10 20:05:11 +01:00
|
|
|
switch (func.paramsConst()[0].param_type) {
|
|
|
|
.type_expr => |type_node| {
|
|
|
|
if (try analysis.resolveTypeOfNode(&document_store, arena, .{
|
|
|
|
.node = type_node,
|
|
|
|
.handle = handle,
|
|
|
|
})) |resolved_type| {
|
2020-06-17 03:12:12 +01:00
|
|
|
if (std.meta.eql(in_container, resolved_type))
|
2020-06-10 20:05:11 +01:00
|
|
|
break :param_check true;
|
|
|
|
}
|
|
|
|
|
2020-07-23 19:13:38 +01:00
|
|
|
if (type_node.castTag(.PtrType)) |ptr_type| {
|
|
|
|
if (try analysis.resolveTypeOfNode(&document_store, arena, .{
|
|
|
|
.node = ptr_type.rhs,
|
|
|
|
.handle = handle,
|
|
|
|
})) |resolved_prefix_op| {
|
|
|
|
if (std.meta.eql(in_container, resolved_prefix_op))
|
|
|
|
break :param_check true;
|
2020-06-10 20:05:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break :param_check false;
|
|
|
|
},
|
|
|
|
else => break :param_check false,
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
false;
|
2020-06-10 19:24:17 +01:00
|
|
|
|
|
|
|
break :blk try analysis.getFunctionSnippet(&arena.allocator, handle.tree, func, skip_self_param);
|
2020-06-06 00:44:43 +01:00
|
|
|
} else
|
2020-05-13 18:30:57 +01:00
|
|
|
null;
|
|
|
|
|
2020-06-10 19:24:17 +01:00
|
|
|
const is_type_function = analysis.isTypeFunction(handle.tree, func);
|
2020-05-17 15:23:04 +01:00
|
|
|
|
|
|
|
try list.append(.{
|
2020-06-10 19:24:17 +01:00
|
|
|
.label = handle.tree.tokenSlice(name_token),
|
2020-05-17 15:23:04 +01:00
|
|
|
.kind = if (is_type_function) .Struct else .Function,
|
2020-05-13 18:30:57 +01:00
|
|
|
.documentation = doc,
|
2020-06-10 19:24:17 +01:00
|
|
|
.detail = analysis.getFunctionSignature(handle.tree, func),
|
2020-05-13 18:30:57 +01:00
|
|
|
.insertText = insert_text,
|
2020-06-01 11:29:06 +01:00
|
|
|
.insertTextFormat = if (use_snippets) .Snippet else .PlainText,
|
2020-05-17 15:23:04 +01:00
|
|
|
});
|
2020-05-13 18:30:57 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
.VarDecl => {
|
2020-05-17 15:23:04 +01:00
|
|
|
const var_decl = node.cast(std.zig.ast.Node.VarDecl).?;
|
2020-06-10 19:24:17 +01:00
|
|
|
const is_const = handle.tree.token_ids[var_decl.mut_token] == .Keyword_const;
|
2020-05-19 05:12:05 +01:00
|
|
|
|
2020-06-14 23:19:21 +01:00
|
|
|
if (try analysis.resolveVarDeclAlias(&document_store, arena, node_handle)) |result| {
|
|
|
|
const context = DeclToCompletionContext{
|
|
|
|
.completions = list,
|
|
|
|
.config = &config,
|
|
|
|
.arena = arena,
|
|
|
|
.orig_handle = orig_handle,
|
|
|
|
};
|
|
|
|
return try declToCompletion(context, result);
|
2020-05-19 05:12:05 +01:00
|
|
|
}
|
2020-05-25 23:51:50 +01:00
|
|
|
|
2020-05-17 15:23:04 +01:00
|
|
|
try list.append(.{
|
2020-06-10 19:24:17 +01:00
|
|
|
.label = handle.tree.tokenSlice(var_decl.name_token),
|
2020-05-17 15:23:04 +01:00
|
|
|
.kind = if (is_const) .Constant else .Variable,
|
2020-05-13 18:30:57 +01:00
|
|
|
.documentation = doc,
|
2020-06-10 19:24:17 +01:00
|
|
|
.detail = analysis.getVariableSignature(handle.tree, var_decl),
|
2020-05-17 15:23:04 +01:00
|
|
|
});
|
2020-05-13 18:30:57 +01:00
|
|
|
},
|
2020-06-10 23:00:13 +01:00
|
|
|
.ContainerField => {
|
|
|
|
const field = node.cast(std.zig.ast.Node.ContainerField).?;
|
|
|
|
try list.append(.{
|
|
|
|
.label = handle.tree.tokenSlice(field.name_token),
|
|
|
|
.kind = .Field,
|
|
|
|
.documentation = doc,
|
|
|
|
.detail = analysis.getContainerFieldSignature(handle.tree, field),
|
|
|
|
});
|
|
|
|
},
|
2020-07-24 11:20:13 +01:00
|
|
|
.SliceType => {
|
|
|
|
try list.append(.{
|
|
|
|
.label = "len",
|
|
|
|
.kind = .Field,
|
|
|
|
});
|
|
|
|
try list.append(.{
|
|
|
|
.label = "ptr",
|
|
|
|
.kind = .Field,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
.ArrayType => {
|
|
|
|
try list.append(.{
|
|
|
|
.label = "len",
|
|
|
|
.kind = .Field,
|
|
|
|
});
|
|
|
|
},
|
2020-07-16 17:04:23 +01:00
|
|
|
.PtrType => {
|
|
|
|
if (config.operator_completions) {
|
|
|
|
try list.append(.{
|
|
|
|
.label = "*",
|
|
|
|
.kind = .Operator,
|
|
|
|
});
|
|
|
|
}
|
2020-07-07 21:26:12 +01:00
|
|
|
|
2020-07-17 15:01:21 +01:00
|
|
|
const ptr_type = node.castTag(.PtrType).?;
|
2020-07-23 18:30:03 +01:00
|
|
|
if (ptr_type.rhs.castTag(.ArrayType) != null) {
|
|
|
|
try list.append(.{
|
|
|
|
.label = "len",
|
|
|
|
.kind = .Field,
|
|
|
|
});
|
2020-07-16 17:04:23 +01:00
|
|
|
} else if (unwrapped) |actual_type| {
|
|
|
|
try typeToCompletion(arena, list, .{ .original = actual_type }, orig_handle, config);
|
2020-05-27 16:49:11 +01:00
|
|
|
}
|
2020-07-16 17:04:23 +01:00
|
|
|
return;
|
|
|
|
},
|
|
|
|
.OptionalType => {
|
|
|
|
if (config.operator_completions) {
|
|
|
|
try list.append(.{
|
|
|
|
.label = "?",
|
|
|
|
.kind = .Operator,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return;
|
2020-05-13 18:30:57 +01:00
|
|
|
},
|
2020-05-17 15:23:04 +01:00
|
|
|
.StringLiteral => {
|
|
|
|
try list.append(.{
|
|
|
|
.label = "len",
|
|
|
|
.kind = .Field,
|
|
|
|
});
|
|
|
|
},
|
2020-06-10 18:48:40 +01:00
|
|
|
else => if (analysis.nodeToString(handle.tree, node)) |string| {
|
2020-05-17 15:23:04 +01:00
|
|
|
try list.append(.{
|
2020-05-13 18:30:57 +01:00
|
|
|
.label = string,
|
|
|
|
.kind = .Field,
|
|
|
|
.documentation = doc,
|
2020-06-10 18:48:40 +01:00
|
|
|
.detail = handle.tree.getNodeSource(node),
|
2020-05-17 15:23:04 +01:00
|
|
|
});
|
2020-05-14 22:16:40 +01:00
|
|
|
},
|
2020-05-13 18:30:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-18 21:19:23 +01:00
|
|
|
fn identifierFromPosition(pos_index: usize, handle: DocumentStore.Handle) []const u8 {
|
2020-05-22 23:03:41 +01:00
|
|
|
const text = handle.document.text;
|
|
|
|
|
|
|
|
if (pos_index + 1 >= text.len) return &[0]u8{};
|
2020-05-18 21:19:23 +01:00
|
|
|
var start_idx = pos_index;
|
2020-05-21 12:36:14 +01:00
|
|
|
|
2020-05-18 21:19:23 +01:00
|
|
|
while (start_idx > 0 and
|
2020-05-22 23:03:41 +01:00
|
|
|
(std.ascii.isAlNum(text[start_idx]) or text[start_idx] == '_')) : (start_idx -= 1)
|
2020-05-18 21:19:23 +01:00
|
|
|
{}
|
|
|
|
|
|
|
|
var end_idx = pos_index;
|
2020-05-23 00:33:42 +01:00
|
|
|
while (end_idx < handle.document.text.len and
|
2020-05-22 23:03:41 +01:00
|
|
|
(std.ascii.isAlNum(text[end_idx]) or text[end_idx] == '_')) : (end_idx += 1)
|
2020-05-18 21:19:23 +01:00
|
|
|
{}
|
|
|
|
|
2020-05-23 00:33:42 +01:00
|
|
|
if (end_idx <= start_idx) return &[0]u8{};
|
2020-05-22 23:58:47 +01:00
|
|
|
return text[start_idx + 1 .. end_idx];
|
2020-05-18 21:19:23 +01:00
|
|
|
}
|
|
|
|
|
2020-06-15 10:37:42 +01:00
|
|
|
fn gotoDefinitionSymbol(id: types.RequestId, arena: *std.heap.ArenaAllocator, decl_handle: analysis.DeclWithHandle, resolve_alias: bool) !void {
|
2020-06-10 18:48:40 +01:00
|
|
|
var handle = decl_handle.handle;
|
2020-05-25 23:51:50 +01:00
|
|
|
|
2020-06-10 17:01:44 +01:00
|
|
|
const location = switch (decl_handle.decl.*) {
|
|
|
|
.ast_node => |node| block: {
|
2020-06-15 10:37:42 +01:00
|
|
|
if (resolve_alias) {
|
|
|
|
if (try analysis.resolveVarDeclAlias(&document_store, arena, .{ .node = node, .handle = handle })) |result| {
|
|
|
|
handle = result.handle;
|
2020-07-07 09:57:02 +01:00
|
|
|
break :block result.location(offset_encoding) catch return;
|
2020-06-15 10:37:42 +01:00
|
|
|
}
|
2020-06-14 23:19:21 +01:00
|
|
|
}
|
2020-06-10 18:48:40 +01:00
|
|
|
|
2020-06-14 23:19:21 +01:00
|
|
|
const name_token = analysis.getDeclNameToken(handle.tree, node) orelse
|
2020-06-10 17:01:44 +01:00
|
|
|
return try respondGeneric(id, null_result_response);
|
2020-07-07 09:57:02 +01:00
|
|
|
break :block offsets.tokenRelativeLocation(handle.tree, 0, name_token, offset_encoding) catch return;
|
2020-06-10 17:01:44 +01:00
|
|
|
},
|
2020-07-07 09:57:02 +01:00
|
|
|
else => decl_handle.location(offset_encoding) catch return,
|
2020-06-10 17:01:44 +01:00
|
|
|
};
|
2020-05-25 23:51:50 +01:00
|
|
|
|
2020-06-30 13:46:43 +01:00
|
|
|
try send(arena, types.Response{
|
2020-06-06 13:40:33 +01:00
|
|
|
.id = id,
|
2020-05-25 23:51:50 +01:00
|
|
|
.result = .{
|
|
|
|
.Location = .{
|
2020-06-10 17:01:44 +01:00
|
|
|
.uri = handle.document.uri,
|
2020-07-07 09:57:02 +01:00
|
|
|
.range = .{
|
|
|
|
.start = .{
|
|
|
|
.line = @intCast(i64, location.line),
|
|
|
|
.character = @intCast(i64, location.column),
|
|
|
|
},
|
|
|
|
.end = .{
|
|
|
|
.line = @intCast(i64, location.line),
|
|
|
|
.character = @intCast(i64, location.column),
|
|
|
|
},
|
|
|
|
},
|
2020-05-25 23:51:50 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-14 23:19:21 +01:00
|
|
|
fn hoverSymbol(id: types.RequestId, arena: *std.heap.ArenaAllocator, decl_handle: analysis.DeclWithHandle) (std.os.WriteError || error{OutOfMemory})!void {
|
2020-06-10 20:52:33 +01:00
|
|
|
const handle = decl_handle.handle;
|
|
|
|
|
2020-06-12 15:42:41 +01:00
|
|
|
const hover_kind: types.MarkupKind = if (client_capabilities.hover_supports_md) .Markdown else .PlainText;
|
2020-06-10 20:52:33 +01:00
|
|
|
const md_string = switch (decl_handle.decl.*) {
|
|
|
|
.ast_node => |node| ast_node: {
|
2020-06-14 23:19:21 +01:00
|
|
|
if (try analysis.resolveVarDeclAlias(&document_store, arena, .{ .node = node, .handle = handle })) |result| {
|
|
|
|
return try hoverSymbol(id, arena, result);
|
|
|
|
}
|
2020-06-03 09:23:14 +01:00
|
|
|
|
2020-06-14 23:19:21 +01:00
|
|
|
const doc_str = if (try analysis.getDocComments(&arena.allocator, handle.tree, node, hover_kind)) |str|
|
2020-06-10 18:48:40 +01:00
|
|
|
str
|
|
|
|
else
|
|
|
|
"";
|
2020-05-25 23:51:50 +01:00
|
|
|
|
2020-07-16 15:56:22 +01:00
|
|
|
const signature_str = switch (node.tag) {
|
2020-06-10 18:48:40 +01:00
|
|
|
.VarDecl => blk: {
|
2020-06-14 23:19:21 +01:00
|
|
|
const var_decl = node.cast(std.zig.ast.Node.VarDecl).?;
|
|
|
|
break :blk analysis.getVariableSignature(handle.tree, var_decl);
|
2020-06-10 18:48:40 +01:00
|
|
|
},
|
|
|
|
.FnProto => blk: {
|
2020-06-14 23:19:21 +01:00
|
|
|
const fn_decl = node.cast(std.zig.ast.Node.FnProto).?;
|
|
|
|
break :blk analysis.getFunctionSignature(handle.tree, fn_decl);
|
2020-06-10 18:48:40 +01:00
|
|
|
},
|
2020-06-10 23:00:13 +01:00
|
|
|
.ContainerField => blk: {
|
|
|
|
const field = node.cast(std.zig.ast.Node.ContainerField).?;
|
2020-06-14 23:19:21 +01:00
|
|
|
break :blk analysis.getContainerFieldSignature(handle.tree, field);
|
2020-06-10 23:00:13 +01:00
|
|
|
},
|
2020-06-14 23:19:21 +01:00
|
|
|
else => analysis.nodeToString(handle.tree, node) orelse return try respondGeneric(id, null_result_response),
|
2020-06-10 18:48:40 +01:00
|
|
|
};
|
|
|
|
|
2020-06-12 15:42:41 +01:00
|
|
|
break :ast_node if (hover_kind == .Markdown)
|
|
|
|
try std.fmt.allocPrint(&arena.allocator, "```zig\n{}\n```\n{}", .{ signature_str, doc_str })
|
|
|
|
else
|
|
|
|
try std.fmt.allocPrint(&arena.allocator, "{}\n{}", .{ signature_str, doc_str });
|
2020-06-10 20:52:33 +01:00
|
|
|
},
|
|
|
|
.param_decl => |param| param_decl: {
|
|
|
|
const doc_str = if (param.doc_comments) |doc_comments|
|
2020-06-12 15:42:41 +01:00
|
|
|
try analysis.collectDocComments(&arena.allocator, handle.tree, doc_comments, hover_kind)
|
2020-06-10 20:52:33 +01:00
|
|
|
else
|
|
|
|
"";
|
|
|
|
|
2020-06-12 15:42:41 +01:00
|
|
|
const signature_str = handle.tree.source[handle.tree.token_locs[param.firstToken()].start..handle.tree.token_locs[param.lastToken()].end];
|
|
|
|
break :param_decl if (hover_kind == .Markdown)
|
|
|
|
try std.fmt.allocPrint(&arena.allocator, "```zig\n{}\n```\n{}", .{ signature_str, doc_str })
|
|
|
|
else
|
|
|
|
try std.fmt.allocPrint(&arena.allocator, "{}\n{}", .{ signature_str, doc_str });
|
2020-05-25 23:51:50 +01:00
|
|
|
},
|
2020-06-12 15:42:41 +01:00
|
|
|
.pointer_payload => |payload| if (hover_kind == .Markdown)
|
|
|
|
try std.fmt.allocPrint(&arena.allocator, "```zig\n{}\n```", .{handle.tree.tokenSlice(payload.node.value_symbol.firstToken())})
|
|
|
|
else
|
|
|
|
try std.fmt.allocPrint(&arena.allocator, "{}", .{handle.tree.tokenSlice(payload.node.value_symbol.firstToken())}),
|
|
|
|
.array_payload => |payload| if (hover_kind == .Markdown)
|
|
|
|
try std.fmt.allocPrint(&arena.allocator, "```zig\n{}\n```", .{handle.tree.tokenSlice(payload.identifier.firstToken())})
|
|
|
|
else
|
|
|
|
try std.fmt.allocPrint(&arena.allocator, "{}", .{handle.tree.tokenSlice(payload.identifier.firstToken())}),
|
|
|
|
.switch_payload => |payload| if (hover_kind == .Markdown)
|
|
|
|
try std.fmt.allocPrint(&arena.allocator, "```zig\n{}\n```", .{handle.tree.tokenSlice(payload.node.value_symbol.firstToken())})
|
|
|
|
else
|
|
|
|
try std.fmt.allocPrint(&arena.allocator, "{}", .{handle.tree.tokenSlice(payload.node.value_symbol.firstToken())}),
|
2020-06-14 20:24:18 +01:00
|
|
|
.label_decl => |label_decl| block: {
|
|
|
|
const source = handle.tree.source[handle.tree.token_locs[label_decl.firstToken()].start..handle.tree.token_locs[label_decl.lastToken()].end];
|
|
|
|
break :block if (hover_kind == .Markdown)
|
|
|
|
try std.fmt.allocPrint(&arena.allocator, "```zig\n{}\n```", .{source})
|
|
|
|
else
|
|
|
|
try std.fmt.allocPrint(&arena.allocator, "```{}```", .{source});
|
|
|
|
},
|
2020-06-10 20:52:33 +01:00
|
|
|
};
|
|
|
|
|
2020-06-30 13:46:43 +01:00
|
|
|
try send(arena, types.Response{
|
2020-06-10 20:52:33 +01:00
|
|
|
.id = id,
|
|
|
|
.result = .{
|
|
|
|
.Hover = .{
|
|
|
|
.contents = .{ .value = md_string },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2020-05-25 23:51:50 +01:00
|
|
|
}
|
|
|
|
|
2020-06-14 20:24:18 +01:00
|
|
|
fn getLabelGlobal(pos_index: usize, handle: *DocumentStore.Handle) !?analysis.DeclWithHandle {
|
|
|
|
const name = identifierFromPosition(pos_index, handle.*);
|
|
|
|
if (name.len == 0) return null;
|
|
|
|
|
|
|
|
return try analysis.lookupLabel(handle, name, pos_index);
|
|
|
|
}
|
|
|
|
|
2020-06-10 17:01:44 +01:00
|
|
|
fn getSymbolGlobal(arena: *std.heap.ArenaAllocator, pos_index: usize, handle: *DocumentStore.Handle) !?analysis.DeclWithHandle {
|
|
|
|
const name = identifierFromPosition(pos_index, handle.*);
|
2020-05-25 22:37:18 +01:00
|
|
|
if (name.len == 0) return null;
|
|
|
|
|
2020-06-11 00:40:11 +01:00
|
|
|
return try analysis.lookupSymbolGlobal(&document_store, arena, handle, name, pos_index);
|
2020-05-25 22:37:18 +01:00
|
|
|
}
|
2020-05-21 12:36:14 +01:00
|
|
|
|
2020-06-30 13:46:43 +01:00
|
|
|
fn gotoDefinitionLabel(arena: *std.heap.ArenaAllocator, id: types.RequestId, pos_index: usize, handle: *DocumentStore.Handle, config: Config) !void {
|
2020-06-14 20:24:18 +01:00
|
|
|
const decl = (try getLabelGlobal(pos_index, handle)) orelse return try respondGeneric(id, null_result_response);
|
2020-06-30 13:46:43 +01:00
|
|
|
return try gotoDefinitionSymbol(id, arena, decl, false);
|
2020-06-14 20:24:18 +01:00
|
|
|
}
|
|
|
|
|
2020-06-30 13:46:43 +01:00
|
|
|
fn gotoDefinitionGlobal(arena: *std.heap.ArenaAllocator, id: types.RequestId, pos_index: usize, handle: *DocumentStore.Handle, config: Config, resolve_alias: bool) !void {
|
|
|
|
const decl = (try getSymbolGlobal(arena, pos_index, handle)) orelse return try respondGeneric(id, null_result_response);
|
|
|
|
return try gotoDefinitionSymbol(id, arena, decl, resolve_alias);
|
2020-05-25 23:51:50 +01:00
|
|
|
}
|
2020-05-18 21:19:23 +01:00
|
|
|
|
2020-06-30 13:46:43 +01:00
|
|
|
fn hoverDefinitionLabel(arena: *std.heap.ArenaAllocator, id: types.RequestId, pos_index: usize, handle: *DocumentStore.Handle, config: Config) !void {
|
2020-06-14 20:24:18 +01:00
|
|
|
const decl = (try getLabelGlobal(pos_index, handle)) orelse return try respondGeneric(id, null_result_response);
|
2020-06-30 13:46:43 +01:00
|
|
|
return try hoverSymbol(id, arena, decl);
|
2020-06-14 20:24:18 +01:00
|
|
|
}
|
|
|
|
|
2020-06-30 13:46:43 +01:00
|
|
|
fn hoverDefinitionGlobal(arena: *std.heap.ArenaAllocator, id: types.RequestId, pos_index: usize, handle: *DocumentStore.Handle, config: Config) !void {
|
|
|
|
const decl = (try getSymbolGlobal(arena, pos_index, handle)) orelse return try respondGeneric(id, null_result_response);
|
|
|
|
return try hoverSymbol(id, arena, decl);
|
2020-05-18 21:19:23 +01:00
|
|
|
}
|
|
|
|
|
2020-05-25 22:37:18 +01:00
|
|
|
fn getSymbolFieldAccess(
|
2020-06-10 18:48:40 +01:00
|
|
|
handle: *DocumentStore.Handle,
|
|
|
|
arena: *std.heap.ArenaAllocator,
|
2020-07-03 00:31:28 +01:00
|
|
|
position: offsets.DocumentPosition,
|
2020-05-26 23:45:18 +01:00
|
|
|
range: analysis.SourceRange,
|
2020-05-25 22:37:18 +01:00
|
|
|
config: Config,
|
2020-06-10 18:48:40 +01:00
|
|
|
) !?analysis.DeclWithHandle {
|
2020-07-03 00:31:28 +01:00
|
|
|
const name = identifierFromPosition(position.absolute_index, handle.*);
|
2020-05-25 22:37:18 +01:00
|
|
|
if (name.len == 0) return null;
|
2020-07-03 00:31:28 +01:00
|
|
|
var tokenizer = std.zig.Tokenizer.init(position.line[range.start..range.end]);
|
2020-05-25 22:37:18 +01:00
|
|
|
|
2020-07-07 21:26:12 +01:00
|
|
|
if (try analysis.getFieldAccessType(&document_store, arena, handle, position.absolute_index, &tokenizer)) |result| {
|
2020-07-07 21:50:32 +01:00
|
|
|
const container_handle = result.unwrapped orelse result.original;
|
2020-06-17 03:12:12 +01:00
|
|
|
const container_handle_node = switch (container_handle.type.data) {
|
|
|
|
.other => |n| n,
|
|
|
|
else => return null,
|
|
|
|
};
|
|
|
|
return try analysis.lookupSymbolContainer(
|
|
|
|
&document_store,
|
|
|
|
arena,
|
|
|
|
.{ .node = container_handle_node, .handle = container_handle.handle },
|
|
|
|
name,
|
|
|
|
true,
|
|
|
|
);
|
2020-05-25 22:37:18 +01:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-05-19 20:09:00 +01:00
|
|
|
fn gotoDefinitionFieldAccess(
|
2020-06-30 13:46:43 +01:00
|
|
|
arena: *std.heap.ArenaAllocator,
|
2020-06-06 13:40:33 +01:00
|
|
|
id: types.RequestId,
|
2020-05-19 20:09:00 +01:00
|
|
|
handle: *DocumentStore.Handle,
|
2020-07-03 00:31:28 +01:00
|
|
|
position: offsets.DocumentPosition,
|
2020-05-26 23:45:18 +01:00
|
|
|
range: analysis.SourceRange,
|
2020-05-19 20:09:00 +01:00
|
|
|
config: Config,
|
2020-06-15 10:37:42 +01:00
|
|
|
resolve_alias: bool,
|
2020-05-19 20:09:00 +01:00
|
|
|
) !void {
|
2020-06-30 13:46:43 +01:00
|
|
|
const decl = (try getSymbolFieldAccess(handle, arena, position, range, config)) orelse return try respondGeneric(id, null_result_response);
|
|
|
|
return try gotoDefinitionSymbol(id, arena, decl, resolve_alias);
|
2020-05-25 23:51:50 +01:00
|
|
|
}
|
2020-05-18 21:19:23 +01:00
|
|
|
|
2020-05-25 23:51:50 +01:00
|
|
|
fn hoverDefinitionFieldAccess(
|
2020-06-30 13:46:43 +01:00
|
|
|
arena: *std.heap.ArenaAllocator,
|
2020-06-06 13:40:33 +01:00
|
|
|
id: types.RequestId,
|
2020-05-25 23:51:50 +01:00
|
|
|
handle: *DocumentStore.Handle,
|
2020-07-03 00:31:28 +01:00
|
|
|
position: offsets.DocumentPosition,
|
2020-05-26 23:45:18 +01:00
|
|
|
range: analysis.SourceRange,
|
2020-05-25 23:51:50 +01:00
|
|
|
config: Config,
|
|
|
|
) !void {
|
2020-06-30 13:46:43 +01:00
|
|
|
const decl = (try getSymbolFieldAccess(handle, arena, position, range, config)) orelse return try respondGeneric(id, null_result_response);
|
|
|
|
return try hoverSymbol(id, arena, decl);
|
2020-05-18 21:19:23 +01:00
|
|
|
}
|
|
|
|
|
2020-06-30 13:46:43 +01:00
|
|
|
fn gotoDefinitionString(arena: *std.heap.ArenaAllocator, id: types.RequestId, pos_index: usize, handle: *DocumentStore.Handle, config: Config) !void {
|
2020-05-24 17:00:21 +01:00
|
|
|
const tree = handle.tree;
|
2020-04-27 21:38:35 +01:00
|
|
|
|
2020-05-22 16:51:57 +01:00
|
|
|
const import_str = analysis.getImportStr(tree, pos_index) orelse return try respondGeneric(id, null_result_response);
|
2020-05-23 14:14:03 +01:00
|
|
|
const uri = (try document_store.uriFromImportStr(
|
2020-05-22 16:51:57 +01:00
|
|
|
&arena.allocator,
|
|
|
|
handle.*,
|
|
|
|
import_str,
|
|
|
|
)) orelse return try respondGeneric(id, null_result_response);
|
|
|
|
|
2020-06-30 13:46:43 +01:00
|
|
|
try send(arena, types.Response{
|
2020-06-06 13:40:33 +01:00
|
|
|
.id = id,
|
2020-05-22 16:51:57 +01:00
|
|
|
.result = .{
|
|
|
|
.Location = .{
|
|
|
|
.uri = uri,
|
|
|
|
.range = .{
|
|
|
|
.start = .{ .line = 0, .character = 0 },
|
|
|
|
.end = .{ .line = 0, .character = 0 },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-30 13:46:43 +01:00
|
|
|
fn renameDefinitionGlobal(arena: *std.heap.ArenaAllocator, id: types.RequestId, handle: *DocumentStore.Handle, pos_index: usize, new_name: []const u8) !void {
|
|
|
|
const decl = (try getSymbolGlobal(arena, pos_index, handle)) orelse return try respondGeneric(id, null_result_response);
|
2020-06-27 13:29:45 +01:00
|
|
|
|
|
|
|
var workspace_edit = types.WorkspaceEdit{
|
|
|
|
.changes = std.StringHashMap([]types.TextEdit).init(&arena.allocator),
|
|
|
|
};
|
2020-07-04 23:40:18 +01:00
|
|
|
try rename.renameSymbol(arena, &document_store, decl, new_name, &workspace_edit.changes.?, offset_encoding);
|
2020-06-30 13:46:43 +01:00
|
|
|
try send(arena, types.Response{
|
2020-06-27 13:29:45 +01:00
|
|
|
.id = id,
|
|
|
|
.result = .{ .WorkspaceEdit = workspace_edit },
|
|
|
|
});
|
2020-06-27 01:16:14 +01:00
|
|
|
}
|
|
|
|
|
2020-06-27 13:29:45 +01:00
|
|
|
fn renameDefinitionFieldAccess(
|
2020-06-30 13:46:43 +01:00
|
|
|
arena: *std.heap.ArenaAllocator,
|
2020-06-27 13:29:45 +01:00
|
|
|
id: types.RequestId,
|
|
|
|
handle: *DocumentStore.Handle,
|
2020-07-03 00:31:28 +01:00
|
|
|
position: offsets.DocumentPosition,
|
2020-06-27 13:29:45 +01:00
|
|
|
range: analysis.SourceRange,
|
|
|
|
new_name: []const u8,
|
|
|
|
config: Config,
|
|
|
|
) !void {
|
2020-06-30 13:46:43 +01:00
|
|
|
const decl = (try getSymbolFieldAccess(handle, arena, position, range, config)) orelse return try respondGeneric(id, null_result_response);
|
2020-06-27 13:29:45 +01:00
|
|
|
|
|
|
|
var workspace_edit = types.WorkspaceEdit{
|
|
|
|
.changes = std.StringHashMap([]types.TextEdit).init(&arena.allocator),
|
|
|
|
};
|
2020-07-04 23:40:18 +01:00
|
|
|
try rename.renameSymbol(arena, &document_store, decl, new_name, &workspace_edit.changes.?, offset_encoding);
|
2020-06-30 13:46:43 +01:00
|
|
|
try send(arena, types.Response{
|
2020-06-27 13:29:45 +01:00
|
|
|
.id = id,
|
|
|
|
.result = .{ .WorkspaceEdit = workspace_edit },
|
|
|
|
});
|
2020-06-27 01:16:14 +01:00
|
|
|
}
|
|
|
|
|
2020-06-30 13:46:43 +01:00
|
|
|
fn renameDefinitionLabel(arena: *std.heap.ArenaAllocator, id: types.RequestId, handle: *DocumentStore.Handle, pos_index: usize, new_name: []const u8) !void {
|
2020-06-27 01:16:14 +01:00
|
|
|
const decl = (try getLabelGlobal(pos_index, handle)) orelse return try respondGeneric(id, null_result_response);
|
|
|
|
|
|
|
|
var workspace_edit = types.WorkspaceEdit{
|
|
|
|
.changes = std.StringHashMap([]types.TextEdit).init(&arena.allocator),
|
|
|
|
};
|
2020-07-04 23:40:18 +01:00
|
|
|
try rename.renameLabel(arena, decl, new_name, &workspace_edit.changes.?, offset_encoding);
|
2020-06-30 13:46:43 +01:00
|
|
|
try send(arena, types.Response{
|
2020-06-27 01:16:14 +01:00
|
|
|
.id = id,
|
|
|
|
.result = .{ .WorkspaceEdit = workspace_edit },
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-05 23:32:14 +01:00
|
|
|
fn referencesDefinitionGlobal(arena: *std.heap.ArenaAllocator, id: types.RequestId, handle: *DocumentStore.Handle, pos_index: usize, include_decl: bool) !void {
|
|
|
|
const decl = (try getSymbolGlobal(arena, pos_index, handle)) orelse return try respondGeneric(id, null_result_response);
|
|
|
|
var locs = std.ArrayList(types.Location).init(&arena.allocator);
|
|
|
|
try references.symbolReferences(arena, &document_store, decl, offset_encoding, include_decl, &locs, std.ArrayList(types.Location).append);
|
|
|
|
try send(arena, types.Response{
|
|
|
|
.id = id,
|
|
|
|
.result = .{ .Locations = locs.items },
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn referencesDefinitionFieldAccess(
|
|
|
|
arena: *std.heap.ArenaAllocator,
|
|
|
|
id: types.RequestId,
|
|
|
|
handle: *DocumentStore.Handle,
|
|
|
|
position: offsets.DocumentPosition,
|
|
|
|
range: analysis.SourceRange,
|
|
|
|
include_decl: bool,
|
|
|
|
config: Config,
|
|
|
|
) !void {
|
|
|
|
const decl = (try getSymbolFieldAccess(handle, arena, position, range, config)) orelse return try respondGeneric(id, null_result_response);
|
|
|
|
var locs = std.ArrayList(types.Location).init(&arena.allocator);
|
|
|
|
try references.symbolReferences(arena, &document_store, decl, offset_encoding, include_decl, &locs, std.ArrayList(types.Location).append);
|
|
|
|
try send(arena, types.Response{
|
|
|
|
.id = id,
|
|
|
|
.result = .{ .Locations = locs.items },
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn referencesDefinitionLabel(arena: *std.heap.ArenaAllocator, id: types.RequestId, handle: *DocumentStore.Handle, pos_index: usize, include_decl: bool) !void {
|
|
|
|
const decl = (try getLabelGlobal(pos_index, handle)) orelse return try respondGeneric(id, null_result_response);
|
|
|
|
var locs = std.ArrayList(types.Location).init(&arena.allocator);
|
|
|
|
try references.labelReferences(arena, decl, offset_encoding, include_decl, &locs, std.ArrayList(types.Location).append);
|
|
|
|
try send(arena, types.Response{
|
|
|
|
.id = id,
|
|
|
|
.result = .{ .Locations = locs.items },
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-10 17:01:44 +01:00
|
|
|
const DeclToCompletionContext = struct {
|
|
|
|
completions: *std.ArrayList(types.CompletionItem),
|
|
|
|
config: *const Config,
|
|
|
|
arena: *std.heap.ArenaAllocator,
|
2020-06-10 18:48:40 +01:00
|
|
|
orig_handle: *DocumentStore.Handle,
|
2020-06-10 17:01:44 +01:00
|
|
|
};
|
|
|
|
|
2020-06-11 00:40:11 +01:00
|
|
|
fn declToCompletion(context: DeclToCompletionContext, decl_handle: analysis.DeclWithHandle) !void {
|
2020-06-10 20:52:33 +01:00
|
|
|
const tree = decl_handle.handle.tree;
|
|
|
|
|
2020-06-10 17:01:44 +01:00
|
|
|
switch (decl_handle.decl.*) {
|
2020-07-07 21:26:12 +01:00
|
|
|
.ast_node => |node| try nodeToCompletion(context.arena, context.completions, .{ .node = node, .handle = decl_handle.handle }, null, context.orig_handle, false, context.config.*),
|
2020-06-10 20:52:33 +01:00
|
|
|
.param_decl => |param| {
|
2020-06-12 15:42:41 +01:00
|
|
|
const doc_kind: types.MarkupKind = if (client_capabilities.completion_doc_supports_md) .Markdown else .PlainText;
|
2020-06-10 20:52:33 +01:00
|
|
|
const doc = if (param.doc_comments) |doc_comments|
|
|
|
|
types.MarkupContent{
|
2020-06-12 15:42:41 +01:00
|
|
|
.kind = doc_kind,
|
|
|
|
.value = try analysis.collectDocComments(&context.arena.allocator, tree, doc_comments, doc_kind),
|
2020-06-10 20:52:33 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
null;
|
|
|
|
|
|
|
|
try context.completions.append(.{
|
|
|
|
.label = tree.tokenSlice(param.name_token.?),
|
|
|
|
.kind = .Constant,
|
|
|
|
.documentation = doc,
|
|
|
|
.detail = tree.source[tree.token_locs[param.firstToken()].start..tree.token_locs[param.lastToken()].end],
|
|
|
|
});
|
|
|
|
},
|
|
|
|
.pointer_payload => |payload| {
|
|
|
|
try context.completions.append(.{
|
|
|
|
.label = tree.tokenSlice(payload.node.value_symbol.firstToken()),
|
|
|
|
.kind = .Variable,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
.array_payload => |payload| {
|
|
|
|
try context.completions.append(.{
|
|
|
|
.label = tree.tokenSlice(payload.identifier.firstToken()),
|
|
|
|
.kind = .Variable,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
.switch_payload => |payload| {
|
|
|
|
try context.completions.append(.{
|
|
|
|
.label = tree.tokenSlice(payload.node.value_symbol.firstToken()),
|
|
|
|
.kind = .Variable,
|
|
|
|
});
|
2020-06-10 17:01:44 +01:00
|
|
|
},
|
2020-06-14 20:24:18 +01:00
|
|
|
.label_decl => |label_decl| {
|
|
|
|
try context.completions.append(.{
|
|
|
|
.label = tree.tokenSlice(label_decl.firstToken()),
|
|
|
|
.kind = .Variable,
|
|
|
|
});
|
|
|
|
},
|
2020-06-10 17:01:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-30 13:46:43 +01:00
|
|
|
fn completeLabel(arena: *std.heap.ArenaAllocator, id: types.RequestId, pos_index: usize, handle: *DocumentStore.Handle, config: Config) !void {
|
2020-06-14 20:24:18 +01:00
|
|
|
var completions = std.ArrayList(types.CompletionItem).init(&arena.allocator);
|
|
|
|
|
|
|
|
const context = DeclToCompletionContext{
|
|
|
|
.completions = &completions,
|
|
|
|
.config = &config,
|
2020-06-30 13:46:43 +01:00
|
|
|
.arena = arena,
|
2020-06-14 20:24:18 +01:00
|
|
|
.orig_handle = handle,
|
|
|
|
};
|
|
|
|
try analysis.iterateLabels(handle, pos_index, declToCompletion, context);
|
|
|
|
|
2020-06-30 13:46:43 +01:00
|
|
|
try send(arena, types.Response{
|
2020-06-14 20:24:18 +01:00
|
|
|
.id = id,
|
|
|
|
.result = .{
|
|
|
|
.CompletionList = .{
|
|
|
|
.isIncomplete = false,
|
|
|
|
.items = completions.items,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-30 13:46:43 +01:00
|
|
|
fn completeGlobal(arena: *std.heap.ArenaAllocator, id: types.RequestId, pos_index: usize, handle: *DocumentStore.Handle, config: Config) !void {
|
2020-05-07 12:10:58 +01:00
|
|
|
var completions = std.ArrayList(types.CompletionItem).init(&arena.allocator);
|
2020-05-07 11:56:08 +01:00
|
|
|
|
2020-06-10 17:01:44 +01:00
|
|
|
const context = DeclToCompletionContext{
|
|
|
|
.completions = &completions,
|
|
|
|
.config = &config,
|
2020-06-30 13:46:43 +01:00
|
|
|
.arena = arena,
|
2020-06-10 18:48:40 +01:00
|
|
|
.orig_handle = handle,
|
2020-06-10 17:01:44 +01:00
|
|
|
};
|
2020-06-30 13:46:43 +01:00
|
|
|
try analysis.iterateSymbolsGlobal(&document_store, arena, handle, pos_index, declToCompletion, context);
|
2020-06-09 04:21:55 +01:00
|
|
|
|
2020-06-30 13:46:43 +01:00
|
|
|
try send(arena, types.Response{
|
2020-06-06 13:40:33 +01:00
|
|
|
.id = id,
|
2020-05-07 13:29:53 +01:00
|
|
|
.result = .{
|
|
|
|
.CompletionList = .{
|
2020-04-27 21:38:35 +01:00
|
|
|
.isIncomplete = false,
|
2020-05-07 11:56:08 +01:00
|
|
|
.items = completions.items,
|
2020-05-07 13:29:53 +01:00
|
|
|
},
|
|
|
|
},
|
2020-04-27 21:38:35 +01:00
|
|
|
});
|
2020-04-24 23:19:03 +01:00
|
|
|
}
|
|
|
|
|
2020-07-03 00:31:28 +01:00
|
|
|
fn completeFieldAccess(
|
|
|
|
arena: *std.heap.ArenaAllocator,
|
|
|
|
id: types.RequestId,
|
|
|
|
handle: *DocumentStore.Handle,
|
|
|
|
position: offsets.DocumentPosition,
|
|
|
|
range: analysis.SourceRange,
|
|
|
|
config: Config,
|
|
|
|
) !void {
|
2020-05-14 00:10:41 +01:00
|
|
|
var completions = std.ArrayList(types.CompletionItem).init(&arena.allocator);
|
2020-07-03 00:31:28 +01:00
|
|
|
var tokenizer = std.zig.Tokenizer.init(position.line[range.start..range.end]);
|
2020-07-07 21:26:12 +01:00
|
|
|
if (try analysis.getFieldAccessType(&document_store, arena, handle, position.absolute_index, &tokenizer)) |result| {
|
|
|
|
try typeToCompletion(arena, &completions, result, handle, config);
|
2020-05-13 15:10:20 +01:00
|
|
|
}
|
2020-06-10 18:48:40 +01:00
|
|
|
|
2020-06-30 13:46:43 +01:00
|
|
|
try send(arena, types.Response{
|
2020-06-06 13:40:33 +01:00
|
|
|
.id = id,
|
2020-05-14 00:10:41 +01:00
|
|
|
.result = .{
|
|
|
|
.CompletionList = .{
|
|
|
|
.isIncomplete = false,
|
|
|
|
.items = completions.items,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2020-05-11 13:28:08 +01:00
|
|
|
}
|
2020-05-07 12:36:40 +01:00
|
|
|
|
2020-06-30 13:46:43 +01:00
|
|
|
fn documentSymbol(arena: *std.heap.ArenaAllocator, id: types.RequestId, handle: *DocumentStore.Handle) !void {
|
|
|
|
try send(arena, types.Response{
|
2020-06-06 13:40:33 +01:00
|
|
|
.id = id,
|
2020-07-07 09:57:02 +01:00
|
|
|
.result = .{ .DocumentSymbols = try analysis.getDocumentSymbols(&arena.allocator, handle.tree, offset_encoding) },
|
2020-05-28 01:39:36 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-07 12:36:40 +01:00
|
|
|
// Compute builtin completions at comptime.
|
|
|
|
const builtin_completions = block: {
|
|
|
|
@setEvalBranchQuota(3_500);
|
2020-05-09 14:43:51 +01:00
|
|
|
const CompletionList = [data.builtins.len]types.CompletionItem;
|
|
|
|
var with_snippets: CompletionList = undefined;
|
|
|
|
var without_snippets: CompletionList = undefined;
|
2020-05-07 12:36:40 +01:00
|
|
|
|
|
|
|
for (data.builtins) |builtin, i| {
|
2020-05-09 14:43:51 +01:00
|
|
|
const cutoff = std.mem.indexOf(u8, builtin, "(") orelse builtin.len;
|
|
|
|
|
|
|
|
const base_completion = types.CompletionItem{
|
2020-05-08 00:53:00 +01:00
|
|
|
.label = builtin[0..cutoff],
|
|
|
|
.kind = .Function,
|
|
|
|
|
|
|
|
.filterText = builtin[1..cutoff],
|
|
|
|
.detail = data.builtin_details[i],
|
|
|
|
.documentation = .{
|
|
|
|
.kind = .Markdown,
|
|
|
|
.value = data.builtin_docs[i],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-05-09 14:43:51 +01:00
|
|
|
with_snippets[i] = base_completion;
|
|
|
|
with_snippets[i].insertText = builtin[1..];
|
|
|
|
with_snippets[i].insertTextFormat = .Snippet;
|
|
|
|
|
|
|
|
without_snippets[i] = base_completion;
|
|
|
|
without_snippets[i].insertText = builtin[1..cutoff];
|
2020-05-07 12:36:40 +01:00
|
|
|
}
|
|
|
|
|
2020-05-14 22:16:40 +01:00
|
|
|
break :block [2]CompletionList{
|
|
|
|
without_snippets, with_snippets,
|
2020-05-09 14:43:51 +01:00
|
|
|
};
|
2020-05-07 12:36:40 +01:00
|
|
|
};
|
|
|
|
|
2020-05-19 20:09:00 +01:00
|
|
|
fn loadConfig(folder_path: []const u8) ?Config {
|
|
|
|
var folder = std.fs.cwd().openDir(folder_path, .{}) catch return null;
|
|
|
|
defer folder.close();
|
|
|
|
|
2020-06-28 11:58:51 +01:00
|
|
|
const file_buf = folder.readFileAlloc(allocator, "zls.json", 0x1000000) catch |err| {
|
|
|
|
if (err != error.FileNotFound)
|
2020-08-14 11:41:34 +01:00
|
|
|
logger.warn("Error while reading configuration file: {}\n", .{err});
|
2020-06-28 11:58:51 +01:00
|
|
|
return null;
|
|
|
|
};
|
2020-05-19 20:09:00 +01:00
|
|
|
defer allocator.free(file_buf);
|
|
|
|
|
|
|
|
// TODO: Better errors? Doesn't seem like std.json can provide us positions or context.
|
|
|
|
var config = std.json.parse(Config, &std.json.TokenStream.init(file_buf), std.json.ParseOptions{ .allocator = allocator }) catch |err| {
|
2020-08-14 11:41:34 +01:00
|
|
|
logger.warn("Error while parsing configuration file: {}\n", .{err});
|
2020-05-19 20:09:00 +01:00
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (config.zig_lib_path) |zig_lib_path| {
|
|
|
|
if (!std.fs.path.isAbsolute(zig_lib_path)) {
|
2020-08-14 11:41:34 +01:00
|
|
|
logger.warn("zig library path is not absolute, defaulting to null.\n", .{});
|
2020-05-19 20:09:00 +01:00
|
|
|
allocator.free(zig_lib_path);
|
|
|
|
config.zig_lib_path = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn loadWorkspaceConfigs() !void {
|
|
|
|
var folder_config_it = workspace_folder_configs.iterator();
|
|
|
|
while (folder_config_it.next()) |entry| {
|
|
|
|
if (entry.value) |_| continue;
|
|
|
|
|
|
|
|
const folder_path = try URI.parse(allocator, entry.key);
|
|
|
|
defer allocator.free(folder_path);
|
|
|
|
|
|
|
|
entry.value = loadConfig(folder_path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn configFromUriOr(uri: []const u8, default: Config) Config {
|
|
|
|
var folder_config_it = workspace_folder_configs.iterator();
|
|
|
|
while (folder_config_it.next()) |entry| {
|
|
|
|
if (std.mem.startsWith(u8, uri, entry.key)) {
|
|
|
|
return entry.value orelse default;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return default;
|
|
|
|
}
|
|
|
|
|
2020-06-29 23:34:21 +01:00
|
|
|
fn initializeHandler(arena: *std.heap.ArenaAllocator, id: types.RequestId, req: requests.Initialize, config: Config) !void {
|
2020-07-03 10:00:00 +01:00
|
|
|
var send_encoding = req.params.capabilities.offsetEncoding.value.len != 0;
|
|
|
|
for (req.params.capabilities.offsetEncoding.value) |encoding| {
|
|
|
|
if (std.mem.eql(u8, encoding, "utf-8")) {
|
|
|
|
offset_encoding = .utf8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-29 23:34:21 +01:00
|
|
|
if (req.params.capabilities.workspace) |workspace| {
|
|
|
|
client_capabilities.supports_workspace_folders = workspace.workspaceFolders.value;
|
2020-05-24 13:39:40 +01:00
|
|
|
}
|
|
|
|
|
2020-06-29 23:34:21 +01:00
|
|
|
if (req.params.capabilities.textDocument) |textDocument| {
|
|
|
|
client_capabilities.supports_semantic_tokens = textDocument.semanticTokens.exists;
|
|
|
|
if (textDocument.hover) |hover| {
|
|
|
|
for (hover.contentFormat.value) |format| {
|
|
|
|
if (std.mem.eql(u8, "markdown", format)) {
|
|
|
|
client_capabilities.hover_supports_md = true;
|
|
|
|
}
|
2020-06-16 16:49:31 +01:00
|
|
|
}
|
|
|
|
}
|
2020-06-29 23:34:21 +01:00
|
|
|
if (textDocument.completion) |completion| {
|
|
|
|
if (completion.completionItem) |completionItem| {
|
|
|
|
client_capabilities.supports_snippets = completionItem.snippetSupport.value;
|
|
|
|
for (completionItem.documentationFormat.value) |documentationFormat| {
|
|
|
|
if (std.mem.eql(u8, "markdown", documentationFormat)) {
|
|
|
|
client_capabilities.completion_doc_supports_md = true;
|
2020-06-12 15:42:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-29 23:34:21 +01:00
|
|
|
}
|
|
|
|
}
|
2020-06-12 15:42:41 +01:00
|
|
|
|
2020-06-29 23:34:21 +01:00
|
|
|
if (req.params.workspaceFolders) |workspaceFolders| {
|
|
|
|
if (workspaceFolders.len != 0) {
|
2020-08-14 11:41:34 +01:00
|
|
|
logger.debug("Got workspace folders in initialization.\n", .{});
|
2020-06-29 23:34:21 +01:00
|
|
|
}
|
|
|
|
for (workspaceFolders) |workspace_folder| {
|
2020-08-14 11:41:34 +01:00
|
|
|
logger.debug("Loaded folder {}\n", .{workspace_folder.uri});
|
2020-06-29 23:34:21 +01:00
|
|
|
const duped_uri = try std.mem.dupe(allocator, u8, workspace_folder.uri);
|
|
|
|
try workspace_folder_configs.putNoClobber(duped_uri, null);
|
2020-06-01 11:29:06 +01:00
|
|
|
}
|
2020-06-29 23:34:21 +01:00
|
|
|
try loadWorkspaceConfigs();
|
|
|
|
}
|
2020-06-01 11:29:06 +01:00
|
|
|
|
2020-07-03 10:00:00 +01:00
|
|
|
if (!send_encoding) {
|
|
|
|
try respondGeneric(id, initialize_response);
|
|
|
|
} else {
|
|
|
|
const response_str = try std.fmt.allocPrint(&arena.allocator, ",\"result\": {{\"offsetEncoding\":\"{}\",{}", .{
|
2020-07-03 10:24:24 +01:00
|
|
|
if (offset_encoding == .utf8) @as([]const u8, "utf-8") else @as([]const u8, "utf-16"),
|
|
|
|
initialize_capabilities,
|
2020-07-03 10:00:00 +01:00
|
|
|
});
|
|
|
|
try respondGeneric(id, response_str);
|
|
|
|
}
|
2020-08-14 11:41:34 +01:00
|
|
|
logger.notice("zls initialized", .{});
|
|
|
|
logger.info("{}\n", .{client_capabilities});
|
|
|
|
logger.notice("Using offset encoding: {}\n", .{std.meta.tagName(offset_encoding)});
|
2020-06-29 23:34:21 +01:00
|
|
|
}
|
2020-06-16 16:49:31 +01:00
|
|
|
|
2020-06-29 23:34:21 +01:00
|
|
|
var keep_running = true;
|
|
|
|
fn shutdownHandler(arena: *std.heap.ArenaAllocator, id: types.RequestId, config: Config) !void {
|
|
|
|
keep_running = false;
|
|
|
|
// Technically we should deinitialize first and send possible errors to the client
|
|
|
|
try respondGeneric(id, null_result_response);
|
|
|
|
}
|
2020-06-16 16:49:31 +01:00
|
|
|
|
2020-06-29 23:34:21 +01:00
|
|
|
fn workspaceFoldersChangeHandler(arena: *std.heap.ArenaAllocator, id: types.RequestId, req: requests.WorkspaceFoldersChange, config: Config) !void {
|
|
|
|
for (req.params.event.removed) |rem| {
|
|
|
|
if (workspace_folder_configs.remove(rem.uri)) |entry| {
|
|
|
|
allocator.free(entry.key);
|
|
|
|
if (entry.value) |c| {
|
|
|
|
std.json.parseFree(Config, c, std.json.ParseOptions{ .allocator = allocator });
|
2020-05-19 20:09:00 +01:00
|
|
|
}
|
|
|
|
}
|
2020-06-29 23:34:21 +01:00
|
|
|
}
|
2020-05-19 20:09:00 +01:00
|
|
|
|
2020-06-29 23:34:21 +01:00
|
|
|
for (req.params.event.added) |add| {
|
|
|
|
const duped_uri = try std.mem.dupe(allocator, u8, add.uri);
|
2020-07-05 22:56:41 +01:00
|
|
|
if (try workspace_folder_configs.fetchPut(duped_uri, null)) |old| {
|
2020-06-29 23:34:21 +01:00
|
|
|
allocator.free(old.key);
|
|
|
|
if (old.value) |c| {
|
|
|
|
std.json.parseFree(Config, c, std.json.ParseOptions{ .allocator = allocator });
|
2020-05-19 20:09:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-24 23:19:03 +01:00
|
|
|
|
2020-06-29 23:34:21 +01:00
|
|
|
try loadWorkspaceConfigs();
|
|
|
|
}
|
2020-06-03 09:32:05 +01:00
|
|
|
|
2020-06-29 23:34:21 +01:00
|
|
|
fn openDocumentHandler(arena: *std.heap.ArenaAllocator, id: types.RequestId, req: requests.OpenDocument, config: Config) !void {
|
|
|
|
const handle = try document_store.openDocument(req.params.textDocument.uri, req.params.textDocument.text);
|
|
|
|
try publishDiagnostics(arena, handle.*, configFromUriOr(req.params.textDocument.uri, config));
|
2020-06-30 16:00:33 +01:00
|
|
|
|
|
|
|
try semanticTokensHandler(arena, id, .{ .params = .{ .textDocument = .{ .uri = req.params.textDocument.uri } } }, config);
|
2020-06-29 23:34:21 +01:00
|
|
|
}
|
2020-04-27 21:38:35 +01:00
|
|
|
|
2020-06-29 23:34:21 +01:00
|
|
|
fn changeDocumentHandler(arena: *std.heap.ArenaAllocator, id: types.RequestId, req: requests.ChangeDocument, config: Config) !void {
|
|
|
|
const handle = document_store.getHandle(req.params.textDocument.uri) orelse {
|
2020-08-14 11:41:34 +01:00
|
|
|
logger.debug("Trying to change non existent document {}", .{req.params.textDocument.uri});
|
2020-06-29 23:34:21 +01:00
|
|
|
return;
|
|
|
|
};
|
2020-06-13 19:20:04 +01:00
|
|
|
|
2020-06-29 23:34:21 +01:00
|
|
|
const local_config = configFromUriOr(req.params.textDocument.uri, config);
|
|
|
|
try document_store.applyChanges(handle, req.params.contentChanges.Array, local_config.zig_lib_path);
|
|
|
|
try publishDiagnostics(arena, handle.*, local_config);
|
|
|
|
}
|
2020-06-13 19:20:04 +01:00
|
|
|
|
2020-06-30 16:00:33 +01:00
|
|
|
fn saveDocumentHandler(arena: *std.heap.ArenaAllocator, id: types.RequestId, req: requests.SaveDocument, config: Config) error{OutOfMemory}!void {
|
2020-06-29 23:34:21 +01:00
|
|
|
const handle = document_store.getHandle(req.params.textDocument.uri) orelse {
|
2020-08-14 11:41:34 +01:00
|
|
|
logger.warn("Trying to save non existent document {}", .{req.params.textDocument.uri});
|
2020-06-29 23:34:21 +01:00
|
|
|
return;
|
|
|
|
};
|
|
|
|
try document_store.applySave(handle);
|
|
|
|
}
|
|
|
|
|
2020-06-30 16:00:33 +01:00
|
|
|
fn closeDocumentHandler(arena: *std.heap.ArenaAllocator, id: types.RequestId, req: requests.CloseDocument, config: Config) error{}!void {
|
2020-06-29 23:34:21 +01:00
|
|
|
document_store.closeDocument(req.params.textDocument.uri);
|
|
|
|
}
|
|
|
|
|
2020-06-30 16:00:33 +01:00
|
|
|
fn semanticTokensHandler(arena: *std.heap.ArenaAllocator, id: types.RequestId, req: requests.SemanticTokens, config: Config) (error{OutOfMemory} || std.fs.File.WriteError)!void {
|
2020-06-29 23:34:21 +01:00
|
|
|
const this_config = configFromUriOr(req.params.textDocument.uri, config);
|
|
|
|
if (this_config.enable_semantic_tokens) {
|
|
|
|
const handle = document_store.getHandle(req.params.textDocument.uri) orelse {
|
2020-08-14 11:41:34 +01:00
|
|
|
logger.warn("Trying to get semantic tokens of non existent document {}", .{req.params.textDocument.uri});
|
2020-06-16 12:27:00 +01:00
|
|
|
return try respondGeneric(id, no_semantic_tokens_response);
|
2020-05-14 00:10:41 +01:00
|
|
|
};
|
|
|
|
|
2020-06-29 23:34:21 +01:00
|
|
|
const semantic_tokens = @import("semantic_tokens.zig");
|
2020-07-03 09:34:42 +01:00
|
|
|
const token_array = try semantic_tokens.writeAllSemanticTokens(arena, &document_store, handle, offset_encoding);
|
2020-07-02 17:13:10 +01:00
|
|
|
defer allocator.free(token_array);
|
2020-05-18 21:19:23 +01:00
|
|
|
|
2020-06-29 23:34:21 +01:00
|
|
|
return try send(arena, types.Response{
|
|
|
|
.id = id,
|
|
|
|
.result = .{ .SemanticTokens = .{ .data = token_array } },
|
|
|
|
});
|
2020-07-23 20:09:28 +01:00
|
|
|
}
|
2020-06-29 23:34:21 +01:00
|
|
|
}
|
2020-05-25 22:37:18 +01:00
|
|
|
|
2020-06-30 13:46:43 +01:00
|
|
|
fn completionHandler(arena: *std.heap.ArenaAllocator, id: types.RequestId, req: requests.Completion, config: Config) !void {
|
|
|
|
const handle = document_store.getHandle(req.params.textDocument.uri) orelse {
|
2020-08-14 11:41:34 +01:00
|
|
|
logger.warn("Trying to complete in non existent document {}", .{req.params.textDocument.uri});
|
2020-06-30 13:46:43 +01:00
|
|
|
return try respondGeneric(id, no_completions_response);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (req.params.position.character >= 0) {
|
2020-07-03 09:34:42 +01:00
|
|
|
const doc_position = try offsets.documentPosition(handle.document, req.params.position, offset_encoding);
|
2020-07-03 00:31:28 +01:00
|
|
|
const pos_context = try analysis.documentPositionContext(arena, handle.document, doc_position);
|
2020-06-30 13:46:43 +01:00
|
|
|
|
|
|
|
const this_config = configFromUriOr(req.params.textDocument.uri, config);
|
|
|
|
const use_snippets = this_config.enable_snippets and client_capabilities.supports_snippets;
|
|
|
|
switch (pos_context) {
|
|
|
|
.builtin => try send(arena, types.Response{
|
|
|
|
.id = id,
|
|
|
|
.result = .{
|
|
|
|
.CompletionList = .{
|
|
|
|
.isIncomplete = false,
|
|
|
|
.items = builtin_completions[@boolToInt(use_snippets)][0..],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
2020-07-03 00:31:28 +01:00
|
|
|
.var_access, .empty => try completeGlobal(arena, id, doc_position.absolute_index, handle, this_config),
|
|
|
|
.field_access => |range| try completeFieldAccess(arena, id, handle, doc_position, range, this_config),
|
2020-06-30 13:46:43 +01:00
|
|
|
.global_error_set => try send(arena, types.Response{
|
|
|
|
.id = id,
|
|
|
|
.result = .{
|
|
|
|
.CompletionList = .{
|
|
|
|
.isIncomplete = false,
|
2020-07-08 02:05:44 +01:00
|
|
|
.items = try document_store.errorCompletionItems(arena, handle),
|
2020-06-30 13:46:43 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
.enum_literal => try send(arena, types.Response{
|
|
|
|
.id = id,
|
|
|
|
.result = .{
|
|
|
|
.CompletionList = .{
|
|
|
|
.isIncomplete = false,
|
2020-07-08 02:05:44 +01:00
|
|
|
.items = try document_store.enumCompletionItems(arena, handle),
|
2020-06-30 13:46:43 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
2020-07-03 00:31:28 +01:00
|
|
|
.label => try completeLabel(arena, id, doc_position.absolute_index, handle, this_config),
|
2020-06-30 13:46:43 +01:00
|
|
|
else => try respondGeneric(id, no_completions_response),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
try respondGeneric(id, no_completions_response);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signatureHelperHandler(arena: *std.heap.ArenaAllocator, id: types.RequestId, config: Config) !void {
|
|
|
|
// TODO Implement this
|
|
|
|
try respondGeneric(id,
|
|
|
|
\\,"result":{"signatures":[]}}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn gotoHandler(arena: *std.heap.ArenaAllocator, id: types.RequestId, req: requests.GotoDefinition, config: Config, resolve_alias: bool) !void {
|
|
|
|
const handle = document_store.getHandle(req.params.textDocument.uri) orelse {
|
2020-08-14 11:41:34 +01:00
|
|
|
logger.warn("Trying to go to definition in non existent document {}", .{req.params.textDocument.uri});
|
2020-06-30 13:46:43 +01:00
|
|
|
return try respondGeneric(id, null_result_response);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (req.params.position.character >= 0) {
|
2020-07-03 09:34:42 +01:00
|
|
|
const doc_position = try offsets.documentPosition(handle.document, req.params.position, offset_encoding);
|
2020-07-03 00:31:28 +01:00
|
|
|
const pos_context = try analysis.documentPositionContext(arena, handle.document, doc_position);
|
2020-06-30 13:46:43 +01:00
|
|
|
|
|
|
|
const this_config = configFromUriOr(req.params.textDocument.uri, config);
|
|
|
|
switch (pos_context) {
|
2020-07-03 00:31:28 +01:00
|
|
|
.var_access => try gotoDefinitionGlobal(arena, id, doc_position.absolute_index, handle, this_config, resolve_alias),
|
|
|
|
.field_access => |range| try gotoDefinitionFieldAccess(arena, id, handle, doc_position, range, this_config, resolve_alias),
|
|
|
|
.string_literal => try gotoDefinitionString(arena, id, doc_position.absolute_index, handle, config),
|
|
|
|
.label => try gotoDefinitionLabel(arena, id, doc_position.absolute_index, handle, this_config),
|
2020-06-30 13:46:43 +01:00
|
|
|
else => try respondGeneric(id, null_result_response),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
try respondGeneric(id, null_result_response);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn gotoDefinitionHandler(arena: *std.heap.ArenaAllocator, id: types.RequestId, req: requests.GotoDefinition, config: Config) !void {
|
|
|
|
try gotoHandler(arena, id, req, config, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn gotoDeclarationHandler(arena: *std.heap.ArenaAllocator, id: types.RequestId, req: requests.GotoDeclaration, config: Config) !void {
|
|
|
|
try gotoHandler(arena, id, req, config, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn hoverHandler(arena: *std.heap.ArenaAllocator, id: types.RequestId, req: requests.Hover, config: Config) !void {
|
|
|
|
const handle = document_store.getHandle(req.params.textDocument.uri) orelse {
|
2020-08-14 11:41:34 +01:00
|
|
|
logger.warn("Trying to get hover in non existent document {}", .{req.params.textDocument.uri});
|
2020-06-30 13:46:43 +01:00
|
|
|
return try respondGeneric(id, null_result_response);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (req.params.position.character >= 0) {
|
2020-07-03 09:34:42 +01:00
|
|
|
const doc_position = try offsets.documentPosition(handle.document, req.params.position, offset_encoding);
|
2020-07-03 00:31:28 +01:00
|
|
|
const pos_context = try analysis.documentPositionContext(arena, handle.document, doc_position);
|
2020-06-30 13:46:43 +01:00
|
|
|
|
|
|
|
const this_config = configFromUriOr(req.params.textDocument.uri, config);
|
|
|
|
switch (pos_context) {
|
2020-07-03 00:31:28 +01:00
|
|
|
.var_access => try hoverDefinitionGlobal(arena, id, doc_position.absolute_index, handle, this_config),
|
|
|
|
.field_access => |range| try hoverDefinitionFieldAccess(arena, id, handle, doc_position, range, this_config),
|
|
|
|
.label => try hoverDefinitionLabel(arena, id, doc_position.absolute_index, handle, this_config),
|
2020-06-30 13:46:43 +01:00
|
|
|
else => try respondGeneric(id, null_result_response),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
try respondGeneric(id, null_result_response);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn documentSymbolsHandler(arena: *std.heap.ArenaAllocator, id: types.RequestId, req: requests.DocumentSymbols, config: Config) !void {
|
|
|
|
const handle = document_store.getHandle(req.params.textDocument.uri) orelse {
|
2020-08-14 11:41:34 +01:00
|
|
|
logger.warn("Trying to get document symbols in non existent document {}", .{req.params.textDocument.uri});
|
2020-06-30 13:46:43 +01:00
|
|
|
return try respondGeneric(id, null_result_response);
|
|
|
|
};
|
|
|
|
try documentSymbol(arena, id, handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn formattingHandler(arena: *std.heap.ArenaAllocator, id: types.RequestId, req: requests.Formatting, config: Config) !void {
|
|
|
|
if (config.zig_exe_path) |zig_exe_path| {
|
|
|
|
const handle = document_store.getHandle(req.params.textDocument.uri) orelse {
|
2020-08-14 11:41:34 +01:00
|
|
|
logger.warn("Trying to got to definition in non existent document {}", .{req.params.textDocument.uri});
|
2020-06-30 13:46:43 +01:00
|
|
|
return try respondGeneric(id, null_result_response);
|
|
|
|
};
|
|
|
|
|
|
|
|
var process = try std.ChildProcess.init(&[_][]const u8{ zig_exe_path, "fmt", "--stdin" }, allocator);
|
|
|
|
defer process.deinit();
|
|
|
|
process.stdin_behavior = .Pipe;
|
|
|
|
process.stdout_behavior = .Pipe;
|
|
|
|
|
|
|
|
process.spawn() catch |err| {
|
2020-08-14 11:41:34 +01:00
|
|
|
logger.warn("Failed to spawn zig fmt process, error: {}\n", .{err});
|
2020-06-30 13:46:43 +01:00
|
|
|
return try respondGeneric(id, null_result_response);
|
|
|
|
};
|
|
|
|
try process.stdin.?.writeAll(handle.document.text);
|
|
|
|
process.stdin.?.close();
|
|
|
|
process.stdin = null;
|
|
|
|
|
|
|
|
const stdout_bytes = try process.stdout.?.reader().readAllAlloc(allocator, std.math.maxInt(usize));
|
|
|
|
defer allocator.free(stdout_bytes);
|
|
|
|
|
|
|
|
switch (try process.wait()) {
|
|
|
|
.Exited => |code| if (code == 0) {
|
2020-07-03 10:24:24 +01:00
|
|
|
return try send(arena, types.Response{
|
2020-06-30 13:46:43 +01:00
|
|
|
.id = id,
|
|
|
|
.result = .{
|
|
|
|
.TextEdits = &[1]types.TextEdit{
|
|
|
|
.{
|
2020-07-03 10:39:58 +01:00
|
|
|
.range = try offsets.documentRange(handle.document, offset_encoding),
|
2020-06-30 13:46:43 +01:00
|
|
|
.newText = stdout_bytes,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
else => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return try respondGeneric(id, null_result_response);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn renameHandler(arena: *std.heap.ArenaAllocator, id: types.RequestId, req: requests.Rename, config: Config) !void {
|
|
|
|
const handle = document_store.getHandle(req.params.textDocument.uri) orelse {
|
2020-08-14 11:41:34 +01:00
|
|
|
logger.warn("Trying to rename in non existent document {}", .{req.params.textDocument.uri});
|
2020-06-30 13:46:43 +01:00
|
|
|
return try respondGeneric(id, null_result_response);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (req.params.position.character >= 0) {
|
2020-07-03 09:34:42 +01:00
|
|
|
const doc_position = try offsets.documentPosition(handle.document, req.params.position, offset_encoding);
|
2020-07-03 00:31:28 +01:00
|
|
|
const pos_context = try analysis.documentPositionContext(arena, handle.document, doc_position);
|
2020-06-30 13:46:43 +01:00
|
|
|
|
|
|
|
const this_config = configFromUriOr(req.params.textDocument.uri, config);
|
|
|
|
switch (pos_context) {
|
2020-07-03 00:31:28 +01:00
|
|
|
.var_access => try renameDefinitionGlobal(arena, id, handle, doc_position.absolute_index, req.params.newName),
|
|
|
|
.field_access => |range| try renameDefinitionFieldAccess(arena, id, handle, doc_position, range, req.params.newName, this_config),
|
|
|
|
.label => try renameDefinitionLabel(arena, id, handle, doc_position.absolute_index, req.params.newName),
|
2020-06-30 13:46:43 +01:00
|
|
|
else => try respondGeneric(id, null_result_response),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
try respondGeneric(id, null_result_response);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-05 23:32:14 +01:00
|
|
|
fn referencesHandler(arena: *std.heap.ArenaAllocator, id: types.RequestId, req: requests.References, config: Config) !void {
|
|
|
|
const handle = document_store.getHandle(req.params.textDocument.uri) orelse {
|
2020-08-14 11:41:34 +01:00
|
|
|
logger.warn("Trying to get references in non existent document {}", .{req.params.textDocument.uri});
|
2020-07-05 23:32:14 +01:00
|
|
|
return try respondGeneric(id, null_result_response);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (req.params.position.character >= 0) {
|
|
|
|
const doc_position = try offsets.documentPosition(handle.document, req.params.position, offset_encoding);
|
|
|
|
const pos_context = try analysis.documentPositionContext(arena, handle.document, doc_position);
|
|
|
|
|
|
|
|
const this_config = configFromUriOr(req.params.textDocument.uri, config);
|
|
|
|
const include_decl = req.params.context.includeDeclaration;
|
|
|
|
switch (pos_context) {
|
|
|
|
.var_access => try referencesDefinitionGlobal(arena, id, handle, doc_position.absolute_index, include_decl),
|
|
|
|
.field_access => |range| try referencesDefinitionFieldAccess(arena, id, handle, doc_position, range, include_decl, this_config),
|
|
|
|
.label => try referencesDefinitionLabel(arena, id, handle, doc_position.absolute_index, include_decl),
|
|
|
|
else => try respondGeneric(id, null_result_response),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
try respondGeneric(id, null_result_response);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-30 16:00:33 +01:00
|
|
|
// Needed for the hack seen below.
|
2020-07-12 20:12:09 +01:00
|
|
|
fn extractErr(val: anytype) anyerror {
|
2020-06-30 16:00:33 +01:00
|
|
|
val catch |e| return e;
|
|
|
|
return error.HackDone;
|
|
|
|
}
|
|
|
|
|
2020-06-29 23:34:21 +01:00
|
|
|
fn processJsonRpc(arena: *std.heap.ArenaAllocator, parser: *std.json.Parser, json: []const u8, config: Config) !void {
|
|
|
|
var tree = try parser.parse(json);
|
|
|
|
defer tree.deinit();
|
2020-05-28 01:39:36 +01:00
|
|
|
|
2020-07-05 22:56:41 +01:00
|
|
|
const id = if (tree.root.Object.get("id")) |id| switch (id) {
|
2020-06-29 23:34:21 +01:00
|
|
|
.Integer => |int| types.RequestId{ .Integer = int },
|
|
|
|
.String => |str| types.RequestId{ .String = str },
|
|
|
|
else => types.RequestId{ .Integer = 0 },
|
|
|
|
} else types.RequestId{ .Integer = 0 };
|
2020-06-16 20:02:31 +01:00
|
|
|
|
2020-07-05 22:56:41 +01:00
|
|
|
std.debug.assert(tree.root.Object.get("method") != null);
|
|
|
|
const method = tree.root.Object.get("method").?.String;
|
2020-06-16 20:02:31 +01:00
|
|
|
|
2020-06-29 23:34:21 +01:00
|
|
|
const start_time = std.time.milliTimestamp();
|
|
|
|
defer {
|
|
|
|
const end_time = std.time.milliTimestamp();
|
2020-08-14 11:41:34 +01:00
|
|
|
logger.debug("Took {}ms to process method {}\n", .{ end_time - start_time, method });
|
2020-06-29 23:34:21 +01:00
|
|
|
}
|
2020-06-27 01:16:14 +01:00
|
|
|
|
2020-06-29 23:34:21 +01:00
|
|
|
const method_map = .{
|
2020-06-30 13:46:43 +01:00
|
|
|
.{"initialized"},
|
|
|
|
.{"$/cancelRequest"},
|
|
|
|
.{"textDocument/willSave"},
|
|
|
|
.{ "initialize", requests.Initialize, initializeHandler },
|
|
|
|
.{ "shutdown", void, shutdownHandler },
|
|
|
|
.{ "workspace/didChangeWorkspaceFolders", requests.WorkspaceFoldersChange, workspaceFoldersChangeHandler },
|
|
|
|
.{ "textDocument/didOpen", requests.OpenDocument, openDocumentHandler },
|
|
|
|
.{ "textDocument/didChange", requests.ChangeDocument, changeDocumentHandler },
|
|
|
|
.{ "textDocument/didSave", requests.SaveDocument, saveDocumentHandler },
|
|
|
|
.{ "textDocument/didClose", requests.CloseDocument, closeDocumentHandler },
|
|
|
|
.{ "textDocument/semanticTokens", requests.SemanticTokens, semanticTokensHandler },
|
|
|
|
.{ "textDocument/completion", requests.Completion, completionHandler },
|
|
|
|
.{ "textDocument/signatureHelp", void, signatureHelperHandler },
|
|
|
|
.{ "textDocument/definition", requests.GotoDefinition, gotoDefinitionHandler },
|
|
|
|
.{ "textDocument/typeDefinition", requests.GotoDefinition, gotoDefinitionHandler },
|
|
|
|
.{ "textDocument/implementation", requests.GotoDefinition, gotoDefinitionHandler },
|
|
|
|
.{ "textDocument/declaration", requests.GotoDeclaration, gotoDeclarationHandler },
|
|
|
|
.{ "textDocument/hover", requests.Hover, hoverHandler },
|
|
|
|
.{ "textDocument/documentSymbol", requests.DocumentSymbols, documentSymbolsHandler },
|
|
|
|
.{ "textDocument/formatting", requests.Formatting, formattingHandler },
|
|
|
|
.{ "textDocument/rename", requests.Rename, renameHandler },
|
2020-07-05 23:32:14 +01:00
|
|
|
.{ "textDocument/references", requests.References, referencesHandler },
|
2020-06-29 23:34:21 +01:00
|
|
|
};
|
|
|
|
|
2020-06-30 16:00:33 +01:00
|
|
|
// Hack to avoid `return`ing in the inline for, which causes bugs.
|
|
|
|
var done: ?anyerror = null;
|
2020-06-29 23:34:21 +01:00
|
|
|
inline for (method_map) |method_info| {
|
2020-06-30 16:00:33 +01:00
|
|
|
if (done == null and std.mem.eql(u8, method, method_info[0])) {
|
2020-06-30 13:46:43 +01:00
|
|
|
if (method_info.len == 1) {
|
2020-06-30 16:00:33 +01:00
|
|
|
done = error.HackDone;
|
2020-06-30 13:46:43 +01:00
|
|
|
} else if (method_info[1] != void) {
|
2020-06-30 16:00:33 +01:00
|
|
|
const ReqT = method_info[1];
|
|
|
|
if (requests.fromDynamicTree(arena, ReqT, tree.root)) |request_obj| {
|
|
|
|
done = error.HackDone;
|
|
|
|
done = extractErr(method_info[2](arena, id, request_obj, config));
|
|
|
|
} else |err| {
|
|
|
|
if (err == error.MalformedJson) {
|
2020-08-14 11:41:34 +01:00
|
|
|
logger.warn("Could not create request type {} from JSON {}\n", .{ @typeName(ReqT), json });
|
2020-06-30 13:46:43 +01:00
|
|
|
}
|
2020-06-30 16:00:33 +01:00
|
|
|
done = err;
|
|
|
|
}
|
2020-06-30 13:46:43 +01:00
|
|
|
} else {
|
2020-06-30 16:00:33 +01:00
|
|
|
done = error.HackDone;
|
2020-07-03 00:31:28 +01:00
|
|
|
(method_info[2])(arena, id, config) catch |err| {
|
|
|
|
done = err;
|
|
|
|
};
|
2020-06-27 01:16:14 +01:00
|
|
|
}
|
|
|
|
}
|
2020-04-24 23:19:03 +01:00
|
|
|
}
|
2020-06-30 16:00:33 +01:00
|
|
|
if (done) |err| switch (err) {
|
|
|
|
error.MalformedJson => return try respondGeneric(id, null_result_response),
|
|
|
|
error.HackDone => return,
|
|
|
|
else => return err,
|
|
|
|
};
|
2020-06-29 23:34:21 +01:00
|
|
|
|
2020-06-30 13:46:43 +01:00
|
|
|
const unimplemented_map = std.ComptimeStringMap(void, .{
|
2020-06-30 16:00:33 +01:00
|
|
|
.{"textDocument/documentHighlight"},
|
|
|
|
.{"textDocument/codeAction"},
|
|
|
|
.{"textDocument/codeLens"},
|
|
|
|
.{"textDocument/documentLink"},
|
|
|
|
.{"textDocument/rangeFormatting"},
|
|
|
|
.{"textDocument/onTypeFormatting"},
|
|
|
|
.{"textDocument/prepareRename"},
|
|
|
|
.{"textDocument/foldingRange"},
|
|
|
|
.{"textDocument/selectionRange"},
|
2020-06-30 13:46:43 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
if (unimplemented_map.has(method)) {
|
|
|
|
// TODO: Unimplemented methods, implement them and add them to server capabilities.
|
|
|
|
return try respondGeneric(id, null_result_response);
|
|
|
|
}
|
2020-07-05 22:56:41 +01:00
|
|
|
if (tree.root.Object.get("id")) |_| {
|
2020-06-30 13:46:43 +01:00
|
|
|
return try respondGeneric(id, not_implemented_response);
|
|
|
|
}
|
2020-08-14 11:41:34 +01:00
|
|
|
logger.debug("Method without return value not implemented: {}", .{method});
|
2020-04-24 23:19:03 +01:00
|
|
|
}
|
|
|
|
|
2020-05-17 12:40:32 +01:00
|
|
|
var debug_alloc_state: DebugAllocator = undefined;
|
2020-05-07 10:58:35 +01:00
|
|
|
// We can now use if(leak_count_alloc) |alloc| { ... } as a comptime check.
|
2020-05-17 12:40:32 +01:00
|
|
|
const debug_alloc: ?*DebugAllocator = if (build_options.allocation_info) &debug_alloc_state else null;
|
2020-05-07 10:50:25 +01:00
|
|
|
|
2020-04-24 23:19:03 +01:00
|
|
|
pub fn main() anyerror!void {
|
2020-05-07 14:20:45 +01:00
|
|
|
// TODO: Use a better purpose general allocator once std has one.
|
|
|
|
// Probably after the generic composable allocators PR?
|
2020-05-14 15:22:15 +01:00
|
|
|
// This is not too bad for now since most allocations happen in local arenas.
|
2020-05-07 14:20:45 +01:00
|
|
|
allocator = std.heap.page_allocator;
|
2020-04-24 23:19:03 +01:00
|
|
|
|
2020-05-08 00:53:00 +01:00
|
|
|
if (build_options.allocation_info) {
|
2020-05-07 10:50:25 +01:00
|
|
|
// Initialize the leak counting allocator.
|
2020-06-09 14:39:00 +01:00
|
|
|
debug_alloc_state = DebugAllocator.init(allocator, build_options.max_bytes_allocated);
|
2020-05-08 00:53:00 +01:00
|
|
|
allocator = &debug_alloc_state.allocator;
|
2020-05-07 10:50:25 +01:00
|
|
|
}
|
|
|
|
|
2020-06-16 22:26:45 +01:00
|
|
|
defer if (debug_alloc) |dbg| {
|
2020-07-02 12:44:12 +01:00
|
|
|
std.debug.print("Finished cleanup, last allocation info.\n", .{});
|
|
|
|
std.debug.print("\n{}\n", .{dbg.info});
|
2020-07-02 17:13:10 +01:00
|
|
|
dbg.printRemainingStackTraces();
|
|
|
|
dbg.deinit();
|
2020-06-16 22:26:45 +01:00
|
|
|
};
|
|
|
|
|
2020-04-27 21:38:35 +01:00
|
|
|
// Init global vars
|
2020-06-26 01:26:09 +01:00
|
|
|
const reader = std.io.getStdIn().reader();
|
2020-05-17 18:26:02 +01:00
|
|
|
stdout = std.io.bufferedOutStream(std.io.getStdOut().outStream());
|
2020-04-24 23:19:03 +01:00
|
|
|
|
2020-05-17 15:39:04 +01:00
|
|
|
// Read the configuration, if any.
|
2020-05-14 22:16:40 +01:00
|
|
|
const config_parse_options = std.json.ParseOptions{ .allocator = allocator };
|
2020-05-17 15:39:04 +01:00
|
|
|
var config = Config{};
|
2020-06-30 16:00:33 +01:00
|
|
|
var config_had_null_zig_path = config.zig_exe_path == null;
|
|
|
|
defer {
|
|
|
|
if (config_had_null_zig_path) {
|
|
|
|
if (config.zig_exe_path) |exe_path| {
|
|
|
|
allocator.free(exe_path);
|
|
|
|
config.zig_exe_path = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std.json.parseFree(Config, config, config_parse_options);
|
|
|
|
}
|
2020-05-09 14:43:51 +01:00
|
|
|
|
|
|
|
config_read: {
|
2020-05-25 01:22:39 +01:00
|
|
|
const known_folders = @import("known-folders");
|
2020-05-25 17:49:04 +01:00
|
|
|
|
2020-05-19 20:09:00 +01:00
|
|
|
const res = try known_folders.getPath(allocator, .local_configuration);
|
|
|
|
if (res) |local_config_path| {
|
|
|
|
defer allocator.free(local_config_path);
|
|
|
|
if (loadConfig(local_config_path)) |conf| {
|
|
|
|
config = conf;
|
|
|
|
break :config_read;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-25 17:33:08 +01:00
|
|
|
var exe_dir_bytes: [std.fs.MAX_PATH_BYTES]u8 = undefined;
|
|
|
|
const exe_dir_path = std.fs.selfExeDirPath(&exe_dir_bytes) catch break :config_read;
|
2020-05-14 22:16:40 +01:00
|
|
|
|
2020-05-25 17:33:08 +01:00
|
|
|
if (loadConfig(exe_dir_path)) |conf| {
|
2020-05-19 20:09:00 +01:00
|
|
|
config = conf;
|
2020-05-17 15:39:04 +01:00
|
|
|
}
|
2020-05-15 11:21:34 +01:00
|
|
|
}
|
|
|
|
|
2020-05-25 14:18:00 +01:00
|
|
|
// Find the zig executable in PATH
|
2020-05-30 21:36:18 +01:00
|
|
|
var zig_exe_path: ?[]const u8 = null;
|
2020-05-25 14:18:00 +01:00
|
|
|
|
|
|
|
find_zig: {
|
2020-05-30 21:36:18 +01:00
|
|
|
if (config.zig_exe_path) |exe_path| {
|
|
|
|
if (std.fs.path.isAbsolute(exe_path)) {
|
|
|
|
zig_exe_path = try std.mem.dupe(allocator, u8, exe_path);
|
|
|
|
break :find_zig;
|
|
|
|
}
|
|
|
|
|
2020-08-14 11:41:34 +01:00
|
|
|
logger.debug("zig path `{}` is not absolute, will look in path\n", .{exe_path});
|
2020-05-30 21:36:18 +01:00
|
|
|
}
|
|
|
|
|
2020-05-25 14:18:00 +01:00
|
|
|
const env_path = std.process.getEnvVarOwned(allocator, "PATH") catch |err| switch (err) {
|
|
|
|
error.EnvironmentVariableNotFound => {
|
2020-08-14 11:41:34 +01:00
|
|
|
logger.warn("Could not get PATH.\n", .{});
|
2020-05-25 14:18:00 +01:00
|
|
|
break :find_zig;
|
|
|
|
},
|
|
|
|
else => return err,
|
|
|
|
};
|
|
|
|
defer allocator.free(env_path);
|
|
|
|
|
|
|
|
const exe_extension = @as(std.zig.CrossTarget, .{}).exeFileExt();
|
|
|
|
const zig_exe = try std.fmt.allocPrint(allocator, "zig{}", .{exe_extension});
|
|
|
|
defer allocator.free(zig_exe);
|
|
|
|
|
2020-05-25 17:33:08 +01:00
|
|
|
var it = std.mem.tokenize(env_path, &[_]u8{std.fs.path.delimiter});
|
2020-05-25 14:18:00 +01:00
|
|
|
while (it.next()) |path| {
|
|
|
|
const full_path = try std.fs.path.join(allocator, &[_][]const u8{
|
|
|
|
path,
|
|
|
|
zig_exe,
|
|
|
|
});
|
2020-05-25 15:24:44 +01:00
|
|
|
defer allocator.free(full_path);
|
|
|
|
|
2020-05-25 14:18:00 +01:00
|
|
|
var buf: [std.fs.MAX_PATH_BYTES]u8 = undefined;
|
2020-06-30 16:00:33 +01:00
|
|
|
zig_exe_path = try std.mem.dupe(allocator, u8, std.os.realpath(full_path, &buf) catch continue);
|
2020-08-14 11:41:34 +01:00
|
|
|
logger.info("Found zig in PATH: {}\n", .{zig_exe_path});
|
2020-05-25 14:18:00 +01:00
|
|
|
break :find_zig;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-30 21:36:18 +01:00
|
|
|
if (zig_exe_path) |exe_path| {
|
2020-06-16 20:02:31 +01:00
|
|
|
config.zig_exe_path = exe_path;
|
2020-08-14 11:41:34 +01:00
|
|
|
logger.info("Using zig executable {}\n", .{exe_path});
|
2020-05-30 21:36:18 +01:00
|
|
|
if (config.zig_lib_path == null) {
|
|
|
|
// Set the lib path relative to the executable path.
|
|
|
|
config.zig_lib_path = try std.fs.path.resolve(allocator, &[_][]const u8{
|
|
|
|
std.fs.path.dirname(exe_path).?, "./lib/zig",
|
|
|
|
});
|
|
|
|
|
2020-08-14 11:41:34 +01:00
|
|
|
logger.info("Resolved standard library from executable: {}\n", .{config.zig_lib_path});
|
2020-05-30 21:36:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-25 18:04:23 +01:00
|
|
|
if (config.build_runner_path) |build_runner_path| {
|
2020-06-10 17:54:01 +01:00
|
|
|
try document_store.init(allocator, zig_exe_path, try std.mem.dupe(allocator, u8, build_runner_path), config.zig_lib_path);
|
2020-05-25 18:04:23 +01:00
|
|
|
} else {
|
2020-05-25 17:33:08 +01:00
|
|
|
var exe_dir_bytes: [std.fs.MAX_PATH_BYTES]u8 = undefined;
|
|
|
|
const exe_dir_path = try std.fs.selfExeDirPath(&exe_dir_bytes);
|
|
|
|
|
2020-05-25 18:04:23 +01:00
|
|
|
const build_runner_path = try std.fs.path.resolve(allocator, &[_][]const u8{ exe_dir_path, "build_runner.zig" });
|
2020-06-10 17:54:01 +01:00
|
|
|
try document_store.init(allocator, zig_exe_path, build_runner_path, config.zig_lib_path);
|
2020-05-25 17:33:08 +01:00
|
|
|
}
|
2020-05-14 00:10:41 +01:00
|
|
|
defer document_store.deinit();
|
2020-05-09 14:43:51 +01:00
|
|
|
|
2020-05-19 20:09:00 +01:00
|
|
|
workspace_folder_configs = std.StringHashMap(?Config).init(allocator);
|
2020-07-02 12:44:12 +01:00
|
|
|
defer {
|
|
|
|
var it = workspace_folder_configs.iterator();
|
|
|
|
while (it.next()) |entry| {
|
|
|
|
allocator.free(entry.key);
|
|
|
|
if (entry.value) |c| {
|
|
|
|
std.json.parseFree(Config, c, std.json.ParseOptions{ .allocator = allocator });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
workspace_folder_configs.deinit();
|
|
|
|
}
|
2020-05-19 20:09:00 +01:00
|
|
|
|
2020-05-09 14:43:51 +01:00
|
|
|
// This JSON parser is passed to processJsonRpc and reset.
|
|
|
|
var json_parser = std.json.Parser.init(allocator, false);
|
|
|
|
defer json_parser.deinit();
|
|
|
|
|
2020-06-29 23:34:21 +01:00
|
|
|
// Arena used for temporary allocations while handlign a request
|
|
|
|
var arena = std.heap.ArenaAllocator.init(allocator);
|
|
|
|
defer arena.deinit();
|
|
|
|
|
2020-06-16 22:26:45 +01:00
|
|
|
while (keep_running) {
|
2020-06-29 23:34:21 +01:00
|
|
|
const headers = readRequestHeader(&arena.allocator, reader) catch |err| {
|
2020-08-14 11:41:34 +01:00
|
|
|
logger.crit("{}; exiting!", .{@errorName(err)});
|
2020-04-24 23:19:03 +01:00
|
|
|
return;
|
2020-05-17 15:50:13 +01:00
|
|
|
};
|
2020-06-29 23:34:21 +01:00
|
|
|
const buf = try arena.allocator.alloc(u8, headers.content_length);
|
2020-06-26 01:26:09 +01:00
|
|
|
try reader.readNoEof(buf);
|
2020-06-29 23:34:21 +01:00
|
|
|
|
|
|
|
try processJsonRpc(&arena, &json_parser, buf, config);
|
2020-05-17 15:50:13 +01:00
|
|
|
json_parser.reset();
|
2020-06-29 23:34:21 +01:00
|
|
|
arena.deinit();
|
2020-07-08 02:05:44 +01:00
|
|
|
arena.state = .{};
|
2020-04-24 23:19:03 +01:00
|
|
|
|
2020-05-08 00:53:00 +01:00
|
|
|
if (debug_alloc) |dbg| {
|
2020-08-14 11:41:34 +01:00
|
|
|
logger.debug("\n{}\n", .{dbg.info});
|
2020-05-07 10:50:25 +01:00
|
|
|
}
|
2020-04-24 23:19:03 +01:00
|
|
|
}
|
|
|
|
}
|