Started adding session tests, finished (buggy) main loop rewrite
This commit is contained in:
parent
ddcf6c677b
commit
671318730a
@ -179,5 +179,10 @@ pub fn build(b: *std.build.Builder) !void {
|
|||||||
unit_tests.addPackage(.{ .name = "analysis", .path = "src/analysis.zig" });
|
unit_tests.addPackage(.{ .name = "analysis", .path = "src/analysis.zig" });
|
||||||
unit_tests.addPackage(.{ .name = "types", .path = "src/types.zig" });
|
unit_tests.addPackage(.{ .name = "types", .path = "src/types.zig" });
|
||||||
unit_tests.setBuildMode(.Debug);
|
unit_tests.setBuildMode(.Debug);
|
||||||
test_step.dependOn(&unit_tests.step);
|
|
||||||
|
var session_tests = b.addTest("tests/sessions.zig");
|
||||||
|
session_tests.addPackage(.{ .name = "header", .path = "src/header.zig" });
|
||||||
|
session_tests.setBuildMode(.Debug);
|
||||||
|
|
||||||
|
test_step.dependOn(&session_tests.step);
|
||||||
}
|
}
|
||||||
|
@ -1249,14 +1249,10 @@ fn tokenRangeAppend(prev: SourceRange, token: std.zig.Token) SourceRange {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn documentPositionContext(allocator: *std.mem.Allocator, document: types.TextDocument, position: types.Position) !PositionContext {
|
pub fn documentPositionContext(arena: *std.heap.ArenaAllocator, document: types.TextDocument, position: types.Position) !PositionContext {
|
||||||
const line = try document.getLine(@intCast(usize, position.line));
|
const line = try document.getLine(@intCast(usize, position.line));
|
||||||
const pos_char = @intCast(usize, position.character);
|
const pos_char = @intCast(usize, position.character);
|
||||||
const idx = if (pos_char > line.len) line.len else pos_char;
|
const idx = if (pos_char > line.len) line.len else pos_char;
|
||||||
|
|
||||||
var arena = std.heap.ArenaAllocator.init(allocator);
|
|
||||||
defer arena.deinit();
|
|
||||||
|
|
||||||
var tokenizer = std.zig.Tokenizer.init(line[0..idx]);
|
var tokenizer = std.zig.Tokenizer.init(line[0..idx]);
|
||||||
var stack = try std.ArrayList(StackState).initCapacity(&arena.allocator, 8);
|
var stack = try std.ArrayList(StackState).initCapacity(&arena.allocator, 8);
|
||||||
|
|
||||||
|
640
src/main.zig
640
src/main.zig
@ -492,7 +492,7 @@ fn gotoDefinitionSymbol(id: types.RequestId, arena: *std.heap.ArenaAllocator, de
|
|||||||
else => decl_handle.location(),
|
else => decl_handle.location(),
|
||||||
};
|
};
|
||||||
|
|
||||||
try send(types.Response{
|
try send(arena, types.Response{
|
||||||
.id = id,
|
.id = id,
|
||||||
.result = .{
|
.result = .{
|
||||||
.Location = .{
|
.Location = .{
|
||||||
@ -572,7 +572,7 @@ fn hoverSymbol(id: types.RequestId, arena: *std.heap.ArenaAllocator, decl_handle
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
try send(types.Response{
|
try send(arena, types.Response{
|
||||||
.id = id,
|
.id = id,
|
||||||
.result = .{
|
.result = .{
|
||||||
.Hover = .{
|
.Hover = .{
|
||||||
@ -596,36 +596,24 @@ fn getSymbolGlobal(arena: *std.heap.ArenaAllocator, pos_index: usize, handle: *D
|
|||||||
return try analysis.lookupSymbolGlobal(&document_store, arena, handle, name, pos_index);
|
return try analysis.lookupSymbolGlobal(&document_store, arena, handle, name, pos_index);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn gotoDefinitionLabel(id: types.RequestId, pos_index: usize, handle: *DocumentStore.Handle, config: Config) !void {
|
fn gotoDefinitionLabel(arena: *std.heap.ArenaAllocator, id: types.RequestId, pos_index: usize, handle: *DocumentStore.Handle, config: Config) !void {
|
||||||
var arena = std.heap.ArenaAllocator.init(allocator);
|
|
||||||
defer arena.deinit();
|
|
||||||
|
|
||||||
const decl = (try getLabelGlobal(pos_index, handle)) orelse return try respondGeneric(id, null_result_response);
|
const decl = (try getLabelGlobal(pos_index, handle)) orelse return try respondGeneric(id, null_result_response);
|
||||||
return try gotoDefinitionSymbol(id, &arena, decl, false);
|
return try gotoDefinitionSymbol(id, arena, decl, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn gotoDefinitionGlobal(id: types.RequestId, pos_index: usize, handle: *DocumentStore.Handle, config: Config, resolve_alias: bool) !void {
|
fn gotoDefinitionGlobal(arena: *std.heap.ArenaAllocator, id: types.RequestId, pos_index: usize, handle: *DocumentStore.Handle, config: Config, resolve_alias: bool) !void {
|
||||||
var arena = std.heap.ArenaAllocator.init(allocator);
|
const decl = (try getSymbolGlobal(arena, pos_index, handle)) orelse return try respondGeneric(id, null_result_response);
|
||||||
defer arena.deinit();
|
return try gotoDefinitionSymbol(id, arena, decl, resolve_alias);
|
||||||
|
|
||||||
const decl = (try getSymbolGlobal(&arena, pos_index, handle)) orelse return try respondGeneric(id, null_result_response);
|
|
||||||
return try gotoDefinitionSymbol(id, &arena, decl, resolve_alias);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn hoverDefinitionLabel(id: types.RequestId, pos_index: usize, handle: *DocumentStore.Handle, config: Config) !void {
|
fn hoverDefinitionLabel(arena: *std.heap.ArenaAllocator, id: types.RequestId, pos_index: usize, handle: *DocumentStore.Handle, config: Config) !void {
|
||||||
var arena = std.heap.ArenaAllocator.init(allocator);
|
|
||||||
defer arena.deinit();
|
|
||||||
|
|
||||||
const decl = (try getLabelGlobal(pos_index, handle)) orelse return try respondGeneric(id, null_result_response);
|
const decl = (try getLabelGlobal(pos_index, handle)) orelse return try respondGeneric(id, null_result_response);
|
||||||
return try hoverSymbol(id, &arena, decl);
|
return try hoverSymbol(id, arena, decl);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn hoverDefinitionGlobal(id: types.RequestId, pos_index: usize, handle: *DocumentStore.Handle, config: Config) !void {
|
fn hoverDefinitionGlobal(arena: *std.heap.ArenaAllocator, id: types.RequestId, pos_index: usize, handle: *DocumentStore.Handle, config: Config) !void {
|
||||||
var arena = std.heap.ArenaAllocator.init(allocator);
|
const decl = (try getSymbolGlobal(arena, pos_index, handle)) orelse return try respondGeneric(id, null_result_response);
|
||||||
defer arena.deinit();
|
return try hoverSymbol(id, arena, decl);
|
||||||
|
|
||||||
const decl = (try getSymbolGlobal(&arena, pos_index, handle)) orelse return try respondGeneric(id, null_result_response);
|
|
||||||
return try hoverSymbol(id, &arena, decl);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn getSymbolFieldAccess(
|
fn getSymbolFieldAccess(
|
||||||
@ -659,6 +647,7 @@ fn getSymbolFieldAccess(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn gotoDefinitionFieldAccess(
|
fn gotoDefinitionFieldAccess(
|
||||||
|
arena: *std.heap.ArenaAllocator,
|
||||||
id: types.RequestId,
|
id: types.RequestId,
|
||||||
handle: *DocumentStore.Handle,
|
handle: *DocumentStore.Handle,
|
||||||
position: types.Position,
|
position: types.Position,
|
||||||
@ -666,33 +655,25 @@ fn gotoDefinitionFieldAccess(
|
|||||||
config: Config,
|
config: Config,
|
||||||
resolve_alias: bool,
|
resolve_alias: bool,
|
||||||
) !void {
|
) !void {
|
||||||
var arena = std.heap.ArenaAllocator.init(allocator);
|
const decl = (try getSymbolFieldAccess(handle, arena, position, range, config)) orelse return try respondGeneric(id, null_result_response);
|
||||||
defer arena.deinit();
|
return try gotoDefinitionSymbol(id, arena, decl, resolve_alias);
|
||||||
|
|
||||||
const decl = (try getSymbolFieldAccess(handle, &arena, position, range, config)) orelse return try respondGeneric(id, null_result_response);
|
|
||||||
return try gotoDefinitionSymbol(id, &arena, decl, resolve_alias);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn hoverDefinitionFieldAccess(
|
fn hoverDefinitionFieldAccess(
|
||||||
|
arena: *std.heap.ArenaAllocator,
|
||||||
id: types.RequestId,
|
id: types.RequestId,
|
||||||
handle: *DocumentStore.Handle,
|
handle: *DocumentStore.Handle,
|
||||||
position: types.Position,
|
position: types.Position,
|
||||||
range: analysis.SourceRange,
|
range: analysis.SourceRange,
|
||||||
config: Config,
|
config: Config,
|
||||||
) !void {
|
) !void {
|
||||||
var arena = std.heap.ArenaAllocator.init(allocator);
|
const decl = (try getSymbolFieldAccess(handle, arena, position, range, config)) orelse return try respondGeneric(id, null_result_response);
|
||||||
defer arena.deinit();
|
return try hoverSymbol(id, arena, decl);
|
||||||
|
|
||||||
const decl = (try getSymbolFieldAccess(handle, &arena, position, range, config)) orelse return try respondGeneric(id, null_result_response);
|
|
||||||
return try hoverSymbol(id, &arena, decl);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn gotoDefinitionString(id: types.RequestId, pos_index: usize, handle: *DocumentStore.Handle, config: Config) !void {
|
fn gotoDefinitionString(arena: *std.heap.ArenaAllocator, id: types.RequestId, pos_index: usize, handle: *DocumentStore.Handle, config: Config) !void {
|
||||||
const tree = handle.tree;
|
const tree = handle.tree;
|
||||||
|
|
||||||
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);
|
const import_str = analysis.getImportStr(tree, pos_index) orelse return try respondGeneric(id, null_result_response);
|
||||||
const uri = (try document_store.uriFromImportStr(
|
const uri = (try document_store.uriFromImportStr(
|
||||||
&arena.allocator,
|
&arena.allocator,
|
||||||
@ -700,7 +681,7 @@ fn gotoDefinitionString(id: types.RequestId, pos_index: usize, handle: *Document
|
|||||||
import_str,
|
import_str,
|
||||||
)) orelse return try respondGeneric(id, null_result_response);
|
)) orelse return try respondGeneric(id, null_result_response);
|
||||||
|
|
||||||
try send(types.Response{
|
try send(arena, types.Response{
|
||||||
.id = id,
|
.id = id,
|
||||||
.result = .{
|
.result = .{
|
||||||
.Location = .{
|
.Location = .{
|
||||||
@ -714,23 +695,21 @@ fn gotoDefinitionString(id: types.RequestId, pos_index: usize, handle: *Document
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
fn renameDefinitionGlobal(id: types.RequestId, handle: *DocumentStore.Handle, pos_index: usize, new_name: []const u8) !void {
|
fn renameDefinitionGlobal(arena: *std.heap.ArenaAllocator, id: types.RequestId, handle: *DocumentStore.Handle, pos_index: usize, new_name: []const u8) !void {
|
||||||
var arena = std.heap.ArenaAllocator.init(allocator);
|
const decl = (try getSymbolGlobal(arena, pos_index, handle)) orelse return try respondGeneric(id, null_result_response);
|
||||||
defer arena.deinit();
|
|
||||||
|
|
||||||
const decl = (try getSymbolGlobal(&arena, pos_index, handle)) orelse return try respondGeneric(id, null_result_response);
|
|
||||||
|
|
||||||
var workspace_edit = types.WorkspaceEdit{
|
var workspace_edit = types.WorkspaceEdit{
|
||||||
.changes = std.StringHashMap([]types.TextEdit).init(&arena.allocator),
|
.changes = std.StringHashMap([]types.TextEdit).init(&arena.allocator),
|
||||||
};
|
};
|
||||||
try rename.renameSymbol(&arena, &document_store, decl, new_name, &workspace_edit.changes.?);
|
try rename.renameSymbol(arena, &document_store, decl, new_name, &workspace_edit.changes.?);
|
||||||
try send(types.Response{
|
try send(arena, types.Response{
|
||||||
.id = id,
|
.id = id,
|
||||||
.result = .{ .WorkspaceEdit = workspace_edit },
|
.result = .{ .WorkspaceEdit = workspace_edit },
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
fn renameDefinitionFieldAccess(
|
fn renameDefinitionFieldAccess(
|
||||||
|
arena: *std.heap.ArenaAllocator,
|
||||||
id: types.RequestId,
|
id: types.RequestId,
|
||||||
handle: *DocumentStore.Handle,
|
handle: *DocumentStore.Handle,
|
||||||
position: types.Position,
|
position: types.Position,
|
||||||
@ -738,32 +717,26 @@ fn renameDefinitionFieldAccess(
|
|||||||
new_name: []const u8,
|
new_name: []const u8,
|
||||||
config: Config,
|
config: Config,
|
||||||
) !void {
|
) !void {
|
||||||
var arena = std.heap.ArenaAllocator.init(allocator);
|
const decl = (try getSymbolFieldAccess(handle, arena, position, range, config)) orelse return try respondGeneric(id, null_result_response);
|
||||||
defer arena.deinit();
|
|
||||||
|
|
||||||
const decl = (try getSymbolFieldAccess(handle, &arena, position, range, config)) orelse return try respondGeneric(id, null_result_response);
|
|
||||||
|
|
||||||
var workspace_edit = types.WorkspaceEdit{
|
var workspace_edit = types.WorkspaceEdit{
|
||||||
.changes = std.StringHashMap([]types.TextEdit).init(&arena.allocator),
|
.changes = std.StringHashMap([]types.TextEdit).init(&arena.allocator),
|
||||||
};
|
};
|
||||||
try rename.renameSymbol(&arena, &document_store, decl, new_name, &workspace_edit.changes.?);
|
try rename.renameSymbol(arena, &document_store, decl, new_name, &workspace_edit.changes.?);
|
||||||
try send(types.Response{
|
try send(arena, types.Response{
|
||||||
.id = id,
|
.id = id,
|
||||||
.result = .{ .WorkspaceEdit = workspace_edit },
|
.result = .{ .WorkspaceEdit = workspace_edit },
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
fn renameDefinitionLabel(id: types.RequestId, handle: *DocumentStore.Handle, pos_index: usize, new_name: []const u8) !void {
|
fn renameDefinitionLabel(arena: *std.heap.ArenaAllocator, id: types.RequestId, handle: *DocumentStore.Handle, pos_index: usize, new_name: []const u8) !void {
|
||||||
var arena = std.heap.ArenaAllocator.init(allocator);
|
|
||||||
defer arena.deinit();
|
|
||||||
|
|
||||||
const decl = (try getLabelGlobal(pos_index, handle)) orelse return try respondGeneric(id, null_result_response);
|
const decl = (try getLabelGlobal(pos_index, handle)) orelse return try respondGeneric(id, null_result_response);
|
||||||
|
|
||||||
var workspace_edit = types.WorkspaceEdit{
|
var workspace_edit = types.WorkspaceEdit{
|
||||||
.changes = std.StringHashMap([]types.TextEdit).init(&arena.allocator),
|
.changes = std.StringHashMap([]types.TextEdit).init(&arena.allocator),
|
||||||
};
|
};
|
||||||
try rename.renameLabel(&arena, decl, new_name, &workspace_edit.changes.?);
|
try rename.renameLabel(arena, decl, new_name, &workspace_edit.changes.?);
|
||||||
try send(types.Response{
|
try send(arena, types.Response{
|
||||||
.id = id,
|
.id = id,
|
||||||
.result = .{ .WorkspaceEdit = workspace_edit },
|
.result = .{ .WorkspaceEdit = workspace_edit },
|
||||||
});
|
});
|
||||||
@ -825,22 +798,18 @@ fn declToCompletion(context: DeclToCompletionContext, decl_handle: analysis.Decl
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn completeLabel(id: types.RequestId, pos_index: usize, handle: *DocumentStore.Handle, config: Config) !void {
|
fn completeLabel(arena: *std.heap.ArenaAllocator, id: types.RequestId, pos_index: usize, handle: *DocumentStore.Handle, config: Config) !void {
|
||||||
// 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);
|
var completions = std.ArrayList(types.CompletionItem).init(&arena.allocator);
|
||||||
// Deallocate all temporary data.
|
|
||||||
defer arena.deinit();
|
|
||||||
|
|
||||||
const context = DeclToCompletionContext{
|
const context = DeclToCompletionContext{
|
||||||
.completions = &completions,
|
.completions = &completions,
|
||||||
.config = &config,
|
.config = &config,
|
||||||
.arena = &arena,
|
.arena = arena,
|
||||||
.orig_handle = handle,
|
.orig_handle = handle,
|
||||||
};
|
};
|
||||||
try analysis.iterateLabels(handle, pos_index, declToCompletion, context);
|
try analysis.iterateLabels(handle, pos_index, declToCompletion, context);
|
||||||
|
|
||||||
try send(types.Response{
|
try send(arena, types.Response{
|
||||||
.id = id,
|
.id = id,
|
||||||
.result = .{
|
.result = .{
|
||||||
.CompletionList = .{
|
.CompletionList = .{
|
||||||
@ -851,22 +820,18 @@ fn completeLabel(id: types.RequestId, pos_index: usize, handle: *DocumentStore.H
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
fn completeGlobal(id: types.RequestId, pos_index: usize, handle: *DocumentStore.Handle, config: Config) !void {
|
fn completeGlobal(arena: *std.heap.ArenaAllocator, id: types.RequestId, pos_index: usize, handle: *DocumentStore.Handle, config: Config) !void {
|
||||||
// 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);
|
var completions = std.ArrayList(types.CompletionItem).init(&arena.allocator);
|
||||||
// Deallocate all temporary data.
|
|
||||||
defer arena.deinit();
|
|
||||||
|
|
||||||
const context = DeclToCompletionContext{
|
const context = DeclToCompletionContext{
|
||||||
.completions = &completions,
|
.completions = &completions,
|
||||||
.config = &config,
|
.config = &config,
|
||||||
.arena = &arena,
|
.arena = arena,
|
||||||
.orig_handle = handle,
|
.orig_handle = handle,
|
||||||
};
|
};
|
||||||
try analysis.iterateSymbolsGlobal(&document_store, &arena, handle, pos_index, declToCompletion, context);
|
try analysis.iterateSymbolsGlobal(&document_store, arena, handle, pos_index, declToCompletion, context);
|
||||||
|
|
||||||
try send(types.Response{
|
try send(arena, types.Response{
|
||||||
.id = id,
|
.id = id,
|
||||||
.result = .{
|
.result = .{
|
||||||
.CompletionList = .{
|
.CompletionList = .{
|
||||||
@ -877,21 +842,18 @@ fn completeGlobal(id: types.RequestId, pos_index: usize, handle: *DocumentStore.
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
fn completeFieldAccess(id: types.RequestId, handle: *DocumentStore.Handle, position: types.Position, range: analysis.SourceRange, config: Config) !void {
|
fn completeFieldAccess(arena: *std.heap.ArenaAllocator, id: types.RequestId, handle: *DocumentStore.Handle, position: types.Position, range: analysis.SourceRange, config: Config) !void {
|
||||||
var arena = std.heap.ArenaAllocator.init(allocator);
|
|
||||||
defer arena.deinit();
|
|
||||||
|
|
||||||
var completions = std.ArrayList(types.CompletionItem).init(&arena.allocator);
|
var completions = std.ArrayList(types.CompletionItem).init(&arena.allocator);
|
||||||
|
|
||||||
const line = try handle.document.getLine(@intCast(usize, position.line));
|
const line = try handle.document.getLine(@intCast(usize, position.line));
|
||||||
var tokenizer = std.zig.Tokenizer.init(line[range.start..range.end]);
|
var tokenizer = std.zig.Tokenizer.init(line[range.start..range.end]);
|
||||||
|
|
||||||
const pos_index = try handle.document.positionToIndex(position);
|
const pos_index = try handle.document.positionToIndex(position);
|
||||||
if (try analysis.getFieldAccessType(&document_store, &arena, handle, pos_index, &tokenizer)) |node| {
|
if (try analysis.getFieldAccessType(&document_store, arena, handle, pos_index, &tokenizer)) |node| {
|
||||||
try typeToCompletion(&arena, &completions, node, handle, config);
|
try typeToCompletion(arena, &completions, node, handle, config);
|
||||||
}
|
}
|
||||||
|
|
||||||
try send(types.Response{
|
try send(arena, types.Response{
|
||||||
.id = id,
|
.id = id,
|
||||||
.result = .{
|
.result = .{
|
||||||
.CompletionList = .{
|
.CompletionList = .{
|
||||||
@ -902,11 +864,8 @@ fn completeFieldAccess(id: types.RequestId, handle: *DocumentStore.Handle, posit
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
fn documentSymbol(id: types.RequestId, handle: *DocumentStore.Handle) !void {
|
fn documentSymbol(arena: *std.heap.ArenaAllocator, id: types.RequestId, handle: *DocumentStore.Handle) !void {
|
||||||
var arena = std.heap.ArenaAllocator.init(allocator);
|
try send(arena, types.Response{
|
||||||
defer arena.deinit();
|
|
||||||
|
|
||||||
try send(types.Response{
|
|
||||||
.id = id,
|
.id = id,
|
||||||
.result = .{ .DocumentSymbols = try analysis.getDocumentSymbols(&arena.allocator, handle.tree) },
|
.result = .{ .DocumentSymbols = try analysis.getDocumentSymbols(&arena.allocator, handle.tree) },
|
||||||
});
|
});
|
||||||
@ -1096,6 +1055,8 @@ fn saveDocumentHandler(arena: *std.heap.ArenaAllocator, id: types.RequestId, req
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn closeDocumentHandler(arena: *std.heap.ArenaAllocator, id: types.RequestId, req: requests.CloseDocument, config: Config) !void {
|
fn closeDocumentHandler(arena: *std.heap.ArenaAllocator, id: types.RequestId, req: requests.CloseDocument, config: Config) !void {
|
||||||
|
std.log.debug(.main, "CLOSING DOCUMENT!!!, id: {}\n", .{id});
|
||||||
|
|
||||||
document_store.closeDocument(req.params.textDocument.uri);
|
document_store.closeDocument(req.params.textDocument.uri);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1103,7 +1064,7 @@ fn semanticTokensHandler(arena: *std.heap.ArenaAllocator, id: types.RequestId, r
|
|||||||
const this_config = configFromUriOr(req.params.textDocument.uri, config);
|
const this_config = configFromUriOr(req.params.textDocument.uri, config);
|
||||||
if (this_config.enable_semantic_tokens) {
|
if (this_config.enable_semantic_tokens) {
|
||||||
const handle = document_store.getHandle(req.params.textDocument.uri) orelse {
|
const handle = document_store.getHandle(req.params.textDocument.uri) orelse {
|
||||||
std.log.debug(.main, "Trying to complete in non existent document {}", .{req.params.textDocument.uri});
|
std.log.debug(.main, "Trying to get semantic tokens of non existent document {}", .{req.params.textDocument.uri});
|
||||||
return try respondGeneric(id, no_semantic_tokens_response);
|
return try respondGeneric(id, no_semantic_tokens_response);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -1118,20 +1079,201 @@ fn semanticTokensHandler(arena: *std.heap.ArenaAllocator, id: types.RequestId, r
|
|||||||
return try respondGeneric(id, no_semantic_tokens_response);
|
return try respondGeneric(id, no_semantic_tokens_response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn completionHandler(arena: *std.heap.ArenaAllocator, id: types.RequestId, req: requests.Completion, config: Config) !void {
|
||||||
|
const handle = document_store.getHandle(req.params.textDocument.uri) orelse {
|
||||||
|
std.log.debug(.main, "Trying to complete in non existent document {}", .{req.params.textDocument.uri});
|
||||||
|
return try respondGeneric(id, no_completions_response);
|
||||||
|
};
|
||||||
|
|
||||||
|
if (req.params.position.character >= 0) {
|
||||||
|
const pos_index = try handle.document.positionToIndex(req.params.position);
|
||||||
|
const pos_context = try analysis.documentPositionContext(arena, handle.document, req.params.position);
|
||||||
|
|
||||||
|
const this_config = configFromUriOr(req.params.textDocument.uri, config);
|
||||||
|
const use_snippets = this_config.enable_snippets and client_capabilities.supports_snippets;
|
||||||
|
switch (pos_context) {
|
||||||
|
.builtin => try send(arena, types.Response{
|
||||||
|
.id = id,
|
||||||
|
.result = .{
|
||||||
|
.CompletionList = .{
|
||||||
|
.isIncomplete = false,
|
||||||
|
.items = builtin_completions[@boolToInt(use_snippets)][0..],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
.var_access, .empty => try completeGlobal(arena, id, pos_index, handle, this_config),
|
||||||
|
.field_access => |range| try completeFieldAccess(arena, id, handle, req.params.position, range, this_config),
|
||||||
|
.global_error_set => try send(arena, types.Response{
|
||||||
|
.id = id,
|
||||||
|
.result = .{
|
||||||
|
.CompletionList = .{
|
||||||
|
.isIncomplete = false,
|
||||||
|
.items = document_store.error_completions.completions.items,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
.enum_literal => try send(arena, types.Response{
|
||||||
|
.id = id,
|
||||||
|
.result = .{
|
||||||
|
.CompletionList = .{
|
||||||
|
.isIncomplete = false,
|
||||||
|
.items = document_store.enum_completions.completions.items,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
.label => try completeLabel(arena, id, pos_index, handle, this_config),
|
||||||
|
else => try respondGeneric(id, no_completions_response),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
try respondGeneric(id, no_completions_response);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn signatureHelperHandler(arena: *std.heap.ArenaAllocator, id: types.RequestId, config: Config) !void {
|
||||||
|
// TODO Implement this
|
||||||
|
try respondGeneric(id,
|
||||||
|
\\,"result":{"signatures":[]}}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn gotoHandler(arena: *std.heap.ArenaAllocator, id: types.RequestId, req: requests.GotoDefinition, config: Config, resolve_alias: bool) !void {
|
||||||
|
const handle = document_store.getHandle(req.params.textDocument.uri) orelse {
|
||||||
|
std.log.debug(.main, "Trying to go to definition in non existent document {}", .{req.params.textDocument.uri});
|
||||||
|
return try respondGeneric(id, null_result_response);
|
||||||
|
};
|
||||||
|
|
||||||
|
if (req.params.position.character >= 0) {
|
||||||
|
const pos_index = try handle.document.positionToIndex(req.params.position);
|
||||||
|
const pos_context = try analysis.documentPositionContext(arena, handle.document, req.params.position);
|
||||||
|
|
||||||
|
const this_config = configFromUriOr(req.params.textDocument.uri, config);
|
||||||
|
switch (pos_context) {
|
||||||
|
.var_access => try gotoDefinitionGlobal(arena, id, pos_index, handle, this_config, resolve_alias),
|
||||||
|
.field_access => |range| try gotoDefinitionFieldAccess(arena, id, handle, req.params.position, range, this_config, resolve_alias),
|
||||||
|
.string_literal => try gotoDefinitionString(arena, id, pos_index, handle, config),
|
||||||
|
.label => try gotoDefinitionLabel(arena, id, pos_index, handle, this_config),
|
||||||
|
else => try respondGeneric(id, null_result_response),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
try respondGeneric(id, null_result_response);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn gotoDefinitionHandler(arena: *std.heap.ArenaAllocator, id: types.RequestId, req: requests.GotoDefinition, config: Config) !void {
|
||||||
|
try gotoHandler(arena, id, req, config, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn gotoDeclarationHandler(arena: *std.heap.ArenaAllocator, id: types.RequestId, req: requests.GotoDeclaration, config: Config) !void {
|
||||||
|
try gotoHandler(arena, id, req, config, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn hoverHandler(arena: *std.heap.ArenaAllocator, id: types.RequestId, req: requests.Hover, config: Config) !void {
|
||||||
|
const handle = document_store.getHandle(req.params.textDocument.uri) orelse {
|
||||||
|
std.log.debug(.main, "Trying to get hover in non existent document {}", .{req.params.textDocument.uri});
|
||||||
|
return try respondGeneric(id, null_result_response);
|
||||||
|
};
|
||||||
|
|
||||||
|
if (req.params.position.character >= 0) {
|
||||||
|
const pos_index = try handle.document.positionToIndex(req.params.position);
|
||||||
|
const pos_context = try analysis.documentPositionContext(arena, handle.document, req.params.position);
|
||||||
|
|
||||||
|
const this_config = configFromUriOr(req.params.textDocument.uri, config);
|
||||||
|
switch (pos_context) {
|
||||||
|
.var_access => try hoverDefinitionGlobal(arena, id, pos_index, handle, this_config),
|
||||||
|
.field_access => |range| try hoverDefinitionFieldAccess(arena, id, handle, req.params.position, range, this_config),
|
||||||
|
.label => try hoverDefinitionLabel(arena, id, pos_index, handle, this_config),
|
||||||
|
else => try respondGeneric(id, null_result_response),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
try respondGeneric(id, null_result_response);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn documentSymbolsHandler(arena: *std.heap.ArenaAllocator, id: types.RequestId, req: requests.DocumentSymbols, config: Config) !void {
|
||||||
|
const handle = document_store.getHandle(req.params.textDocument.uri) orelse {
|
||||||
|
std.log.debug(.main, "Trying to get document symbols in non existent document {}", .{req.params.textDocument.uri});
|
||||||
|
return try respondGeneric(id, null_result_response);
|
||||||
|
};
|
||||||
|
try documentSymbol(arena, id, handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn formattingHandler(arena: *std.heap.ArenaAllocator, id: types.RequestId, req: requests.Formatting, config: Config) !void {
|
||||||
|
if (config.zig_exe_path) |zig_exe_path| {
|
||||||
|
const handle = document_store.getHandle(req.params.textDocument.uri) orelse {
|
||||||
|
std.log.debug(.main, "Trying to got to definition in non existent document {}", .{req.params.textDocument.uri});
|
||||||
|
return try respondGeneric(id, null_result_response);
|
||||||
|
};
|
||||||
|
|
||||||
|
var process = try std.ChildProcess.init(&[_][]const u8{ zig_exe_path, "fmt", "--stdin" }, allocator);
|
||||||
|
defer process.deinit();
|
||||||
|
process.stdin_behavior = .Pipe;
|
||||||
|
process.stdout_behavior = .Pipe;
|
||||||
|
|
||||||
|
process.spawn() catch |err| {
|
||||||
|
std.log.debug(.main, "Failed to spawn zig fmt process, error: {}\n", .{err});
|
||||||
|
return try respondGeneric(id, null_result_response);
|
||||||
|
};
|
||||||
|
try process.stdin.?.writeAll(handle.document.text);
|
||||||
|
process.stdin.?.close();
|
||||||
|
process.stdin = null;
|
||||||
|
|
||||||
|
const stdout_bytes = try process.stdout.?.reader().readAllAlloc(allocator, std.math.maxInt(usize));
|
||||||
|
defer allocator.free(stdout_bytes);
|
||||||
|
|
||||||
|
switch (try process.wait()) {
|
||||||
|
.Exited => |code| if (code == 0) {
|
||||||
|
try send(arena, types.Response{
|
||||||
|
.id = id,
|
||||||
|
.result = .{
|
||||||
|
.TextEdits = &[1]types.TextEdit{
|
||||||
|
.{
|
||||||
|
.range = handle.document.range(),
|
||||||
|
.newText = stdout_bytes,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
},
|
||||||
|
else => {},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return try respondGeneric(id, null_result_response);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn renameHandler(arena: *std.heap.ArenaAllocator, id: types.RequestId, req: requests.Rename, config: Config) !void {
|
||||||
|
const handle = document_store.getHandle(req.params.textDocument.uri) orelse {
|
||||||
|
std.log.debug(.main, "Trying to got to definition in non existent document {}", .{req.params.textDocument.uri});
|
||||||
|
return try respondGeneric(id, null_result_response);
|
||||||
|
};
|
||||||
|
|
||||||
|
if (req.params.position.character >= 0) {
|
||||||
|
const pos_index = try handle.document.positionToIndex(req.params.position);
|
||||||
|
const pos_context = try analysis.documentPositionContext(arena, handle.document, req.params.position);
|
||||||
|
|
||||||
|
const this_config = configFromUriOr(req.params.textDocument.uri, config);
|
||||||
|
switch (pos_context) {
|
||||||
|
.var_access => try renameDefinitionGlobal(arena, id, handle, pos_index, req.params.newName),
|
||||||
|
.field_access => |range| try renameDefinitionFieldAccess(arena, id, handle, req.params.position, range, req.params.newName, this_config),
|
||||||
|
.label => try renameDefinitionLabel(arena, id, handle, pos_index, req.params.newName),
|
||||||
|
else => try respondGeneric(id, null_result_response),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
try respondGeneric(id, null_result_response);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn processJsonRpc(arena: *std.heap.ArenaAllocator, parser: *std.json.Parser, json: []const u8, config: Config) !void {
|
fn processJsonRpc(arena: *std.heap.ArenaAllocator, parser: *std.json.Parser, json: []const u8, config: Config) !void {
|
||||||
var tree = try parser.parse(json);
|
var tree = try parser.parse(json);
|
||||||
defer tree.deinit();
|
defer tree.deinit();
|
||||||
|
|
||||||
const root = tree.root;
|
const id = if (tree.root.Object.getValue("id")) |id| switch (id) {
|
||||||
|
|
||||||
const id = if (root.Object.getValue("id")) |id| switch (id) {
|
|
||||||
.Integer => |int| types.RequestId{ .Integer = int },
|
.Integer => |int| types.RequestId{ .Integer = int },
|
||||||
.String => |str| types.RequestId{ .String = str },
|
.String => |str| types.RequestId{ .String = str },
|
||||||
else => types.RequestId{ .Integer = 0 },
|
else => types.RequestId{ .Integer = 0 },
|
||||||
} else types.RequestId{ .Integer = 0 };
|
} else types.RequestId{ .Integer = 0 };
|
||||||
|
|
||||||
std.debug.assert(root.Object.getValue("method") != null);
|
std.debug.assert(tree.root.Object.getValue("method") != null);
|
||||||
const method = root.Object.getValue("method").?.String;
|
const method = tree.root.Object.getValue("method").?.String;
|
||||||
|
|
||||||
const start_time = std.time.milliTimestamp();
|
const start_time = std.time.milliTimestamp();
|
||||||
defer {
|
defer {
|
||||||
@ -1140,271 +1282,73 @@ fn processJsonRpc(arena: *std.heap.ArenaAllocator, parser: *std.json.Parser, jso
|
|||||||
}
|
}
|
||||||
|
|
||||||
const method_map = .{
|
const method_map = .{
|
||||||
.{ "initialize", .{ requests.Initialize, initializeHandler } },
|
.{"initialized"},
|
||||||
.{ "shutdown", .{ void, shutdownHandler } },
|
.{"$/cancelRequest"},
|
||||||
.{ "initialized", .{} },
|
.{"textDocument/willSave"},
|
||||||
.{ "$/cancelRequest", .{} },
|
.{ "initialize", requests.Initialize, initializeHandler },
|
||||||
.{ "workspace/didChangeWorkspaceFolders", .{ requests.WorkspaceFoldersChange, workspaceFoldersChangeHandler } },
|
.{ "shutdown", void, shutdownHandler },
|
||||||
.{ "textDocument/didOpen", .{ requests.OpenDocument, openDocumentHandler } },
|
.{ "workspace/didChangeWorkspaceFolders", requests.WorkspaceFoldersChange, workspaceFoldersChangeHandler },
|
||||||
.{ "textDocument/didChange", .{ requests.ChangeDocument, changeDocumentHandler } },
|
.{ "textDocument/didOpen", requests.OpenDocument, openDocumentHandler },
|
||||||
.{ "textDocument/didSave", .{ requests.SaveDocument, saveDocumentHandler } },
|
.{ "textDocument/didChange", requests.ChangeDocument, changeDocumentHandler },
|
||||||
.{ "textDocument/willSave", .{} },
|
.{ "textDocument/didSave", requests.SaveDocument, saveDocumentHandler },
|
||||||
.{ "textDocument/didClose", .{ requests.CloseDocument, closeDocumentHandler } },
|
.{ "textDocument/didClose", requests.CloseDocument, closeDocumentHandler },
|
||||||
.{ "textDocument/semanticTokens", .{ requests.SemanticTokens, semanticTokensHandler } },
|
.{ "textDocument/semanticTokens", requests.SemanticTokens, semanticTokensHandler },
|
||||||
|
.{ "textDocument/completion", requests.Completion, completionHandler },
|
||||||
|
.{ "textDocument/signatureHelp", void, signatureHelperHandler },
|
||||||
|
.{ "textDocument/definition", requests.GotoDefinition, gotoDefinitionHandler },
|
||||||
|
.{ "textDocument/typeDefinition", requests.GotoDefinition, gotoDefinitionHandler },
|
||||||
|
.{ "textDocument/implementation", requests.GotoDefinition, gotoDefinitionHandler },
|
||||||
|
.{ "textDocument/declaration", requests.GotoDeclaration, gotoDeclarationHandler },
|
||||||
|
.{ "textDocument/hover", requests.Hover, hoverHandler },
|
||||||
|
.{ "textDocument/documentSymbol", requests.DocumentSymbols, documentSymbolsHandler },
|
||||||
|
.{ "textDocument/formatting", requests.Formatting, formattingHandler },
|
||||||
|
.{ "textDocument/rename", requests.Rename, renameHandler },
|
||||||
};
|
};
|
||||||
|
|
||||||
inline for (method_map) |method_info| {
|
inline for (method_map) |method_info| {
|
||||||
if (std.mem.eql(u8, method_info[0], method)) {
|
if (std.mem.eql(u8, method, method_info[0])) {
|
||||||
if (method_info[1].len != 0) {
|
if (method_info.len == 1) {
|
||||||
const info = method_info[1];
|
return;
|
||||||
if (info[0] != void) {
|
} else if (method_info[1] != void) {
|
||||||
const request_obj = requests.fromDynamicTree(arena, info[0], tree.root) catch |err| {
|
const request_obj = requests.fromDynamicTree(arena, method_info[1], tree.root) catch |err| {
|
||||||
switch (err) {
|
switch (err) {
|
||||||
error.MalformedJson => {
|
error.MalformedJson => {
|
||||||
std.log.debug(.main, "Could not create request type {} from JSON {}\n", .{ @typeName(info[0]), json });
|
std.log.debug(.main, "Could not create request type {} from JSON {}\n", .{ @typeName(method_info[1]), json });
|
||||||
// @TODO What should we return to the client in this case?
|
return try respondGeneric(id, null_result_response);
|
||||||
return;
|
},
|
||||||
},
|
error.OutOfMemory => return err,
|
||||||
error.OutOfMemory => return err,
|
}
|
||||||
}
|
};
|
||||||
};
|
|
||||||
return try info[1](arena, id, request_obj, config);
|
std.log.debug(.TEMPORARY, "{} {}\n", .{method, method_info[0]});
|
||||||
} else {
|
return try (method_info[2])(arena, id, request_obj, config);
|
||||||
return try info[1](arena, id, config);
|
} else {
|
||||||
}
|
return try (method_info[2])(arena, id, config);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// if (std.mem.eql(u8, method, "textDocument/completion")) {
|
const unimplemented_map = std.ComptimeStringMap(void, .{
|
||||||
// const params = root.Object.getValue("params").?.Object;
|
.{ "textDocument/references" },
|
||||||
// const text_document = params.getValue("textDocument").?.Object;
|
.{ "textDocument/documentHighlight" },
|
||||||
// const uri = text_document.getValue("uri").?.String;
|
.{ "textDocument/codeAction" },
|
||||||
// const position = params.getValue("position").?.Object;
|
.{ "textDocument/codeLens" },
|
||||||
|
.{ "textDocument/documentLink" },
|
||||||
|
.{ "textDocument/rangeFormatting" },
|
||||||
|
.{ "textDocument/onTypeFormatting" },
|
||||||
|
.{ "textDocument/prepareRename" },
|
||||||
|
.{ "textDocument/foldingRange" },
|
||||||
|
.{ "textDocument/selectionRange" },
|
||||||
|
});
|
||||||
|
|
||||||
// const handle = document_store.getHandle(uri) orelse {
|
if (unimplemented_map.has(method)) {
|
||||||
// std.log.debug(.main, "Trying to complete in non existent document {}", .{uri});
|
// TODO: Unimplemented methods, implement them and add them to server capabilities.
|
||||||
// return try respondGeneric(id, no_completions_response);
|
return try respondGeneric(id, null_result_response);
|
||||||
// };
|
}
|
||||||
|
if (tree.root.Object.getValue("id")) |_| {
|
||||||
// const pos = types.Position{
|
return try respondGeneric(id, not_implemented_response);
|
||||||
// .line = position.getValue("line").?.Integer,
|
}
|
||||||
// .character = position.getValue("character").?.Integer,
|
std.log.debug(.main, "Method without return value not implemented: {}", .{method});
|
||||||
// };
|
|
||||||
// if (pos.character >= 0) {
|
|
||||||
// const pos_index = try handle.document.positionToIndex(pos);
|
|
||||||
// const pos_context = try analysis.documentPositionContext(allocator, handle.document, pos);
|
|
||||||
|
|
||||||
// const this_config = configFromUriOr(uri, config);
|
|
||||||
// const use_snippets = this_config.enable_snippets and client_capabilities.supports_snippets;
|
|
||||||
// switch (pos_context) {
|
|
||||||
// .builtin => try send(types.Response{
|
|
||||||
// .id = id,
|
|
||||||
// .result = .{
|
|
||||||
// .CompletionList = .{
|
|
||||||
// .isIncomplete = false,
|
|
||||||
// .items = builtin_completions[@boolToInt(use_snippets)][0..],
|
|
||||||
// },
|
|
||||||
// },
|
|
||||||
// }),
|
|
||||||
// .var_access, .empty => try completeGlobal(id, pos_index, handle, this_config),
|
|
||||||
// .field_access => |range| try completeFieldAccess(id, handle, pos, range, this_config),
|
|
||||||
// .global_error_set => try send(types.Response{
|
|
||||||
// .id = id,
|
|
||||||
// .result = .{
|
|
||||||
// .CompletionList = .{
|
|
||||||
// .isIncomplete = false,
|
|
||||||
// .items = document_store.error_completions.completions.items,
|
|
||||||
// },
|
|
||||||
// },
|
|
||||||
// }),
|
|
||||||
// .enum_literal => try send(types.Response{
|
|
||||||
// .id = id,
|
|
||||||
// .result = .{
|
|
||||||
// .CompletionList = .{
|
|
||||||
// .isIncomplete = false,
|
|
||||||
// .items = document_store.enum_completions.completions.items,
|
|
||||||
// },
|
|
||||||
// },
|
|
||||||
// }),
|
|
||||||
// .label => try completeLabel(id, pos_index, handle, this_config),
|
|
||||||
// else => try respondGeneric(id, no_completions_response),
|
|
||||||
// }
|
|
||||||
// } else {
|
|
||||||
// try respondGeneric(id, no_completions_response);
|
|
||||||
// }
|
|
||||||
// } else if (std.mem.eql(u8, method, "textDocument/signatureHelp")) {
|
|
||||||
// // TODO: Implement this
|
|
||||||
// try respondGeneric(id,
|
|
||||||
// \\,"result":{"signatures":[]}}
|
|
||||||
// );
|
|
||||||
// } 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") or
|
|
||||||
// std.mem.eql(u8, method, "textDocument/implementation"))
|
|
||||||
// {
|
|
||||||
// const params = root.Object.getValue("params").?.Object;
|
|
||||||
// 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.log.debug(.main, "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,
|
|
||||||
// };
|
|
||||||
// if (pos.character >= 0) {
|
|
||||||
// const resolve_alias = !std.mem.eql(u8, method, "textDocument/declaration");
|
|
||||||
// const pos_index = try handle.document.positionToIndex(pos);
|
|
||||||
// const pos_context = try analysis.documentPositionContext(allocator, handle.document, pos);
|
|
||||||
|
|
||||||
// switch (pos_context) {
|
|
||||||
// .var_access => try gotoDefinitionGlobal(id, pos_index, handle, configFromUriOr(uri, config), resolve_alias),
|
|
||||||
// .field_access => |range| try gotoDefinitionFieldAccess(id, handle, pos, range, configFromUriOr(uri, config), resolve_alias),
|
|
||||||
// .string_literal => try gotoDefinitionString(id, pos_index, handle, config),
|
|
||||||
// .label => try gotoDefinitionLabel(id, pos_index, handle, configFromUriOr(uri, config)),
|
|
||||||
// else => try respondGeneric(id, null_result_response),
|
|
||||||
// }
|
|
||||||
// } else {
|
|
||||||
// try respondGeneric(id, null_result_response);
|
|
||||||
// }
|
|
||||||
// } else if (std.mem.eql(u8, method, "textDocument/hover")) {
|
|
||||||
// const params = root.Object.getValue("params").?.Object;
|
|
||||||
// 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.log.debug(.main, "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,
|
|
||||||
// };
|
|
||||||
// if (pos.character >= 0) {
|
|
||||||
// const pos_index = try handle.document.positionToIndex(pos);
|
|
||||||
// const pos_context = try analysis.documentPositionContext(allocator, handle.document, pos);
|
|
||||||
|
|
||||||
// switch (pos_context) {
|
|
||||||
// .var_access => try hoverDefinitionGlobal(id, pos_index, handle, configFromUriOr(uri, config)),
|
|
||||||
// .field_access => |range| try hoverDefinitionFieldAccess(id, handle, pos, range, configFromUriOr(uri, config)),
|
|
||||||
// .label => try hoverDefinitionLabel(id, pos_index, handle, configFromUriOr(uri, config)),
|
|
||||||
// else => try respondGeneric(id, null_result_response),
|
|
||||||
// }
|
|
||||||
// } else {
|
|
||||||
// try respondGeneric(id, null_result_response);
|
|
||||||
// }
|
|
||||||
// } else if (std.mem.eql(u8, method, "textDocument/documentSymbol")) {
|
|
||||||
// const params = root.Object.getValue("params").?.Object;
|
|
||||||
// const document = params.getValue("textDocument").?.Object;
|
|
||||||
// const uri = document.getValue("uri").?.String;
|
|
||||||
|
|
||||||
// const handle = document_store.getHandle(uri) orelse {
|
|
||||||
// std.log.debug(.main, "Trying to got to definition in non existent document {}", .{uri});
|
|
||||||
// return try respondGeneric(id, null_result_response);
|
|
||||||
// };
|
|
||||||
|
|
||||||
// try documentSymbol(id, handle);
|
|
||||||
// } else if (std.mem.eql(u8, method, "textDocument/formatting")) {
|
|
||||||
// if (config.zig_exe_path) |zig_exe_path| {
|
|
||||||
// const params = root.Object.getValue("params").?.Object;
|
|
||||||
// const document = params.getValue("textDocument").?.Object;
|
|
||||||
// const uri = document.getValue("uri").?.String;
|
|
||||||
|
|
||||||
// const handle = document_store.getHandle(uri) orelse {
|
|
||||||
// std.log.debug(.main, "Trying to got to definition in non existent document {}", .{uri});
|
|
||||||
// return try respondGeneric(id, null_result_response);
|
|
||||||
// };
|
|
||||||
|
|
||||||
// var process = try std.ChildProcess.init(&[_][]const u8{ zig_exe_path, "fmt", "--stdin" }, allocator);
|
|
||||||
// defer process.deinit();
|
|
||||||
// process.stdin_behavior = .Pipe;
|
|
||||||
// process.stdout_behavior = .Pipe;
|
|
||||||
|
|
||||||
// process.spawn() catch |err| {
|
|
||||||
// std.log.debug(.main, "Failed to spawn zig fmt process, error: {}\n", .{err});
|
|
||||||
// return try respondGeneric(id, null_result_response);
|
|
||||||
// };
|
|
||||||
// try process.stdin.?.writeAll(handle.document.text);
|
|
||||||
// process.stdin.?.close();
|
|
||||||
// process.stdin = null;
|
|
||||||
|
|
||||||
// const stdout_bytes = try process.stdout.?.reader().readAllAlloc(allocator, std.math.maxInt(usize));
|
|
||||||
// defer allocator.free(stdout_bytes);
|
|
||||||
|
|
||||||
// switch (try process.wait()) {
|
|
||||||
// .Exited => |code| if (code == 0) {
|
|
||||||
// try send(types.Response{
|
|
||||||
// .id = id,
|
|
||||||
// .result = .{
|
|
||||||
// .TextEdits = &[1]types.TextEdit{
|
|
||||||
// .{
|
|
||||||
// .range = handle.document.range(),
|
|
||||||
// .newText = stdout_bytes,
|
|
||||||
// },
|
|
||||||
// },
|
|
||||||
// },
|
|
||||||
// });
|
|
||||||
// },
|
|
||||||
// else => {},
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// return try respondGeneric(id, null_result_response);
|
|
||||||
// } else if (std.mem.eql(u8, method, "textDocument/rename")) {
|
|
||||||
// const params = root.Object.getValue("params").?.Object;
|
|
||||||
// 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.log.debug(.main, "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,
|
|
||||||
// };
|
|
||||||
// if (pos.character >= 0) {
|
|
||||||
// const new_name = params.getValue("newName").?.String;
|
|
||||||
// const pos_index = try handle.document.positionToIndex(pos);
|
|
||||||
// const pos_context = try analysis.documentPositionContext(allocator, handle.document, pos);
|
|
||||||
|
|
||||||
// const this_config = configFromUriOr(uri, config);
|
|
||||||
// switch (pos_context) {
|
|
||||||
// .var_access => try renameDefinitionGlobal(id, handle, pos_index, new_name),
|
|
||||||
// .field_access => |range| try renameDefinitionFieldAccess(id, handle, pos, range, new_name, this_config),
|
|
||||||
// .label => try renameDefinitionLabel(id, handle, pos_index, new_name),
|
|
||||||
// else => try respondGeneric(id, null_result_response),
|
|
||||||
// }
|
|
||||||
// } else {
|
|
||||||
// try respondGeneric(id, null_result_response);
|
|
||||||
// }
|
|
||||||
// } 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/rangeFormatting") or
|
|
||||||
// std.mem.eql(u8, method, "textDocument/onTypeFormatting") 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);
|
|
||||||
// } else if (root.Object.getValue("id")) |_| {
|
|
||||||
// std.log.debug(.main, "Method with return value not implemented: {}", .{method});
|
|
||||||
// try respondGeneric(id, not_implemented_response);
|
|
||||||
// } else {
|
|
||||||
// std.log.debug(.main, "Method without return value not implemented: {}", .{method});
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var debug_alloc_state: DebugAllocator = undefined;
|
var debug_alloc_state: DebugAllocator = undefined;
|
||||||
|
@ -186,3 +186,24 @@ const TextDocumentIdentifierRequest = struct {
|
|||||||
pub const SaveDocument = TextDocumentIdentifierRequest;
|
pub const SaveDocument = TextDocumentIdentifierRequest;
|
||||||
pub const CloseDocument = TextDocumentIdentifierRequest;
|
pub const CloseDocument = TextDocumentIdentifierRequest;
|
||||||
pub const SemanticTokens = TextDocumentIdentifierRequest;
|
pub const SemanticTokens = TextDocumentIdentifierRequest;
|
||||||
|
|
||||||
|
const TextDocumentIdentifierPositionRequest = struct {
|
||||||
|
params: struct {
|
||||||
|
textDocument: TextDocumentIdentifier,
|
||||||
|
position: types.Position,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const Completion = TextDocumentIdentifierPositionRequest;
|
||||||
|
pub const GotoDefinition = TextDocumentIdentifierPositionRequest;
|
||||||
|
pub const GotoDeclaration = TextDocumentIdentifierPositionRequest;
|
||||||
|
pub const Hover = TextDocumentIdentifierPositionRequest;
|
||||||
|
pub const DocumentSymbols = TextDocumentIdentifierRequest;
|
||||||
|
pub const Formatting = TextDocumentIdentifierRequest;
|
||||||
|
pub const Rename = struct {
|
||||||
|
params: struct {
|
||||||
|
textDocument: TextDocumentIdentifier,
|
||||||
|
position: types.Position,
|
||||||
|
newName: types.String,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
@ -18,11 +18,11 @@ pub const Object = json.ObjectMap;
|
|||||||
pub const DocumentUri = String;
|
pub const DocumentUri = String;
|
||||||
|
|
||||||
pub const Position = struct {
|
pub const Position = struct {
|
||||||
line: Integer, character: Integer
|
line: Integer, character: Integer,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const Range = struct {
|
pub const Range = struct {
|
||||||
start: Position, end: Position
|
start: Position, end: Position,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const Location = struct {
|
pub const Location = struct {
|
||||||
|
82
tests/sessions.zig
Normal file
82
tests/sessions.zig
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
const std = @import("std");
|
||||||
|
const headerPkg = @import("header");
|
||||||
|
|
||||||
|
const suffix = if (std.builtin.os.tag == .windows) ".exe" else "";
|
||||||
|
const allocator = std.heap.page_allocator;
|
||||||
|
|
||||||
|
const initialize_message = \\{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":6896,"clientInfo":{"name":"vscode","version":"1.46.1"},"rootPath":null,"rootUri":null,"capabilities":{"workspace":{"applyEdit":true,"workspaceEdit":{"documentChanges":true,"resourceOperations":["create","rename","delete"],"failureHandling":"textOnlyTransactional"},"didChangeConfiguration":{"dynamicRegistration":true},"didChangeWatchedFiles":{"dynamicRegistration":true},"symbol":{"dynamicRegistration":true,"symbolKind":{"valueSet":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26]},"tagSupport":{"valueSet":[1]}},"executeCommand":{"dynamicRegistration":true},"configuration":true,"workspaceFolders":true},"textDocument":{"publishDiagnostics":{"relatedInformation":true,"versionSupport":false,"tagSupport":{"valueSet":[1,2]},"complexDiagnosticCodeSupport":true},"synchronization":{"dynamicRegistration":true,"willSave":true,"willSaveWaitUntil":true,"didSave":true},"completion":{"dynamicRegistration":true,"contextSupport":true,"completionItem":{"snippetSupport":true,"commitCharactersSupport":true,"documentationFormat":["markdown","plaintext"],"deprecatedSupport":true,"preselectSupport":true,"tagSupport":{"valueSet":[1]},"insertReplaceSupport":true},"completionItemKind":{"valueSet":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]}},"hover":{"dynamicRegistration":true,"contentFormat":["markdown","plaintext"]},"signatureHelp":{"dynamicRegistration":true,"signatureInformation":{"documentationFormat":["markdown","plaintext"],"parameterInformation":{"labelOffsetSupport":true}},"contextSupport":true},"definition":{"dynamicRegistration":true,"linkSupport":true},"references":{"dynamicRegistration":true},"documentHighlight":{"dynamicRegistration":true},"documentSymbol":{"dynamicRegistration":true,"symbolKind":{"valueSet":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26]},"hierarchicalDocumentSymbolSupport":true,"tagSupport":{"valueSet":[1]}},"codeAction":{"dynamicRegistration":true,"isPreferredSupport":true,"codeActionLiteralSupport":{"codeActionKind":{"valueSet":["","quickfix","refactor","refactor.extract","refactor.inline","refactor.rewrite","source","source.organizeImports"]}}},"codeLens":{"dynamicRegistration":true},"formatting":{"dynamicRegistration":true},"rangeFormatting":{"dynamicRegistration":true},"onTypeFormatting":{"dynamicRegistration":true},"rename":{"dynamicRegistration":true,"prepareSupport":true},"documentLink":{"dynamicRegistration":true,"tooltipSupport":true},"typeDefinition":{"dynamicRegistration":true,"linkSupport":true},"implementation":{"dynamicRegistration":true,"linkSupport":true},"colorProvider":{"dynamicRegistration":true},"foldingRange":{"dynamicRegistration":true,"rangeLimit":5000,"lineFoldingOnly":true},"declaration":{"dynamicRegistration":true,"linkSupport":true},"selectionRange":{"dynamicRegistration":true},"semanticTokens":{"dynamicRegistration":true,"tokenTypes":["comment","keyword","number","regexp","operator","namespace","type","struct","class","interface","enum","typeParameter","function","member","macro","variable","parameter","property","label"],"tokenModifiers":["declaration","documentation","static","abstract","deprecated","readonly"]}},"window":{"workDoneProgress":true}},"trace":"off","workspaceFolders":null}}
|
||||||
|
;
|
||||||
|
|
||||||
|
const initialized_message = \\{"jsonrpc":"2.0","method":"initialized","params":{}}
|
||||||
|
;
|
||||||
|
|
||||||
|
const shutdown_message = \\{"jsonrpc":"2.0", "id":"STDWN", "method":"shutdown","params":{}}
|
||||||
|
;
|
||||||
|
|
||||||
|
fn sendRequest(req: []const u8, process: var) !void {
|
||||||
|
try process.stdin.?.writer().print("Content-Length: {}\r\n\r\n", .{req.len});
|
||||||
|
try process.stdin.?.writeAll(req);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn readResponses(process: var) !void {
|
||||||
|
while (true) {
|
||||||
|
const header = headerPkg.readRequestHeader(allocator, process.stdout.?.reader()) catch |err| {
|
||||||
|
switch(err) {
|
||||||
|
error.EndOfStream => break,
|
||||||
|
else => return err,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
defer header.deinit(allocator);
|
||||||
|
|
||||||
|
var stdout_mem = try allocator.alloc(u8, header.content_length);
|
||||||
|
defer allocator.free(stdout_mem);
|
||||||
|
|
||||||
|
const stdout_bytes = stdout_mem[0..try process.stdout.?.reader().readAll(stdout_mem)];
|
||||||
|
std.debug.print("GOT MESSAGE: {}\n", .{stdout_bytes});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
test "Open file, ask for semantic tokens" {
|
||||||
|
var process = try std.ChildProcess.init(&[_][]const u8{ "zig-cache/bin/zls" ++ suffix }, allocator);
|
||||||
|
defer process.deinit();
|
||||||
|
process.stdin_behavior = .Pipe;
|
||||||
|
process.stdout_behavior = .Pipe;
|
||||||
|
process.stderr_behavior = std.ChildProcess.StdIo.Inherit;
|
||||||
|
|
||||||
|
process.spawn() catch |err| {
|
||||||
|
std.log.debug(.main, "Failed to spawn zls process, error: {}\n", .{err});
|
||||||
|
return err;
|
||||||
|
};
|
||||||
|
|
||||||
|
try sendRequest(initialize_message, process);
|
||||||
|
try sendRequest(initialized_message, process);
|
||||||
|
|
||||||
|
const new_file_req = \\{"jsonrpc":"2.0","method":"textDocument/didOpen","params":{"textDocument":{"uri":"file:///test.zig","languageId":"zig","version":420,"text":"const std = @import(\"std\");"}}}
|
||||||
|
;
|
||||||
|
try sendRequest(new_file_req, process);
|
||||||
|
const sem_toks_req = \\{"jsonrpc":"2.0","id":2,"method":"textDocument/semanticTokens","params":{"textDocument":{"uri":"file:///test.zig"}}}
|
||||||
|
;
|
||||||
|
try sendRequest(sem_toks_req, process);
|
||||||
|
|
||||||
|
try sendRequest(shutdown_message, process);
|
||||||
|
|
||||||
|
process.stdin.?.close();
|
||||||
|
process.stdin = null;
|
||||||
|
|
||||||
|
try readResponses(process);
|
||||||
|
const result = try process.wait();
|
||||||
|
|
||||||
|
// const stderr_bytes = try process.stderr.?.reader().readAllAlloc(allocator, std.math.maxInt(usize));
|
||||||
|
// defer allocator.free(stderr_bytes);
|
||||||
|
// if (stderr_bytes.len != 0) {
|
||||||
|
// std.debug.print("Stderr output\n{}\n", .{stderr_bytes});
|
||||||
|
// }
|
||||||
|
|
||||||
|
switch (result) {
|
||||||
|
.Exited => |code| if (code == 0) {
|
||||||
|
return;
|
||||||
|
},
|
||||||
|
else => {},
|
||||||
|
}
|
||||||
|
return error.ShutdownWithError;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user