-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinline-values.ts
144 lines (130 loc) · 4.58 KB
/
inline-values.ts
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import * as vscode from 'vscode';
import { PipelineOutput } from '@eagleoutice/flowr/core/steps/pipeline/pipeline';
import { TREE_SITTER_DATAFLOW_PIPELINE, createDataflowPipeline } from '@eagleoutice/flowr/core/steps/pipeline/default-pipelines';
import { TreeSitterExecutor } from '@eagleoutice/flowr/r-bridge/lang-4.x/tree-sitter/tree-sitter-executor';
import { requestFromInput } from '@eagleoutice/flowr/r-bridge/retriever';
import { NodeId } from '@eagleoutice/flowr/r-bridge/lang-4.x/ast/model/processing/node-id';
import { VertexType } from '@eagleoutice/flowr/dataflow/graph/vertex';
import { resolve } from '@eagleoutice/flowr/dataflow/environments/resolve-by-name';
import { RLogicalValue } from '@eagleoutice/flowr/r-bridge/lang-4.x/ast/model/nodes/r-logical';
import { RNumberValue, RStringValue } from '@eagleoutice/flowr/r-bridge/lang-4.x/convert-values';
export function registerInlineHints(output: vscode.OutputChannel): vscode.Disposable {
return vscode.languages.registerInlayHintsProvider(
// only for r
{ scheme: 'file', language: 'r' },
new FlowrInlayHintsProvider(output)
)
}
class FlowrInlayHintsProvider implements vscode.InlayHintsProvider {
private readonly output: vscode.OutputChannel;
private readonly updateEvent = new vscode.EventEmitter<void>();
public onDidChangeInlayHints = this.updateEvent.event;
// TODO: work with the server as well
// TODO: merge infrastructure with dependency viewer?
private analysisInfo: PipelineOutput<typeof TREE_SITTER_DATAFLOW_PIPELINE> | undefined;
// TODO: on update event etc.
constructor(output: vscode.OutputChannel) {
this.output = output;
// TODO: register disposables
vscode.workspace.onDidChangeTextDocument(e => {
if(e.document.languageId === 'r') {
void this.update();
}
})
vscode.window.onDidChangeActiveTextEditor(e => {
if(e?.document.languageId === 'r') {
void this.update();
}
})
setTimeout(() => void this.update(), 50);
setTimeout(() => void this.update(), 250);
}
private lastEditorContent: string | undefined;
async update(): Promise<void> {
this.output.appendLine('Updating inlay hints');
const active = vscode.window.activeTextEditor;
if(!active) {
return;
}
const content = active.document.getText();
if(content.trim() === this.lastEditorContent) {
return;
}
this.lastEditorContent = content.trim();
this.analysisInfo = await createDataflowPipeline(new TreeSitterExecutor(), {
request: requestFromInput(content)
}).allRemainingSteps();
this.updateEvent.fire();
}
private collectAllVariables(): Set<NodeId> {
if(!this.analysisInfo) {
return new Set();
}
const variables = new Set<NodeId>();
for(const [v,info] of this.analysisInfo.dataflow.graph.vertices(true)) {
if(info.tag === VertexType.Use) {
variables.add(v);
}
}
return variables;
}
private getValuesForVariable(variable: NodeId): string[] {
if(!this.analysisInfo) {
return [];
}
const values = resolve(variable, { graph: this.analysisInfo.dataflow.graph, full: true, idMap: this.analysisInfo.normalize.idMap });
return values?.map(unwrapRValue).filter(isNotUndefined) ?? [];
}
provideInlayHints(document: vscode.TextDocument, range: vscode.Range, token: vscode.CancellationToken): vscode.ProviderResult<vscode.InlayHint[]> {
if(!this.analysisInfo) {
return [];
}
// TODO: respect hints
const variables = [...this.collectAllVariables()].map(v => [v, this.getValuesForVariable(v)] as const);
const results: vscode.InlayHint[] = [];
for(const [variable, values] of variables) {
if(values.length === 0) {
continue;
}
const loc = this.analysisInfo.normalize.idMap.get(variable);
if(!loc?.location) {
continue;
}
const vals = values.join(' | ');
results.push({
label: `: ${vals}`,
kind: vscode.InlayHintKind.Type,
position: new vscode.Position(loc.location[2], loc.location[3] - 1),
paddingLeft: true
})
}
return results;
}
}
// maybe take from flowR
function unwrapRValue(value: RLogicalValue | RStringValue | RNumberValue | string | number | unknown): string | undefined {
if(value === undefined) {
return undefined;
}
switch(typeof value) {
case 'string':
return value;
case 'number':
return value.toString();
case 'boolean':
return value ? 'TRUE' : 'FALSE';
}
if(typeof value !== 'object' || value === null) {
return JSON.stringify(value);
}
if('str' in value) {
return (value as RStringValue).str;
} else if('num' in value) {
return (value as RNumberValue).num.toString();
} else {
return JSON.stringify(value);
}
}
function isNotUndefined<T>(value: T | undefined): value is T {
return value !== undefined;
}