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

Feature: hover for variables #309

Closed
Closed
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
2 changes: 2 additions & 0 deletions src/parser/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export interface Variable {
character: number;
method: Method;
isGlobal: boolean;
comment: string;
full: string;
}

export class Label {
Expand Down
40 changes: 39 additions & 1 deletion src/parser/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ export class Parser {
}
}
for (const filePath of this.documentCache.keys()) {
if (filePath === document.uri.path) {
continue;
}
for (const method of this.documentCache.get(filePath).methods) {
if (method.name.toLowerCase() === name) {
return method;
Expand Down Expand Up @@ -162,6 +165,9 @@ export class Parser {
}
}
for (const filePath of this.documentCache.keys()) {
if (filePath === document.uri.path) {
continue;
}
for (const label of this.documentCache.get(filePath).labels) {
if (label.name.toLowerCase() === name) {
return label;
Expand All @@ -170,6 +176,33 @@ export class Parser {
}
}

/**
* Returns the first variable whose name matches one in the global document cache.
* Searches the provided document before searching all other documents in no particular order.
*/
public static async getVariableByName(
kyklish marked this conversation as resolved.
Show resolved Hide resolved
document: vscode.TextDocument,
name: string,
) {
name = name.toLowerCase();
for (const variable of this.documentCache.get(document.uri.path)
.variables) {
if (variable.name.toLowerCase() === name) {
return variable;
}
}
for (const filePath of this.documentCache.keys()) {
if (filePath === document.uri.path) {
continue;
}
for (const variable of this.documentCache.get(filePath).variables) {
kyklish marked this conversation as resolved.
Show resolved Hide resolved
if (variable.name.toLowerCase() === name) {
return variable;
}
}
}
}

public static getAllRefByName(name: string): Ref[] {
const refs = [];
name = name.toLowerCase();
Expand Down Expand Up @@ -226,7 +259,8 @@ export class Parser {
document: vscode.TextDocument,
line: number,
): Variable | Variable[] {
const lineText = CodeUtil.purify(document.lineAt(line).text);
const original = document.lineAt(line).text;
const lineText = CodeUtil.purify(original);

const defMatch = lineText.match(Parser.varDefPattern);
if (defMatch) {
Expand All @@ -238,6 +272,8 @@ export class Parser {
method: null,
name: varName,
character: lineText.indexOf(varName),
comment: Parser.getRemarkByLine(document, line - 1),
full: original,
};
} else {
const vars = [];
Expand All @@ -260,6 +296,8 @@ export class Parser {
method: null,
name: varName,
character: lineText.indexOf(commandMatchAll[index][0]),
comment: Parser.getRemarkByLine(document, line - 1),
full: original,
});
}
return vars;
Expand Down
11 changes: 11 additions & 0 deletions src/providers/ahkHoverProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ export class AhkHoverProvider implements HoverProvider {
return new Hover(contents);
}

const variable = await Parser.getVariableByName(document, context.word);
if (variable) {
const contents = new MarkdownString('', true).appendCodeblock(
variable.full,
);
if (variable.comment) {
contents.appendText(variable.comment);
}
return new Hover(contents);
}

return null;
}

Expand Down