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

Enable hover for var @"foo-bar" #1320

Merged
merged 1 commit into from
Jul 16, 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
20 changes: 15 additions & 5 deletions src/analysis.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3242,20 +3242,29 @@ pub fn resolveExpressionTypeFromAncestors(
}

pub fn identifierFromPosition(pos_index: usize, handle: DocumentStore.Handle) []const u8 {
if (pos_index + 1 >= handle.text.len) return "";
const loc = identifierLocFromPosition(pos_index, &handle) orelse return "";
return offsets.locToSlice(handle.text, loc);
}

pub fn identifierLocFromPosition(pos_index: usize, handle: *const DocumentStore.Handle) ?std.zig.Token.Loc {
if (pos_index + 1 >= handle.text.len) return null;
var start_idx = pos_index;

while (start_idx > 0 and Analyser.isSymbolChar(handle.text[start_idx - 1])) {
start_idx -= 1;
}

const token_index = offsets.sourceIndexToTokenIndex(handle.tree, start_idx);
if (handle.tree.tokens.items(.tag)[token_index] == .identifier)
return offsets.tokenToLoc(handle.tree, token_index);

var end_idx = pos_index;
while (end_idx < handle.text.len and Analyser.isSymbolChar(handle.text[end_idx])) {
end_idx += 1;
}

if (end_idx <= start_idx) return "";
return handle.text[start_idx..end_idx];
if (end_idx <= start_idx) return null;
return .{ .start = start_idx, .end = end_idx };
}

pub fn getLabelGlobal(pos_index: usize, handle: *const DocumentStore.Handle) error{OutOfMemory}!?DeclWithHandle {
Expand Down Expand Up @@ -3309,10 +3318,11 @@ pub fn getSymbolFieldAccesses(
const tracy_zone = tracy.trace(@src());
defer tracy_zone.end();

const name = identifierFromPosition(source_index, handle.*);
const name_loc = identifierLocFromPosition(source_index, handle) orelse return null;
const name = offsets.locToSlice(handle.text, name_loc);
if (name.len == 0) return null;

const held_range = try arena.dupeZ(u8, offsets.locToSlice(handle.text, loc));
const held_range = try arena.dupeZ(u8, offsets.locToSlice(handle.text, offsets.locMerge(loc, name_loc)));
var tokenizer = std.zig.Tokenizer.init(held_range);

var decls_with_handles = std.ArrayListUnmanaged(DeclWithHandle){};
Expand Down
4 changes: 2 additions & 2 deletions src/features/completions.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1075,9 +1075,9 @@ fn resolveContainer(
/// If the identifier is a `fn_name`, `fn_arg_index` is the index of the fn's param
fn getIdentifierTokenIndexAndFnArgIndex(
tree: Ast,
dot_index: usize,
dot_index: Ast.TokenIndex,
fn_arg_index_out: *usize,
) ?usize {
) ?Ast.TokenIndex {
// at least 3 tokens should be present, `x{.`
if (dot_index < 2) return null;
const token_tags = tree.tokens.items(.tag);
Expand Down
4 changes: 2 additions & 2 deletions src/offsets.zig
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub fn positionToIndex(text: []const u8, position: types.Position, encoding: Enc
return line_start_index + line_byte_length;
}

pub fn sourceIndexToTokenIndex(tree: Ast, source_index: usize) usize {
pub fn sourceIndexToTokenIndex(tree: Ast, source_index: usize) Ast.TokenIndex {
std.debug.assert(source_index < tree.source.len);

const tokens_start = tree.tokens.items(.start);
Expand Down Expand Up @@ -111,7 +111,7 @@ pub fn sourceIndexToTokenIndex(tree: Ast, source_index: usize) usize {
break;
}

return upper_index;
return @intCast(upper_index);
}

pub fn tokenToIndex(tree: Ast, token_index: Ast.TokenIndex) usize {
Expand Down