2020-04-24 23:19:03 +01:00
|
|
|
const std = @import("std");
|
2020-05-07 16:29:40 +01:00
|
|
|
const build_options = @import("build_options");
|
|
|
|
|
2020-05-09 14:43:51 +01:00
|
|
|
const Config = @import("config.zig");
|
2020-05-14 00:10:41 +01:00
|
|
|
const DocumentStore = @import("document_store.zig");
|
2020-05-17 12:40:32 +01:00
|
|
|
const DebugAllocator = @import("debug_allocator.zig");
|
2020-05-17 15:50:13 +01:00
|
|
|
const readRequestHeader = @import("header.zig").readRequestHeader;
|
2020-05-07 16:29:40 +01:00
|
|
|
const data = @import("data/" ++ build_options.data_version ++ ".zig");
|
2020-04-27 21:38:35 +01:00
|
|
|
const types = @import("types.zig");
|
|
|
|
const analysis = @import("analysis.zig");
|
2020-05-19 20:09:00 +01:00
|
|
|
const URI = @import("uri.zig");
|
2020-04-24 23:19:03 +01:00
|
|
|
|
|
|
|
// Code is largely based off of https://github.com/andersfr/zig-lsp/blob/master/server.zig
|
|
|
|
|
2020-05-17 18:26:02 +01:00
|
|
|
var stdout: std.io.BufferedOutStream(4096, std.fs.File.OutStream) = undefined;
|
2020-04-24 23:19:03 +01:00
|
|
|
var allocator: *std.mem.Allocator = undefined;
|
|
|
|
|
2020-05-14 00:10:41 +01:00
|
|
|
var document_store: DocumentStore = undefined;
|
2020-05-19 20:09:00 +01:00
|
|
|
var workspace_folder_configs: std.StringHashMap(?Config) = undefined;
|
2020-04-27 21:38:35 +01:00
|
|
|
|
2020-06-01 11:29:06 +01:00
|
|
|
const ClientCapabilities = struct {
|
|
|
|
supports_snippets: bool = false,
|
|
|
|
};
|
|
|
|
|
|
|
|
var client_capabilities = ClientCapabilities{};
|
|
|
|
|
2020-05-14 22:16:40 +01:00
|
|
|
const initialize_response =
|
2020-06-02 14:48:23 +01:00
|
|
|
\\,"result":{"capabilities":{"signatureHelpProvider":{"triggerCharacters":["(",","]},"textDocumentSync":1,"completionProvider":{"resolveProvider":false,"triggerCharacters":[".",":","@"]},"documentHighlightProvider":false,"hoverProvider":true,"codeActionProvider":false,"declarationProvider":true,"definitionProvider":true,"typeDefinitionProvider":true,"implementationProvider":false,"referencesProvider":false,"documentSymbolProvider":true,"colorProvider":false,"documentFormattingProvider":false,"documentRangeFormattingProvider":false,"foldingRangeProvider":false,"selectionRangeProvider":false,"workspaceSymbolProvider":false,"semanticTokensProvider":{"legend":{"tokenTypes":["type","struct","enum","parameter","variable","enumMember","function","member","keyword","modifier","comment","string","number","operator"],"tokenModifiers":["definition","async","documentation"]},"rangeProvider":false,"documentProvider":true},"workspace":{"workspaceFolders":{"supported":true,"changeNotifications":true}}}}}
|
2020-04-24 23:19:03 +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
|
|
|
;
|
|
|
|
|
2020-04-27 21:38:35 +01:00
|
|
|
/// Sends a request or response
|
2020-05-08 03:27:42 +01:00
|
|
|
fn send(reqOrRes: var) !void {
|
2020-05-28 01:39:36 +01:00
|
|
|
var arena = std.heap.ArenaAllocator.init(allocator);
|
|
|
|
defer arena.deinit();
|
|
|
|
|
|
|
|
var arr = std.ArrayList(u8).init(&arena.allocator);
|
|
|
|
try std.json.stringify(reqOrRes, std.json.StringifyOptions{}, arr.outStream());
|
2020-04-27 21:38:35 +01:00
|
|
|
|
2020-05-17 18:26:02 +01:00
|
|
|
const stdout_stream = stdout.outStream();
|
2020-05-28 01:39:36 +01:00
|
|
|
try stdout_stream.print("Content-Length: {}\r\n\r\n", .{arr.items.len});
|
|
|
|
try stdout_stream.writeAll(arr.items);
|
2020-05-17 18:26:02 +01:00
|
|
|
try stdout.flush();
|
2020-04-24 23:19:03 +01:00
|
|
|
}
|
|
|
|
|
2020-06-06 13:40:33 +01:00
|
|
|
fn respondGeneric(id: types.RequestId, response: []const u8) !void {
|
|
|
|
const id_len = switch (id) {
|
|
|
|
.Integer => |id_val| blk: {
|
|
|
|
if (id_val == 0) break :blk 1;
|
|
|
|
var digits: usize = 1;
|
|
|
|
var value = @divTrunc(id_val, 10);
|
|
|
|
while (value != 0) : (value = @divTrunc(value, 10)) {
|
|
|
|
digits += 1;
|
|
|
|
}
|
|
|
|
break :blk digits;
|
|
|
|
},
|
|
|
|
.String => |str_val| str_val.len + 2,
|
|
|
|
else => unreachable,
|
2020-04-24 23:19:03 +01:00
|
|
|
};
|
|
|
|
|
2020-06-06 13:40:33 +01:00
|
|
|
// Numbers of character that will be printed from this string: len - 1 brackets
|
|
|
|
const json_fmt = "{{\"jsonrpc\":\"2.0\",\"id\":";
|
2020-05-17 18:26:02 +01:00
|
|
|
|
|
|
|
const stdout_stream = stdout.outStream();
|
2020-06-06 13:40:33 +01:00
|
|
|
try stdout_stream.print("Content-Length: {}\r\n\r\n" ++ json_fmt, .{ response.len + id_len + json_fmt.len - 1 });
|
|
|
|
switch (id) {
|
|
|
|
.Integer => |int| try stdout_stream.print("{}", .{int}),
|
|
|
|
.String => |str| try stdout_stream.print("\"{}\"", .{str}),
|
|
|
|
else => unreachable,
|
|
|
|
}
|
|
|
|
|
2020-05-17 18:26:02 +01:00
|
|
|
try stdout_stream.writeAll(response);
|
|
|
|
try stdout.flush();
|
2020-05-08 00:53:00 +01:00
|
|
|
}
|
|
|
|
|
2020-05-14 22:16:40 +01:00
|
|
|
// TODO: Is this correct or can we get a better end?
|
2020-05-08 16:01:34 +01:00
|
|
|
fn astLocationToRange(loc: std.zig.ast.Tree.Location) types.Range {
|
|
|
|
return .{
|
|
|
|
.start = .{
|
|
|
|
.line = @intCast(i64, loc.line),
|
|
|
|
.character = @intCast(i64, loc.column),
|
|
|
|
},
|
|
|
|
.end = .{
|
|
|
|
.line = @intCast(i64, loc.line),
|
|
|
|
.character = @intCast(i64, loc.column),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-05-14 00:10:41 +01:00
|
|
|
fn publishDiagnostics(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
|
|
|
// Use an arena for our local memory allocations.
|
|
|
|
var arena = std.heap.ArenaAllocator.init(allocator);
|
|
|
|
defer arena.deinit();
|
|
|
|
|
|
|
|
var diagnostics = std.ArrayList(types.Diagnostic).init(&arena.allocator);
|
2020-04-24 23:19:03 +01:00
|
|
|
|
2020-05-23 23:21:02 +01:00
|
|
|
for (tree.errors) |*err| {
|
2020-05-09 02:02:29 +01:00
|
|
|
const loc = tree.tokenLocation(0, err.loc());
|
|
|
|
|
|
|
|
var mem_buffer: [256]u8 = undefined;
|
|
|
|
var fbs = std.io.fixedBufferStream(&mem_buffer);
|
|
|
|
try tree.renderError(err, fbs.outStream());
|
|
|
|
|
|
|
|
try diagnostics.append(.{
|
|
|
|
.range = astLocationToRange(loc),
|
|
|
|
.severity = .Error,
|
|
|
|
.code = @tagName(err.*),
|
|
|
|
.source = "zls",
|
|
|
|
.message = try std.mem.dupe(&arena.allocator, u8, fbs.getWritten()),
|
|
|
|
// .relatedInformation = undefined
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tree.errors.len == 0) {
|
2020-05-23 23:21:02 +01:00
|
|
|
for (tree.root_node.decls()) |decl| {
|
2020-05-04 03:17:19 +01:00
|
|
|
switch (decl.id) {
|
2020-05-08 18:02:46 +01:00
|
|
|
.FnProto => blk: {
|
2020-05-04 03:17:19 +01:00
|
|
|
const func = decl.cast(std.zig.ast.Node.FnProto).?;
|
2020-05-08 16:01:34 +01:00
|
|
|
const is_extern = func.extern_export_inline_token != null;
|
|
|
|
if (is_extern)
|
2020-05-08 18:02:46 +01:00
|
|
|
break :blk;
|
2020-05-08 16:01:34 +01:00
|
|
|
|
2020-05-15 20:10:53 +01:00
|
|
|
if (config.warn_style) {
|
|
|
|
if (func.name_token) |name_token| {
|
|
|
|
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-04-27 21:38:35 +01:00
|
|
|
try send(types.Notification{
|
|
|
|
.method = "textDocument/publishDiagnostics",
|
2020-05-07 13:29:53 +01:00
|
|
|
.params = .{
|
|
|
|
.PublishDiagnosticsParams = .{
|
2020-05-14 00:10:41 +01:00
|
|
|
.uri = handle.uri(),
|
2020-05-07 11:56:08 +01:00
|
|
|
.diagnostics = diagnostics.items,
|
2020-05-07 13:29:53 +01:00
|
|
|
},
|
2020-05-07 14:23:13 +01:00
|
|
|
},
|
2020-04-27 21:38:35 +01:00
|
|
|
});
|
|
|
|
}
|
2020-04-24 23:19:03 +01:00
|
|
|
|
2020-05-23 14:14:03 +01:00
|
|
|
fn containerToCompletion(
|
|
|
|
list: *std.ArrayList(types.CompletionItem),
|
|
|
|
analysis_ctx: *DocumentStore.AnalysisContext,
|
|
|
|
orig_handle: *DocumentStore.Handle,
|
|
|
|
container: *std.zig.ast.Node,
|
|
|
|
config: Config,
|
|
|
|
) !void {
|
2020-05-24 15:07:48 +01:00
|
|
|
var child_idx: usize = 0;
|
|
|
|
while (container.iterate(child_idx)) |child_node| : (child_idx += 1) {
|
2020-05-23 14:14:03 +01:00
|
|
|
// Declarations in the same file do not need to be public.
|
2020-05-24 17:00:21 +01:00
|
|
|
if (orig_handle == analysis_ctx.handle or analysis.isNodePublic(analysis_ctx.tree(), child_node)) {
|
2020-05-24 13:24:18 +01:00
|
|
|
try nodeToCompletion(list, analysis_ctx, orig_handle, child_node, config);
|
2020-05-17 15:23:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-25 23:51:50 +01:00
|
|
|
const ResolveVarDeclFnAliasRewsult = struct {
|
|
|
|
decl: *std.zig.ast.Node,
|
|
|
|
analysis_ctx: DocumentStore.AnalysisContext,
|
|
|
|
};
|
|
|
|
|
2020-05-26 15:19:16 +01:00
|
|
|
fn resolveVarDeclFnAlias(analysis_ctx: *DocumentStore.AnalysisContext, decl: *std.zig.ast.Node) !ResolveVarDeclFnAliasRewsult {
|
|
|
|
var child_analysis_context = try analysis_ctx.clone();
|
2020-05-25 23:51:50 +01:00
|
|
|
if (decl.cast(std.zig.ast.Node.VarDecl)) |var_decl| {
|
|
|
|
const child_node = block: {
|
|
|
|
if (var_decl.type_node) |type_node| {
|
|
|
|
if (std.mem.eql(u8, "type", analysis_ctx.tree().tokenSlice(type_node.firstToken()))) {
|
|
|
|
break :block var_decl.init_node orelse type_node;
|
|
|
|
}
|
|
|
|
break :block type_node;
|
|
|
|
}
|
|
|
|
break :block var_decl.init_node.?;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (analysis.resolveTypeOfNode(&child_analysis_context, child_node)) |resolved_node| {
|
|
|
|
if (resolved_node.id == .FnProto) {
|
2020-05-26 15:19:16 +01:00
|
|
|
return ResolveVarDeclFnAliasRewsult{
|
|
|
|
.decl = resolved_node,
|
|
|
|
.analysis_ctx = child_analysis_context,
|
|
|
|
};
|
2020-05-25 23:51:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-26 15:19:16 +01:00
|
|
|
return ResolveVarDeclFnAliasRewsult{
|
|
|
|
.decl = decl,
|
|
|
|
.analysis_ctx = analysis_ctx.*,
|
|
|
|
};
|
2020-05-25 23:51:50 +01:00
|
|
|
}
|
|
|
|
|
2020-05-23 14:14:03 +01:00
|
|
|
fn nodeToCompletion(
|
|
|
|
list: *std.ArrayList(types.CompletionItem),
|
|
|
|
analysis_ctx: *DocumentStore.AnalysisContext,
|
|
|
|
orig_handle: *DocumentStore.Handle,
|
|
|
|
node: *std.zig.ast.Node,
|
|
|
|
config: Config,
|
|
|
|
) error{OutOfMemory}!void {
|
2020-06-03 09:23:14 +01:00
|
|
|
const doc = if (try analysis.getDocComments(list.allocator, analysis_ctx.tree(), node)) |doc_comments|
|
2020-05-14 22:16:40 +01:00
|
|
|
types.MarkupContent{
|
|
|
|
.kind = .Markdown,
|
|
|
|
.value = doc_comments,
|
|
|
|
}
|
|
|
|
else
|
|
|
|
null;
|
2020-05-13 18:30:57 +01:00
|
|
|
|
2020-05-17 15:23:04 +01:00
|
|
|
switch (node.id) {
|
|
|
|
.ErrorSetDecl, .Root, .ContainerDecl => {
|
2020-05-23 14:14:03 +01:00
|
|
|
try containerToCompletion(list, analysis_ctx, orig_handle, node, config);
|
2020-05-17 15:23:04 +01:00
|
|
|
},
|
2020-05-13 18:30:57 +01:00
|
|
|
.FnProto => {
|
2020-05-17 15:23:04 +01:00
|
|
|
const func = node.cast(std.zig.ast.Node.FnProto).?;
|
2020-05-13 18:30:57 +01: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: {
|
|
|
|
const skip_self_param = if (func.params_len > 0) param_check: {
|
|
|
|
var child_analysis_ctx = try analysis_ctx.clone();
|
|
|
|
break :param_check switch (func.paramsConst()[0].param_type) {
|
|
|
|
.type_expr => |type_node| if (analysis_ctx.in_container == analysis.resolveTypeOfNode(&child_analysis_ctx, type_node))
|
|
|
|
true
|
|
|
|
else if (type_node.cast(std.zig.ast.Node.PrefixOp)) |prefix_op|
|
|
|
|
prefix_op.op == .PtrType and analysis_ctx.in_container == analysis.resolveTypeOfNode(&child_analysis_ctx, prefix_op.rhs)
|
|
|
|
else
|
|
|
|
false,
|
|
|
|
else => false,
|
|
|
|
};
|
|
|
|
} else
|
|
|
|
false;
|
|
|
|
|
|
|
|
break :blk try analysis.getFunctionSnippet(list.allocator, analysis_ctx.tree(), func, skip_self_param);
|
|
|
|
} else
|
2020-05-13 18:30:57 +01:00
|
|
|
null;
|
|
|
|
|
2020-05-24 17:00:21 +01:00
|
|
|
const is_type_function = analysis.isTypeFunction(analysis_ctx.tree(), func);
|
2020-05-17 15:23:04 +01:00
|
|
|
|
|
|
|
try list.append(.{
|
2020-05-24 17:00:21 +01:00
|
|
|
.label = analysis_ctx.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-05-24 17:00:21 +01:00
|
|
|
.detail = analysis.getFunctionSignature(analysis_ctx.tree(), func),
|
2020-05-13 18:30:57 +01:00
|
|
|
.insertText = insert_text,
|
2020-06-01 11:29:06 +01:00
|
|
|
.insertTextFormat = if (use_snippets) .Snippet else .PlainText,
|
2020-05-17 15:23:04 +01:00
|
|
|
});
|
2020-05-13 18:30:57 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
.VarDecl => {
|
2020-05-17 15:23:04 +01:00
|
|
|
const var_decl = node.cast(std.zig.ast.Node.VarDecl).?;
|
2020-05-24 17:00:21 +01:00
|
|
|
const is_const = analysis_ctx.tree().token_ids[var_decl.mut_token] == .Keyword_const;
|
2020-05-19 05:12:05 +01:00
|
|
|
|
2020-05-26 15:19:16 +01:00
|
|
|
var result = try resolveVarDeclFnAlias(analysis_ctx, node);
|
2020-05-25 23:51:50 +01:00
|
|
|
if (result.decl != node) {
|
|
|
|
return try nodeToCompletion(list, &result.analysis_ctx, orig_handle, result.decl, config);
|
2020-05-19 05:12:05 +01:00
|
|
|
}
|
2020-05-25 23:51:50 +01:00
|
|
|
|
2020-05-17 15:23:04 +01:00
|
|
|
try list.append(.{
|
2020-05-24 17:00:21 +01:00
|
|
|
.label = analysis_ctx.tree().tokenSlice(var_decl.name_token),
|
2020-05-17 15:23:04 +01:00
|
|
|
.kind = if (is_const) .Constant else .Variable,
|
2020-05-13 18:30:57 +01:00
|
|
|
.documentation = doc,
|
2020-05-24 17:00:21 +01:00
|
|
|
.detail = analysis.getVariableSignature(analysis_ctx.tree(), var_decl),
|
2020-05-17 15:23:04 +01:00
|
|
|
});
|
2020-05-13 18:30:57 +01:00
|
|
|
},
|
2020-05-17 15:23:04 +01:00
|
|
|
.PrefixOp => {
|
2020-05-27 16:49:11 +01:00
|
|
|
const prefix_op = node.cast(std.zig.ast.Node.PrefixOp).?;
|
|
|
|
switch (prefix_op.op) {
|
|
|
|
.ArrayType, .SliceType => {},
|
|
|
|
.PtrType => {
|
|
|
|
if (prefix_op.rhs.cast(std.zig.ast.Node.PrefixOp)) |child_pop| {
|
|
|
|
switch (child_pop.op) {
|
|
|
|
.ArrayType => {},
|
|
|
|
else => return,
|
|
|
|
}
|
|
|
|
} else return;
|
|
|
|
},
|
|
|
|
else => return,
|
|
|
|
}
|
|
|
|
|
2020-05-17 15:23:04 +01:00
|
|
|
try list.append(.{
|
|
|
|
.label = "len",
|
|
|
|
.kind = .Field,
|
|
|
|
});
|
|
|
|
try list.append(.{
|
|
|
|
.label = "ptr",
|
|
|
|
.kind = .Field,
|
|
|
|
});
|
2020-05-13 18:30:57 +01:00
|
|
|
},
|
2020-05-17 15:23:04 +01:00
|
|
|
.StringLiteral => {
|
|
|
|
try list.append(.{
|
|
|
|
.label = "len",
|
|
|
|
.kind = .Field,
|
|
|
|
});
|
|
|
|
},
|
2020-05-24 17:00:21 +01:00
|
|
|
else => if (analysis.nodeToString(analysis_ctx.tree(), node)) |string| {
|
2020-05-17 15:23:04 +01:00
|
|
|
try list.append(.{
|
2020-05-13 18:30:57 +01:00
|
|
|
.label = string,
|
|
|
|
.kind = .Field,
|
|
|
|
.documentation = doc,
|
2020-06-07 14:49:21 +01:00
|
|
|
.detail = analysis_ctx.tree().getNodeSource(node)
|
2020-05-17 15:23:04 +01:00
|
|
|
});
|
2020-05-14 22:16:40 +01:00
|
|
|
},
|
2020-05-13 18:30:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-18 21:19:23 +01:00
|
|
|
fn identifierFromPosition(pos_index: usize, handle: DocumentStore.Handle) []const u8 {
|
2020-05-22 23:03:41 +01:00
|
|
|
const text = handle.document.text;
|
|
|
|
|
|
|
|
if (pos_index + 1 >= text.len) return &[0]u8{};
|
2020-05-18 21:19:23 +01:00
|
|
|
var start_idx = pos_index;
|
2020-05-21 12:36:14 +01:00
|
|
|
|
2020-05-18 21:19:23 +01:00
|
|
|
while (start_idx > 0 and
|
2020-05-22 23:03:41 +01:00
|
|
|
(std.ascii.isAlNum(text[start_idx]) or text[start_idx] == '_')) : (start_idx -= 1)
|
2020-05-18 21:19:23 +01:00
|
|
|
{}
|
|
|
|
|
|
|
|
var end_idx = pos_index;
|
2020-05-23 00:33:42 +01:00
|
|
|
while (end_idx < handle.document.text.len and
|
2020-05-22 23:03:41 +01:00
|
|
|
(std.ascii.isAlNum(text[end_idx]) or text[end_idx] == '_')) : (end_idx += 1)
|
2020-05-18 21:19:23 +01:00
|
|
|
{}
|
|
|
|
|
2020-05-23 00:33:42 +01:00
|
|
|
if (end_idx <= start_idx) return &[0]u8{};
|
2020-05-22 23:58:47 +01:00
|
|
|
return text[start_idx + 1 .. end_idx];
|
2020-05-18 21:19:23 +01:00
|
|
|
}
|
|
|
|
|
2020-06-06 13:40:33 +01:00
|
|
|
fn gotoDefinitionSymbol(id: types.RequestId, analysis_ctx: *DocumentStore.AnalysisContext, decl: *std.zig.ast.Node) !void {
|
2020-05-26 15:19:16 +01:00
|
|
|
const result = try resolveVarDeclFnAlias(analysis_ctx, decl);
|
2020-05-25 23:51:50 +01:00
|
|
|
|
2020-05-31 16:02:33 +01:00
|
|
|
const name_token = analysis.getDeclNameToken(result.analysis_ctx.tree(), result.decl) orelse
|
|
|
|
return try respondGeneric(id, null_result_response);
|
2020-05-25 23:51:50 +01:00
|
|
|
|
|
|
|
try send(types.Response{
|
2020-06-06 13:40:33 +01:00
|
|
|
.id = id,
|
2020-05-25 23:51:50 +01:00
|
|
|
.result = .{
|
|
|
|
.Location = .{
|
|
|
|
.uri = result.analysis_ctx.handle.document.uri,
|
|
|
|
.range = astLocationToRange(result.analysis_ctx.tree().tokenLocation(0, name_token)),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-06 13:40:33 +01:00
|
|
|
fn hoverSymbol(id: types.RequestId, analysis_ctx: *DocumentStore.AnalysisContext, decl: *std.zig.ast.Node) !void {
|
2020-05-26 15:19:16 +01:00
|
|
|
const result = try resolveVarDeclFnAlias(analysis_ctx, decl);
|
2020-05-25 23:51:50 +01:00
|
|
|
|
2020-06-03 09:23:14 +01:00
|
|
|
const doc_str = if (try analysis.getDocComments(&analysis_ctx.arena.allocator, result.analysis_ctx.tree(), result.decl)) |str|
|
|
|
|
str
|
|
|
|
else
|
|
|
|
"";
|
|
|
|
|
|
|
|
const signature_str = switch (result.decl.id) {
|
2020-05-25 23:51:50 +01:00
|
|
|
.VarDecl => blk: {
|
|
|
|
const var_decl = result.decl.cast(std.zig.ast.Node.VarDecl).?;
|
|
|
|
break :blk analysis.getVariableSignature(result.analysis_ctx.tree(), var_decl);
|
|
|
|
},
|
|
|
|
.FnProto => blk: {
|
|
|
|
const fn_decl = result.decl.cast(std.zig.ast.Node.FnProto).?;
|
|
|
|
break :blk analysis.getFunctionSignature(result.analysis_ctx.tree(), fn_decl);
|
|
|
|
},
|
|
|
|
else => analysis.nodeToString(result.analysis_ctx.tree(), result.decl) orelse return try respondGeneric(id, null_result_response),
|
|
|
|
};
|
|
|
|
|
2020-06-04 22:19:00 +01:00
|
|
|
const md_string = try std.fmt.allocPrint(&analysis_ctx.arena.allocator, "```zig\n{}\n```\n{}", .{ signature_str, doc_str });
|
2020-05-25 23:51:50 +01:00
|
|
|
try send(types.Response{
|
2020-06-06 13:40:33 +01:00
|
|
|
.id = id,
|
2020-05-25 23:51:50 +01:00
|
|
|
.result = .{
|
|
|
|
.Hover = .{
|
2020-06-03 09:23:14 +01:00
|
|
|
.contents = .{ .value = md_string },
|
2020-05-25 23:51:50 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-25 22:37:18 +01:00
|
|
|
fn getSymbolGlobal(arena: *std.heap.ArenaAllocator, pos_index: usize, handle: DocumentStore.Handle) !?*std.zig.ast.Node {
|
2020-05-18 21:19:23 +01:00
|
|
|
const name = identifierFromPosition(pos_index, handle);
|
2020-05-25 22:37:18 +01:00
|
|
|
if (name.len == 0) return null;
|
|
|
|
|
|
|
|
var decl_nodes = std.ArrayList(*std.zig.ast.Node).init(&arena.allocator);
|
|
|
|
_ = try analysis.declsFromIndex(arena, &decl_nodes, handle.tree, pos_index);
|
|
|
|
|
|
|
|
return analysis.getChildOfSlice(handle.tree, decl_nodes.items, name);
|
|
|
|
}
|
2020-05-21 12:36:14 +01:00
|
|
|
|
2020-06-06 13:40:33 +01:00
|
|
|
fn gotoDefinitionGlobal(id: types.RequestId, pos_index: usize, handle: *DocumentStore.Handle, config: Config) !void {
|
2020-05-18 21:19:23 +01:00
|
|
|
var arena = std.heap.ArenaAllocator.init(allocator);
|
|
|
|
defer arena.deinit();
|
|
|
|
|
2020-05-25 23:51:50 +01:00
|
|
|
const decl = (try getSymbolGlobal(&arena, pos_index, handle.*)) orelse return try respondGeneric(id, null_result_response);
|
|
|
|
var analysis_ctx = try document_store.analysisContext(handle, &arena, pos_index, config.zig_lib_path);
|
|
|
|
return try gotoDefinitionSymbol(id, &analysis_ctx, decl);
|
|
|
|
}
|
2020-05-18 21:19:23 +01:00
|
|
|
|
2020-06-06 13:40:33 +01:00
|
|
|
fn hoverDefinitionGlobal(id: types.RequestId, pos_index: usize, handle: *DocumentStore.Handle, config: Config) !void {
|
2020-05-25 23:51:50 +01:00
|
|
|
var arena = std.heap.ArenaAllocator.init(allocator);
|
|
|
|
defer arena.deinit();
|
|
|
|
|
|
|
|
const decl = (try getSymbolGlobal(&arena, pos_index, handle.*)) orelse return try respondGeneric(id, null_result_response);
|
|
|
|
var analysis_ctx = try document_store.analysisContext(handle, &arena, pos_index, config.zig_lib_path);
|
|
|
|
return try hoverSymbol(id, &analysis_ctx, decl);
|
2020-05-18 21:19:23 +01:00
|
|
|
}
|
|
|
|
|
2020-05-25 22:37:18 +01:00
|
|
|
fn getSymbolFieldAccess(
|
|
|
|
analysis_ctx: *DocumentStore.AnalysisContext,
|
|
|
|
position: types.Position,
|
2020-05-26 23:45:18 +01:00
|
|
|
range: analysis.SourceRange,
|
2020-05-25 22:37:18 +01:00
|
|
|
config: Config,
|
|
|
|
) !?*std.zig.ast.Node {
|
|
|
|
const pos_index = try analysis_ctx.handle.document.positionToIndex(position);
|
|
|
|
var name = identifierFromPosition(pos_index, analysis_ctx.handle.*);
|
|
|
|
if (name.len == 0) return null;
|
|
|
|
|
|
|
|
const line = try analysis_ctx.handle.document.getLine(@intCast(usize, position.line));
|
2020-05-26 23:45:18 +01:00
|
|
|
var tokenizer = std.zig.Tokenizer.init(line[range.start..range.end]);
|
2020-05-25 22:37:18 +01:00
|
|
|
|
|
|
|
name = try std.mem.dupe(&analysis_ctx.arena.allocator, u8, name);
|
2020-05-26 23:45:18 +01:00
|
|
|
if (analysis.getFieldAccessTypeNode(analysis_ctx, &tokenizer)) |container| {
|
2020-05-25 22:37:18 +01:00
|
|
|
return analysis.getChild(analysis_ctx.tree(), container, name);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-05-19 20:09:00 +01:00
|
|
|
fn gotoDefinitionFieldAccess(
|
2020-06-06 13:40:33 +01:00
|
|
|
id: types.RequestId,
|
2020-05-19 20:09:00 +01:00
|
|
|
handle: *DocumentStore.Handle,
|
|
|
|
position: types.Position,
|
2020-05-26 23:45:18 +01:00
|
|
|
range: analysis.SourceRange,
|
2020-05-19 20:09:00 +01:00
|
|
|
config: Config,
|
|
|
|
) !void {
|
2020-05-18 21:19:23 +01:00
|
|
|
var arena = std.heap.ArenaAllocator.init(allocator);
|
|
|
|
defer arena.deinit();
|
|
|
|
|
2020-05-23 21:22:50 +01:00
|
|
|
var analysis_ctx = try document_store.analysisContext(handle, &arena, try handle.document.positionToIndex(position), config.zig_lib_path);
|
2020-05-26 23:45:18 +01:00
|
|
|
const decl = (try getSymbolFieldAccess(&analysis_ctx, position, range, config)) orelse return try respondGeneric(id, null_result_response);
|
2020-05-25 23:51:50 +01:00
|
|
|
return try gotoDefinitionSymbol(id, &analysis_ctx, decl);
|
|
|
|
}
|
2020-05-18 21:19:23 +01:00
|
|
|
|
2020-05-25 23:51:50 +01:00
|
|
|
fn hoverDefinitionFieldAccess(
|
2020-06-06 13:40:33 +01:00
|
|
|
id: types.RequestId,
|
2020-05-25 23:51:50 +01:00
|
|
|
handle: *DocumentStore.Handle,
|
|
|
|
position: types.Position,
|
2020-05-26 23:45:18 +01:00
|
|
|
range: analysis.SourceRange,
|
2020-05-25 23:51:50 +01:00
|
|
|
config: Config,
|
|
|
|
) !void {
|
|
|
|
var arena = std.heap.ArenaAllocator.init(allocator);
|
|
|
|
defer arena.deinit();
|
|
|
|
|
|
|
|
var analysis_ctx = try document_store.analysisContext(handle, &arena, try handle.document.positionToIndex(position), config.zig_lib_path);
|
2020-05-26 23:45:18 +01:00
|
|
|
const decl = (try getSymbolFieldAccess(&analysis_ctx, position, range, config)) orelse return try respondGeneric(id, null_result_response);
|
2020-05-25 23:51:50 +01:00
|
|
|
return try hoverSymbol(id, &analysis_ctx, decl);
|
2020-05-18 21:19:23 +01:00
|
|
|
}
|
|
|
|
|
2020-06-06 13:40:33 +01:00
|
|
|
fn gotoDefinitionString(id: types.RequestId, pos_index: usize, handle: *DocumentStore.Handle, config: Config) !void {
|
2020-05-24 17:00:21 +01:00
|
|
|
const tree = handle.tree;
|
2020-04-27 21:38:35 +01:00
|
|
|
|
2020-05-22 16:51:57 +01:00
|
|
|
var arena = std.heap.ArenaAllocator.init(allocator);
|
|
|
|
defer arena.deinit();
|
|
|
|
|
|
|
|
const import_str = analysis.getImportStr(tree, pos_index) orelse return try respondGeneric(id, null_result_response);
|
2020-05-23 14:14:03 +01:00
|
|
|
const uri = (try document_store.uriFromImportStr(
|
2020-05-22 16:51:57 +01:00
|
|
|
&arena.allocator,
|
|
|
|
handle.*,
|
|
|
|
import_str,
|
|
|
|
try DocumentStore.stdUriFromLibPath(&arena.allocator, config.zig_lib_path),
|
|
|
|
)) orelse return try respondGeneric(id, null_result_response);
|
|
|
|
|
|
|
|
try send(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-06 13:40:33 +01:00
|
|
|
fn completeGlobal(id: types.RequestId, pos_index: usize, handle: *DocumentStore.Handle, config: Config) !void {
|
2020-05-07 12:10:58 +01:00
|
|
|
// We use a local arena allocator to deallocate all temporary data without iterating
|
|
|
|
var arena = std.heap.ArenaAllocator.init(allocator);
|
|
|
|
var completions = std.ArrayList(types.CompletionItem).init(&arena.allocator);
|
|
|
|
// Deallocate all temporary data.
|
|
|
|
defer arena.deinit();
|
2020-05-07 11:56:08 +01:00
|
|
|
|
2020-05-23 21:22:50 +01:00
|
|
|
var analysis_ctx = try document_store.analysisContext(handle, &arena, pos_index, config.zig_lib_path);
|
|
|
|
for (analysis_ctx.scope_nodes) |decl_ptr| {
|
2020-04-27 21:38:35 +01:00
|
|
|
var decl = decl_ptr.*;
|
2020-05-24 13:24:18 +01:00
|
|
|
try nodeToCompletion(&completions, &analysis_ctx, handle, decl_ptr, config);
|
2020-04-27 21:38:35 +01:00
|
|
|
}
|
2020-04-24 23:19:03 +01:00
|
|
|
|
2020-04-27 21:38:35 +01:00
|
|
|
try send(types.Response{
|
2020-06-06 13:40:33 +01:00
|
|
|
.id = id,
|
2020-05-07 13:29:53 +01:00
|
|
|
.result = .{
|
|
|
|
.CompletionList = .{
|
2020-04-27 21:38:35 +01:00
|
|
|
.isIncomplete = false,
|
2020-05-07 11:56:08 +01:00
|
|
|
.items = completions.items,
|
2020-05-07 13:29:53 +01:00
|
|
|
},
|
|
|
|
},
|
2020-04-27 21:38:35 +01:00
|
|
|
});
|
2020-04-24 23:19:03 +01:00
|
|
|
}
|
|
|
|
|
2020-06-06 13:40:33 +01:00
|
|
|
fn completeFieldAccess(id: types.RequestId, handle: *DocumentStore.Handle, position: types.Position, range: analysis.SourceRange, config: Config) !void {
|
2020-05-14 10:23:20 +01:00
|
|
|
var arena = std.heap.ArenaAllocator.init(allocator);
|
|
|
|
defer arena.deinit();
|
2020-05-11 13:28:08 +01:00
|
|
|
|
2020-05-23 21:22:50 +01:00
|
|
|
var analysis_ctx = try document_store.analysisContext(handle, &arena, try handle.document.positionToIndex(position), config.zig_lib_path);
|
2020-05-14 00:10:41 +01:00
|
|
|
var completions = std.ArrayList(types.CompletionItem).init(&arena.allocator);
|
|
|
|
|
2020-05-17 15:39:04 +01:00
|
|
|
const line = try handle.document.getLine(@intCast(usize, position.line));
|
2020-05-26 23:45:18 +01:00
|
|
|
var tokenizer = std.zig.Tokenizer.init(line[range.start..range.end]);
|
2020-05-14 00:10:41 +01:00
|
|
|
|
2020-05-26 23:45:18 +01:00
|
|
|
if (analysis.getFieldAccessTypeNode(&analysis_ctx, &tokenizer)) |node| {
|
2020-05-24 13:24:18 +01:00
|
|
|
try nodeToCompletion(&completions, &analysis_ctx, handle, node, config);
|
2020-05-13 15:10:20 +01:00
|
|
|
}
|
2020-05-14 00:10:41 +01:00
|
|
|
try send(types.Response{
|
2020-06-06 13:40:33 +01:00
|
|
|
.id = id,
|
2020-05-14 00:10:41 +01:00
|
|
|
.result = .{
|
|
|
|
.CompletionList = .{
|
|
|
|
.isIncomplete = false,
|
|
|
|
.items = completions.items,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2020-05-11 13:28:08 +01:00
|
|
|
}
|
2020-05-07 12:36:40 +01:00
|
|
|
|
2020-06-06 13:40:33 +01:00
|
|
|
fn documentSymbol(id: types.RequestId, handle: *DocumentStore.Handle) !void {
|
2020-05-28 01:39:36 +01:00
|
|
|
var arena = std.heap.ArenaAllocator.init(allocator);
|
|
|
|
defer arena.deinit();
|
|
|
|
|
|
|
|
try send(types.Response{
|
2020-06-06 13:40:33 +01:00
|
|
|
.id = id,
|
2020-05-30 21:36:18 +01:00
|
|
|
.result = .{ .DocumentSymbols = try analysis.getDocumentSymbols(&arena.allocator, handle.tree) },
|
2020-05-28 01:39:36 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-07 12:36:40 +01:00
|
|
|
// Compute builtin completions at comptime.
|
|
|
|
const builtin_completions = block: {
|
|
|
|
@setEvalBranchQuota(3_500);
|
2020-05-09 14:43:51 +01:00
|
|
|
const CompletionList = [data.builtins.len]types.CompletionItem;
|
|
|
|
var with_snippets: CompletionList = undefined;
|
|
|
|
var without_snippets: CompletionList = undefined;
|
2020-05-07 12:36:40 +01:00
|
|
|
|
|
|
|
for (data.builtins) |builtin, i| {
|
2020-05-09 14:43:51 +01:00
|
|
|
const cutoff = std.mem.indexOf(u8, builtin, "(") orelse builtin.len;
|
|
|
|
|
|
|
|
const base_completion = types.CompletionItem{
|
2020-05-08 00:53:00 +01:00
|
|
|
.label = builtin[0..cutoff],
|
|
|
|
.kind = .Function,
|
|
|
|
|
|
|
|
.filterText = builtin[1..cutoff],
|
|
|
|
.detail = data.builtin_details[i],
|
|
|
|
.documentation = .{
|
|
|
|
.kind = .Markdown,
|
|
|
|
.value = data.builtin_docs[i],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-05-09 14:43:51 +01:00
|
|
|
with_snippets[i] = base_completion;
|
|
|
|
with_snippets[i].insertText = builtin[1..];
|
|
|
|
with_snippets[i].insertTextFormat = .Snippet;
|
|
|
|
|
|
|
|
without_snippets[i] = base_completion;
|
|
|
|
without_snippets[i].insertText = builtin[1..cutoff];
|
2020-05-07 12:36:40 +01:00
|
|
|
}
|
|
|
|
|
2020-05-14 22:16:40 +01:00
|
|
|
break :block [2]CompletionList{
|
|
|
|
without_snippets, with_snippets,
|
2020-05-09 14:43:51 +01:00
|
|
|
};
|
2020-05-07 12:36:40 +01:00
|
|
|
};
|
|
|
|
|
2020-05-19 20:09:00 +01:00
|
|
|
fn loadConfig(folder_path: []const u8) ?Config {
|
|
|
|
var folder = std.fs.cwd().openDir(folder_path, .{}) catch return null;
|
|
|
|
defer folder.close();
|
|
|
|
|
|
|
|
const conf_file = folder.openFile("zls.json", .{}) catch return null;
|
|
|
|
defer conf_file.close();
|
|
|
|
|
|
|
|
// Max 1MB
|
|
|
|
const file_buf = conf_file.inStream().readAllAlloc(allocator, 0x1000000) catch return null;
|
|
|
|
defer allocator.free(file_buf);
|
|
|
|
|
|
|
|
// TODO: Better errors? Doesn't seem like std.json can provide us positions or context.
|
|
|
|
var config = std.json.parse(Config, &std.json.TokenStream.init(file_buf), std.json.ParseOptions{ .allocator = allocator }) catch |err| {
|
|
|
|
std.debug.warn("Error while parsing configuration file: {}\nUsing default config.\n", .{err});
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (config.zig_lib_path) |zig_lib_path| {
|
|
|
|
if (!std.fs.path.isAbsolute(zig_lib_path)) {
|
|
|
|
std.debug.warn("zig library path is not absolute, defaulting to null.\n", .{});
|
|
|
|
allocator.free(zig_lib_path);
|
|
|
|
config.zig_lib_path = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn loadWorkspaceConfigs() !void {
|
|
|
|
var folder_config_it = workspace_folder_configs.iterator();
|
|
|
|
while (folder_config_it.next()) |entry| {
|
|
|
|
if (entry.value) |_| continue;
|
|
|
|
|
|
|
|
const folder_path = try URI.parse(allocator, entry.key);
|
|
|
|
defer allocator.free(folder_path);
|
|
|
|
|
|
|
|
entry.value = loadConfig(folder_path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn configFromUriOr(uri: []const u8, default: Config) Config {
|
|
|
|
var folder_config_it = workspace_folder_configs.iterator();
|
|
|
|
while (folder_config_it.next()) |entry| {
|
|
|
|
if (std.mem.startsWith(u8, uri, entry.key)) {
|
|
|
|
return entry.value orelse default;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return default;
|
|
|
|
}
|
|
|
|
|
2020-05-09 14:43:51 +01:00
|
|
|
fn processJsonRpc(parser: *std.json.Parser, json: []const u8, config: Config) !void {
|
2020-04-24 23:19:03 +01:00
|
|
|
var tree = try parser.parse(json);
|
|
|
|
defer tree.deinit();
|
|
|
|
|
|
|
|
const root = tree.root;
|
|
|
|
|
2020-06-06 13:40:33 +01:00
|
|
|
const id = if (root.Object.getValue("id")) |id| switch (id) {
|
|
|
|
.Integer => |int| types.RequestId{ .Integer = int },
|
|
|
|
.String => |str| types.RequestId{ .String = str },
|
|
|
|
else => types.RequestId{ .Integer = 0 },
|
|
|
|
} else types.RequestId{ .Integer = 0 };
|
|
|
|
|
|
|
|
if (id == .Integer and id.Integer == 1337 and (root.Object.getValue("method") == null or std.mem.eql(u8, root.Object.getValue("method").?.String, ""))) {
|
2020-05-19 21:24:24 +01:00
|
|
|
if (root.Object.getValue("result")) |result_obj| {
|
|
|
|
if (result_obj == .Array) {
|
|
|
|
const result = result_obj.Array;
|
2020-05-19 20:09:00 +01:00
|
|
|
|
2020-05-19 21:24:24 +01:00
|
|
|
for (result.items) |workspace_folder| {
|
|
|
|
const duped_uri = try std.mem.dupe(allocator, u8, workspace_folder.Object.getValue("uri").?.String);
|
|
|
|
try workspace_folder_configs.putNoClobber(duped_uri, null);
|
|
|
|
}
|
|
|
|
}
|
2020-05-19 20:09:00 +01:00
|
|
|
}
|
2020-05-14 22:16:40 +01:00
|
|
|
|
2020-05-19 20:09:00 +01:00
|
|
|
try loadWorkspaceConfigs();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std.debug.assert(root.Object.getValue("method") != null);
|
|
|
|
const method = root.Object.getValue("method").?.String;
|
2020-04-24 23:19:03 +01:00
|
|
|
const params = root.Object.getValue("params").?.Object;
|
2020-05-14 22:16:40 +01:00
|
|
|
|
2020-05-24 13:39:40 +01:00
|
|
|
const start_time = std.time.milliTimestamp();
|
|
|
|
defer {
|
|
|
|
const end_time = std.time.milliTimestamp();
|
2020-05-25 17:33:08 +01:00
|
|
|
std.debug.warn("Took {}ms to process method {}\n", .{ end_time - start_time, method });
|
2020-05-24 13:39:40 +01:00
|
|
|
}
|
|
|
|
|
2020-04-24 23:19:03 +01:00
|
|
|
// Core
|
|
|
|
if (std.mem.eql(u8, method, "initialize")) {
|
2020-06-01 11:29:06 +01:00
|
|
|
const client_capabs = params.getValue("capabilities").?.Object;
|
|
|
|
if (client_capabs.getValue("textDocument")) |text_doc_capabs| {
|
|
|
|
if (text_doc_capabs.Object.getValue("completion")) |completion_capabs| {
|
|
|
|
if (completion_capabs.Object.getValue("completionItem")) |item_capabs| {
|
|
|
|
const maybe_support_snippet = item_capabs.Object.getValue("snippetSupport");
|
|
|
|
client_capabilities.supports_snippets = maybe_support_snippet != null and maybe_support_snippet.?.Bool;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-24 23:19:03 +01:00
|
|
|
try respondGeneric(id, initialize_response);
|
|
|
|
} else if (std.mem.eql(u8, method, "initialized")) {
|
2020-05-19 20:09:00 +01:00
|
|
|
// Send the workspaceFolders request
|
|
|
|
try send(types.Request{
|
|
|
|
.id = .{ .Integer = 1337 },
|
|
|
|
.method = "workspace/workspaceFolders",
|
|
|
|
.params = {},
|
|
|
|
});
|
2020-04-24 23:19:03 +01:00
|
|
|
} else if (std.mem.eql(u8, method, "$/cancelRequest")) {
|
|
|
|
// noop
|
|
|
|
}
|
2020-05-19 20:09:00 +01:00
|
|
|
// Workspace folder changes
|
|
|
|
else if (std.mem.eql(u8, method, "workspace/didChangeWorkspaceFolders")) {
|
|
|
|
const event = params.getValue("event").?.Object;
|
|
|
|
const added = event.getValue("added").?.Array;
|
|
|
|
const removed = event.getValue("removed").?.Array;
|
|
|
|
|
|
|
|
for (removed.items) |rem| {
|
|
|
|
const uri = rem.Object.getValue("uri").?.String;
|
|
|
|
if (workspace_folder_configs.remove(uri)) |entry| {
|
|
|
|
allocator.free(entry.key);
|
|
|
|
if (entry.value) |c| {
|
|
|
|
std.json.parseFree(Config, c, std.json.ParseOptions{ .allocator = allocator });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (added.items) |add| {
|
|
|
|
const duped_uri = try std.mem.dupe(allocator, u8, add.Object.getValue("uri").?.String);
|
|
|
|
if (try workspace_folder_configs.put(duped_uri, null)) |old| {
|
|
|
|
allocator.free(old.key);
|
|
|
|
if (old.value) |c| {
|
|
|
|
std.json.parseFree(Config, c, std.json.ParseOptions{ .allocator = allocator });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try loadWorkspaceConfigs();
|
|
|
|
}
|
2020-04-24 23:19:03 +01:00
|
|
|
// File changes
|
|
|
|
else if (std.mem.eql(u8, method, "textDocument/didOpen")) {
|
|
|
|
const document = params.getValue("textDocument").?.Object;
|
|
|
|
const uri = document.getValue("uri").?.String;
|
|
|
|
const text = document.getValue("text").?.String;
|
|
|
|
|
2020-05-14 00:10:41 +01:00
|
|
|
const handle = try document_store.openDocument(uri, text);
|
2020-05-19 20:09:00 +01:00
|
|
|
try publishDiagnostics(handle.*, configFromUriOr(uri, config));
|
2020-04-24 23:19:03 +01:00
|
|
|
} else if (std.mem.eql(u8, method, "textDocument/didChange")) {
|
2020-04-27 21:38:35 +01:00
|
|
|
const text_document = params.getValue("textDocument").?.Object;
|
|
|
|
const uri = text_document.getValue("uri").?.String;
|
|
|
|
const content_changes = params.getValue("contentChanges").?.Array;
|
|
|
|
|
2020-05-14 00:10:41 +01:00
|
|
|
const handle = document_store.getHandle(uri) orelse {
|
2020-05-18 16:36:47 +01:00
|
|
|
std.debug.warn("Trying to change non existent document {}", .{uri});
|
2020-05-14 00:10:41 +01:00
|
|
|
return;
|
|
|
|
};
|
2020-04-24 23:19:03 +01:00
|
|
|
|
2020-05-19 20:09:00 +01:00
|
|
|
const local_config = configFromUriOr(uri, config);
|
|
|
|
try document_store.applyChanges(handle, content_changes, local_config.zig_lib_path);
|
|
|
|
try publishDiagnostics(handle.*, local_config);
|
2020-06-03 09:32:05 +01:00
|
|
|
} else if (std.mem.eql(u8, method, "textDocument/didSave")) {
|
|
|
|
const text_document = params.getValue("textDocument").?.Object;
|
|
|
|
const uri = text_document.getValue("uri").?.String;
|
|
|
|
const handle = document_store.getHandle(uri) orelse {
|
|
|
|
std.debug.warn("Trying to save non existent document {}", .{uri});
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
try document_store.applySave(handle);
|
|
|
|
} else if (std.mem.eql(u8, method, "textDocument/willSave")) {
|
2020-04-24 23:19:03 +01:00
|
|
|
// noop
|
|
|
|
} else if (std.mem.eql(u8, method, "textDocument/didClose")) {
|
2020-04-27 21:38:35 +01:00
|
|
|
const document = params.getValue("textDocument").?.Object;
|
|
|
|
const uri = document.getValue("uri").?.String;
|
|
|
|
|
2020-05-14 00:10:41 +01:00
|
|
|
document_store.closeDocument(uri);
|
2020-04-24 23:19:03 +01:00
|
|
|
}
|
2020-06-02 14:48:23 +01:00
|
|
|
// Semantic highlighting
|
|
|
|
else if (std.mem.eql(u8, method, "textDocument/semanticTokens")) {
|
|
|
|
// @TODO Implement this (we dont get here from vscode atm even when we get the client capab.)
|
|
|
|
return try respondGeneric(id, empty_array_response);
|
|
|
|
}
|
2020-04-24 23:19:03 +01:00
|
|
|
// Autocomplete / Signatures
|
|
|
|
else if (std.mem.eql(u8, method, "textDocument/completion")) {
|
2020-04-27 21:38:35 +01:00
|
|
|
const text_document = params.getValue("textDocument").?.Object;
|
|
|
|
const uri = text_document.getValue("uri").?.String;
|
|
|
|
const position = params.getValue("position").?.Object;
|
|
|
|
|
2020-05-14 00:10:41 +01:00
|
|
|
const handle = document_store.getHandle(uri) orelse {
|
2020-05-18 16:36:47 +01:00
|
|
|
std.debug.warn("Trying to complete in non existent document {}", .{uri});
|
2020-05-18 21:19:23 +01:00
|
|
|
return try respondGeneric(id, no_completions_response);
|
2020-05-14 00:10:41 +01:00
|
|
|
};
|
|
|
|
|
2020-04-27 21:38:35 +01:00
|
|
|
const pos = types.Position{
|
|
|
|
.line = position.getValue("line").?.Integer,
|
|
|
|
.character = position.getValue("character").?.Integer - 1,
|
|
|
|
};
|
|
|
|
if (pos.character >= 0) {
|
2020-05-14 00:10:41 +01:00
|
|
|
const pos_index = try handle.document.positionToIndex(pos);
|
2020-05-26 23:45:18 +01:00
|
|
|
const pos_context = try analysis.documentPositionContext(allocator, handle.document, pos);
|
2020-05-08 04:05:17 +01:00
|
|
|
|
2020-05-19 20:09:00 +01:00
|
|
|
const this_config = configFromUriOr(uri, config);
|
2020-06-01 11:29:06 +01:00
|
|
|
const use_snippets = this_config.enable_snippets and client_capabilities.supports_snippets;
|
2020-05-14 22:16:40 +01:00
|
|
|
switch (pos_context) {
|
|
|
|
.builtin => try send(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-06-01 11:29:06 +01:00
|
|
|
.items = builtin_completions[@boolToInt(use_snippets)][0..],
|
2020-05-07 13:29:53 +01:00
|
|
|
},
|
|
|
|
},
|
2020-05-14 22:16:40 +01:00
|
|
|
}),
|
2020-05-19 20:09:00 +01:00
|
|
|
.var_access, .empty => try completeGlobal(id, pos_index, handle, this_config),
|
2020-05-26 23:45:18 +01:00
|
|
|
.field_access => |range| try completeFieldAccess(id, handle, pos, range, this_config),
|
2020-05-30 21:36:18 +01:00
|
|
|
.global_error_set => try send(types.Response{
|
2020-06-06 13:40:33 +01:00
|
|
|
.id = id,
|
2020-05-16 17:04:07 +01:00
|
|
|
.result = .{
|
|
|
|
.CompletionList = .{
|
|
|
|
.isIncomplete = false,
|
|
|
|
.items = document_store.error_completions.completions.items,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
2020-05-30 21:36:18 +01:00
|
|
|
.enum_literal => try send(types.Response{
|
2020-06-06 13:40:33 +01:00
|
|
|
.id = id,
|
2020-05-28 16:18:48 +01:00
|
|
|
.result = .{
|
|
|
|
.CompletionList = .{
|
|
|
|
.isIncomplete = false,
|
|
|
|
.items = document_store.enum_completions.completions.items,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
2020-05-14 22:16:40 +01:00
|
|
|
else => try respondGeneric(id, no_completions_response),
|
2020-04-27 21:38:35 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
try respondGeneric(id, no_completions_response);
|
|
|
|
}
|
2020-04-24 23:19:03 +01:00
|
|
|
} else if (std.mem.eql(u8, method, "textDocument/signatureHelp")) {
|
2020-06-01 10:28:33 +01:00
|
|
|
// TODO: Implement this
|
2020-05-14 22:16:40 +01:00
|
|
|
try respondGeneric(id,
|
|
|
|
\\,"result":{"signatures":[]}}
|
2020-05-14 00:36:51 +01:00
|
|
|
);
|
2020-05-18 21:19:23 +01:00
|
|
|
} else if (std.mem.eql(u8, method, "textDocument/definition") or
|
|
|
|
std.mem.eql(u8, method, "textDocument/declaration") or
|
2020-06-01 10:28:33 +01:00
|
|
|
std.mem.eql(u8, method, "textDocument/typeDefinition") or
|
|
|
|
std.mem.eql(u8, method, "textDocument/implementation"))
|
2020-05-18 21:19:23 +01:00
|
|
|
{
|
|
|
|
const document = params.getValue("textDocument").?.Object;
|
|
|
|
const uri = document.getValue("uri").?.String;
|
|
|
|
const position = params.getValue("position").?.Object;
|
|
|
|
|
|
|
|
const handle = document_store.getHandle(uri) orelse {
|
|
|
|
std.debug.warn("Trying to got to definition in non existent document {}", .{uri});
|
|
|
|
return try respondGeneric(id, null_result_response);
|
|
|
|
};
|
|
|
|
|
|
|
|
const pos = types.Position{
|
|
|
|
.line = position.getValue("line").?.Integer,
|
|
|
|
.character = position.getValue("character").?.Integer - 1,
|
|
|
|
};
|
|
|
|
if (pos.character >= 0) {
|
|
|
|
const pos_index = try handle.document.positionToIndex(pos);
|
2020-05-26 23:45:18 +01:00
|
|
|
const pos_context = try analysis.documentPositionContext(allocator, handle.document, pos);
|
2020-05-18 21:19:23 +01:00
|
|
|
|
|
|
|
switch (pos_context) {
|
2020-05-25 23:51:50 +01:00
|
|
|
.var_access => try gotoDefinitionGlobal(
|
|
|
|
id,
|
|
|
|
pos_index,
|
|
|
|
handle,
|
|
|
|
configFromUriOr(uri, config),
|
|
|
|
),
|
2020-05-26 23:45:18 +01:00
|
|
|
.field_access => |range| try gotoDefinitionFieldAccess(
|
2020-05-19 20:09:00 +01:00
|
|
|
id,
|
|
|
|
handle,
|
|
|
|
pos,
|
2020-05-26 23:45:18 +01:00
|
|
|
range,
|
2020-05-19 20:09:00 +01:00
|
|
|
configFromUriOr(uri, config),
|
|
|
|
),
|
2020-05-22 16:51:57 +01:00
|
|
|
.string_literal => try gotoDefinitionString(id, pos_index, handle, config),
|
2020-05-18 21:19:23 +01:00
|
|
|
else => try respondGeneric(id, null_result_response),
|
|
|
|
}
|
2020-05-26 23:45:18 +01:00
|
|
|
} else {
|
|
|
|
try respondGeneric(id, null_result_response);
|
2020-05-18 21:19:23 +01:00
|
|
|
}
|
2020-05-25 22:37:18 +01:00
|
|
|
} else if (std.mem.eql(u8, method, "textDocument/hover")) {
|
|
|
|
const document = params.getValue("textDocument").?.Object;
|
|
|
|
const uri = document.getValue("uri").?.String;
|
|
|
|
const position = params.getValue("position").?.Object;
|
|
|
|
|
|
|
|
const handle = document_store.getHandle(uri) orelse {
|
|
|
|
std.debug.warn("Trying to got to definition in non existent document {}", .{uri});
|
|
|
|
return try respondGeneric(id, null_result_response);
|
|
|
|
};
|
|
|
|
|
|
|
|
const pos = types.Position{
|
|
|
|
.line = position.getValue("line").?.Integer,
|
|
|
|
.character = position.getValue("character").?.Integer - 1,
|
|
|
|
};
|
|
|
|
if (pos.character >= 0) {
|
2020-05-25 23:51:50 +01:00
|
|
|
const pos_index = try handle.document.positionToIndex(pos);
|
2020-05-26 23:45:18 +01:00
|
|
|
const pos_context = try analysis.documentPositionContext(allocator, handle.document, pos);
|
2020-05-25 23:51:50 +01:00
|
|
|
|
|
|
|
switch (pos_context) {
|
|
|
|
.var_access => try hoverDefinitionGlobal(
|
|
|
|
id,
|
|
|
|
pos_index,
|
|
|
|
handle,
|
|
|
|
configFromUriOr(uri, config),
|
|
|
|
),
|
2020-05-26 23:45:18 +01:00
|
|
|
.field_access => |range| try hoverDefinitionFieldAccess(
|
2020-05-25 23:51:50 +01:00
|
|
|
id,
|
|
|
|
handle,
|
|
|
|
pos,
|
2020-05-26 23:45:18 +01:00
|
|
|
range,
|
2020-05-25 23:51:50 +01:00
|
|
|
configFromUriOr(uri, config),
|
|
|
|
),
|
|
|
|
else => try respondGeneric(id, null_result_response),
|
|
|
|
}
|
2020-05-26 23:45:18 +01:00
|
|
|
} else {
|
|
|
|
try respondGeneric(id, null_result_response);
|
2020-05-25 22:37:18 +01:00
|
|
|
}
|
2020-05-28 01:39:36 +01:00
|
|
|
} else if (std.mem.eql(u8, method, "textDocument/documentSymbol")) {
|
|
|
|
const document = params.getValue("textDocument").?.Object;
|
|
|
|
const uri = document.getValue("uri").?.String;
|
|
|
|
|
|
|
|
const handle = document_store.getHandle(uri) orelse {
|
|
|
|
std.debug.warn("Trying to got to definition in non existent document {}", .{uri});
|
|
|
|
return try respondGeneric(id, null_result_response);
|
|
|
|
};
|
|
|
|
|
|
|
|
try documentSymbol(id, handle);
|
2020-06-01 10:28:33 +01:00
|
|
|
} else if (std.mem.eql(u8, method, "textDocument/references") or
|
|
|
|
std.mem.eql(u8, method, "textDocument/documentHighlight") or
|
|
|
|
std.mem.eql(u8, method, "textDocument/codeAction") or
|
|
|
|
std.mem.eql(u8, method, "textDocument/codeLens") or
|
|
|
|
std.mem.eql(u8, method, "textDocument/documentLink") or
|
|
|
|
std.mem.eql(u8, method, "textDocument/formatting") or
|
|
|
|
std.mem.eql(u8, method, "textDocument/rangeFormatting") or
|
|
|
|
std.mem.eql(u8, method, "textDocument/onTypeFormatting") or
|
|
|
|
std.mem.eql(u8, method, "textDocument/rename") or
|
|
|
|
std.mem.eql(u8, method, "textDocument/prepareRename") or
|
|
|
|
std.mem.eql(u8, method, "textDocument/foldingRange") or
|
|
|
|
std.mem.eql(u8, method, "textDocument/selectionRange"))
|
|
|
|
{
|
|
|
|
// TODO: Unimplemented methods, implement them and add them to server capabilities.
|
|
|
|
try respondGeneric(id, null_result_response);
|
2020-04-24 23:19:03 +01:00
|
|
|
} else if (root.Object.getValue("id")) |_| {
|
2020-05-18 16:36:47 +01:00
|
|
|
std.debug.warn("Method with return value not implemented: {}", .{method});
|
2020-04-24 23:19:03 +01:00
|
|
|
try respondGeneric(id, not_implemented_response);
|
|
|
|
} else {
|
2020-05-18 16:36:47 +01:00
|
|
|
std.debug.warn("Method without return value not implemented: {}", .{method});
|
2020-04-24 23:19:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-17 12:40:32 +01:00
|
|
|
var debug_alloc_state: DebugAllocator = undefined;
|
2020-05-07 10:58:35 +01:00
|
|
|
// We can now use if(leak_count_alloc) |alloc| { ... } as a comptime check.
|
2020-05-17 12:40:32 +01:00
|
|
|
const debug_alloc: ?*DebugAllocator = if (build_options.allocation_info) &debug_alloc_state else null;
|
2020-05-07 10:50:25 +01:00
|
|
|
|
2020-04-24 23:19:03 +01:00
|
|
|
pub fn main() anyerror!void {
|
2020-05-07 14:20:45 +01:00
|
|
|
// TODO: Use a better purpose general allocator once std has one.
|
|
|
|
// Probably after the generic composable allocators PR?
|
2020-05-14 15:22:15 +01:00
|
|
|
// This is not too bad for now since most allocations happen in local arenas.
|
2020-05-07 14:20:45 +01:00
|
|
|
allocator = std.heap.page_allocator;
|
2020-04-24 23:19:03 +01:00
|
|
|
|
2020-05-08 00:53:00 +01:00
|
|
|
if (build_options.allocation_info) {
|
|
|
|
// TODO: Use a better debugging allocator, track size in bytes, memory reserved etc..
|
2020-05-07 10:50:25 +01:00
|
|
|
// Initialize the leak counting allocator.
|
2020-05-17 12:40:32 +01:00
|
|
|
debug_alloc_state = DebugAllocator.init(allocator);
|
2020-05-08 00:53:00 +01:00
|
|
|
allocator = &debug_alloc_state.allocator;
|
2020-05-07 10:50:25 +01:00
|
|
|
}
|
|
|
|
|
2020-04-27 21:38:35 +01:00
|
|
|
// Init global vars
|
2020-05-18 14:31:17 +01:00
|
|
|
const in_stream = std.io.getStdIn().inStream();
|
2020-05-17 18:26:02 +01:00
|
|
|
stdout = std.io.bufferedOutStream(std.io.getStdOut().outStream());
|
2020-04-24 23:19:03 +01:00
|
|
|
|
2020-05-17 15:39:04 +01:00
|
|
|
// Read the configuration, if any.
|
2020-05-14 22:16:40 +01:00
|
|
|
const config_parse_options = std.json.ParseOptions{ .allocator = allocator };
|
2020-05-17 15:39:04 +01:00
|
|
|
var config = Config{};
|
|
|
|
defer std.json.parseFree(Config, config, config_parse_options);
|
2020-05-09 14:43:51 +01:00
|
|
|
|
|
|
|
config_read: {
|
2020-05-25 01:22:39 +01:00
|
|
|
const known_folders = @import("known-folders");
|
2020-05-25 17:49:04 +01:00
|
|
|
|
2020-05-19 20:09:00 +01:00
|
|
|
const res = try known_folders.getPath(allocator, .local_configuration);
|
|
|
|
if (res) |local_config_path| {
|
|
|
|
defer allocator.free(local_config_path);
|
|
|
|
if (loadConfig(local_config_path)) |conf| {
|
|
|
|
config = conf;
|
|
|
|
break :config_read;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-25 17:33:08 +01:00
|
|
|
var exe_dir_bytes: [std.fs.MAX_PATH_BYTES]u8 = undefined;
|
|
|
|
const exe_dir_path = std.fs.selfExeDirPath(&exe_dir_bytes) catch break :config_read;
|
2020-05-14 22:16:40 +01:00
|
|
|
|
2020-05-25 17:33:08 +01:00
|
|
|
if (loadConfig(exe_dir_path)) |conf| {
|
2020-05-19 20:09:00 +01:00
|
|
|
config = conf;
|
2020-05-17 15:39:04 +01:00
|
|
|
}
|
2020-05-15 11:21:34 +01:00
|
|
|
}
|
|
|
|
|
2020-05-25 14:18:00 +01:00
|
|
|
// Find the zig executable in PATH
|
2020-05-30 21:36:18 +01:00
|
|
|
var zig_exe_path: ?[]const u8 = null;
|
|
|
|
defer if (zig_exe_path) |exe_path| allocator.free(exe_path);
|
2020-05-25 14:18:00 +01:00
|
|
|
|
|
|
|
find_zig: {
|
2020-05-30 21:36:18 +01:00
|
|
|
if (config.zig_exe_path) |exe_path| {
|
|
|
|
if (std.fs.path.isAbsolute(exe_path)) {
|
|
|
|
zig_exe_path = try std.mem.dupe(allocator, u8, exe_path);
|
|
|
|
break :find_zig;
|
|
|
|
}
|
|
|
|
|
|
|
|
std.debug.warn("zig path `{}` is not absolute, will look in path\n", .{exe_path});
|
|
|
|
}
|
|
|
|
|
2020-05-25 14:18:00 +01:00
|
|
|
const env_path = std.process.getEnvVarOwned(allocator, "PATH") catch |err| switch (err) {
|
|
|
|
error.EnvironmentVariableNotFound => {
|
|
|
|
std.debug.warn("Could not get PATH.\n", .{});
|
|
|
|
break :find_zig;
|
|
|
|
},
|
|
|
|
else => return err,
|
|
|
|
};
|
|
|
|
defer allocator.free(env_path);
|
|
|
|
|
|
|
|
const exe_extension = @as(std.zig.CrossTarget, .{}).exeFileExt();
|
|
|
|
const zig_exe = try std.fmt.allocPrint(allocator, "zig{}", .{exe_extension});
|
|
|
|
defer allocator.free(zig_exe);
|
|
|
|
|
2020-05-25 17:33:08 +01:00
|
|
|
var it = std.mem.tokenize(env_path, &[_]u8{std.fs.path.delimiter});
|
2020-05-25 14:18:00 +01:00
|
|
|
while (it.next()) |path| {
|
|
|
|
const full_path = try std.fs.path.join(allocator, &[_][]const u8{
|
|
|
|
path,
|
|
|
|
zig_exe,
|
|
|
|
});
|
2020-05-25 15:24:44 +01:00
|
|
|
defer allocator.free(full_path);
|
|
|
|
|
2020-05-25 14:18:00 +01:00
|
|
|
var buf: [std.fs.MAX_PATH_BYTES]u8 = undefined;
|
2020-05-30 21:36:18 +01:00
|
|
|
zig_exe_path = std.os.realpath(full_path, &buf) catch continue;
|
|
|
|
std.debug.warn("Found zig in PATH: {}\n", .{zig_exe_path});
|
2020-05-25 14:18:00 +01:00
|
|
|
break :find_zig;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-30 21:36:18 +01:00
|
|
|
if (zig_exe_path) |exe_path| {
|
|
|
|
std.debug.warn("Using zig executable {}\n", .{exe_path});
|
|
|
|
if (config.zig_lib_path == null) {
|
|
|
|
// Set the lib path relative to the executable path.
|
|
|
|
config.zig_lib_path = try std.fs.path.resolve(allocator, &[_][]const u8{
|
|
|
|
std.fs.path.dirname(exe_path).?, "./lib/zig",
|
|
|
|
});
|
|
|
|
|
|
|
|
std.debug.warn("Resolved standard library from executable: {}\n", .{config.zig_lib_path});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-25 18:04:23 +01:00
|
|
|
if (config.build_runner_path) |build_runner_path| {
|
2020-05-30 21:36:18 +01:00
|
|
|
try document_store.init(allocator, zig_exe_path, try std.mem.dupe(allocator, u8, build_runner_path));
|
2020-05-25 18:04:23 +01:00
|
|
|
} else {
|
2020-05-25 17:33:08 +01:00
|
|
|
var exe_dir_bytes: [std.fs.MAX_PATH_BYTES]u8 = undefined;
|
|
|
|
const exe_dir_path = try std.fs.selfExeDirPath(&exe_dir_bytes);
|
|
|
|
|
2020-05-25 18:04:23 +01:00
|
|
|
const build_runner_path = try std.fs.path.resolve(allocator, &[_][]const u8{ exe_dir_path, "build_runner.zig" });
|
2020-05-30 21:36:18 +01:00
|
|
|
try document_store.init(allocator, zig_exe_path, build_runner_path);
|
2020-05-25 17:33:08 +01:00
|
|
|
}
|
|
|
|
|
2020-05-14 00:10:41 +01:00
|
|
|
defer document_store.deinit();
|
2020-05-09 14:43:51 +01:00
|
|
|
|
2020-05-19 20:09:00 +01:00
|
|
|
workspace_folder_configs = std.StringHashMap(?Config).init(allocator);
|
|
|
|
defer workspace_folder_configs.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();
|
|
|
|
|
2020-05-17 15:50:13 +01:00
|
|
|
while (true) {
|
2020-05-17 16:26:00 +01:00
|
|
|
const headers = readRequestHeader(allocator, in_stream) catch |err| {
|
2020-05-18 16:36:47 +01:00
|
|
|
std.debug.warn("{}; exiting!", .{@errorName(err)});
|
2020-04-24 23:19:03 +01:00
|
|
|
return;
|
2020-05-17 15:50:13 +01:00
|
|
|
};
|
|
|
|
defer headers.deinit(allocator);
|
|
|
|
const buf = try allocator.alloc(u8, headers.content_length);
|
|
|
|
defer allocator.free(buf);
|
2020-05-17 16:26:00 +01:00
|
|
|
try in_stream.readNoEof(buf);
|
2020-05-17 15:50:13 +01:00
|
|
|
try processJsonRpc(&json_parser, buf, config);
|
|
|
|
json_parser.reset();
|
2020-04-24 23:19:03 +01:00
|
|
|
|
2020-05-08 00:53:00 +01:00
|
|
|
if (debug_alloc) |dbg| {
|
2020-05-18 17:37:56 +01:00
|
|
|
std.debug.warn("{}\n", .{dbg.info});
|
2020-05-07 10:50:25 +01:00
|
|
|
}
|
2020-04-24 23:19:03 +01:00
|
|
|
}
|
|
|
|
}
|