Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing nodes in outline #990

Merged
merged 2 commits into from
Feb 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/analysis.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1851,6 +1851,7 @@ fn addOutlineNodes(allocator: std.mem.Allocator, tree: Ast, child: Ast.Node.Inde
.error_set_decl,
=> return,
.container_decl,
.container_decl_trailing,
SuperAuguste marked this conversation as resolved.
Show resolved Hide resolved
.container_decl_arg,
.container_decl_arg_trailing,
.container_decl_two,
Expand Down
2 changes: 1 addition & 1 deletion src/debug.zig
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub fn printDocumentScope(doc_scope: analysis.DocumentScope) void {
if (!std.debug.runtime_safety) @compileError("this function should only be used in debug mode!");

var index: usize = 0;
while(index < doc_scope.scopes.len) : (index += 1) {
while (index < doc_scope.scopes.len) : (index += 1) {
const scope = doc_scope.scopes.get(index);
if (index != 0) std.debug.print("\n\n", .{});
std.debug.print(
Expand Down
2 changes: 1 addition & 1 deletion src/semantic_tokens.zig
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ fn writeNodeTokens(builder: *Builder, maybe_node: ?Ast.Node.Index) error{OutOfMe
// Maybe we can hook into it insead? Also applies to Identifier and VarDecl
var bound_type_params = analysis.BoundTypeParams{};
defer bound_type_params.deinit(builder.store.allocator);

const lhs_type = try analysis.resolveFieldAccessLhsType(
builder.store,
(try analysis.resolveTypeOfNodeInternal(
Expand Down
11 changes: 9 additions & 2 deletions tests/context.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const std = @import("std");
const zls = @import("zls");
const builtin = @import("builtin");

const tres = @import("tres");

Expand All @@ -8,7 +9,6 @@ const Config = zls.Config;
const Server = zls.Server;
const types = zls.types;


/// initialize request taken from Visual Studio Code with the following changes:
/// - removed locale, rootPath, rootUri, trace, workspaceFolders
/// - removed capabilities.workspace.configuration
Expand Down Expand Up @@ -149,7 +149,12 @@ pub const Context = struct {
}

// helper
pub fn requestDidOpen(self: *Context, uri: []const u8, source: []const u8) !void {
pub fn addDocument(self: *Context, source: []const u8) ![]const u8 {
const uri: []const u8 = switch (builtin.os.tag) {
.windows => "file:///C:\\test.zig",
else => "file:///test.zig",
};

const open_document = types.DidOpenTextDocumentParams{
.textDocument = .{
.uri = uri,
Expand All @@ -160,7 +165,9 @@ pub const Context = struct {
};
const params = try std.json.stringifyAlloc(allocator, open_document, .{});
defer allocator.free(params);

try self.notification("textDocument/didOpen", params);
return uri;
}

pub fn Response(comptime Result: type) type {
Expand Down
7 changes: 1 addition & 6 deletions tests/lsp_features/completion.zig
Original file line number Diff line number Diff line change
Expand Up @@ -400,12 +400,7 @@ fn testCompletion(source: []const u8, expected_completions: []const Completion)
var ctx = try Context.init();
defer ctx.deinit();

const test_uri: []const u8 = switch (builtin.os.tag) {
.windows => "file:///C:\\test.zig",
else => "file:///test.zig",
};

try ctx.requestDidOpen(test_uri, text);
const test_uri = try ctx.addDocument(text);

const params = types.CompletionParams{
.textDocument = .{ .uri = test_uri },
Expand Down
7 changes: 1 addition & 6 deletions tests/lsp_features/definition.zig
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,7 @@ fn testDefinition(source: []const u8) !void {
var ctx = try Context.init();
defer ctx.deinit();

const test_uri: []const u8 = switch (builtin.os.tag) {
.windows => "file:///C:\\test.zig",
else => "file:///test.zig",
};

try ctx.requestDidOpen(test_uri, phr.new_source);
const test_uri = try ctx.addDocument(phr.new_source);

const params = types.TextDocumentPositionParams{
.textDocument = .{ .uri = test_uri },
Expand Down
10 changes: 4 additions & 6 deletions tests/lsp_features/document_symbol.zig
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,17 @@ test "documentSymbol - nested struct with self" {
\\};
,
\\Variable Foo
\\ Variable Self
\\ Function foo
\\ Variable Bar
);
}

fn testDocumentSymbol(source: []const u8, want: []const u8) !void {
var ctx = try Context.init();
defer ctx.deinit();

const test_uri: []const u8 = switch (builtin.os.tag) {
.windows => "file:///C:\\test.zig",
else => "file:///test.zig",
};

try ctx.requestDidOpen(test_uri, source);
const test_uri = try ctx.addDocument(source);

const params = types.DocumentSymbolParams{
.textDocument = .{ .uri = test_uri },
Expand Down
9 changes: 2 additions & 7 deletions tests/lsp_features/folding_range.zig
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ test "foldingRange - call" {
\\extern fn foo(a: bool, b: ?usize) void;
\\const result = foo(
\\ false,
\\ null,
\\ null,
\\);
, &.{
.{ .startLine = 1, .startCharacter = 19, .endLine = 4, .endCharacter = 0 },
Expand All @@ -205,12 +205,7 @@ fn testFoldingRange(source: []const u8, expect: []const types.FoldingRange) !voi
var ctx = try Context.init();
defer ctx.deinit();

const test_uri: []const u8 = switch (builtin.os.tag) {
.windows => "file:///C:\\test.zig",
else => "file:///test.zig",
};

try ctx.requestDidOpen(test_uri, source);
const test_uri = try ctx.addDocument(source);

const params = types.FoldingRangeParams{ .textDocument = .{ .uri = test_uri } };

Expand Down
13 changes: 4 additions & 9 deletions tests/lsp_features/inlay_hints.zig
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,7 @@ fn testInlayHints(source: []const u8) !void {
var ctx = try Context.init();
defer ctx.deinit();

const test_uri: []const u8 = switch (builtin.os.tag) {
.windows => "file:///C:\\test.zig",
else => "file:///test.zig",
};

try ctx.requestDidOpen(test_uri, phr.new_source);
const test_uri = try ctx.addDocument(phr.new_source);

const range = types.Range{
.start = types.Position{ .line = 0, .character = 0 },
Expand Down Expand Up @@ -120,10 +115,10 @@ fn testInlayHints(source: []const u8) !void {
for (hints) |hint| {
if (position.line != hint.position.line or position.character != hint.position.character) continue;

if(!std.mem.endsWith(u8, hint.label, ":")) {
try error_builder.msgAtLoc("label `{s}` must end with a colon!", new_loc, .err, .{ hint.label });
if (!std.mem.endsWith(u8, hint.label, ":")) {
try error_builder.msgAtLoc("label `{s}` must end with a colon!", new_loc, .err, .{hint.label});
}
const actual_label = hint.label[0..hint.label.len - 1];
const actual_label = hint.label[0 .. hint.label.len - 1];

if (!std.mem.eql(u8, expected_label, actual_label)) {
try error_builder.msgAtLoc("expected label `{s}` here but got `{s}`!", new_loc, .err, .{ expected_label, actual_label });
Expand Down
6 changes: 1 addition & 5 deletions tests/lsp_features/references.zig
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,6 @@ test "references - label" {
}

fn testReferences(source: []const u8) !void {
const file_uri: []const u8 = switch (builtin.os.tag) {
.windows => "file:///C:\\test.zig",
else => "file:///test.zig",
};
const new_name = "placeholder";

var phr = try helper.collectReplacePlaceholders(allocator, source, new_name);
Expand All @@ -127,7 +123,7 @@ fn testReferences(source: []const u8) !void {
var ctx = try Context.init();
defer ctx.deinit();

try ctx.requestDidOpen(file_uri, phr.new_source);
const file_uri = try ctx.addDocument(phr.new_source);

try std.testing.expect(phr.locations.len != 0);

Expand Down
7 changes: 1 addition & 6 deletions tests/lsp_features/selection_range.zig
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,7 @@ fn testSelectionRange(source: []const u8, want: []const []const u8) !void {
var ctx = try Context.init();
defer ctx.deinit();

const test_uri: []const u8 = switch (builtin.os.tag) {
.windows => "file:///C:\\test.zig",
else => "file:///test.zig",
};

try ctx.requestDidOpen(test_uri, phr.new_source);
const test_uri = try ctx.addDocument(phr.new_source);

const position = offsets.locToRange(phr.new_source, phr.locations.items(.new)[0], .@"utf-16").start;

Expand Down
12 changes: 5 additions & 7 deletions tests/lsp_features/semantic_tokens.zig
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,11 @@ test "semantic tokens - string literals" {
);
}

const file_uri = switch (builtin.os.tag) {
.windows => "file:///C:/test.zig",
else => "file:///test.zig",
};

fn testSemanticTokens(source: []const u8, expected: []const u32) !void {
var ctx = try Context.init();
defer ctx.deinit();

try ctx.requestDidOpen(file_uri, source);
const file_uri = try ctx.addDocument(source);

const Response = struct {
data: []const u32,
Expand All @@ -62,9 +57,12 @@ fn testSemanticTokens(source: []const u8, expected: []const u32) !void {
const expected_bytes = try std.json.stringifyAlloc(allocator, Response{ .data = expected }, .{});
defer allocator.free(expected_bytes);

const params = try std.json.stringifyAlloc(allocator, .{ .textDocument = .{ .uri = file_uri } }, .{});
defer allocator.free(params);

try ctx.request(
"textDocument/semanticTokens/full",
"{\"textDocument\":{\"uri\":\"" ++ file_uri ++ "\"}}",
params,
expected_bytes,
);
}