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

optimize folding range #926

Merged
merged 1 commit into from
Jan 21, 2023
Merged
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
62 changes: 29 additions & 33 deletions src/Server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2508,6 +2508,9 @@ fn inlayHintHandler(server: *Server, request: types.InlayHintParams) Error!?[]ty
}

fn codeActionHandler(server: *Server, request: types.CodeActionParams) Error!?[]types.CodeAction {
const tracy_zone = tracy.trace(@src());
defer tracy_zone.end();

const handle = server.document_store.getHandle(request.textDocument.uri) orelse return null;

var builder = code_actions.Builder{
Expand Down Expand Up @@ -2536,44 +2539,36 @@ fn codeActionHandler(server: *Server, request: types.CodeActionParams) Error!?[]
}

fn foldingRangeHandler(server: *Server, request: types.FoldingRangeParams) Error!?[]types.FoldingRange {
const tracy_zone = tracy.trace(@src());
defer tracy_zone.end();

const Token = std.zig.Token;
const Node = Ast.Node;
const allocator = server.arena.allocator();
const handle = server.document_store.getHandle(request.textDocument.uri) orelse return null;

const helper = struct {
const Inclusivity = enum { inclusive, exclusive };
/// Returns true if added.
fn maybeAddTokRange(
p_ranges: *std.ArrayList(types.FoldingRange),
tree: Ast,
start: Ast.TokenIndex,
end: Ast.TokenIndex,
end_reach: Inclusivity,
encoding: offsets.Encoding,
) std.mem.Allocator.Error!bool {
const can_add = start < end and !tree.tokensOnSameLine(start, end);
if (can_add) {
try addTokRange(p_ranges, tree, start, end, end_reach, encoding);
}
return can_add;
}

fn addTokRange(
p_ranges: *std.ArrayList(types.FoldingRange),
tree: Ast,
start: Ast.TokenIndex,
end: Ast.TokenIndex,
end_reach: Inclusivity,
encoding: offsets.Encoding,
) std.mem.Allocator.Error!void {
std.debug.assert(!std.debug.runtime_safety or !tree.tokensOnSameLine(start, end));
if (tree.tokensOnSameLine(start, end)) return;
std.debug.assert(start <= end);

const start_index = offsets.tokenToIndex(tree, start);
const end_index = offsets.tokenToIndex(tree, end);

const start_line = offsets.tokenToPosition(tree, start, encoding).line;
const end_line = offsets.tokenToPosition(tree, end, encoding).line;
const start_line = std.mem.count(u8, tree.source[0..start_index], "\n");
const end_line = start_line + std.mem.count(u8, tree.source[start_index..end_index], "\n");

try p_ranges.append(.{
.startLine = start_line,
.endLine = end_line - @boolToInt(end_reach == .exclusive),
.startLine = @intCast(u32, start_line),
.endLine = @intCast(u32, end_line) - @boolToInt(end_reach == .exclusive),
});
}
};
Expand Down Expand Up @@ -2604,21 +2599,22 @@ fn foldingRangeHandler(server: *Server, request: types.FoldingRangeParams) Error
const node = @intCast(Node.Index, i);

switch (node_tag) {
.root => continue,
// only fold the expression pertaining to the if statement, and the else statement, each respectively.
// TODO: Should folding multiline condition expressions also be supported? Ditto for the other control flow structures.
.@"if", .if_simple => {
const if_full = ast.ifFull(handle.tree, node);

const start_tok_1 = ast.lastToken(handle.tree, if_full.ast.cond_expr);
const end_tok_1 = ast.lastToken(handle.tree, if_full.ast.then_expr);
_ = try helper.maybeAddTokRange(&ranges, handle.tree, start_tok_1, end_tok_1, .inclusive, server.offset_encoding);
try helper.addTokRange(&ranges, handle.tree, start_tok_1, end_tok_1, .inclusive);

if (if_full.ast.else_expr == 0) continue;

const start_tok_2 = if_full.else_token;
const end_tok_2 = ast.lastToken(handle.tree, if_full.ast.else_expr);

_ = try helper.maybeAddTokRange(&ranges, handle.tree, start_tok_2, end_tok_2, .inclusive, server.offset_encoding);
try helper.addTokRange(&ranges, handle.tree, start_tok_2, end_tok_2, .inclusive);
},

// same as if/else
Expand All @@ -2632,13 +2628,13 @@ fn foldingRangeHandler(server: *Server, request: types.FoldingRangeParams) Error

const start_tok_1 = ast.lastToken(handle.tree, loop_full.ast.cond_expr);
const end_tok_1 = ast.lastToken(handle.tree, loop_full.ast.then_expr);
_ = try helper.maybeAddTokRange(&ranges, handle.tree, start_tok_1, end_tok_1, .inclusive, server.offset_encoding);
try helper.addTokRange(&ranges, handle.tree, start_tok_1, end_tok_1, .inclusive);

if (loop_full.ast.else_expr == 0) continue;

const start_tok_2 = loop_full.else_token;
const end_tok_2 = ast.lastToken(handle.tree, loop_full.ast.else_expr);
_ = try helper.maybeAddTokRange(&ranges, handle.tree, start_tok_2, end_tok_2, .inclusive, server.offset_encoding);
try helper.addTokRange(&ranges, handle.tree, start_tok_2, end_tok_2, .inclusive);
},

.global_var_decl,
Expand Down Expand Up @@ -2666,7 +2662,7 @@ fn foldingRangeHandler(server: *Server, request: types.FoldingRangeParams) Error
start_doc_tok -= 1;
}

_ = try helper.maybeAddTokRange(&ranges, handle.tree, start_doc_tok, end_doc_tok, .inclusive, server.offset_encoding);
try helper.addTokRange(&ranges, handle.tree, start_doc_tok, end_doc_tok, .inclusive);
}

// Function prototype folding regions
Expand All @@ -2678,10 +2674,7 @@ fn foldingRangeHandler(server: *Server, request: types.FoldingRangeParams) Error
const list_end_tok: Ast.TokenIndex = ast.lastToken(handle.tree, fn_proto.ast.proto_node);

if (handle.tree.tokensOnSameLine(list_start_tok, list_end_tok)) break :decl_node_blk;
try ranges.ensureUnusedCapacity(1 + fn_proto.ast.params.len); // best guess, doesn't include anytype params
helper.addTokRange(&ranges, handle.tree, list_start_tok, list_end_tok, .exclusive, server.offset_encoding) catch |err| switch (err) {
error.OutOfMemory => unreachable,
};
try helper.addTokRange(&ranges, handle.tree, list_start_tok, list_end_tok, .exclusive);

var it = fn_proto.iterate(&handle.tree);
while (ast.nextFnParam(&it)) |param| {
Expand All @@ -2691,7 +2684,7 @@ fn foldingRangeHandler(server: *Server, request: types.FoldingRangeParams) Error
while (token_tags[doc_end_tok + 1] == .doc_comment)
doc_end_tok += 1;

_ = try helper.maybeAddTokRange(&ranges, handle.tree, doc_start_tok, doc_end_tok, .inclusive, server.offset_encoding);
try helper.addTokRange(&ranges, handle.tree, doc_start_tok, doc_end_tok, .inclusive);
}
},

Expand All @@ -2703,7 +2696,7 @@ fn foldingRangeHandler(server: *Server, request: types.FoldingRangeParams) Error
=> {
const start_tok = handle.tree.firstToken(node);
const end_tok = ast.lastToken(handle.tree, node);
_ = try helper.maybeAddTokRange(&ranges, handle.tree, start_tok, end_tok, .inclusive, server.offset_encoding);
try helper.addTokRange(&ranges, handle.tree, start_tok, end_tok, .inclusive);
},

// most other trivial cases can go through here.
Expand Down Expand Up @@ -2750,7 +2743,7 @@ fn foldingRangeHandler(server: *Server, request: types.FoldingRangeParams) Error

const start_tok = handle.tree.firstToken(node);
const end_tok = ast.lastToken(handle.tree, node);
_ = try helper.maybeAddTokRange(&ranges, handle.tree, start_tok, end_tok, .exclusive, server.offset_encoding);
try helper.addTokRange(&ranges, handle.tree, start_tok, end_tok, .exclusive);
},
}
}
Expand Down Expand Up @@ -2799,6 +2792,9 @@ pub const SelectionRange = struct {
};

fn selectionRangeHandler(server: *Server, request: types.SelectionRangeParams) Error!?[]*SelectionRange {
const tracy_zone = tracy.trace(@src());
defer tracy_zone.end();

const allocator = server.arena.allocator();
const handle = server.document_store.getHandle(request.textDocument.uri) orelse return null;

Expand Down