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-04-24 23:19:03 +01:00
|
|
|
const Uri = @import("uri.zig");
|
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
|
|
|
|
|
|
|
|
var stdout: std.fs.File.OutStream = undefined;
|
|
|
|
var allocator: *std.mem.Allocator = undefined;
|
|
|
|
|
2020-04-27 21:38:35 +01:00
|
|
|
/// Documents hashmap, types.DocumentUri:types.TextDocument
|
|
|
|
var documents: std.StringHashMap(types.TextDocument) = undefined;
|
|
|
|
|
|
|
|
const initialize_response = \\,"result":{"capabilities":{"signatureHelpProvider":{"triggerCharacters":["(",","]},"textDocumentSync":1,"completionProvider":{"resolveProvider":false,"triggerCharacters":[".",":","@"]},"documentHighlightProvider":false,"codeActionProvider":false,"workspace":{"workspaceFolders":{"supported":true}}}}}
|
2020-04-24 23:19:03 +01:00
|
|
|
;
|
|
|
|
|
|
|
|
const not_implemented_response = \\,"error":{"code":-32601,"message":"NotImplemented"}}
|
|
|
|
;
|
|
|
|
|
|
|
|
const null_result_response = \\,"result":null}
|
|
|
|
;
|
|
|
|
const empty_result_response = \\,"result":{}}
|
|
|
|
;
|
|
|
|
const empty_array_response = \\,"result":[]}
|
|
|
|
;
|
|
|
|
const edit_not_applied_response = \\,"result":{"applied":false,"failureReason":"feature not implemented"}}
|
|
|
|
;
|
|
|
|
const no_completions_response = \\,"result":{"isIncomplete":false,"items":[]}}
|
|
|
|
;
|
|
|
|
|
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-08 00:53:00 +01:00
|
|
|
try stdout.print("Content-Length: {}\r\n\r\n", .{fbs.pos});
|
|
|
|
try stdout.writeAll(fbs.getWritten());
|
2020-04-27 21:38:35 +01:00
|
|
|
}
|
|
|
|
|
2020-05-08 03:27:42 +01:00
|
|
|
fn log(comptime fmt: []const u8, args: var) !void {
|
2020-05-08 00:53:00 +01:00
|
|
|
// Disable logs on Release modes.
|
|
|
|
if (std.builtin.mode != .Debug) return;
|
|
|
|
|
2020-04-27 21:38:35 +01:00
|
|
|
var message = try std.fmt.allocPrint(allocator, fmt, args);
|
|
|
|
defer allocator.free(message);
|
|
|
|
|
|
|
|
try send(types.Notification{
|
|
|
|
.method = "window/logMessage",
|
2020-05-07 13:29:53 +01:00
|
|
|
.params = .{
|
|
|
|
.LogMessageParams = .{
|
|
|
|
.@"type" = .Log,
|
|
|
|
.message = message,
|
2020-04-27 21:38:35 +01:00
|
|
|
}
|
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-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\":{}";
|
|
|
|
try stdout.print("Content-Length: {}\r\n\r\n" ++ json_fmt, .{ response.len + id_digits + json_fmt.len - 3, id });
|
|
|
|
try stdout.writeAll(response);
|
|
|
|
}
|
|
|
|
|
2020-05-08 03:27:42 +01:00
|
|
|
fn freeDocument(document: types.TextDocument) void {
|
2020-05-08 00:53:00 +01:00
|
|
|
allocator.free(document.uri);
|
|
|
|
allocator.free(document.mem);
|
|
|
|
if (document.sane_text) |str| {
|
|
|
|
allocator.free(str);
|
|
|
|
}
|
2020-04-24 23:19:03 +01:00
|
|
|
}
|
|
|
|
|
2020-05-08 03:27:42 +01:00
|
|
|
fn openDocument(uri: []const u8, text: []const u8) !void {
|
2020-05-07 15:58:31 +01:00
|
|
|
const duped_uri = try std.mem.dupe(allocator, u8, uri);
|
|
|
|
const duped_text = try std.mem.dupe(allocator, u8, text);
|
|
|
|
|
|
|
|
const res = try documents.put(duped_uri, .{
|
|
|
|
.uri = duped_uri,
|
|
|
|
.text = duped_text,
|
|
|
|
.mem = duped_text,
|
2020-04-27 21:38:35 +01:00
|
|
|
});
|
2020-05-07 15:58:31 +01:00
|
|
|
|
2020-05-08 00:53:00 +01:00
|
|
|
if (res) |entry| {
|
|
|
|
try log("Document already open: {}, closing old.", .{uri});
|
|
|
|
freeDocument(entry.value);
|
|
|
|
} else {
|
|
|
|
try log("Opened document: {}", .{uri});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-08 03:27:42 +01:00
|
|
|
fn closeDocument(uri: []const u8) !void {
|
2020-05-08 00:53:00 +01:00
|
|
|
if (documents.remove(uri)) |entry| {
|
|
|
|
try log("Closing document: {}", .{uri});
|
|
|
|
freeDocument(entry.value);
|
2020-05-07 15:58:31 +01:00
|
|
|
}
|
2020-04-27 21:38:35 +01:00
|
|
|
}
|
2020-04-24 23:19:03 +01:00
|
|
|
|
2020-05-08 03:27:42 +01:00
|
|
|
fn cacheSane(document: *types.TextDocument) !void {
|
2020-05-08 00:53:00 +01:00
|
|
|
try log("Caching sane text for document: {}", .{document.uri});
|
|
|
|
|
2020-05-07 16:29:40 +01:00
|
|
|
if (document.sane_text) |old_sane| {
|
|
|
|
allocator.free(old_sane);
|
|
|
|
}
|
|
|
|
document.sane_text = try std.mem.dupe(allocator, u8, document.text);
|
|
|
|
}
|
|
|
|
|
2020-05-08 16:01:34 +01:00
|
|
|
// TODO: Is this correct or can we get a better end?
|
|
|
|
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-08 03:27:42 +01:00
|
|
|
fn publishDiagnostics(document: *types.TextDocument) !void {
|
2020-04-27 21:38:35 +01:00
|
|
|
const tree = try std.zig.parse(allocator, document.text);
|
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
|
|
|
|
|
|
|
if (tree.errors.len > 0) {
|
|
|
|
var index: usize = 0;
|
|
|
|
while (index < tree.errors.len) : (index += 1) {
|
|
|
|
const err = tree.errors.at(index);
|
|
|
|
const loc = tree.tokenLocation(0, err.loc());
|
|
|
|
|
2020-04-27 21:38:35 +01:00
|
|
|
var mem_buffer: [256]u8 = undefined;
|
|
|
|
var fbs = std.io.fixedBufferStream(&mem_buffer);
|
2020-05-08 00:53:00 +01:00
|
|
|
try tree.renderError(err, fbs.outStream());
|
2020-04-27 21:38:35 +01:00
|
|
|
|
2020-05-07 13:29:53 +01:00
|
|
|
try diagnostics.append(.{
|
2020-05-08 16:01:34 +01:00
|
|
|
.range = astLocationToRange(loc),
|
2020-05-07 13:29:53 +01:00
|
|
|
.severity = .Error,
|
2020-04-27 21:38:35 +01:00
|
|
|
.code = @tagName(err.*),
|
|
|
|
.source = "zls",
|
2020-05-07 13:01:16 +01:00
|
|
|
.message = try std.mem.dupe(&arena.allocator, u8, fbs.getWritten()),
|
2020-04-27 21:38:35 +01:00
|
|
|
// .relatedInformation = undefined
|
|
|
|
});
|
2020-04-24 23:19:03 +01:00
|
|
|
}
|
2020-05-04 03:17:19 +01:00
|
|
|
} else {
|
2020-05-07 16:29:40 +01:00
|
|
|
try cacheSane(document);
|
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-04 03:17:19 +01:00
|
|
|
if (func.name_token) |name_token| {
|
|
|
|
const loc = tree.tokenLocation(0, name_token);
|
2020-05-08 16:01:34 +01:00
|
|
|
|
2020-05-08 16:07:14 +01:00
|
|
|
const is_type_function = switch (func.return_type) {
|
2020-05-08 16:01:34 +01:00
|
|
|
.Explicit => |node| if (node.cast(std.zig.ast.Node.Identifier)) |ident|
|
|
|
|
std.mem.eql(u8, tree.tokenSlice(ident.token), "type")
|
|
|
|
else
|
|
|
|
false,
|
|
|
|
.InferErrorSet => false,
|
|
|
|
};
|
|
|
|
|
|
|
|
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)) {
|
2020-05-07 13:29:53 +01:00
|
|
|
try diagnostics.append(.{
|
2020-05-08 16:01:34 +01:00
|
|
|
.range = astLocationToRange(loc),
|
2020-05-07 13:29:53 +01:00
|
|
|
.severity = .Information,
|
2020-05-04 03:17:19 +01:00
|
|
|
.code = "BadStyle",
|
|
|
|
.source = "zls",
|
2020-05-08 16:01:34 +01:00
|
|
|
.message = "Type functions should be PascalCase"
|
2020-05-04 03:17:19 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
else => {}
|
|
|
|
}
|
|
|
|
}
|
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-04-27 21:38:35 +01:00
|
|
|
.uri = document.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-08 03:27:42 +01:00
|
|
|
fn completeGlobal(id: i64, document: *types.TextDocument) !void {
|
2020-05-07 12:10:58 +01:00
|
|
|
// The tree uses its own arena, so we just pass our main allocator.
|
2020-05-07 16:29:40 +01:00
|
|
|
var tree = try std.zig.parse(allocator, document.text);
|
2020-04-27 21:38:35 +01:00
|
|
|
|
2020-05-07 16:29:40 +01:00
|
|
|
if (tree.errors.len > 0) {
|
|
|
|
if (document.sane_text) |sane_text| {
|
|
|
|
tree.deinit();
|
|
|
|
tree = try std.zig.parse(allocator, sane_text);
|
|
|
|
} else return try respondGeneric(id, no_completions_response);
|
|
|
|
}
|
|
|
|
else {try cacheSane(document);}
|
|
|
|
|
|
|
|
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-07 10:58:35 +01:00
|
|
|
// try log("{}", .{&tree.root_node.decls});
|
2020-04-27 21:38:35 +01:00
|
|
|
var decls = tree.root_node.decls.iterator(0);
|
|
|
|
while (decls.next()) |decl_ptr| {
|
|
|
|
|
|
|
|
var decl = decl_ptr.*;
|
|
|
|
switch (decl.id) {
|
|
|
|
.FnProto => {
|
|
|
|
const func = decl.cast(std.zig.ast.Node.FnProto).?;
|
2020-05-08 18:02:46 +01:00
|
|
|
if (func.name_token) |name_token| {
|
|
|
|
var doc_comments = try analysis.getDocComments(&arena.allocator, tree, decl);
|
|
|
|
var doc = types.MarkupContent{
|
|
|
|
.kind = .Markdown,
|
|
|
|
.value = doc_comments orelse "",
|
|
|
|
};
|
|
|
|
try completions.append(.{
|
|
|
|
.label = tree.tokenSlice(name_token),
|
|
|
|
.kind = .Function,
|
|
|
|
.documentation = doc,
|
|
|
|
.detail = analysis.getFunctionSignature(tree, func),
|
|
|
|
});
|
|
|
|
}
|
2020-04-27 21:38:35 +01:00
|
|
|
},
|
|
|
|
.VarDecl => {
|
2020-05-04 03:17:19 +01:00
|
|
|
const var_decl = decl.cast(std.zig.ast.Node.VarDecl).?;
|
2020-05-07 12:10:58 +01:00
|
|
|
var doc_comments = try analysis.getDocComments(&arena.allocator, tree, decl);
|
2020-05-04 03:17:19 +01:00
|
|
|
var doc = types.MarkupContent{
|
2020-05-07 13:29:53 +01:00
|
|
|
.kind = .Markdown,
|
|
|
|
.value = doc_comments orelse "",
|
2020-05-04 03:17:19 +01:00
|
|
|
};
|
2020-05-07 13:29:53 +01:00
|
|
|
try completions.append(.{
|
2020-05-04 03:17:19 +01:00
|
|
|
.label = tree.tokenSlice(var_decl.name_token),
|
2020-05-07 13:29:53 +01:00
|
|
|
.kind = .Variable,
|
2020-05-04 03:17:19 +01:00
|
|
|
.documentation = doc,
|
2020-05-07 13:29:53 +01:00
|
|
|
.detail = analysis.getVariableSignature(tree, var_decl),
|
2020-04-27 21:38:35 +01:00
|
|
|
});
|
|
|
|
},
|
|
|
|
else => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-04-24 23:19:03 +01:00
|
|
|
|
2020-04-27 21:38:35 +01:00
|
|
|
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-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-07 12:36:40 +01:00
|
|
|
|
|
|
|
// Compute builtin completions at comptime.
|
|
|
|
const builtin_completions = block: {
|
|
|
|
@setEvalBranchQuota(3_500);
|
|
|
|
var temp: [data.builtins.len]types.CompletionItem = undefined;
|
|
|
|
|
|
|
|
for (data.builtins) |builtin, i| {
|
|
|
|
var cutoff = std.mem.indexOf(u8, builtin, "(") orelse builtin.len;
|
2020-05-08 00:53:00 +01:00
|
|
|
temp[i] = .{
|
|
|
|
.label = builtin[0..cutoff],
|
|
|
|
.kind = .Function,
|
|
|
|
|
|
|
|
.filterText = builtin[1..cutoff],
|
|
|
|
.insertText = builtin[1..],
|
|
|
|
.detail = data.builtin_details[i],
|
|
|
|
.documentation = .{
|
|
|
|
.kind = .Markdown,
|
|
|
|
.value = data.builtin_docs[i],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!build_options.no_snippets)
|
|
|
|
temp[i].insertTextFormat = .Snippet;
|
2020-05-07 12:36:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
break :block temp;
|
|
|
|
};
|
|
|
|
|
2020-05-08 12:36:54 +01:00
|
|
|
const PositionContext = enum {
|
2020-05-08 11:50:21 +01:00
|
|
|
builtin,
|
|
|
|
comment,
|
|
|
|
string_literal,
|
|
|
|
field_access,
|
|
|
|
var_access,
|
|
|
|
other,
|
2020-05-08 14:17:32 +01:00
|
|
|
empty
|
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.
|
|
|
|
curr_position = 0;
|
2020-05-08 14:17:32 +01:00
|
|
|
while (curr_position < line.len and (line[curr_position] == ' ' or line[curr_position] == '\t')) : (curr_position += 1) {}
|
|
|
|
if (curr_position >= line.len) return .empty;
|
2020-05-08 11:50:21 +01:00
|
|
|
line = line[curr_position .. ];
|
|
|
|
|
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;
|
|
|
|
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 == '"') {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c == ' ' or c == '\t') {
|
|
|
|
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;
|
|
|
|
context = .field_access;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new_token) {
|
2020-05-08 12:36:54 +01:00
|
|
|
const access_ctx: PositionContext = if (context == .field_access) .field_access 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-08 03:27:42 +01:00
|
|
|
fn processJsonRpc(parser: *std.json.Parser, json: []const u8) !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;
|
|
|
|
|
|
|
|
const params = root.Object.getValue("params").?.Object;
|
|
|
|
|
|
|
|
// 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-04-27 21:38:35 +01:00
|
|
|
try openDocument(uri, text);
|
2020-05-07 16:29:40 +01:00
|
|
|
try publishDiagnostics(&(documents.get(uri).?.value));
|
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;
|
|
|
|
|
|
|
|
var document = &(documents.get(uri).?.value);
|
|
|
|
const content_changes = params.getValue("contentChanges").?.Array;
|
|
|
|
|
|
|
|
for (content_changes.items) |change| {
|
|
|
|
if (change.Object.getValue("range")) |range| {
|
|
|
|
const start_pos = types.Position{
|
|
|
|
.line = range.Object.getValue("start").?.Object.getValue("line").?.Integer,
|
|
|
|
.character = range.Object.getValue("start").?.Object.getValue("character").?.Integer
|
|
|
|
};
|
|
|
|
const end_pos = types.Position{
|
|
|
|
.line = range.Object.getValue("end").?.Object.getValue("line").?.Integer,
|
|
|
|
.character = range.Object.getValue("end").?.Object.getValue("character").?.Integer
|
|
|
|
};
|
|
|
|
|
2020-05-07 15:58:31 +01:00
|
|
|
const change_text = change.Object.getValue("text").?.String;
|
|
|
|
const start_index = try document.positionToIndex(start_pos);
|
|
|
|
const end_index = try document.positionToIndex(end_pos);
|
|
|
|
|
|
|
|
const old_len = document.text.len;
|
|
|
|
const new_len = old_len + change_text.len;
|
|
|
|
if (new_len > document.mem.len) {
|
|
|
|
// We need to reallocate memory.
|
|
|
|
// We reallocate twice the current filesize or the new length, if it's more than that
|
|
|
|
// so that we can reduce the amount of realloc calls.
|
|
|
|
// We can tune this to find a better size if needed.
|
|
|
|
const realloc_len = std.math.max(2 * old_len, new_len);
|
|
|
|
document.mem = try allocator.realloc(document.mem, realloc_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The first part of the string, [0 .. start_index] need not be changed.
|
|
|
|
// We then copy the last part of the string, [end_index ..] to its
|
|
|
|
// new position, [start_index + change_len .. ]
|
|
|
|
std.mem.copy(u8, document.mem[start_index + change_text.len..][0 .. old_len - end_index], document.mem[end_index .. old_len]);
|
|
|
|
// Finally, we copy the changes over.
|
|
|
|
std.mem.copy(u8, document.mem[start_index..][0 .. change_text.len], change_text);
|
|
|
|
|
|
|
|
// Reset the text substring.
|
|
|
|
document.text = document.mem[0 .. new_len];
|
2020-04-27 21:38:35 +01:00
|
|
|
} else {
|
2020-05-07 15:58:31 +01:00
|
|
|
const change_text = change.Object.getValue("text").?.String;
|
|
|
|
const old_len = document.text.len;
|
|
|
|
|
|
|
|
if (change_text.len > document.mem.len) {
|
|
|
|
// Like above.
|
|
|
|
const realloc_len = std.math.max(2 * old_len, change_text.len);
|
|
|
|
document.mem = try allocator.realloc(document.mem, realloc_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
std.mem.copy(u8, document.mem[0 .. change_text.len], change_text);
|
|
|
|
document.text = document.mem[0 .. change_text.len];
|
2020-04-27 21:38:35 +01:00
|
|
|
}
|
|
|
|
}
|
2020-04-24 23:19:03 +01:00
|
|
|
|
2020-05-07 16:29:40 +01:00
|
|
|
try publishDiagnostics(document);
|
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-08 00:53:00 +01:00
|
|
|
try 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-07 16:29:40 +01:00
|
|
|
var document = &(documents.get(uri).?.value);
|
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) {
|
|
|
|
const pos_index = try document.positionToIndex(pos);
|
2020-05-08 15:24:29 +01:00
|
|
|
const pos_context = documentPositionContext(document.*, pos_index);
|
2020-05-08 04:05:17 +01:00
|
|
|
|
2020-05-08 12:36:54 +01:00
|
|
|
if (pos_context == .builtin) {
|
2020-04-27 21:38:35 +01:00
|
|
|
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-07 13:29:53 +01:00
|
|
|
.items = builtin_completions[0..],
|
|
|
|
},
|
|
|
|
},
|
2020-04-27 21:38:35 +01:00
|
|
|
});
|
2020-05-08 14:17:32 +01:00
|
|
|
} else if (pos_context == .var_access or pos_context == .empty) {
|
2020-04-27 21:38:35 +01:00
|
|
|
try completeGlobal(id, document);
|
|
|
|
} else {
|
|
|
|
try respondGeneric(id, no_completions_response);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
try respondGeneric(id, no_completions_response);
|
|
|
|
}
|
2020-04-24 23:19:03 +01:00
|
|
|
} else if (std.mem.eql(u8, method, "textDocument/signatureHelp")) {
|
|
|
|
try respondGeneric(id,
|
2020-05-02 17:43:26 +01:00
|
|
|
\\,"result":{"signatures":[{
|
|
|
|
\\"label": "nameOfFunction(aNumber: u8)",
|
|
|
|
\\"documentation": {"kind": "markdown", "value": "Description of the function in **Markdown**!"},
|
|
|
|
\\"parameters": [
|
|
|
|
\\{"label": [15, 27], "documentation": {"kind": "markdown", "value": "An argument"}}
|
|
|
|
\\]
|
|
|
|
\\}]}}
|
2020-04-24 23:19:03 +01:00
|
|
|
);
|
2020-05-02 17:43:26 +01:00
|
|
|
// try respondGeneric(id,
|
|
|
|
// \\,"result":{"signatures":[]}}
|
|
|
|
// );
|
2020-04-24 23:19:03 +01:00
|
|
|
} else if (root.Object.getValue("id")) |_| {
|
|
|
|
try log("Method with return value not implemented: {}", .{method});
|
|
|
|
try respondGeneric(id, not_implemented_response);
|
|
|
|
} else {
|
|
|
|
try log("Method without return value not implemented: {}", .{method});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-08 00:53:00 +01:00
|
|
|
var debug_alloc_state: std.testing.LeakCountAllocator = undefined;
|
2020-05-07 10:58:35 +01:00
|
|
|
// We can now use if(leak_count_alloc) |alloc| { ... } as a comptime check.
|
2020-05-08 00:53:00 +01:00
|
|
|
const debug_alloc: ?*std.testing.LeakCountAllocator = 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?
|
|
|
|
// This is not too bad for now since most allocations happen in local areans.
|
|
|
|
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-08 00:53:00 +01:00
|
|
|
debug_alloc_state = std.testing.LeakCountAllocator.init(allocator);
|
|
|
|
allocator = &debug_alloc_state.allocator;
|
2020-05-07 10:50:25 +01:00
|
|
|
}
|
|
|
|
|
2020-04-27 21:38:35 +01:00
|
|
|
// Init buffer for stdin read
|
|
|
|
|
2020-04-24 23:19:03 +01:00
|
|
|
var buffer = std.ArrayList(u8).init(allocator);
|
|
|
|
defer buffer.deinit();
|
|
|
|
|
|
|
|
try buffer.resize(4096);
|
|
|
|
|
2020-04-27 21:38:35 +01:00
|
|
|
// Init global vars
|
|
|
|
|
2020-04-24 23:19:03 +01:00
|
|
|
const stdin = std.io.getStdIn().inStream();
|
|
|
|
stdout = std.io.getStdOut().outStream();
|
|
|
|
|
2020-04-27 21:38:35 +01:00
|
|
|
documents = std.StringHashMap(types.TextDocument).init(allocator);
|
|
|
|
|
2020-04-24 23:19:03 +01:00
|
|
|
var offset: usize = 0;
|
|
|
|
var bytes_read: usize = 0;
|
|
|
|
|
|
|
|
var index: usize = 0;
|
|
|
|
var content_len: usize = 0;
|
|
|
|
|
2020-05-08 00:53:00 +01:00
|
|
|
// This JSON parser is passed to processJsonRpc and reset.
|
|
|
|
var parser = std.json.Parser.init(allocator, false);
|
|
|
|
defer parser.deinit();
|
2020-04-24 23:19:03 +01:00
|
|
|
|
2020-05-08 00:53:00 +01:00
|
|
|
stdin_poll: while (true) {
|
2020-05-07 10:50:25 +01:00
|
|
|
if (offset >= 16 and std.mem.startsWith(u8, buffer.items, "Content-Length: ")) {
|
2020-04-24 23:19:03 +01:00
|
|
|
|
|
|
|
index = 16;
|
|
|
|
while (index <= offset + 10) : (index += 1) {
|
|
|
|
const c = buffer.items[index];
|
|
|
|
if (c >= '0' and c <= '9') {
|
|
|
|
content_len = content_len * 10 + (c - '0');
|
|
|
|
} if (c == '\r' and buffer.items[index + 1] == '\n') {
|
|
|
|
index += 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buffer.items[index] == '\r') {
|
|
|
|
index += 2;
|
|
|
|
if (buffer.items.len < index + content_len) {
|
|
|
|
try buffer.resize(index + content_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
body_poll: while (offset < content_len + index) {
|
2020-05-08 00:53:00 +01:00
|
|
|
bytes_read = try stdin.readAll(buffer.items[offset .. index + content_len]);
|
2020-04-24 23:19:03 +01:00
|
|
|
if (bytes_read == 0) {
|
2020-05-08 00:53:00 +01:00
|
|
|
try log("0 bytes read; exiting!", .{});
|
2020-04-24 23:19:03 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
offset += bytes_read;
|
|
|
|
}
|
2020-05-08 00:53:00 +01:00
|
|
|
|
|
|
|
try processJsonRpc(&parser, buffer.items[index .. index + content_len]);
|
|
|
|
parser.reset();
|
2020-04-24 23:19:03 +01:00
|
|
|
|
|
|
|
offset = 0;
|
|
|
|
content_len = 0;
|
|
|
|
} else {
|
|
|
|
try log("\\r not found", .{});
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (offset >= 16) {
|
2020-05-07 10:58:35 +01:00
|
|
|
try log("Offset is greater than 16!", .{});
|
2020-04-24 23:19:03 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (offset < 16) {
|
2020-05-08 00:53:00 +01:00
|
|
|
bytes_read = try stdin.readAll(buffer.items[offset..25]);
|
2020-04-24 23:19:03 +01:00
|
|
|
} else {
|
|
|
|
if (offset == buffer.items.len) {
|
|
|
|
try buffer.resize(buffer.items.len * 2);
|
|
|
|
}
|
|
|
|
if (index + content_len > buffer.items.len) {
|
2020-05-08 00:53:00 +01:00
|
|
|
bytes_read = try stdin.readAll(buffer.items[offset..buffer.items.len]);
|
2020-04-24 23:19:03 +01:00
|
|
|
} else {
|
2020-05-08 00:53:00 +01:00
|
|
|
bytes_read = try stdin.readAll(buffer.items[offset .. index + content_len]);
|
2020-04-24 23:19:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bytes_read == 0) {
|
2020-05-08 00:53:00 +01:00
|
|
|
try log("0 bytes read; exiting!", .{});
|
2020-04-24 23:19:03 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
offset += bytes_read;
|
|
|
|
|
2020-05-08 00:53:00 +01:00
|
|
|
if (debug_alloc) |dbg| {
|
|
|
|
try log("Allocations alive: {}", .{dbg.count});
|
2020-05-07 10:50:25 +01:00
|
|
|
}
|
2020-04-24 23:19:03 +01:00
|
|
|
}
|
|
|
|
}
|