-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathhoverProvider.ts
More file actions
76 lines (69 loc) · 2.7 KB
/
hoverProvider.ts
File metadata and controls
76 lines (69 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { Hover, HoverParams, MarkupKind, ASTNode, PropertyASTNode } from "../languageTypes";
import { GalaxyWorkflowLanguageServer } from "../server";
import { Provider } from "./provider";
import { ArrayASTNode, BooleanASTNode, NullASTNode, NumberASTNode, StringASTNode } from "vscode-json-languageservice";
export class HoverProvider extends Provider {
public static register(server: GalaxyWorkflowLanguageServer): HoverProvider {
return new HoverProvider(server);
}
constructor(server: GalaxyWorkflowLanguageServer) {
super(server);
this.connection.onHover((params) => this.onHoverShowParsingData(params));
}
/**
* This is a temporary Hover provider to see metadata about the JSON parsing.
* Will be replaced by a proper one at some point before the first release.
*/
private onHoverShowParsingData(params: HoverParams): Hover | undefined {
const workflowDocument = this.workflowDocuments.get(params.textDocument.uri);
if (!workflowDocument) {
return undefined;
}
const node = workflowDocument.getNodeAtPosition(params.position);
if (!node) {
return undefined;
}
const contentLines = this.printNode(node);
const hoverRange = workflowDocument.getNodeRange(node);
const markdown = {
kind: MarkupKind.Markdown,
value: contentLines.join("\n\n"),
};
const result: Hover = {
contents: markdown,
range: hoverRange,
};
return result;
}
private printNode(node: ASTNode): string[] {
const contentLines = [`## ${node.type}`];
if (node.type === "object") {
for (const property of node.properties) {
contentLines.push(this.printNodeProperty(property));
}
} else if (node.type === "array") {
contentLines.push(this.printArrayNode(node));
} else if (node.type === "property") {
contentLines.push(this.printNodeProperty(node));
} else {
contentLines.push(this.printNodeValue(node));
}
return contentLines;
}
private printNodeProperty(property: PropertyASTNode): string {
return `[${property.valueNode?.type}]\`${property.keyNode.value}\`: ${
property.valueNode?.value || "hover children for details"
}`;
}
private printArrayNode(node: ArrayASTNode): string {
const nodeStr = `${node.items.length} items (${node.offset}, ${node.offset + node.length})\n`;
const itemsStr: string[] = [];
node.items.forEach((item) => {
itemsStr.push(` - [${item.type}] (${item.offset}, ${item.offset + item.length})\n`);
});
return nodeStr.concat(...itemsStr);
}
private printNodeValue(node: StringASTNode | NumberASTNode | BooleanASTNode | NullASTNode): string {
return `\`${node.value}\`: (${node.offset}, ${node.offset + node.length})`;
}
}