Skip to content

Commit

Permalink
Show section properties in Hover
Browse files Browse the repository at this point in the history
Previously only variable defaults were rendered.
  • Loading branch information
mauve committed May 3, 2018
1 parent a077fee commit c0748ad
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# 1.0.5

## New Features

- The Hover now shows values of more references (previously only variable `default` were shown)

## Fixes

- Correctly parse references to list and map variables as well as references in nested expressions (Closes #75)
Expand Down
43 changes: 26 additions & 17 deletions src/hover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,46 @@ export class HoverProvider implements vscode.HoverProvider {
if (!section)
return new vscode.Hover(new vscode.MarkdownString(`Unknown target \`${reference.targetId}\``), reference.location.range);

if (section.sectionType !== "variable")
return null;
let valuePath = reference.valuePath();
if (section.sectionType === "variable") {
// valuePath should actually be empty for variables
valuePath = ["default"];
} else {
// we need an attribute to read and it cannot be a splat
if (valuePath.length === 0 || valuePath[0] === "*") {
return null;
}
}

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

let defaultString = "";
let formattedString = "";

// guess type (ignore type= key because it might be missing anyway)
if (defaultValueNode.List && (defaultValueNode.List as AstList).Items) {
if (valueNode.List && (valueNode.List as AstList).Items) {
// map
let map = getValue(defaultValueNode, { stripQuotes: true }) as Map<string, string>;
let map = getValue(valueNode, { stripQuotes: true }) as Map<string, string>;
let pairs = [...map.entries()].map((v) => v.map((i) => `\`${i}\``).join(' = ')).map((i) => ` - ${i}`);
if (pairs.length === 0)
defaultString = "default: *empty map*";
formattedString = `${valuePath[0]}: *empty map*`;
else
defaultString = "default:\n" + pairs.join("\n");
} else if (defaultValueNode.List) {
formattedString = `${valuePath[0]}:\n` + pairs.join("\n");
} else if (valueNode.List) {
// list
let list = getValue(defaultValueNode, { stripQuotes: true }) as string[];
let list = getValue(valueNode, { stripQuotes: true }) as string[];
if (list.length === 0)
defaultString = "default: *empty list*";
formattedString = `${valuePath[0]}: *empty list*`;
else
defaultString = "default:\n" + list.map((i, idx) => `${idx}. \`${i}\``).join("\n");
formattedString = `${valuePath[0]}:\n` + list.map((i, idx) => `${idx}. \`${i}\``).join("\n");
} else {
// string
defaultString = getStringValue(defaultValueNode, "<failed to extract value>", { stripQuotes: true });
defaultString = `default: \`${defaultString}\``;
formattedString = getStringValue(valueNode, "<failed to extract value>", { stripQuotes: true });
formattedString = `${valuePath[0]}: \`${formattedString}\``;
}

return new vscode.Hover(new vscode.MarkdownString(defaultString), reference.location.range);
return new vscode.Hover(new vscode.MarkdownString(formattedString), reference.location.range);
}
}

0 comments on commit c0748ad

Please sign in to comment.