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-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-04-27 21:38:35 +01:00
|
|
|
|
2020-05-14 22:16:40 +01:00
|
|
|
const initialize_response =
|
2020-05-18 21:19:23 +01:00
|
|
|
\\,"result":{"capabilities":{"signatureHelpProvider":{"triggerCharacters":["(",","]},"textDocumentSync":1,"completionProvider":{"resolveProvider":false,"triggerCharacters":[".",":","@"]},"documentHighlightProvider":false,"codeActionProvider":false,"declarationProvider":true,"definitionProvider":true,"typeDefinitionProvider":true,"workspace":{"workspaceFolders":{"supported":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-04-27 21:38:35 +01:00
|
|
|
// The most memory we'll probably need
|
|
|
|
var mem_buffer: [1024 * 128]u8 = undefined;
|
|
|
|
var fbs = std.io.fixedBufferStream(&mem_buffer);
|
|
|
|
try std.json.stringify(reqOrRes, std.json.StringifyOptions{}, fbs.outStream());
|
|
|
|
|
2020-05-17 18:26:02 +01:00
|
|
|
const stdout_stream = stdout.outStream();
|
|
|
|
try stdout_stream.print("Content-Length: {}\r\n\r\n", .{fbs.pos});
|
|
|
|
try stdout_stream.writeAll(fbs.getWritten());
|
|
|
|
try stdout.flush();
|
2020-04-24 23:19:03 +01:00
|
|
|
}
|
|
|
|
|
2020-05-08 03:27:42 +01:00
|
|
|
fn respondGeneric(id: i64, response: []const u8) !void {
|
2020-04-24 23:19:03 +01:00
|
|
|
const id_digits = blk: {
|
|
|
|
if (id == 0) break :blk 1;
|
|
|
|
var digits: usize = 1;
|
|
|
|
var value = @divTrunc(id, 10);
|
|
|
|
while (value != 0) : (value = @divTrunc(value, 10)) {
|
|
|
|
digits += 1;
|
|
|
|
}
|
|
|
|
break :blk digits;
|
|
|
|
};
|
|
|
|
|
2020-05-08 00:53:00 +01:00
|
|
|
// Numbers of character that will be printed from this string: len - 3 brackets
|
|
|
|
// 1 from the beginning (escaped) and the 2 from the arg {}
|
|
|
|
const json_fmt = "{{\"jsonrpc\":\"2.0\",\"id\":{}";
|
2020-05-17 18:26:02 +01:00
|
|
|
|
|
|
|
const stdout_stream = stdout.outStream();
|
|
|
|
try stdout_stream.print("Content-Length: {}\r\n\r\n" ++ json_fmt, .{ response.len + id_digits + json_fmt.len - 3, id });
|
|
|
|
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-14 15:22:15 +01:00
|
|
|
const tree = try handle.tree(allocator);
|
2020-04-24 23:19:03 +01:00
|
|
|
defer tree.deinit();
|
|
|
|
|
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-09 02:02:29 +01:00
|
|
|
var error_it = tree.errors.iterator(0);
|
|
|
|
while (error_it.next()) |err| {
|
|
|
|
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-04 03:17:19 +01:00
|
|
|
var decls = tree.root_node.decls.iterator(0);
|
|
|
|
while (decls.next()) |decl_ptr| {
|
|
|
|
var decl = decl_ptr.*;
|
|
|
|
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-19 05:12:05 +01:00
|
|
|
fn containerToCompletion(list: *std.ArrayList(types.CompletionItem), analysis_ctx: *DocumentStore.AnalysisContext, container: *std.zig.ast.Node, config: Config) !void {
|
2020-05-17 15:23:04 +01:00
|
|
|
var index: usize = 0;
|
2020-05-18 21:19:23 +01:00
|
|
|
while (container.iterate(index)) |child_node| : (index += 1) {
|
2020-05-19 05:12:05 +01:00
|
|
|
if (analysis.isNodePublic(analysis_ctx.tree, child_node)) {
|
|
|
|
try nodeToCompletion(list, analysis_ctx, child_node, config);
|
2020-05-17 15:23:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-19 05:12:05 +01:00
|
|
|
fn nodeToCompletion(list: *std.ArrayList(types.CompletionItem), analysis_ctx: *DocumentStore.AnalysisContext, node: *std.zig.ast.Node, config: Config) error{OutOfMemory}!void {
|
|
|
|
var 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-19 05:12:05 +01:00
|
|
|
try containerToCompletion(list, analysis_ctx, 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| {
|
|
|
|
const insert_text = if (config.enable_snippets)
|
2020-05-19 05:12:05 +01:00
|
|
|
try analysis.getFunctionSnippet(list.allocator, analysis_ctx.tree, func)
|
2020-05-13 18:30:57 +01:00
|
|
|
else
|
|
|
|
null;
|
|
|
|
|
2020-05-19 05:12:05 +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-19 05:12:05 +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-19 05:12:05 +01:00
|
|
|
.detail = analysis.getFunctionSignature(analysis_ctx.tree, func),
|
2020-05-13 18:30:57 +01:00
|
|
|
.insertText = insert_text,
|
|
|
|
.insertTextFormat = if (config.enable_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-19 05:12:05 +01:00
|
|
|
const is_const = analysis_ctx.tree.tokens.at(var_decl.mut_token).id == .Keyword_const;
|
|
|
|
|
|
|
|
var child_analysis_context = try analysis_ctx.clone();
|
|
|
|
defer child_analysis_context.deinit();
|
|
|
|
|
|
|
|
const child_node = var_decl.type_node orelse var_decl.init_node.?;
|
|
|
|
const maybe_resolved_node = analysis.resolveTypeOfNode(&child_analysis_context, child_node);
|
|
|
|
|
|
|
|
if (maybe_resolved_node) |resolved_node| {
|
|
|
|
if (resolved_node.id == .FnProto) {
|
|
|
|
try nodeToCompletion(list, &child_analysis_context, resolved_node, config);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2020-05-17 15:23:04 +01:00
|
|
|
try list.append(.{
|
2020-05-19 05:12:05 +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-19 05:12:05 +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-16 19:06:48 +01:00
|
|
|
.ParamDecl => {
|
2020-05-17 15:23:04 +01:00
|
|
|
const param = node.cast(std.zig.ast.Node.ParamDecl).?;
|
2020-05-16 19:06:48 +01:00
|
|
|
if (param.name_token) |name_token|
|
2020-05-17 15:23:04 +01:00
|
|
|
try list.append(.{
|
2020-05-19 05:12:05 +01:00
|
|
|
.label = analysis_ctx.tree.tokenSlice(name_token),
|
2020-05-17 15:23:04 +01:00
|
|
|
.kind = .Constant,
|
2020-05-16 19:06:48 +01:00
|
|
|
.documentation = doc,
|
2020-05-19 05:12:05 +01:00
|
|
|
.detail = analysis.getParamSignature(analysis_ctx.tree, param),
|
2020-05-17 15:23:04 +01:00
|
|
|
});
|
2020-05-16 19:06:48 +01:00
|
|
|
},
|
2020-05-17 15:23:04 +01:00
|
|
|
.PrefixOp => {
|
|
|
|
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-19 05:12:05 +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-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 {
|
|
|
|
var start_idx = pos_index;
|
|
|
|
while (start_idx > 0 and
|
|
|
|
(std.ascii.isAlNum(handle.document.text[start_idx]) or handle.document.text[start_idx] == '_')) : (start_idx -= 1)
|
|
|
|
{}
|
|
|
|
|
|
|
|
var end_idx = pos_index;
|
|
|
|
while (end_idx < handle.document.text.len and
|
|
|
|
(std.ascii.isAlNum(handle.document.text[end_idx]) or handle.document.text[end_idx] == '_')) : (end_idx += 1)
|
|
|
|
{}
|
|
|
|
|
|
|
|
return handle.document.text[start_idx + 1 .. end_idx];
|
|
|
|
}
|
|
|
|
|
|
|
|
fn gotoDefinitionGlobal(id: i64, pos_index: usize, handle: DocumentStore.Handle) !void {
|
|
|
|
var tree = try handle.tree(allocator);
|
|
|
|
defer tree.deinit();
|
|
|
|
|
|
|
|
const name = identifierFromPosition(pos_index, handle);
|
|
|
|
var arena = std.heap.ArenaAllocator.init(allocator);
|
|
|
|
defer arena.deinit();
|
|
|
|
|
|
|
|
var decl_nodes = std.ArrayList(*std.zig.ast.Node).init(&arena.allocator);
|
|
|
|
try analysis.declsFromIndex(&decl_nodes, tree, pos_index);
|
|
|
|
|
|
|
|
const decl = analysis.getChildOfSlice(tree, decl_nodes.items, name) orelse return try respondGeneric(id, null_result_response);
|
|
|
|
const name_token = analysis.getDeclNameToken(tree, decl) orelse unreachable;
|
|
|
|
|
|
|
|
try send(types.Response{
|
|
|
|
.id = .{ .Integer = id },
|
|
|
|
.result = .{
|
|
|
|
.Location = .{
|
|
|
|
.uri = handle.document.uri,
|
|
|
|
.range = astLocationToRange(tree.tokenLocation(0, name_token)),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn gotoDefinitionFieldAccess(id: i64, handle: *DocumentStore.Handle, position: types.Position, line_start_idx: usize) !void {
|
|
|
|
var arena = std.heap.ArenaAllocator.init(allocator);
|
|
|
|
defer arena.deinit();
|
|
|
|
|
|
|
|
var analysis_ctx = try document_store.analysisContext(handle, &arena, position);
|
|
|
|
defer analysis_ctx.deinit();
|
|
|
|
|
|
|
|
const pos_index = try handle.document.positionToIndex(position);
|
|
|
|
var name = identifierFromPosition(pos_index, handle.*);
|
|
|
|
|
|
|
|
const line = try handle.document.getLine(@intCast(usize, position.line));
|
|
|
|
var tokenizer = std.zig.Tokenizer.init(line[line_start_idx..]);
|
|
|
|
|
|
|
|
const line_length = @ptrToInt(name.ptr) - @ptrToInt(line.ptr) + name.len - line_start_idx;
|
|
|
|
name = try std.mem.dupe(&arena.allocator, u8, name);
|
|
|
|
|
|
|
|
if (analysis.getFieldAccessTypeNode(&analysis_ctx, &tokenizer, line_length)) |container| {
|
|
|
|
const decl = analysis.getChild(analysis_ctx.tree, container, name) orelse return try respondGeneric(id, null_result_response);
|
|
|
|
const name_token = analysis.getDeclNameToken(analysis_ctx.tree, decl) orelse unreachable;
|
|
|
|
return try send(types.Response{
|
|
|
|
.id = .{ .Integer = id },
|
|
|
|
.result = .{
|
|
|
|
.Location = .{
|
|
|
|
.uri = analysis_ctx.handle.document.uri,
|
|
|
|
.range = astLocationToRange(analysis_ctx.tree.tokenLocation(0, name_token)),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
try respondGeneric(id, null_result_response);
|
|
|
|
}
|
|
|
|
|
2020-05-19 05:12:05 +01:00
|
|
|
fn completeGlobal(id: i64, pos_index: usize, handle: *DocumentStore.Handle, config: Config) !void {
|
2020-05-14 15:22:15 +01:00
|
|
|
var tree = try handle.tree(allocator);
|
2020-05-07 16:29:40 +01:00
|
|
|
defer tree.deinit();
|
2020-04-27 21:38:35 +01:00
|
|
|
|
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-19 05:12:05 +01:00
|
|
|
var analysis_ctx = try document_store.analysisContext(handle, &arena, types.Position{.line = 0, .character = 0,});
|
|
|
|
defer analysis_ctx.deinit();
|
|
|
|
|
2020-05-18 12:26:52 +01:00
|
|
|
var decl_nodes = std.ArrayList(*std.zig.ast.Node).init(&arena.allocator);
|
|
|
|
try analysis.declsFromIndex(&decl_nodes, tree, pos_index);
|
|
|
|
for (decl_nodes.items) |decl_ptr| {
|
2020-04-27 21:38:35 +01:00
|
|
|
var decl = decl_ptr.*;
|
2020-05-19 05:12:05 +01:00
|
|
|
try nodeToCompletion(&completions, &analysis_ctx, 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-05-14 22:16:40 +01:00
|
|
|
.id = .{ .Integer = 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-05-18 09:37:15 +01:00
|
|
|
fn nodePosition(tree: *std.zig.ast.Tree, node: *std.zig.ast.Node) types.Position {
|
|
|
|
const location = tree.tokenLocation(0, node.firstToken());
|
|
|
|
|
|
|
|
return types.Position{
|
|
|
|
.line = @intCast(i64, location.line),
|
|
|
|
.character = @intCast(i64, location.column),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-05-14 22:16:40 +01:00
|
|
|
fn completeFieldAccess(id: i64, handle: *DocumentStore.Handle, position: types.Position, line_start_idx: usize, 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-16 19:06:48 +01:00
|
|
|
var analysis_ctx = try document_store.analysisContext(handle, &arena, position);
|
2020-05-14 10:23:20 +01:00
|
|
|
defer analysis_ctx.deinit();
|
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-14 22:16:40 +01:00
|
|
|
var tokenizer = std.zig.Tokenizer.init(line[line_start_idx..]);
|
2020-05-18 14:21:16 +01:00
|
|
|
const line_length = line.len - line_start_idx;
|
2020-05-14 00:10:41 +01:00
|
|
|
|
2020-05-18 14:21:16 +01:00
|
|
|
if (analysis.getFieldAccessTypeNode(&analysis_ctx, &tokenizer, line_length)) |node| {
|
2020-05-19 05:18:39 +01:00
|
|
|
try nodeToCompletion(&completions, &analysis_ctx, node, config);
|
2020-05-13 15:10:20 +01:00
|
|
|
}
|
2020-05-14 00:10:41 +01:00
|
|
|
try send(types.Response{
|
2020-05-14 22:16:40 +01:00
|
|
|
.id = .{ .Integer = 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
|
|
|
|
|
|
|
// 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-14 22:16:40 +01:00
|
|
|
const PositionContext = union(enum) {
|
2020-05-08 11:50:21 +01:00
|
|
|
builtin,
|
|
|
|
comment,
|
|
|
|
string_literal,
|
2020-05-14 22:16:40 +01:00
|
|
|
field_access: usize,
|
2020-05-08 11:50:21 +01:00
|
|
|
var_access,
|
|
|
|
other,
|
2020-05-14 22:16:40 +01:00
|
|
|
empty,
|
|
|
|
};
|
|
|
|
|
|
|
|
const token_separators = [_]u8{
|
|
|
|
' ', '\t', '(', ')', '[', ']',
|
|
|
|
'{', '}', '|', '=', '!', ';',
|
|
|
|
',', '?', ':', '%', '+', '*',
|
|
|
|
'>', '<', '~', '-', '/', '&',
|
2020-05-08 11:50:21 +01:00
|
|
|
};
|
|
|
|
|
2020-05-08 15:24:29 +01:00
|
|
|
fn documentPositionContext(doc: types.TextDocument, pos_index: usize) PositionContext {
|
2020-05-08 11:50:21 +01:00
|
|
|
// First extract the whole current line up to the cursor.
|
|
|
|
var curr_position = pos_index;
|
|
|
|
while (curr_position > 0) : (curr_position -= 1) {
|
|
|
|
if (doc.text[curr_position - 1] == '\n') break;
|
|
|
|
}
|
|
|
|
|
|
|
|
var line = doc.text[curr_position .. pos_index + 1];
|
|
|
|
// Strip any leading whitespace.
|
2020-05-14 22:16:40 +01:00
|
|
|
var skipped_ws: usize = 0;
|
|
|
|
while (skipped_ws < line.len and (line[skipped_ws] == ' ' or line[skipped_ws] == '\t')) : (skipped_ws += 1) {}
|
|
|
|
if (skipped_ws >= line.len) return .empty;
|
|
|
|
line = line[skipped_ws..];
|
2020-05-08 11:50:21 +01:00
|
|
|
|
2020-05-08 15:24:29 +01:00
|
|
|
// Quick exit for comment lines and multi line string literals.
|
|
|
|
if (line.len >= 2 and line[0] == '/' and line[1] == '/')
|
2020-05-08 11:50:21 +01:00
|
|
|
return .comment;
|
2020-05-08 15:25:22 +01:00
|
|
|
if (line.len >= 2 and line[0] == '\\' and line[1] == '\\')
|
2020-05-08 15:24:29 +01:00
|
|
|
return .string_literal;
|
2020-05-08 11:50:21 +01:00
|
|
|
|
|
|
|
// TODO: This does not detect if we are in a string literal over multiple lines.
|
|
|
|
// Find out what context we are in.
|
|
|
|
// Go over the current line character by character
|
|
|
|
// and determine the context.
|
|
|
|
curr_position = 0;
|
2020-05-14 22:16:40 +01:00
|
|
|
var expr_start: usize = skipped_ws;
|
|
|
|
|
2020-05-08 11:50:21 +01:00
|
|
|
var new_token = true;
|
2020-05-08 12:36:54 +01:00
|
|
|
var context: PositionContext = .other;
|
|
|
|
var string_pop_ctx: PositionContext = .other;
|
2020-05-08 11:50:21 +01:00
|
|
|
while (curr_position < line.len) : (curr_position += 1) {
|
|
|
|
const c = line[curr_position];
|
|
|
|
const next_char = if (curr_position < line.len - 1) line[curr_position + 1] else null;
|
|
|
|
|
|
|
|
if (context != .string_literal and c == '"') {
|
2020-05-14 22:16:40 +01:00
|
|
|
expr_start = curr_position + skipped_ws;
|
2020-05-08 11:50:21 +01:00
|
|
|
context = .string_literal;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (context == .string_literal) {
|
|
|
|
// Skip over escaped quotes
|
|
|
|
if (c == '\\' and next_char != null and next_char.? == '"') {
|
|
|
|
curr_position += 1;
|
|
|
|
} else if (c == '"') {
|
|
|
|
context = string_pop_ctx;
|
|
|
|
string_pop_ctx = .other;
|
|
|
|
new_token = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c == '/' and next_char != null and next_char.? == '/') {
|
|
|
|
context = .comment;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-05-14 22:16:40 +01:00
|
|
|
if (std.mem.indexOfScalar(u8, &token_separators, c) != null) {
|
|
|
|
expr_start = curr_position + skipped_ws + 1;
|
2020-05-08 11:50:21 +01:00
|
|
|
new_token = true;
|
|
|
|
context = .other;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-05-08 13:07:14 +01:00
|
|
|
if (c == '.' and (!new_token or context == .string_literal)) {
|
2020-05-08 11:50:21 +01:00
|
|
|
new_token = true;
|
2020-05-14 22:16:40 +01:00
|
|
|
if (next_char != null and next_char.? == '.') continue;
|
|
|
|
context = .{ .field_access = expr_start };
|
2020-05-08 11:50:21 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new_token) {
|
2020-05-14 22:16:40 +01:00
|
|
|
const access_ctx: PositionContext = if (context == .field_access)
|
|
|
|
.{ .field_access = expr_start }
|
|
|
|
else
|
|
|
|
.var_access;
|
|
|
|
|
2020-05-08 11:50:21 +01:00
|
|
|
new_token = false;
|
|
|
|
|
|
|
|
if (c == '_' or std.ascii.isAlpha(c)) {
|
|
|
|
context = access_ctx;
|
|
|
|
} else if (c == '@') {
|
|
|
|
// This checks for @"..." identifiers by controlling
|
|
|
|
// the context the string will set after it is over.
|
|
|
|
if (next_char != null and next_char.? == '"') {
|
|
|
|
string_pop_ctx = access_ctx;
|
|
|
|
}
|
|
|
|
context = .builtin;
|
2020-05-08 13:09:31 +01:00
|
|
|
} else {
|
|
|
|
context = .other;
|
2020-05-08 11:50:21 +01:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (context == .field_access or context == .var_access or context == .builtin) {
|
|
|
|
if (c != '_' and !std.ascii.isAlNum(c)) {
|
|
|
|
context = .other;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
context = .other;
|
|
|
|
}
|
|
|
|
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
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-05-08 00:53:00 +01:00
|
|
|
std.debug.assert(root.Object.getValue("method") != null);
|
2020-04-27 21:38:35 +01:00
|
|
|
|
2020-04-24 23:19:03 +01:00
|
|
|
const method = root.Object.getValue("method").?.String;
|
|
|
|
const id = if (root.Object.getValue("id")) |id| id.Integer else 0;
|
2020-05-14 22:16:40 +01:00
|
|
|
|
2020-04-24 23:19:03 +01:00
|
|
|
const params = root.Object.getValue("params").?.Object;
|
2020-05-14 22:16:40 +01:00
|
|
|
|
2020-04-24 23:19:03 +01:00
|
|
|
// Core
|
|
|
|
if (std.mem.eql(u8, method, "initialize")) {
|
|
|
|
try respondGeneric(id, initialize_response);
|
|
|
|
} else if (std.mem.eql(u8, method, "initialized")) {
|
|
|
|
// noop
|
|
|
|
} else if (std.mem.eql(u8, method, "$/cancelRequest")) {
|
|
|
|
// noop
|
|
|
|
}
|
|
|
|
// 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);
|
|
|
|
try publishDiagnostics(handle.*, 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-14 00:10:41 +01:00
|
|
|
try document_store.applyChanges(handle, content_changes);
|
|
|
|
try publishDiagnostics(handle.*, config);
|
2020-04-24 23:19:03 +01:00
|
|
|
} else if (std.mem.eql(u8, method, "textDocument/didSave")) {
|
|
|
|
// 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
|
|
|
}
|
|
|
|
// 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);
|
|
|
|
const pos_context = documentPositionContext(handle.document, pos_index);
|
2020-05-08 04:05:17 +01:00
|
|
|
|
2020-05-14 22:16:40 +01:00
|
|
|
switch (pos_context) {
|
|
|
|
.builtin => try send(types.Response{
|
|
|
|
.id = .{ .Integer = id },
|
2020-05-07 13:29:53 +01:00
|
|
|
.result = .{
|
|
|
|
.CompletionList = .{
|
2020-04-27 21:38:35 +01:00
|
|
|
.isIncomplete = false,
|
2020-05-09 14:43:51 +01:00
|
|
|
.items = builtin_completions[@boolToInt(config.enable_snippets)][0..],
|
2020-05-07 13:29:53 +01:00
|
|
|
},
|
|
|
|
},
|
2020-05-14 22:16:40 +01:00
|
|
|
}),
|
2020-05-19 05:12:05 +01:00
|
|
|
.var_access, .empty => try completeGlobal(id, pos_index, handle, config),
|
2020-05-14 22:16:40 +01:00
|
|
|
.field_access => |start_idx| try completeFieldAccess(id, handle, pos, start_idx, config),
|
|
|
|
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-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
|
|
|
|
std.mem.eql(u8, method, "textDocument/typeDefinition"))
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
const pos_context = documentPositionContext(handle.document, pos_index);
|
|
|
|
|
|
|
|
switch (pos_context) {
|
|
|
|
.var_access => try gotoDefinitionGlobal(id, pos_index, handle.*),
|
|
|
|
.field_access => |start_idx| try gotoDefinitionFieldAccess(id, handle, pos, start_idx),
|
|
|
|
else => 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
|
|
|
|
|
|
|
// TODO: Investigate using std.fs.Watch to detect writes to the config and reload it.
|
|
|
|
config_read: {
|
|
|
|
var exec_dir_bytes: [std.fs.MAX_PATH_BYTES]u8 = undefined;
|
|
|
|
const exec_dir_path = std.fs.selfExeDirPath(&exec_dir_bytes) catch break :config_read;
|
2020-05-14 22:16:40 +01:00
|
|
|
|
2020-05-09 14:43:51 +01:00
|
|
|
var exec_dir = std.fs.cwd().openDir(exec_dir_path, .{}) catch break :config_read;
|
|
|
|
defer exec_dir.close();
|
|
|
|
|
2020-05-17 15:39:04 +01:00
|
|
|
const conf_file = exec_dir.openFile("zls.json", .{}) catch break :config_read;
|
2020-05-09 14:43:51 +01:00
|
|
|
defer conf_file.close();
|
|
|
|
|
2020-05-17 15:39:04 +01:00
|
|
|
// Max 1MB
|
|
|
|
const file_buf = conf_file.inStream().readAllAlloc(allocator, 0x1000000) catch break :config_read;
|
2020-05-09 14:43:51 +01:00
|
|
|
defer allocator.free(file_buf);
|
|
|
|
|
2020-05-14 15:22:15 +01:00
|
|
|
// TODO: Better errors? Doesn't seem like std.json can provide us positions or context.
|
2020-05-14 00:10:41 +01:00
|
|
|
config = std.json.parse(Config, &std.json.TokenStream.init(file_buf), config_parse_options) catch |err| {
|
2020-05-09 14:43:51 +01:00
|
|
|
std.debug.warn("Error while parsing configuration file: {}\nUsing default config.\n", .{err});
|
|
|
|
break :config_read;
|
|
|
|
};
|
|
|
|
}
|
2020-05-14 00:10:41 +01:00
|
|
|
|
2020-05-17 15:39:04 +01:00
|
|
|
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;
|
|
|
|
}
|
2020-05-15 11:21:34 +01:00
|
|
|
}
|
|
|
|
|
2020-05-14 02:54:05 +01:00
|
|
|
try document_store.init(allocator, 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();
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|