Skip to content

Commit

Permalink
Enable hover for var @"foo-bar" (#1320)
Browse files Browse the repository at this point in the history
  • Loading branch information
FnControlOption authored Jul 16, 2023
1 parent 2ad3823 commit 8da21e1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
20 changes: 15 additions & 5 deletions src/analysis.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3287,20 +3287,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 @@ -3354,10 +3363,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 @@ -1103,9 +1103,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

0 comments on commit 8da21e1

Please sign in to comment.