Skip to content

Commit

Permalink
don't show value tooltips on shadowed variables
Browse files Browse the repository at this point in the history
  • Loading branch information
berekuk committed Jan 18, 2024
1 parent 475b08a commit 2c5b2e1
Showing 1 changed file with 30 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,10 @@ function buildWordHoverExtension({
};
};

const createTopLevelVariableNameTooltip = (node: SyntaxNode) => {
const name = getText(node);

const bindings = project.getBindings(sourceId);
if (!bindings.ok) return null;

const value = bindings.value.get(name);
if (!value) return null;

const createTopLevelVariableNameTooltip = (
node: SyntaxNode,
value: SqValue
) => {
return {
pos: node.from,
end: node.to,
Expand Down Expand Up @@ -156,8 +151,32 @@ function buildWordHoverExtension({
while (cursor.type.is("Statement") && cursor.parent());

// Is this a top-level variable?
if (cursor.type.is("Program")) {
return createTopLevelVariableNameTooltip(node);
if (!cursor.type.is("Program")) {
return null;
}

const name = getText(node);

const bindings = project.getBindings(sourceId);
if (!bindings.ok) return null;

const value = bindings.value.get(name);
if (!value) return null;

// Should be LetStatement or DefunStatement
const valueAst = value.context?.valueAst;

if (
valueAst &&
(valueAst.type === "LetStatement" ||
valueAst.type === "DefunStatement") &&
// If these don't match then variable was probably shadowed by a later statement and we can't show its value.
// Or it could be caused by code rot, if we change the logic of how `valueAst` is computed, or add another statement type in AST.
// TODO - if we can prove that the variable was shadowed, show the tooltip pointing to the latest assignment.
valueAst.variable.location.start.offset === node.from &&
valueAst.variable.location.end.offset === node.to
) {
return createTopLevelVariableNameTooltip(node, value);
}
}
}
Expand Down

0 comments on commit 2c5b2e1

Please sign in to comment.