2020-04-24 23:19:03 +01:00
|
|
|
const std = @import("std");
|
2021-10-07 12:34:14 +01:00
|
|
|
const zig_builtin = @import("builtin");
|
2020-05-07 16:29:40 +01:00
|
|
|
const build_options = @import("build_options");
|
2021-10-02 21:44:04 +01:00
|
|
|
const Config = @import("./Config.zig");
|
2021-10-02 21:25:49 +01:00
|
|
|
const DocumentStore = @import("./DocumentStore.zig");
|
2021-10-01 01:48:40 +01:00
|
|
|
const readRequestHeader = @import("./header.zig").readRequestHeader;
|
|
|
|
const requests = @import("./requests.zig");
|
|
|
|
const types = @import("./types.zig");
|
|
|
|
const analysis = @import("./analysis.zig");
|
2021-10-03 00:39:24 +01:00
|
|
|
const ast = @import("./ast.zig");
|
2021-10-01 01:48:40 +01:00
|
|
|
const references = @import("./references.zig");
|
|
|
|
const rename = @import("./rename.zig");
|
|
|
|
const offsets = @import("./offsets.zig");
|
|
|
|
const setup = @import("./setup.zig");
|
|
|
|
const semantic_tokens = @import("./semantic_tokens.zig");
|
2021-10-02 06:37:11 +01:00
|
|
|
const shared = @import("./shared.zig");
|
2021-10-01 02:44:06 +01:00
|
|
|
const Ast = std.zig.Ast;
|
2021-01-12 11:10:51 +00:00
|
|
|
const known_folders = @import("known-folders");
|
2021-10-02 06:37:11 +01:00
|
|
|
|
|
|
|
const data = switch (build_options.data_version) {
|
|
|
|
.master => @import("data/master.zig"),
|
|
|
|
.@"0.7.0" => @import("data/0.7.0.zig"),
|
|
|
|
.@"0.7.1" => @import("data/0.7.1.zig"),
|
|
|
|
.@"0.8.0" => @import("data/0.8.0.zig"),
|
|
|
|
.@"0.8.1" => @import("data/0.8.1.zig"),
|
2021-06-24 11:38:01 +01:00
|
|
|
};
|
2020-04-24 23:19:03 +01:00
|
|
|
|
2020-08-14 11:41:34 +01:00
|
|
|
const logger = std.log.scoped(.main);
|
|
|
|
|
2020-11-08 11:40:16 +00:00
|
|
|
// Always set this to debug to make std.log call into our handler, then control the runtime
|
|
|
|
// value in the definition below.
|
|
|
|
pub const log_level = .debug;
|
|
|
|
|
2021-10-07 12:34:14 +01:00
|
|
|
var actual_log_level: std.log.Level = switch (zig_builtin.mode) {
|
2020-06-26 12:29:59 +01:00
|
|
|
.Debug => .debug,
|
2021-11-03 09:31:20 +00:00
|
|
|
else => @intToEnum(std.log.Level, @enumToInt(build_options.log_level)), //temporary fix to build failing on release-safe due to a Zig bug
|
2020-06-26 12:29:59 +01:00
|
|
|
};
|
|
|
|
|
2021-10-01 02:45:45 +01:00
|
|
|
pub fn log(comptime message_level: std.log.Level, comptime scope: @Type(.EnumLiteral), comptime format: []const u8, args: anytype) void {
|
2020-11-08 11:40:16 +00:00
|
|
|
if (@enumToInt(message_level) > @enumToInt(actual_log_level)) {
|
|
|
|
return;
|
|
|
|
}
|
2020-10-10 10:44:37 +01:00
|
|
|
// After shutdown, pipe output to stderr
|
|
|
|
if (!keep_running) {
|
2021-04-07 14:10:18 +01:00
|
|
|
std.debug.print("[{s}-{s}] " ++ format ++ "\n", .{ @tagName(message_level), @tagName(scope) } ++ args);
|
2020-10-10 10:44:37 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-29 23:34:21 +01:00
|
|
|
var arena = std.heap.ArenaAllocator.init(allocator);
|
|
|
|
defer arena.deinit();
|
|
|
|
|
2021-06-24 11:38:01 +01:00
|
|
|
var message = std.fmt.allocPrint(&arena.allocator, "[{s}-{s}] " ++ format, .{ @tagName(message_level), @tagName(scope) } ++ args) catch {
|
2021-03-10 11:18:28 +00:00
|
|
|
std.debug.print("Failed to allocPrint message.\n", .{});
|
2020-06-26 12:29:59 +01:00
|
|
|
return;
|
|
|
|
};
|
2020-10-10 10:44:37 +01:00
|
|
|
|
2021-10-25 11:56:04 +01:00
|
|
|
const message_type: types.MessageType = switch (message_level) {
|
|
|
|
.debug => .Log,
|
|
|
|
.info => .Info,
|
|
|
|
.warn => .Warning,
|
|
|
|
.err => .Error,
|
|
|
|
};
|
|
|
|
send(&arena, types.Notification{
|
2021-11-04 08:55:38 +00:00
|
|
|
.method = "window/logMessage",
|
2021-10-25 11:56:04 +01:00
|
|
|
.params = types.Notification.Params{
|
2021-11-04 08:55:38 +00:00
|
|
|
.LogMessage = .{
|
2021-10-25 11:56:04 +01:00
|
|
|
.type = message_type,
|
|
|
|
.message = message,
|
2020-06-26 12:29:59 +01:00
|
|
|
},
|
2021-10-25 11:56:04 +01:00
|
|
|
},
|
|
|
|
}) catch |err| {
|
|
|
|
std.debug.print("Failed to send show message notification (error: {}).\n", .{err});
|
|
|
|
};
|
2020-06-26 12:29:59 +01:00
|
|
|
}
|
|
|
|
|
2020-04-24 23:19:03 +01:00
|
|
|
// Code is largely based off of https://github.com/andersfr/zig-lsp/blob/master/server.zig
|
2021-01-10 07:12:11 +00:00
|
|
|
var stdout: std.io.BufferedWriter(4096, std.fs.File.Writer) = 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-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-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-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
|
|
|
;
|
2021-04-01 12:14:49 +01:00
|
|
|
const no_signatures_response =
|
2021-04-02 18:49:01 +01:00
|
|
|
\\,"result":{"signatures":[]}}
|
2021-04-01 12:14:49 +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
|
|
|
}
|
|
|
|
|
2021-03-27 19:37:51 +00:00
|
|
|
fn truncateCompletions(list: []types.CompletionItem, max_detail_length: usize) void {
|
|
|
|
for (list) |*item| {
|
|
|
|
if (item.detail) |det| {
|
|
|
|
if (det.len > max_detail_length) {
|
|
|
|
item.detail = det[0..max_detail_length];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2021-01-10 07:12:11 +00:00
|
|
|
const stdout_stream = stdout.writer();
|
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}),
|
2021-01-04 17:51:26 +00:00
|
|
|
.String => |str| try stdout_stream.print("\"{s}\"", .{str}),
|
2020-06-06 13:40:33 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-06-24 11:38:01 +01:00
|
|
|
fn showMessage(message_type: types.MessageType, message: []const u8) !void {
|
2020-06-09 04:21:55 +01:00
|
|
|
try send(types.Notification{
|
|
|
|
.method = "window/showMessage",
|
|
|
|
.params = .{
|
|
|
|
.ShowMessageParams = .{
|
2021-06-24 11:38:01 +01:00
|
|
|
.type = message_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?
|
2021-10-01 02:44:06 +01:00
|
|
|
fn astLocationToRange(loc: Ast.Location) types.Range {
|
2020-05-08 16:01:34 +01:00
|
|
|
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
|
|
|
|
2021-02-26 20:26:52 +00:00
|
|
|
for (tree.errors) |err| {
|
|
|
|
const loc = tree.tokenLocation(0, err.token);
|
2020-05-09 02:02:29 +01:00
|
|
|
|
|
|
|
var mem_buffer: [256]u8 = undefined;
|
|
|
|
var fbs = std.io.fixedBufferStream(&mem_buffer);
|
2021-01-10 07:12:11 +00:00
|
|
|
try tree.renderError(err, fbs.writer());
|
2020-05-09 02:02:29 +01:00
|
|
|
|
|
|
|
try diagnostics.append(.{
|
|
|
|
.range = astLocationToRange(loc),
|
|
|
|
.severity = .Error,
|
2021-02-26 20:26:52 +00:00
|
|
|
.code = @tagName(err.tag),
|
2020-05-09 02:02:29 +01:00
|
|
|
.source = "zls",
|
|
|
|
.message = try std.mem.dupe(&arena.allocator, u8, fbs.getWritten()),
|
|
|
|
// .relatedInformation = undefined
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-15 10:07:43 +01:00
|
|
|
// TODO: style warnings for types, values and declarations below root scope
|
2020-05-09 02:02:29 +01:00
|
|
|
if (tree.errors.len == 0) {
|
2021-02-26 20:26:52 +00:00
|
|
|
for (tree.rootDecls()) |decl_idx| {
|
|
|
|
const decl = tree.nodes.items(.tag)[decl_idx];
|
|
|
|
switch (decl) {
|
2021-03-05 21:38:42 +00:00
|
|
|
.fn_proto,
|
|
|
|
.fn_proto_multi,
|
|
|
|
.fn_proto_one,
|
|
|
|
.fn_proto_simple,
|
|
|
|
.fn_decl,
|
|
|
|
=> blk: {
|
2021-10-01 02:44:06 +01:00
|
|
|
var buf: [1]Ast.Node.Index = undefined;
|
2021-10-03 00:39:24 +01:00
|
|
|
const func = ast.fnProto(tree, decl_idx, &buf).?;
|
2021-05-23 17:01:03 +01:00
|
|
|
if (func.extern_export_inline_token != null) break :blk;
|
2020-05-08 16:01:34 +01:00
|
|
|
|
2020-05-15 20:10:53 +01:00
|
|
|
if (config.warn_style) {
|
2021-02-26 20:26:52 +00:00
|
|
|
if (func.name_token) |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 = .{
|
2020-11-06 08:08:20 +00:00
|
|
|
.PublishDiagnostics = .{
|
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
|
|
|
|
2021-10-01 01:48:40 +01:00
|
|
|
fn typeToCompletion(arena: *std.heap.ArenaAllocator, list: *std.ArrayList(types.CompletionItem), field_access: analysis.FieldAccessReturn, 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,
|
2021-03-28 15:02:48 +01:00
|
|
|
.insertText = "len",
|
|
|
|
.insertTextFormat = .PlainText,
|
2020-06-17 03:12:12 +01:00
|
|
|
});
|
|
|
|
try list.append(.{
|
|
|
|
.label = "ptr",
|
|
|
|
.kind = .Field,
|
2021-03-28 15:02:48 +01:00
|
|
|
.insertText = "ptr",
|
|
|
|
.insertTextFormat = .PlainText,
|
2020-06-17 03:12:12 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
.error_union => {},
|
2020-07-24 11:20:13 +01:00
|
|
|
.pointer => |n| {
|
|
|
|
if (config.operator_completions) {
|
|
|
|
try list.append(.{
|
|
|
|
.label = "*",
|
|
|
|
.kind = .Operator,
|
2021-03-28 15:02:48 +01:00
|
|
|
.insertText = "*",
|
|
|
|
.insertTextFormat = .PlainText,
|
2020-07-24 11:20:13 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
try nodeToCompletion(
|
|
|
|
arena,
|
|
|
|
list,
|
|
|
|
.{ .node = n, .handle = type_handle.handle },
|
|
|
|
null,
|
|
|
|
orig_handle,
|
|
|
|
type_handle.type.is_type_val,
|
2021-04-03 16:54:26 +01:00
|
|
|
null,
|
2020-07-24 11:20:13 +01:00
|
|
|
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,
|
2021-04-03 16:54:26 +01:00
|
|
|
null,
|
2020-06-17 03:12:12 +01:00
|
|
|
config,
|
|
|
|
),
|
2020-06-17 21:36:40 +01:00
|
|
|
.primitive => {},
|
2020-06-17 03:12:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-01 01:51:51 +01:00
|
|
|
fn nodeToCompletion(arena: *std.heap.ArenaAllocator, list: *std.ArrayList(types.CompletionItem), node_handle: analysis.NodeWithHandle, unwrapped: ?analysis.TypeWithHandle, orig_handle: *DocumentStore.Handle, is_type_val: bool, parent_is_type_val: ?bool, config: Config) error{OutOfMemory}!void {
|
2020-06-10 18:48:40 +01:00
|
|
|
const node = node_handle.node;
|
|
|
|
const handle = node_handle.handle;
|
2021-03-01 15:02:24 +00:00
|
|
|
const tree = handle.tree;
|
|
|
|
const node_tags = tree.nodes.items(.tag);
|
|
|
|
const token_tags = tree.tokens.items(.tag);
|
2020-06-10 18:48:40 +01:00
|
|
|
|
2020-11-06 08:08:20 +00:00
|
|
|
const doc_kind: types.MarkupContent.Kind = if (client_capabilities.completion_doc_supports_md)
|
|
|
|
.Markdown
|
|
|
|
else
|
|
|
|
.PlainText;
|
|
|
|
|
2020-06-12 15:42:41 +01:00
|
|
|
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
|
|
|
|
2021-10-03 00:39:24 +01:00
|
|
|
if (ast.isContainer(handle.tree, node)) {
|
2020-06-17 03:12:12 +01:00
|
|
|
const context = DeclToCompletionContext{
|
|
|
|
.completions = list,
|
|
|
|
.config = &config,
|
|
|
|
.arena = arena,
|
|
|
|
.orig_handle = orig_handle,
|
2021-04-03 16:54:26 +01:00
|
|
|
.parent_is_type_val = is_type_val,
|
2020-06-17 03:12:12 +01:00
|
|
|
};
|
2021-04-03 16:54:26 +01:00
|
|
|
try analysis.iterateSymbolsContainer(
|
|
|
|
&document_store,
|
|
|
|
arena,
|
|
|
|
node_handle,
|
|
|
|
orig_handle,
|
|
|
|
declToCompletion,
|
|
|
|
context,
|
|
|
|
!is_type_val,
|
|
|
|
);
|
2020-06-17 03:12:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_type_val) return;
|
|
|
|
|
2021-03-01 15:02:24 +00:00
|
|
|
switch (node_tags[node]) {
|
2021-03-05 21:38:42 +00:00
|
|
|
.fn_proto,
|
|
|
|
.fn_proto_multi,
|
|
|
|
.fn_proto_one,
|
|
|
|
.fn_proto_simple,
|
|
|
|
.fn_decl,
|
|
|
|
=> {
|
2021-10-01 02:44:06 +01:00
|
|
|
var buf: [1]Ast.Node.Index = undefined;
|
2021-10-03 00:39:24 +01:00
|
|
|
const func = ast.fnProto(tree, node, &buf).?;
|
2021-03-01 15:02:24 +00:00
|
|
|
if (func.name_token) |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: {
|
2021-04-03 16:54:26 +01:00
|
|
|
const skip_self_param = !(parent_is_type_val orelse true) and
|
|
|
|
try analysis.hasSelfParam(arena, &document_store, handle, func);
|
2021-03-01 15:02:24 +00:00
|
|
|
break :blk try analysis.getFunctionSnippet(&arena.allocator, tree, func, skip_self_param);
|
2021-03-28 15:02:48 +01:00
|
|
|
} else tree.tokenSlice(func.name_token.?);
|
2020-05-13 18:30:57 +01:00
|
|
|
|
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
|
|
|
}
|
|
|
|
},
|
2021-03-06 19:55:59 +00:00
|
|
|
.global_var_decl,
|
|
|
|
.local_var_decl,
|
|
|
|
.aligned_var_decl,
|
|
|
|
.simple_var_decl,
|
|
|
|
=> {
|
2021-10-03 00:39:24 +01:00
|
|
|
const var_decl = ast.varDecl(tree, node).?;
|
2021-03-01 15:02:24 +00:00
|
|
|
const is_const = token_tags[var_decl.ast.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(.{
|
2021-03-01 15:02:24 +00:00
|
|
|
.label = handle.tree.tokenSlice(var_decl.ast.mut_token + 1),
|
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,
|
2021-03-01 15:02:24 +00:00
|
|
|
.detail = analysis.getVariableSignature(tree, var_decl),
|
2021-03-28 15:02:48 +01:00
|
|
|
.insertText = tree.tokenSlice(var_decl.ast.mut_token + 1),
|
|
|
|
.insertTextFormat = .PlainText,
|
2020-05-17 15:23:04 +01:00
|
|
|
});
|
2020-05-13 18:30:57 +01:00
|
|
|
},
|
2021-03-06 19:55:59 +00:00
|
|
|
.container_field,
|
|
|
|
.container_field_align,
|
|
|
|
.container_field_init,
|
|
|
|
=> {
|
2021-10-03 00:39:24 +01:00
|
|
|
const field = ast.containerField(tree, node).?;
|
2020-06-10 23:00:13 +01:00
|
|
|
try list.append(.{
|
2021-03-01 15:02:24 +00:00
|
|
|
.label = handle.tree.tokenSlice(field.ast.name_token),
|
2020-06-10 23:00:13 +01:00
|
|
|
.kind = .Field,
|
|
|
|
.documentation = doc,
|
|
|
|
.detail = analysis.getContainerFieldSignature(handle.tree, field),
|
2021-03-28 15:02:48 +01:00
|
|
|
.insertText = tree.tokenSlice(field.ast.name_token),
|
|
|
|
.insertTextFormat = .PlainText,
|
2020-06-10 23:00:13 +01:00
|
|
|
});
|
|
|
|
},
|
2021-03-06 19:55:59 +00:00
|
|
|
.array_type,
|
|
|
|
.array_type_sentinel,
|
|
|
|
=> {
|
2020-07-24 11:20:13 +01:00
|
|
|
try list.append(.{
|
|
|
|
.label = "len",
|
|
|
|
.kind = .Field,
|
2021-03-28 15:02:48 +01:00
|
|
|
.insertText = "len",
|
|
|
|
.insertTextFormat = .PlainText,
|
2020-07-24 11:20:13 +01:00
|
|
|
});
|
|
|
|
},
|
2021-03-06 19:55:59 +00:00
|
|
|
.ptr_type,
|
|
|
|
.ptr_type_aligned,
|
|
|
|
.ptr_type_bit_range,
|
|
|
|
.ptr_type_sentinel,
|
|
|
|
=> {
|
2021-10-03 00:39:24 +01:00
|
|
|
const ptr_type = ast.ptrType(tree, node).?;
|
2021-03-01 15:02:24 +00:00
|
|
|
|
|
|
|
switch (ptr_type.size) {
|
2021-03-11 12:59:09 +00:00
|
|
|
.One, .C, .Many => if (config.operator_completions) {
|
2021-03-01 15:02:24 +00:00
|
|
|
try list.append(.{
|
|
|
|
.label = "*",
|
|
|
|
.kind = .Operator,
|
2021-03-28 15:02:48 +01:00
|
|
|
.insertText = "*",
|
|
|
|
.insertTextFormat = .PlainText,
|
2021-03-01 15:02:24 +00:00
|
|
|
});
|
|
|
|
},
|
2021-03-11 12:59:09 +00:00
|
|
|
.Slice => {
|
2021-03-28 15:02:48 +01:00
|
|
|
try list.append(.{
|
|
|
|
.label = "ptr",
|
|
|
|
.kind = .Field,
|
|
|
|
.insertText = "ptr",
|
|
|
|
.insertTextFormat = .PlainText,
|
|
|
|
});
|
|
|
|
try list.append(.{
|
|
|
|
.label = "len",
|
|
|
|
.kind = .Field,
|
|
|
|
.insertText = "len",
|
|
|
|
.insertTextFormat = .PlainText,
|
|
|
|
});
|
2021-03-11 12:59:09 +00:00
|
|
|
return;
|
|
|
|
},
|
2020-07-16 17:04:23 +01:00
|
|
|
}
|
2020-07-07 21:26:12 +01:00
|
|
|
|
2021-03-01 15:02:24 +00:00
|
|
|
if (unwrapped) |actual_type| {
|
2020-07-16 17:04:23 +01:00
|
|
|
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;
|
|
|
|
},
|
2021-03-01 15:02:24 +00:00
|
|
|
.optional_type => {
|
2020-07-16 17:04:23 +01:00
|
|
|
if (config.operator_completions) {
|
|
|
|
try list.append(.{
|
|
|
|
.label = "?",
|
|
|
|
.kind = .Operator,
|
2021-03-28 15:02:48 +01:00
|
|
|
.insertText = "?",
|
|
|
|
.insertTextFormat = .PlainText,
|
2020-07-16 17:04:23 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return;
|
2020-05-13 18:30:57 +01:00
|
|
|
},
|
2021-03-01 15:02:24 +00:00
|
|
|
.string_literal => {
|
2020-05-17 15:23:04 +01:00
|
|
|
try list.append(.{
|
|
|
|
.label = "len",
|
|
|
|
.kind = .Field,
|
2021-03-28 15:02:48 +01:00
|
|
|
.insertText = "len",
|
|
|
|
.insertTextFormat = .PlainText,
|
2020-05-17 15:23:04 +01:00
|
|
|
});
|
|
|
|
},
|
2021-03-06 19:55:59 +00:00
|
|
|
else => if (analysis.nodeToString(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,
|
2021-03-01 15:02:24 +00:00
|
|
|
.detail = tree.getNodeSource(node),
|
2021-03-28 15:02:48 +01:00
|
|
|
.insertText = string,
|
|
|
|
.insertTextFormat = .PlainText,
|
2020-05-17 15:23:04 +01:00
|
|
|
});
|
2020-05-14 22:16:40 +01:00
|
|
|
},
|
2020-05-13 18:30:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-02 18:49:01 +01:00
|
|
|
pub fn identifierFromPosition(pos_index: usize, handle: DocumentStore.Handle) []const u8 {
|
2020-05-22 23:03:41 +01:00
|
|
|
const text = handle.document.text;
|
|
|
|
|
2021-10-01 02:47:48 +01:00
|
|
|
if (pos_index + 1 >= text.len) return "";
|
2020-05-18 21:19:23 +01:00
|
|
|
var start_idx = pos_index;
|
2020-05-21 12:36:14 +01:00
|
|
|
|
2021-05-03 13:36:29 +01:00
|
|
|
while (start_idx > 0 and isSymbolChar(text[start_idx - 1])) {
|
2021-05-02 18:15:31 +01:00
|
|
|
start_idx -= 1;
|
|
|
|
}
|
2020-05-18 21:19:23 +01:00
|
|
|
|
|
|
|
var end_idx = pos_index;
|
2021-05-02 18:15:31 +01:00
|
|
|
while (end_idx < handle.document.text.len and isSymbolChar(text[end_idx])) {
|
|
|
|
end_idx += 1;
|
|
|
|
}
|
2020-05-18 21:19:23 +01:00
|
|
|
|
2021-10-01 02:47:48 +01:00
|
|
|
if (end_idx <= start_idx) return "";
|
2021-05-03 13:36:29 +01:00
|
|
|
return text[start_idx..end_idx];
|
2021-05-02 18:15:31 +01:00
|
|
|
}
|
2021-10-01 01:53:14 +01:00
|
|
|
|
2021-05-02 18:15:31 +01:00
|
|
|
fn isSymbolChar(char: u8) bool {
|
|
|
|
return std.ascii.isAlNum(char) or char == '_';
|
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);
|
2021-03-07 13:51:47 +00:00
|
|
|
break :block offsets.tokenRelativeLocation(handle.tree, 0, handle.tree.tokens.items(.start)[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
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-10-01 01:51:51 +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;
|
2021-03-01 15:02:24 +00:00
|
|
|
const tree = handle.tree;
|
2020-06-10 20:52:33 +01:00
|
|
|
|
2020-11-06 08:08:20 +00:00
|
|
|
const hover_kind: types.MarkupContent.Kind = if (client_capabilities.hover_supports_md) .Markdown else .PlainText;
|
2021-04-07 14:10:18 +01:00
|
|
|
var doc_str: ?[]const u8 = null;
|
|
|
|
|
|
|
|
const def_str = switch (decl_handle.decl.*) {
|
|
|
|
.ast_node => |node| def: {
|
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);
|
|
|
|
}
|
2021-04-07 14:10:18 +01:00
|
|
|
doc_str = try analysis.getDocComments(&arena.allocator, tree, node, hover_kind);
|
2020-05-25 23:51:50 +01:00
|
|
|
|
2021-10-01 02:44:06 +01:00
|
|
|
var buf: [1]Ast.Node.Index = undefined;
|
2020-06-10 18:48:40 +01:00
|
|
|
|
2021-10-03 00:39:24 +01:00
|
|
|
if (ast.varDecl(tree, node)) |var_decl| {
|
2021-04-07 14:10:18 +01:00
|
|
|
break :def analysis.getVariableSignature(tree, var_decl);
|
2021-10-03 00:39:24 +01:00
|
|
|
} else if (ast.fnProto(tree, node, &buf)) |fn_proto| {
|
2021-04-07 14:10:18 +01:00
|
|
|
break :def analysis.getFunctionSignature(tree, fn_proto);
|
2021-10-03 00:39:24 +01:00
|
|
|
} else if (ast.containerField(tree, node)) |field| {
|
2021-04-07 14:10:18 +01:00
|
|
|
break :def analysis.getContainerFieldSignature(tree, field);
|
|
|
|
} else {
|
|
|
|
break :def analysis.nodeToString(tree, node) orelse
|
|
|
|
return try respondGeneric(id, null_result_response);
|
|
|
|
}
|
2020-06-10 20:52:33 +01:00
|
|
|
},
|
2021-04-07 14:10:18 +01:00
|
|
|
.param_decl => |param| def: {
|
|
|
|
if (param.first_doc_comment) |doc_comments| {
|
2021-04-15 10:07:43 +01:00
|
|
|
doc_str = try analysis.collectDocComments(&arena.allocator, handle.tree, doc_comments, hover_kind, false);
|
2021-04-07 14:10:18 +01:00
|
|
|
}
|
2020-06-10 20:52:33 +01:00
|
|
|
|
2021-03-01 15:02:24 +00:00
|
|
|
const first_token = param.first_doc_comment orelse
|
|
|
|
param.comptime_noalias orelse
|
|
|
|
param.name_token orelse
|
2021-03-07 20:54:54 +00:00
|
|
|
tree.firstToken(param.type_expr); // extern fn
|
|
|
|
const last_token = param.anytype_ellipsis3 orelse tree.lastToken(param.type_expr);
|
2021-03-01 15:02:24 +00:00
|
|
|
|
2021-03-05 21:38:42 +00:00
|
|
|
const start = offsets.tokenLocation(tree, first_token).start;
|
|
|
|
const end = offsets.tokenLocation(tree, last_token).end;
|
2021-04-07 14:10:18 +01:00
|
|
|
break :def tree.source[start..end];
|
2020-05-25 23:51:50 +01:00
|
|
|
},
|
2021-04-07 14:10:18 +01:00
|
|
|
.pointer_payload => |payload| tree.tokenSlice(payload.name),
|
|
|
|
.array_payload => |payload| handle.tree.tokenSlice(payload.identifier),
|
|
|
|
.array_index => |payload| handle.tree.tokenSlice(payload),
|
|
|
|
.switch_payload => |payload| tree.tokenSlice(payload.node),
|
|
|
|
.label_decl => |label_decl| tree.tokenSlice(label_decl),
|
|
|
|
};
|
|
|
|
|
|
|
|
var hover_text: []const u8 = undefined;
|
|
|
|
if (hover_kind == .Markdown) {
|
|
|
|
hover_text =
|
|
|
|
if (doc_str) |doc|
|
|
|
|
try std.fmt.allocPrint(&arena.allocator, "```zig\n{s}\n```\n{s}", .{ def_str, doc })
|
2021-03-09 18:53:59 +00:00
|
|
|
else
|
2021-04-07 14:10:18 +01:00
|
|
|
try std.fmt.allocPrint(&arena.allocator, "```zig\n{s}\n```", .{def_str});
|
|
|
|
} else {
|
|
|
|
hover_text =
|
|
|
|
if (doc_str) |doc|
|
|
|
|
try std.fmt.allocPrint(&arena.allocator, "{s}\n{s}", .{ def_str, doc })
|
2020-06-12 15:42:41 +01:00
|
|
|
else
|
2021-04-07 14:10:18 +01:00
|
|
|
def_str;
|
|
|
|
}
|
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 = .{
|
2021-04-07 14:10:18 +01:00
|
|
|
.contents = .{ .value = hover_text },
|
2020-06-10 20:52:33 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
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 {
|
2021-06-24 11:38:01 +01:00
|
|
|
_ = config;
|
|
|
|
|
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 {
|
2021-06-24 11:38:01 +01:00
|
|
|
_ = config;
|
|
|
|
|
2020-06-30 13:46:43 +01:00
|
|
|
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 {
|
2021-06-24 11:38:01 +01:00
|
|
|
_ = config;
|
|
|
|
|
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-11-04 22:54:47 +00:00
|
|
|
fn hoverDefinitionBuiltin(arena: *std.heap.ArenaAllocator, id: types.RequestId, pos_index: usize, handle: *DocumentStore.Handle) !void {
|
|
|
|
const name = identifierFromPosition(pos_index, handle.*);
|
|
|
|
if (name.len == 0) return try respondGeneric(id, null_result_response);
|
|
|
|
|
|
|
|
inline for (data.builtins) |builtin| {
|
|
|
|
if (std.mem.eql(u8, builtin.name[1..], name)) {
|
|
|
|
try send(arena, types.Response{
|
|
|
|
.id = id,
|
|
|
|
.result = .{
|
|
|
|
.Hover = .{
|
2021-04-07 14:10:18 +01:00
|
|
|
.contents = .{
|
|
|
|
.value = try std.fmt.allocPrint(
|
|
|
|
&arena.allocator,
|
|
|
|
"```zig\n{s}\n```\n{s}",
|
|
|
|
.{ builtin.signature, builtin.documentation },
|
|
|
|
),
|
|
|
|
},
|
2020-11-04 22:54:47 +00: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 {
|
2021-06-24 11:38:01 +01:00
|
|
|
_ = config;
|
|
|
|
|
2020-06-30 13:46:43 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-10-01 01:51:51 +01:00
|
|
|
fn getSymbolFieldAccess(handle: *DocumentStore.Handle, arena: *std.heap.ArenaAllocator, position: offsets.DocumentPosition, range: analysis.SourceRange, config: Config) !?analysis.DeclWithHandle {
|
2021-06-24 11:38:01 +01:00
|
|
|
_ = config;
|
|
|
|
|
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;
|
|
|
|
|
2021-07-10 17:58:37 +01:00
|
|
|
const line_mem_start = @ptrToInt(position.line.ptr) - @ptrToInt(handle.document.mem.ptr);
|
|
|
|
var held_range = handle.document.borrowNullTerminatedSlice(line_mem_start + range.start, line_mem_start + range.end);
|
|
|
|
var tokenizer = std.zig.Tokenizer.init(held_range.data());
|
|
|
|
|
|
|
|
errdefer held_range.release();
|
2020-07-07 21:26:12 +01:00
|
|
|
if (try analysis.getFieldAccessType(&document_store, arena, handle, position.absolute_index, &tokenizer)) |result| {
|
2021-07-10 17:58:37 +01:00
|
|
|
held_range.release();
|
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;
|
|
|
|
}
|
|
|
|
|
2021-10-01 01:51:51 +01:00
|
|
|
fn gotoDefinitionFieldAccess(arena: *std.heap.ArenaAllocator, id: types.RequestId, handle: *DocumentStore.Handle, position: offsets.DocumentPosition, range: analysis.SourceRange, config: Config, resolve_alias: bool) !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
|
|
|
|
2021-10-01 01:51:51 +01:00
|
|
|
fn hoverDefinitionFieldAccess(arena: *std.heap.ArenaAllocator, id: types.RequestId, handle: *DocumentStore.Handle, position: offsets.DocumentPosition, range: analysis.SourceRange, 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 {
|
2021-06-24 11:38:01 +01:00
|
|
|
_ = config;
|
|
|
|
|
2020-05-24 17:00:21 +01:00
|
|
|
const tree = handle.tree;
|
2020-04-27 21:38:35 +01:00
|
|
|
|
2021-03-01 13:32:19 +00:00
|
|
|
const import_str = analysis.getImportStr(tree, 0, 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
|
|
|
}
|
|
|
|
|
2021-10-01 01:51:51 +01:00
|
|
|
fn renameDefinitionFieldAccess(arena: *std.heap.ArenaAllocator, id: types.RequestId, handle: *DocumentStore.Handle, position: offsets.DocumentPosition, 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 },
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-10-01 01:51:51 +01:00
|
|
|
fn referencesDefinitionGlobal(arena: *std.heap.ArenaAllocator, id: types.RequestId, handle: *DocumentStore.Handle, pos_index: usize, include_decl: bool, skip_std_references: bool) !void {
|
2020-07-05 23:32:14 +01:00
|
|
|
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);
|
2021-03-06 19:55:59 +00:00
|
|
|
try references.symbolReferences(
|
|
|
|
arena,
|
|
|
|
&document_store,
|
|
|
|
decl,
|
|
|
|
offset_encoding,
|
|
|
|
include_decl,
|
|
|
|
&locs,
|
|
|
|
std.ArrayList(types.Location).append,
|
|
|
|
skip_std_references,
|
|
|
|
);
|
2020-07-05 23:32:14 +01:00
|
|
|
try send(arena, types.Response{
|
|
|
|
.id = id,
|
|
|
|
.result = .{ .Locations = locs.items },
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-10-01 01:51:51 +01:00
|
|
|
fn referencesDefinitionFieldAccess(arena: *std.heap.ArenaAllocator, id: types.RequestId, handle: *DocumentStore.Handle, position: offsets.DocumentPosition, range: analysis.SourceRange, include_decl: bool, config: Config) !void {
|
2020-07-05 23:32:14 +01:00
|
|
|
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);
|
2021-03-06 19:55:59 +00:00
|
|
|
try references.symbolReferences(arena, &document_store, decl, offset_encoding, include_decl, &locs, std.ArrayList(types.Location).append, config.skip_std_references);
|
2020-07-05 23:32:14 +01:00
|
|
|
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 },
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-10-01 02:44:06 +01:00
|
|
|
fn hasComment(tree: Ast.Tree, start_token: Ast.TokenIndex, end_token: Ast.TokenIndex) bool {
|
2021-03-01 13:32:19 +00:00
|
|
|
const token_starts = tree.tokens.items(.start);
|
|
|
|
|
|
|
|
const start = token_starts[start_token];
|
|
|
|
const end = token_starts[end_token];
|
|
|
|
|
|
|
|
return std.mem.indexOf(u8, tree.source[start..end], "//") != null;
|
|
|
|
}
|
|
|
|
|
2021-04-03 16:54:26 +01:00
|
|
|
const DeclToCompletionContext = struct {
|
|
|
|
completions: *std.ArrayList(types.CompletionItem),
|
|
|
|
config: *const Config,
|
|
|
|
arena: *std.heap.ArenaAllocator,
|
|
|
|
orig_handle: *DocumentStore.Handle,
|
|
|
|
parent_is_type_val: ?bool = null,
|
|
|
|
};
|
|
|
|
|
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.*) {
|
2021-03-06 19:55:59 +00:00
|
|
|
.ast_node => |node| try nodeToCompletion(
|
|
|
|
context.arena,
|
|
|
|
context.completions,
|
|
|
|
.{ .node = node, .handle = decl_handle.handle },
|
|
|
|
null,
|
|
|
|
context.orig_handle,
|
|
|
|
false,
|
2021-04-03 16:54:26 +01:00
|
|
|
context.parent_is_type_val,
|
2021-03-06 19:55:59 +00:00
|
|
|
context.config.*,
|
|
|
|
),
|
2020-06-10 20:52:33 +01:00
|
|
|
.param_decl => |param| {
|
2020-11-06 08:08:20 +00:00
|
|
|
const doc_kind: types.MarkupContent.Kind = if (client_capabilities.completion_doc_supports_md) .Markdown else .PlainText;
|
2021-03-01 13:32:19 +00:00
|
|
|
const doc = if (param.first_doc_comment) |doc_comments|
|
2020-06-10 20:52:33 +01:00
|
|
|
types.MarkupContent{
|
2020-06-12 15:42:41 +01:00
|
|
|
.kind = doc_kind,
|
2021-04-15 10:07:43 +01:00
|
|
|
.value = try analysis.collectDocComments(&context.arena.allocator, tree, doc_comments, doc_kind, false),
|
2020-06-10 20:52:33 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
null;
|
|
|
|
|
2021-03-01 13:32:19 +00:00
|
|
|
const first_token = param.first_doc_comment orelse
|
|
|
|
param.comptime_noalias orelse
|
|
|
|
param.name_token orelse
|
|
|
|
tree.firstToken(param.type_expr);
|
2021-03-08 18:46:23 +00:00
|
|
|
const last_token = param.anytype_ellipsis3 orelse tree.lastToken(param.type_expr);
|
2021-03-01 13:32:19 +00:00
|
|
|
|
2020-06-10 20:52:33 +01:00
|
|
|
try context.completions.append(.{
|
|
|
|
.label = tree.tokenSlice(param.name_token.?),
|
|
|
|
.kind = .Constant,
|
|
|
|
.documentation = doc,
|
2021-03-05 21:38:42 +00:00
|
|
|
.detail = tree.source[offsets.tokenLocation(tree, first_token).start..offsets.tokenLocation(tree, last_token).end],
|
2021-03-28 15:02:48 +01:00
|
|
|
.insertText = tree.tokenSlice(param.name_token.?),
|
|
|
|
.insertTextFormat = .PlainText,
|
2020-06-10 20:52:33 +01:00
|
|
|
});
|
|
|
|
},
|
|
|
|
.pointer_payload => |payload| {
|
|
|
|
try context.completions.append(.{
|
2021-03-01 13:32:19 +00:00
|
|
|
.label = tree.tokenSlice(payload.name),
|
2020-06-10 20:52:33 +01:00
|
|
|
.kind = .Variable,
|
2021-03-28 15:02:48 +01:00
|
|
|
.insertText = tree.tokenSlice(payload.name),
|
|
|
|
.insertTextFormat = .PlainText,
|
2020-06-10 20:52:33 +01:00
|
|
|
});
|
|
|
|
},
|
2021-03-08 18:46:23 +00:00
|
|
|
.array_payload => |payload| {
|
|
|
|
try context.completions.append(.{
|
|
|
|
.label = tree.tokenSlice(payload.identifier),
|
|
|
|
.kind = .Variable,
|
2021-03-28 15:02:48 +01:00
|
|
|
.insertText = tree.tokenSlice(payload.identifier),
|
|
|
|
.insertTextFormat = .PlainText,
|
2021-03-08 18:46:23 +00:00
|
|
|
});
|
|
|
|
},
|
2021-03-09 18:53:59 +00:00
|
|
|
.array_index => |payload| {
|
|
|
|
try context.completions.append(.{
|
|
|
|
.label = tree.tokenSlice(payload),
|
|
|
|
.kind = .Variable,
|
2021-03-28 15:02:48 +01:00
|
|
|
.insertText = tree.tokenSlice(payload),
|
|
|
|
.insertTextFormat = .PlainText,
|
2021-03-09 18:53:59 +00:00
|
|
|
});
|
|
|
|
},
|
2020-06-10 20:52:33 +01:00
|
|
|
.switch_payload => |payload| {
|
|
|
|
try context.completions.append(.{
|
2021-03-09 11:35:56 +00:00
|
|
|
.label = tree.tokenSlice(payload.node),
|
2020-06-10 20:52:33 +01:00
|
|
|
.kind = .Variable,
|
2021-03-28 15:02:48 +01:00
|
|
|
.insertText = tree.tokenSlice(payload.node),
|
|
|
|
.insertTextFormat = .PlainText,
|
2020-06-10 20:52:33 +01:00
|
|
|
});
|
2020-06-10 17:01:44 +01:00
|
|
|
},
|
2020-06-14 20:24:18 +01:00
|
|
|
.label_decl => |label_decl| {
|
|
|
|
try context.completions.append(.{
|
2021-03-01 13:32:19 +00:00
|
|
|
.label = tree.tokenSlice(label_decl),
|
2020-06-14 20:24:18 +01:00
|
|
|
.kind = .Variable,
|
2021-03-28 15:02:48 +01:00
|
|
|
.insertText = tree.tokenSlice(label_decl),
|
|
|
|
.insertTextFormat = .PlainText,
|
2020-06-14 20:24:18 +01:00
|
|
|
});
|
|
|
|
},
|
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);
|
2021-03-27 19:37:51 +00:00
|
|
|
truncateCompletions(completions.items, config.max_detail_length);
|
2020-06-14 20:24:18 +01:00
|
|
|
|
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-11-04 22:39:24 +00:00
|
|
|
var builtin_completions: ?[]types.CompletionItem = null;
|
|
|
|
fn completeBuiltin(arena: *std.heap.ArenaAllocator, id: types.RequestId, config: Config) !void {
|
|
|
|
if (builtin_completions == null) {
|
|
|
|
builtin_completions = try allocator.alloc(types.CompletionItem, data.builtins.len);
|
|
|
|
for (data.builtins) |builtin, idx| {
|
|
|
|
builtin_completions.?[idx] = types.CompletionItem{
|
|
|
|
.label = builtin.name,
|
|
|
|
.kind = .Function,
|
|
|
|
.filterText = builtin.name[1..],
|
|
|
|
.detail = builtin.signature,
|
|
|
|
.documentation = .{
|
|
|
|
.kind = .Markdown,
|
|
|
|
.value = builtin.documentation,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-03-26 09:20:20 +00:00
|
|
|
var insert_text: []const u8 = undefined;
|
2020-11-04 22:39:24 +00:00
|
|
|
if (config.enable_snippets) {
|
2021-03-26 09:20:20 +00:00
|
|
|
insert_text = builtin.snippet;
|
2020-11-04 22:39:24 +00:00
|
|
|
builtin_completions.?[idx].insertTextFormat = .Snippet;
|
|
|
|
} else {
|
2021-03-26 09:20:20 +00:00
|
|
|
insert_text = builtin.name;
|
2020-11-04 22:39:24 +00:00
|
|
|
}
|
2021-03-28 15:02:48 +01:00
|
|
|
builtin_completions.?[idx].insertText =
|
2021-03-26 09:20:20 +00:00
|
|
|
if (config.include_at_in_builtins)
|
2021-03-28 15:02:48 +01:00
|
|
|
insert_text
|
|
|
|
else
|
|
|
|
insert_text[1..];
|
2020-11-04 22:39:24 +00:00
|
|
|
}
|
2021-03-27 19:37:51 +00:00
|
|
|
truncateCompletions(builtin_completions.?, config.max_detail_length);
|
2020-11-04 22:39:24 +00:00
|
|
|
}
|
2021-03-28 15:02:48 +01:00
|
|
|
|
2020-11-04 22:39:24 +00:00
|
|
|
try send(arena, types.Response{
|
|
|
|
.id = id,
|
|
|
|
.result = .{
|
|
|
|
.CompletionList = .{
|
|
|
|
.isIncomplete = false,
|
|
|
|
.items = builtin_completions.?,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-10-01 01:51:51 +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);
|
2021-03-27 19:37:51 +00:00
|
|
|
truncateCompletions(completions.items, config.max_detail_length);
|
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
|
|
|
}
|
|
|
|
|
2021-10-01 01:51:51 +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);
|
2021-07-10 17:58:37 +01:00
|
|
|
|
|
|
|
const line_mem_start = @ptrToInt(position.line.ptr) - @ptrToInt(handle.document.mem.ptr);
|
|
|
|
var held_range = handle.document.borrowNullTerminatedSlice(line_mem_start + range.start, line_mem_start + range.end);
|
|
|
|
errdefer held_range.release();
|
|
|
|
var tokenizer = std.zig.Tokenizer.init(held_range.data());
|
|
|
|
|
2020-07-07 21:26:12 +01:00
|
|
|
if (try analysis.getFieldAccessType(&document_store, arena, handle, position.absolute_index, &tokenizer)) |result| {
|
2021-07-10 17:58:37 +01:00
|
|
|
held_range.release();
|
2020-07-07 21:26:12 +01:00
|
|
|
try typeToCompletion(arena, &completions, result, handle, config);
|
2021-03-30 14:45:49 +01:00
|
|
|
truncateCompletions(completions.items, config.max_detail_length);
|
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
|
|
|
|
2021-10-01 01:51:51 +01:00
|
|
|
fn completeError(arena: *std.heap.ArenaAllocator, id: types.RequestId, handle: *DocumentStore.Handle, config: Config) !void {
|
2021-03-27 19:37:51 +00:00
|
|
|
const completions = try document_store.errorCompletionItems(arena, handle);
|
|
|
|
truncateCompletions(completions, config.max_detail_length);
|
2021-04-07 14:10:18 +01:00
|
|
|
logger.debug("Completing error:", .{});
|
2021-03-27 19:37:51 +00:00
|
|
|
|
|
|
|
try send(arena, types.Response{
|
|
|
|
.id = id,
|
|
|
|
.result = .{
|
|
|
|
.CompletionList = .{
|
|
|
|
.isIncomplete = false,
|
|
|
|
.items = completions,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-10-01 01:51:51 +01:00
|
|
|
fn completeDot(arena: *std.heap.ArenaAllocator, id: types.RequestId, handle: *DocumentStore.Handle, config: Config) !void {
|
2021-03-27 19:37:51 +00:00
|
|
|
var completions = try document_store.enumCompletionItems(arena, handle);
|
|
|
|
truncateCompletions(completions, config.max_detail_length);
|
2021-03-28 15:02:48 +01:00
|
|
|
|
2021-03-27 19:37:51 +00:00
|
|
|
try send(arena, types.Response{
|
|
|
|
.id = id,
|
|
|
|
.result = .{
|
|
|
|
.CompletionList = .{
|
|
|
|
.isIncomplete = false,
|
|
|
|
.items = completions,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-05-03 14:07:53 +01:00
|
|
|
fn loadConfigFile(file_path: []const u8) ?Config {
|
|
|
|
var file = std.fs.cwd().openFile(file_path, .{}) catch |err| {
|
2020-06-28 11:58:51 +01:00
|
|
|
if (err != error.FileNotFound)
|
2020-11-15 19:32:27 +00:00
|
|
|
logger.warn("Error while reading configuration file: {}", .{err});
|
2020-06-28 11:58:51 +01:00
|
|
|
return null;
|
|
|
|
};
|
2020-05-19 20:09:00 +01:00
|
|
|
|
2021-05-03 14:07:53 +01:00
|
|
|
defer file.close();
|
|
|
|
|
|
|
|
const file_buf = file.readToEndAlloc(allocator, 0x1000000) catch return null;
|
|
|
|
defer allocator.free(file_buf);
|
2021-03-27 19:37:51 +00:00
|
|
|
@setEvalBranchQuota(2000);
|
2020-05-19 20:09:00 +01:00
|
|
|
// 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-11-15 19:32:27 +00:00
|
|
|
logger.warn("Error while parsing configuration file: {}", .{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-11-15 19:32:27 +00:00
|
|
|
logger.warn("zig library path is not absolute, defaulting to null.", .{});
|
2020-05-19 20:09:00 +01:00
|
|
|
allocator.free(zig_lib_path);
|
|
|
|
config.zig_lib_path = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
2021-05-03 14:07:53 +01:00
|
|
|
fn loadConfigInFolder(folder_path: []const u8) ?Config {
|
2021-10-01 01:52:36 +01:00
|
|
|
const full_path = std.fs.path.resolve(allocator, &.{ folder_path, "zls.json" }) catch return null;
|
2021-05-03 14:07:53 +01:00
|
|
|
defer allocator.free(full_path);
|
|
|
|
return loadConfigFile(full_path);
|
|
|
|
}
|
|
|
|
|
2020-06-29 23:34:21 +01:00
|
|
|
fn initializeHandler(arena: *std.heap.ArenaAllocator, id: types.RequestId, req: requests.Initialize, config: Config) !void {
|
2021-06-24 11:38:01 +01:00
|
|
|
_ = config;
|
|
|
|
|
2020-07-03 10:00:00 +01:00
|
|
|
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.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-11-06 08:08:20 +00:00
|
|
|
try send(arena, types.Response{
|
|
|
|
.id = id,
|
|
|
|
.result = .{
|
|
|
|
.InitializeResult = .{
|
|
|
|
.offsetEncoding = if (offset_encoding == .utf8)
|
|
|
|
@as([]const u8, "utf-8")
|
|
|
|
else
|
|
|
|
"utf-16",
|
|
|
|
.serverInfo = .{
|
|
|
|
.name = "zls",
|
|
|
|
.version = "0.1.0",
|
|
|
|
},
|
|
|
|
.capabilities = .{
|
|
|
|
.signatureHelpProvider = .{
|
2021-04-01 12:14:49 +01:00
|
|
|
.triggerCharacters = &.{"("},
|
|
|
|
.retriggerCharacters = &.{","},
|
2020-11-06 08:08:20 +00:00
|
|
|
},
|
2021-01-07 08:35:36 +00:00
|
|
|
.textDocumentSync = .Full,
|
2020-11-06 08:08:20 +00:00
|
|
|
.renameProvider = true,
|
|
|
|
.completionProvider = .{
|
|
|
|
.resolveProvider = false,
|
|
|
|
.triggerCharacters = &[_][]const u8{ ".", ":", "@" },
|
|
|
|
},
|
|
|
|
.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 = .{
|
2020-11-15 18:51:56 +00:00
|
|
|
.supported = false,
|
|
|
|
.changeNotifications = false,
|
2020-11-06 08:08:20 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
.semanticTokensProvider = .{
|
2020-11-06 13:55:00 +00:00
|
|
|
.full = true,
|
|
|
|
.range = false,
|
2020-11-06 08:08:20 +00:00
|
|
|
.legend = .{
|
|
|
|
.tokenTypes = comptime block: {
|
|
|
|
const tokTypeFields = std.meta.fields(semantic_tokens.TokenType);
|
|
|
|
var names: [tokTypeFields.len][]const u8 = undefined;
|
|
|
|
for (tokTypeFields) |field, i| {
|
|
|
|
names[i] = field.name;
|
|
|
|
}
|
|
|
|
break :block &names;
|
|
|
|
},
|
|
|
|
.tokenModifiers = comptime block: {
|
|
|
|
const tokModFields = std.meta.fields(semantic_tokens.TokenModifiers);
|
|
|
|
var names: [tokModFields.len][]const u8 = undefined;
|
|
|
|
for (tokModFields) |field, i| {
|
|
|
|
names[i] = field.name;
|
|
|
|
}
|
|
|
|
break :block &names;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-10-25 11:56:04 +01:00
|
|
|
logger.info("zls initialized", .{});
|
2020-11-15 19:32:27 +00:00
|
|
|
logger.info("{}", .{client_capabilities});
|
2021-10-25 11:56:04 +01:00
|
|
|
logger.info("Using offset encoding: {s}", .{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 {
|
2021-06-24 11:38:01 +01:00
|
|
|
_ = config;
|
|
|
|
_ = arena;
|
|
|
|
|
2021-10-25 11:56:04 +01:00
|
|
|
logger.info("Server closing...", .{});
|
2020-10-10 10:44:37 +01:00
|
|
|
|
2020-06-29 23:34:21 +01:00
|
|
|
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 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);
|
2020-11-15 18:51:56 +00:00
|
|
|
try publishDiagnostics(arena, handle.*, config);
|
2020-06-30 16:00:33 +01:00
|
|
|
|
2021-04-07 14:10:18 +01:00
|
|
|
if (client_capabilities.supports_semantic_tokens)
|
|
|
|
try semanticTokensFullHandler(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 {
|
2021-06-24 11:38:01 +01:00
|
|
|
_ = id;
|
|
|
|
|
2020-06-29 23:34:21 +01:00
|
|
|
const handle = document_store.getHandle(req.params.textDocument.uri) orelse {
|
2021-01-04 17:51:26 +00:00
|
|
|
logger.debug("Trying to change non existent document {s}", .{req.params.textDocument.uri});
|
2020-06-29 23:34:21 +01:00
|
|
|
return;
|
|
|
|
};
|
2020-06-13 19:20:04 +01:00
|
|
|
|
2021-03-30 19:41:04 +01:00
|
|
|
try document_store.applyChanges(handle, req.params.contentChanges.Array, offset_encoding);
|
2020-11-15 18:51:56 +00:00
|
|
|
try publishDiagnostics(arena, handle.*, config);
|
2020-06-29 23:34:21 +01:00
|
|
|
}
|
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 {
|
2021-06-24 11:38:01 +01:00
|
|
|
_ = config;
|
|
|
|
_ = id;
|
|
|
|
_ = arena;
|
2020-06-29 23:34:21 +01:00
|
|
|
const handle = document_store.getHandle(req.params.textDocument.uri) orelse {
|
2021-01-04 17:51:26 +00:00
|
|
|
logger.warn("Trying to save non existent document {s}", .{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 {
|
2021-06-24 11:38:01 +01:00
|
|
|
_ = config;
|
|
|
|
_ = id;
|
|
|
|
_ = arena;
|
2020-06-29 23:34:21 +01:00
|
|
|
document_store.closeDocument(req.params.textDocument.uri);
|
|
|
|
}
|
|
|
|
|
2021-05-02 18:15:31 +01:00
|
|
|
fn semanticTokensFullHandler(arena: *std.heap.ArenaAllocator, id: types.RequestId, req: requests.SemanticTokensFull, config: Config) !void {
|
2021-04-07 14:10:18 +01:00
|
|
|
if (config.enable_semantic_tokens) blk: {
|
2020-06-29 23:34:21 +01:00
|
|
|
const handle = document_store.getHandle(req.params.textDocument.uri) orelse {
|
2021-01-04 17:51:26 +00:00
|
|
|
logger.warn("Trying to get semantic tokens of non existent document {s}", .{req.params.textDocument.uri});
|
2021-04-07 14:10:18 +01:00
|
|
|
break :blk;
|
2020-05-14 00:10:41 +01:00
|
|
|
};
|
|
|
|
|
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,
|
2020-09-25 22:24:10 +01:00
|
|
|
.result = .{ .SemanticTokensFull = .{ .data = token_array } },
|
2020-06-29 23:34:21 +01:00
|
|
|
});
|
2020-07-23 20:09:28 +01:00
|
|
|
}
|
2021-04-07 14:10:18 +01:00
|
|
|
return try respondGeneric(id, no_semantic_tokens_response);
|
2020-06-29 23:34:21 +01:00
|
|
|
}
|
2020-05-25 22:37:18 +01:00
|
|
|
|
2021-10-01 01:51:51 +01:00
|
|
|
fn completionHandler(arena: *std.heap.ArenaAllocator, id: types.RequestId, req: requests.Completion, config: Config) !void {
|
2020-06-30 13:46:43 +01:00
|
|
|
const handle = document_store.getHandle(req.params.textDocument.uri) orelse {
|
2021-01-04 17:51:26 +00:00
|
|
|
logger.warn("Trying to complete in non existent document {s}", .{req.params.textDocument.uri});
|
2020-06-30 13:46:43 +01:00
|
|
|
return try respondGeneric(id, no_completions_response);
|
|
|
|
};
|
|
|
|
|
2021-04-01 12:14:49 +01:00
|
|
|
if (req.params.position.character == 0)
|
|
|
|
return try respondGeneric(id, no_completions_response);
|
2021-03-27 19:37:51 +00:00
|
|
|
|
2021-04-01 12:14:49 +01:00
|
|
|
const doc_position = try offsets.documentPosition(handle.document, req.params.position, offset_encoding);
|
|
|
|
const pos_context = try analysis.documentPositionContext(arena, handle.document, doc_position);
|
|
|
|
|
|
|
|
switch (pos_context) {
|
|
|
|
.builtin => try completeBuiltin(arena, id, config),
|
|
|
|
.var_access, .empty => try completeGlobal(arena, id, doc_position.absolute_index, handle, config),
|
|
|
|
.field_access => |range| try completeFieldAccess(arena, id, handle, doc_position, range, config),
|
|
|
|
.global_error_set => try completeError(arena, id, handle, config),
|
|
|
|
.enum_literal => try completeDot(arena, id, handle, config),
|
|
|
|
.label => try completeLabel(arena, id, doc_position.absolute_index, handle, config),
|
|
|
|
else => try respondGeneric(id, no_completions_response),
|
2020-06-30 13:46:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-01 01:51:51 +01:00
|
|
|
fn signatureHelpHandler(arena: *std.heap.ArenaAllocator, id: types.RequestId, req: requests.SignatureHelp, config: Config) !void {
|
2021-06-24 11:38:01 +01:00
|
|
|
_ = config;
|
|
|
|
|
2021-04-02 18:49:01 +01:00
|
|
|
const getSignatureInfo = @import("signature_help.zig").getSignatureInfo;
|
2021-04-01 12:14:49 +01:00
|
|
|
const handle = document_store.getHandle(req.params.textDocument.uri) orelse {
|
|
|
|
logger.warn("Trying to get signature help in non existent document {s}", .{req.params.textDocument.uri});
|
|
|
|
return try respondGeneric(id, no_signatures_response);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (req.params.position.character == 0)
|
|
|
|
return try respondGeneric(id, no_signatures_response);
|
|
|
|
|
|
|
|
const doc_position = try offsets.documentPosition(handle.document, req.params.position, offset_encoding);
|
2021-04-02 18:49:01 +01:00
|
|
|
if (try getSignatureInfo(
|
|
|
|
&document_store,
|
|
|
|
arena,
|
|
|
|
handle,
|
|
|
|
doc_position.absolute_index,
|
|
|
|
data,
|
|
|
|
)) |sig_info| {
|
|
|
|
return try send(arena, types.Response{
|
|
|
|
.id = id,
|
2021-04-07 14:10:18 +01:00
|
|
|
.result = .{
|
|
|
|
.SignatureHelp = .{
|
|
|
|
.signatures = &[1]types.SignatureInformation{sig_info},
|
|
|
|
.activeSignature = 0,
|
|
|
|
.activeParameter = sig_info.activeParameter,
|
|
|
|
},
|
|
|
|
},
|
2021-04-02 18:49:01 +01:00
|
|
|
});
|
2021-04-01 12:14:49 +01:00
|
|
|
}
|
2021-04-02 18:49:01 +01:00
|
|
|
return try respondGeneric(id, no_signatures_response);
|
2020-06-30 13:46:43 +01:00
|
|
|
}
|
|
|
|
|
2021-10-01 01:51:51 +01:00
|
|
|
fn gotoHandler(arena: *std.heap.ArenaAllocator, id: types.RequestId, req: requests.GotoDefinition, config: Config, resolve_alias: bool) !void {
|
2020-06-30 13:46:43 +01:00
|
|
|
const handle = document_store.getHandle(req.params.textDocument.uri) orelse {
|
2021-01-04 17:51:26 +00:00
|
|
|
logger.warn("Trying to go to definition in non existent document {s}", .{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
|
|
|
|
|
|
|
switch (pos_context) {
|
2020-11-15 18:51:56 +00:00
|
|
|
.var_access => try gotoDefinitionGlobal(arena, id, doc_position.absolute_index, handle, config, resolve_alias),
|
|
|
|
.field_access => |range| try gotoDefinitionFieldAccess(arena, id, handle, doc_position, range, config, resolve_alias),
|
2020-07-03 00:31:28 +01:00
|
|
|
.string_literal => try gotoDefinitionString(arena, id, doc_position.absolute_index, handle, config),
|
2020-11-15 18:51:56 +00:00
|
|
|
.label => try gotoDefinitionLabel(arena, id, doc_position.absolute_index, handle, 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 {
|
2021-01-04 17:51:26 +00:00
|
|
|
logger.warn("Trying to get hover in non existent document {s}", .{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
|
|
|
switch (pos_context) {
|
2020-11-04 22:54:47 +00:00
|
|
|
.builtin => try hoverDefinitionBuiltin(arena, id, doc_position.absolute_index, handle),
|
2020-11-15 18:51:56 +00:00
|
|
|
.var_access => try hoverDefinitionGlobal(arena, id, doc_position.absolute_index, handle, config),
|
|
|
|
.field_access => |range| try hoverDefinitionFieldAccess(arena, id, handle, doc_position, range, config),
|
|
|
|
.label => try hoverDefinitionLabel(arena, id, doc_position.absolute_index, handle, 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 {
|
2021-06-24 11:38:01 +01:00
|
|
|
_ = config;
|
|
|
|
|
2020-06-30 13:46:43 +01:00
|
|
|
const handle = document_store.getHandle(req.params.textDocument.uri) orelse {
|
2021-01-04 17:51:26 +00:00
|
|
|
logger.warn("Trying to get document symbols in non existent document {s}", .{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 {
|
2021-01-04 17:51:26 +00:00
|
|
|
logger.warn("Trying to got to definition in non existent document {s}", .{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-11-15 19:32:27 +00:00
|
|
|
logger.warn("Failed to spawn zig fmt process, error: {}", .{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 {
|
2021-01-04 17:51:26 +00:00
|
|
|
logger.warn("Trying to rename in non existent document {s}", .{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
|
|
|
|
|
|
|
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),
|
2020-11-15 18:51:56 +00:00
|
|
|
.field_access => |range| try renameDefinitionFieldAccess(arena, id, handle, doc_position, range, req.params.newName, config),
|
2020-07-03 00:31:28 +01:00
|
|
|
.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 {
|
2021-01-04 17:51:26 +00:00
|
|
|
logger.warn("Trying to get references in non existent document {s}", .{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 include_decl = req.params.context.includeDeclaration;
|
|
|
|
switch (pos_context) {
|
2021-03-06 19:55:59 +00:00
|
|
|
.var_access => try referencesDefinitionGlobal(arena, id, handle, doc_position.absolute_index, include_decl, config.skip_std_references),
|
2020-11-15 18:51:56 +00:00
|
|
|
.field_access => |range| try referencesDefinitionFieldAccess(arena, id, handle, doc_position, range, include_decl, config),
|
2020-07-05 23:32:14 +01:00
|
|
|
.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();
|
2021-01-04 17:51:26 +00:00
|
|
|
logger.debug("Took {}ms to process method {s}", .{ 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 },
|
|
|
|
.{ "textDocument/didOpen", requests.OpenDocument, openDocumentHandler },
|
|
|
|
.{ "textDocument/didChange", requests.ChangeDocument, changeDocumentHandler },
|
|
|
|
.{ "textDocument/didSave", requests.SaveDocument, saveDocumentHandler },
|
|
|
|
.{ "textDocument/didClose", requests.CloseDocument, closeDocumentHandler },
|
2020-09-25 22:24:10 +01:00
|
|
|
.{ "textDocument/semanticTokens/full", requests.SemanticTokensFull, semanticTokensFullHandler },
|
2020-06-30 13:46:43 +01:00
|
|
|
.{ "textDocument/completion", requests.Completion, completionHandler },
|
2021-04-02 18:49:01 +01:00
|
|
|
.{ "textDocument/signatureHelp", requests.SignatureHelp, signatureHelpHandler },
|
2020-06-30 13:46:43 +01:00
|
|
|
.{ "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) {
|
2021-01-04 17:51:26 +00:00
|
|
|
logger.warn("Could not create request type {s} from JSON {s}", .{ @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-09-25 22:24:10 +01:00
|
|
|
.{"textDocument/semanticTokens/range"},
|
2020-11-15 18:51:56 +00:00
|
|
|
.{"workspace/didChangeWorkspaceFolders"},
|
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);
|
|
|
|
}
|
2021-01-04 17:51:26 +00:00
|
|
|
logger.debug("Method without return value not implemented: {s}", .{method});
|
2020-04-24 23:19:03 +01:00
|
|
|
}
|
|
|
|
|
2021-10-07 12:34:14 +01:00
|
|
|
const stack_frames = switch (zig_builtin.mode) {
|
2021-03-31 00:25:49 +01:00
|
|
|
.Debug => 10,
|
|
|
|
else => 0,
|
|
|
|
};
|
2021-03-30 11:38:07 +01:00
|
|
|
var gpa_state = std.heap.GeneralPurposeAllocator(.{ .stack_trace_frames = stack_frames }){};
|
|
|
|
|
2020-04-24 23:19:03 +01:00
|
|
|
pub fn main() anyerror!void {
|
2020-10-10 10:44:37 +01:00
|
|
|
defer _ = gpa_state.deinit();
|
2021-04-07 14:10:18 +01:00
|
|
|
defer keep_running = false;
|
2021-07-10 17:58:37 +01:00
|
|
|
// allocator = &gpa_state.allocator;
|
|
|
|
// @TODO Using the GPA here, realloc calls hang currently for some reason
|
|
|
|
allocator = std.heap.page_allocator;
|
2020-06-16 22:26:45 +01:00
|
|
|
|
2021-04-07 14:10:18 +01:00
|
|
|
analysis.init(allocator);
|
|
|
|
defer analysis.deinit();
|
|
|
|
|
2020-11-08 11:40:16 +00:00
|
|
|
// Check arguments.
|
|
|
|
var args_it = std.process.args();
|
2021-03-31 15:54:27 +01:00
|
|
|
defer args_it.deinit();
|
2021-04-07 14:10:18 +01:00
|
|
|
const prog_name = try args_it.next(allocator) orelse @panic("Could not find self argument");
|
2020-11-08 11:40:16 +00:00
|
|
|
allocator.free(prog_name);
|
|
|
|
|
2021-05-03 14:07:53 +01:00
|
|
|
var config_path: ?[]const u8 = null;
|
|
|
|
var next_arg_config_path = false;
|
2020-11-08 11:40:16 +00:00
|
|
|
while (args_it.next(allocator)) |maybe_arg| {
|
|
|
|
const arg = try maybe_arg;
|
|
|
|
defer allocator.free(arg);
|
2021-05-03 14:07:53 +01:00
|
|
|
|
|
|
|
if (next_arg_config_path) {
|
|
|
|
config_path = try allocator.dupe(u8, arg);
|
|
|
|
next_arg_config_path = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-11-08 11:40:16 +00:00
|
|
|
if (std.mem.eql(u8, arg, "--debug-log")) {
|
|
|
|
actual_log_level = .debug;
|
2021-03-10 11:18:28 +00:00
|
|
|
std.debug.print("Enabled debug logging\n", .{});
|
2021-05-03 14:07:53 +01:00
|
|
|
} else if (std.mem.eql(u8, arg, "--config-path")) {
|
|
|
|
next_arg_config_path = true;
|
|
|
|
continue;
|
|
|
|
} else if (std.mem.eql(u8, arg, "config") or std.mem.eql(u8, arg, "configure")) {
|
2021-03-31 12:46:56 +01:00
|
|
|
try setup.wizard(allocator);
|
2021-03-30 18:49:29 +01:00
|
|
|
return;
|
2020-11-08 11:40:16 +00:00
|
|
|
} else {
|
2021-03-10 11:18:28 +00:00
|
|
|
std.debug.print("Unrecognized argument {s}\n", .{arg});
|
2020-11-08 11:40:16 +00:00
|
|
|
std.os.exit(1);
|
|
|
|
}
|
|
|
|
}
|
2021-05-03 14:07:53 +01:00
|
|
|
if (next_arg_config_path) {
|
|
|
|
std.debug.print("Expected configuration file path after --config-path argument\n", .{});
|
|
|
|
return;
|
|
|
|
}
|
2020-11-08 11:40:16 +00: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();
|
2021-01-10 07:12:11 +00:00
|
|
|
stdout = std.io.bufferedWriter(std.io.getStdOut().writer());
|
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{};
|
2021-03-30 11:33:31 +01:00
|
|
|
defer std.json.parseFree(Config, config, config_parse_options);
|
2020-05-09 14:43:51 +01:00
|
|
|
|
|
|
|
config_read: {
|
2021-05-03 14:07:53 +01:00
|
|
|
if (config_path) |path| {
|
|
|
|
defer allocator.free(path);
|
|
|
|
if (loadConfigFile(path)) |conf| {
|
|
|
|
config = conf;
|
|
|
|
break :config_read;
|
|
|
|
}
|
|
|
|
std.debug.print("Could not open configuration file '{s}'\n", .{path});
|
|
|
|
std.debug.print("Falling back to a lookup in the local and global configuration folders\n", .{});
|
|
|
|
}
|
2021-03-31 15:54:27 +01:00
|
|
|
if (try known_folders.getPath(allocator, .local_configuration)) |path| {
|
|
|
|
defer allocator.free(path);
|
2021-05-03 14:07:53 +01:00
|
|
|
if (loadConfigInFolder(path)) |conf| {
|
2020-05-19 20:09:00 +01:00
|
|
|
config = conf;
|
|
|
|
break :config_read;
|
|
|
|
}
|
|
|
|
}
|
2021-03-31 15:54:27 +01:00
|
|
|
if (try known_folders.getPath(allocator, .global_configuration)) |path| {
|
|
|
|
defer allocator.free(path);
|
2021-05-03 14:07:53 +01:00
|
|
|
if (loadConfigInFolder(path)) |conf| {
|
2021-03-31 15:54:27 +01:00
|
|
|
config = conf;
|
|
|
|
break :config_read;
|
|
|
|
}
|
2020-05-17 15:39:04 +01:00
|
|
|
}
|
2021-03-31 15:54:27 +01:00
|
|
|
logger.info("No config file zls.json found.", .{});
|
2020-05-15 11:21:34 +01:00
|
|
|
}
|
|
|
|
|
2020-05-25 14:18:00 +01:00
|
|
|
// Find the zig executable in PATH
|
|
|
|
find_zig: {
|
2020-05-30 21:36:18 +01:00
|
|
|
if (config.zig_exe_path) |exe_path| {
|
2021-03-30 11:33:31 +01:00
|
|
|
if (std.fs.path.isAbsolute(exe_path)) not_valid: {
|
|
|
|
std.fs.cwd().access(exe_path, .{}) catch break :not_valid;
|
|
|
|
break :find_zig;
|
2020-05-30 21:36:18 +01:00
|
|
|
}
|
2021-01-04 17:51:26 +00:00
|
|
|
logger.debug("zig path `{s}` is not absolute, will look in path", .{exe_path});
|
2021-03-30 11:33:31 +01:00
|
|
|
allocator.free(exe_path);
|
2020-05-30 21:36:18 +01:00
|
|
|
}
|
2021-03-30 18:49:29 +01:00
|
|
|
config.zig_exe_path = try setup.findZig(allocator);
|
2020-05-25 14:18:00 +01:00
|
|
|
}
|
|
|
|
|
2021-03-30 11:33:31 +01:00
|
|
|
if (config.zig_exe_path) |exe_path| {
|
2021-01-04 17:51:26 +00:00
|
|
|
logger.info("Using zig executable {s}", .{exe_path});
|
2021-03-30 11:33:31 +01:00
|
|
|
|
2020-11-15 19:32:27 +00:00
|
|
|
if (config.zig_lib_path == null) find_lib_path: {
|
|
|
|
// Use `zig env` to find the lib path
|
|
|
|
const zig_env_result = try std.ChildProcess.exec(.{
|
|
|
|
.allocator = allocator,
|
|
|
|
.argv = &[_][]const u8{ exe_path, "env" },
|
2020-05-30 21:36:18 +01:00
|
|
|
});
|
|
|
|
|
2020-11-15 19:32:27 +00:00
|
|
|
defer {
|
|
|
|
allocator.free(zig_env_result.stdout);
|
|
|
|
allocator.free(zig_env_result.stderr);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (zig_env_result.term) {
|
|
|
|
.Exited => |exit_code| {
|
|
|
|
if (exit_code == 0) {
|
|
|
|
const Env = struct {
|
|
|
|
zig_exe: []const u8,
|
|
|
|
lib_dir: ?[]const u8,
|
|
|
|
std_dir: []const u8,
|
|
|
|
global_cache_dir: []const u8,
|
|
|
|
version: []const u8,
|
|
|
|
};
|
|
|
|
|
|
|
|
var json_env = std.json.parse(
|
|
|
|
Env,
|
|
|
|
&std.json.TokenStream.init(zig_env_result.stdout),
|
|
|
|
.{ .allocator = allocator },
|
|
|
|
) catch {
|
|
|
|
logger.alert("Failed to parse zig env JSON result", .{});
|
|
|
|
break :find_lib_path;
|
|
|
|
};
|
|
|
|
defer std.json.parseFree(Env, json_env, .{ .allocator = allocator });
|
|
|
|
// We know this is allocated with `allocator`, we just steal it!
|
|
|
|
config.zig_lib_path = json_env.lib_dir.?;
|
|
|
|
json_env.lib_dir = null;
|
2021-10-25 11:56:04 +01:00
|
|
|
logger.info("Using zig lib path '{s}'", .{config.zig_lib_path});
|
2020-11-15 19:32:27 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
else => logger.alert("zig env invocation failed", .{}),
|
|
|
|
}
|
2020-05-30 21:36:18 +01:00
|
|
|
}
|
2020-11-15 19:32:27 +00:00
|
|
|
} else {
|
|
|
|
logger.warn("Zig executable path not specified in zls.json and could not be found in PATH", .{});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config.zig_lib_path == null) {
|
|
|
|
logger.warn("Zig standard library path not specified in zls.json and could not be resolved from the zig executable", .{});
|
2020-05-30 21:36:18 +01:00
|
|
|
}
|
|
|
|
|
2021-01-12 11:10:51 +00:00
|
|
|
const build_runner_path = if (config.build_runner_path) |p|
|
|
|
|
try allocator.dupe(u8, p)
|
|
|
|
else blk: {
|
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);
|
2021-01-12 11:10:51 +00:00
|
|
|
break :blk try std.fs.path.resolve(allocator, &[_][]const u8{ exe_dir_path, "build_runner.zig" });
|
|
|
|
};
|
2020-05-25 17:33:08 +01:00
|
|
|
|
2021-05-08 16:48:36 +01:00
|
|
|
const build_runner_cache_path = if (config.build_runner_cache_path) |p|
|
2021-01-12 11:10:51 +00:00
|
|
|
try allocator.dupe(u8, p)
|
|
|
|
else blk: {
|
|
|
|
const cache_dir_path = (try known_folders.getPath(allocator, .cache)) orelse {
|
|
|
|
logger.warn("Known-folders could not fetch the cache path", .{});
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
defer allocator.free(cache_dir_path);
|
|
|
|
break :blk try std.fs.path.resolve(allocator, &[_][]const u8{ cache_dir_path, "zls" });
|
|
|
|
};
|
|
|
|
|
2021-03-31 14:26:55 +01:00
|
|
|
try document_store.init(
|
|
|
|
allocator,
|
|
|
|
config.zig_exe_path,
|
|
|
|
build_runner_path,
|
|
|
|
build_runner_cache_path,
|
|
|
|
config.zig_lib_path,
|
|
|
|
);
|
2020-05-14 00:10:41 +01:00
|
|
|
defer document_store.deinit();
|
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();
|
|
|
|
|
2021-04-23 11:50:13 +01:00
|
|
|
// Arena used for temporary allocations while handling a request
|
2020-06-29 23:34:21 +01:00
|
|
|
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| {
|
2021-01-04 17:51:26 +00:00
|
|
|
logger.crit("{s}; 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-11-04 22:39:24 +00:00
|
|
|
|
|
|
|
if (builtin_completions) |compls| {
|
|
|
|
allocator.free(compls);
|
|
|
|
}
|
2020-04-24 23:19:03 +01:00
|
|
|
}
|