Skip to content

Commit c0748ad

Browse files
committed
Show section properties in Hover
Previously only variable defaults were rendered.
1 parent a077fee commit c0748ad

2 files changed

Lines changed: 30 additions & 17 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# 1.0.5
22

3+
## New Features
4+
5+
- The Hover now shows values of more references (previously only variable `default` were shown)
6+
37
## Fixes
48

59
- Correctly parse references to list and map variables as well as references in nested expressions (Closes #75)

src/hover.ts

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,37 +14,46 @@ export class HoverProvider implements vscode.HoverProvider {
1414
if (!section)
1515
return new vscode.Hover(new vscode.MarkdownString(`Unknown target \`${reference.targetId}\``), reference.location.range);
1616

17-
if (section.sectionType !== "variable")
18-
return null;
17+
let valuePath = reference.valuePath();
18+
if (section.sectionType === "variable") {
19+
// valuePath should actually be empty for variables
20+
valuePath = ["default"];
21+
} else {
22+
// we need an attribute to read and it cannot be a splat
23+
if (valuePath.length === 0 || valuePath[0] === "*") {
24+
return null;
25+
}
26+
}
1927

20-
let defaultValueNode = findValue(section.node, "default");
21-
if (!defaultValueNode)
22-
return new vscode.Hover("no default specified", reference.location.range);
28+
// for now only support single level value extraction
29+
let valueNode = findValue(section.node, valuePath[0]);
30+
if (!valueNode)
31+
return new vscode.Hover(`\`${valuePath[0]}\` not specified`, reference.location.range);
2332

24-
let defaultString = "";
33+
let formattedString = "";
2534

2635
// guess type (ignore type= key because it might be missing anyway)
27-
if (defaultValueNode.List && (defaultValueNode.List as AstList).Items) {
36+
if (valueNode.List && (valueNode.List as AstList).Items) {
2837
// map
29-
let map = getValue(defaultValueNode, { stripQuotes: true }) as Map<string, string>;
38+
let map = getValue(valueNode, { stripQuotes: true }) as Map<string, string>;
3039
let pairs = [...map.entries()].map((v) => v.map((i) => `\`${i}\``).join(' = ')).map((i) => ` - ${i}`);
3140
if (pairs.length === 0)
32-
defaultString = "default: *empty map*";
41+
formattedString = `${valuePath[0]}: *empty map*`;
3342
else
34-
defaultString = "default:\n" + pairs.join("\n");
35-
} else if (defaultValueNode.List) {
43+
formattedString = `${valuePath[0]}:\n` + pairs.join("\n");
44+
} else if (valueNode.List) {
3645
// list
37-
let list = getValue(defaultValueNode, { stripQuotes: true }) as string[];
46+
let list = getValue(valueNode, { stripQuotes: true }) as string[];
3847
if (list.length === 0)
39-
defaultString = "default: *empty list*";
48+
formattedString = `${valuePath[0]}: *empty list*`;
4049
else
41-
defaultString = "default:\n" + list.map((i, idx) => `${idx}. \`${i}\``).join("\n");
50+
formattedString = `${valuePath[0]}:\n` + list.map((i, idx) => `${idx}. \`${i}\``).join("\n");
4251
} else {
4352
// string
44-
defaultString = getStringValue(defaultValueNode, "<failed to extract value>", { stripQuotes: true });
45-
defaultString = `default: \`${defaultString}\``;
53+
formattedString = getStringValue(valueNode, "<failed to extract value>", { stripQuotes: true });
54+
formattedString = `${valuePath[0]}: \`${formattedString}\``;
4655
}
4756

48-
return new vscode.Hover(new vscode.MarkdownString(defaultString), reference.location.range);
57+
return new vscode.Hover(new vscode.MarkdownString(formattedString), reference.location.range);
4958
}
5059
}

0 commit comments

Comments
 (0)