Skip to content

wip: working on resolve values #200

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
10 changes: 10 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import { DropPathsOption, InferWorkingDirectory, VariableResolve , defaultConfigOptions, setConfig } from '@eagleoutice/flowr/config';
import type { BuiltInDefinitions } from '@eagleoutice/flowr/dataflow/environments/built-in-config';
import { deepMergeObject } from '@eagleoutice/flowr/util/objects';
import { registerInlineHints } from './flowr/views/inline-values';

export const MINIMUM_R_MAJOR = 3;
export const BEST_R_MAJOR = 4;
Expand Down Expand Up @@ -95,6 +96,15 @@

context.subscriptions.push(new vscode.Disposable(() => destroySession()));

const { dispose: disposeDep, update: updateDependencyView } = registerDependencyView(outputChannel);

context.subscriptions.push(vscode.commands.registerCommand('vscode-flowr.dependencyView.update', () => {
updateDependencyView();
}));

context.subscriptions.push(registerInlineHints(outputChannel))

Check failure on line 105 in src/extension.ts

View workflow job for this annotation

GitHub Actions / lint

Missing semicolon

Check failure on line 105 in src/extension.ts

View workflow job for this annotation

GitHub Actions / test

Missing semicolon

Check failure on line 105 in src/extension.ts

View workflow job for this annotation

GitHub Actions / lint

Missing semicolon

Check failure on line 105 in src/extension.ts

View workflow job for this annotation

GitHub Actions / test

Missing semicolon

context.subscriptions.push(new vscode.Disposable(() => disposeDep()));
setTimeout(() => {
const { dispose: disposeDep, update: updateDependencyView } = registerDependencyView(outputChannel);
context.subscriptions.push(vscode.commands.registerCommand('vscode-flowr.dependencyView.update', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/flowr/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
retrieveDataflowMermaid: (document: vscode.TextDocument, simplified?: boolean) => Promise<string>
retrieveAstMermaid: (document: vscode.TextDocument) => Promise<string>
retrieveCfgMermaid: (document: vscode.TextDocument) => Promise<string>
retrieveQuery: <T extends SupportedQueryTypes>(document: vscode.TextDocument, query: Queries<T>) => Promise<{ result: QueryResults<T>, hasError: boolean, dfg?: DataflowGraph, ast?: NormalizedAst }>
retrieveQuery: <T extends SupportedQueryTypes>(document: vscode.TextDocument, query: Queries<T>) => Promise<{ result: QueryResults<T>, hasError: boolean, dfg?: DataflowGraph, ast?: NormalizedAst }>

Check failure on line 30 in src/flowr/utils.ts

View workflow job for this annotation

GitHub Actions / lint

Extra space before value for key 'retrieveQuery'

Check failure on line 30 in src/flowr/utils.ts

View workflow job for this annotation

GitHub Actions / test

Extra space before value for key 'retrieveQuery'

Check failure on line 30 in src/flowr/utils.ts

View workflow job for this annotation

GitHub Actions / lint

Extra space before value for key 'retrieveQuery'

Check failure on line 30 in src/flowr/utils.ts

View workflow job for this annotation

GitHub Actions / test

Extra space before value for key 'retrieveQuery'
runRepl: (output: Omit<FlowrReplOptions, 'parser'>) => Promise<void>
}

Expand Down
148 changes: 148 additions & 0 deletions src/flowr/views/inline-values.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import * as vscode from 'vscode';
import { NodeId } from '@eagleoutice/flowr/r-bridge/lang-4.x/ast/model/processing/node-id';

Check failure on line 2 in src/flowr/views/inline-values.ts

View workflow job for this annotation

GitHub Actions / lint

All imports in the declaration are only used as types. Use `import type`

Check failure on line 2 in src/flowr/views/inline-values.ts

View workflow job for this annotation

GitHub Actions / test

All imports in the declaration are only used as types. Use `import type`

Check failure on line 2 in src/flowr/views/inline-values.ts

View workflow job for this annotation

GitHub Actions / lint

All imports in the declaration are only used as types. Use `import type`

Check failure on line 2 in src/flowr/views/inline-values.ts

View workflow job for this annotation

GitHub Actions / test

All imports in the declaration are only used as types. Use `import type`
import { VertexType } from '@eagleoutice/flowr/dataflow/graph/vertex';
import { resolveIdToValue } from '@eagleoutice/flowr/dataflow/environments/resolve-by-name';
import { RLogicalValue } from '@eagleoutice/flowr/r-bridge/lang-4.x/ast/model/nodes/r-logical';

Check failure on line 5 in src/flowr/views/inline-values.ts

View workflow job for this annotation

GitHub Actions / lint

All imports in the declaration are only used as types. Use `import type`

Check failure on line 5 in src/flowr/views/inline-values.ts

View workflow job for this annotation

GitHub Actions / test

All imports in the declaration are only used as types. Use `import type`

Check failure on line 5 in src/flowr/views/inline-values.ts

View workflow job for this annotation

GitHub Actions / lint

All imports in the declaration are only used as types. Use `import type`

Check failure on line 5 in src/flowr/views/inline-values.ts

View workflow job for this annotation

GitHub Actions / test

All imports in the declaration are only used as types. Use `import type`
import { RNumberValue, RStringValue } from '@eagleoutice/flowr/r-bridge/lang-4.x/convert-values';

Check failure on line 6 in src/flowr/views/inline-values.ts

View workflow job for this annotation

GitHub Actions / lint

All imports in the declaration are only used as types. Use `import type`

Check failure on line 6 in src/flowr/views/inline-values.ts

View workflow job for this annotation

GitHub Actions / test

All imports in the declaration are only used as types. Use `import type`

Check failure on line 6 in src/flowr/views/inline-values.ts

View workflow job for this annotation

GitHub Actions / lint

All imports in the declaration are only used as types. Use `import type`

Check failure on line 6 in src/flowr/views/inline-values.ts

View workflow job for this annotation

GitHub Actions / test

All imports in the declaration are only used as types. Use `import type`
import { getFlowrSession } from '../../extension';
import { DataflowGraph } from '@eagleoutice/flowr/dataflow/graph/graph';

Check failure on line 8 in src/flowr/views/inline-values.ts

View workflow job for this annotation

GitHub Actions / lint

All imports in the declaration are only used as types. Use `import type`

Check failure on line 8 in src/flowr/views/inline-values.ts

View workflow job for this annotation

GitHub Actions / test

All imports in the declaration are only used as types. Use `import type`

Check failure on line 8 in src/flowr/views/inline-values.ts

View workflow job for this annotation

GitHub Actions / lint

All imports in the declaration are only used as types. Use `import type`

Check failure on line 8 in src/flowr/views/inline-values.ts

View workflow job for this annotation

GitHub Actions / test

All imports in the declaration are only used as types. Use `import type`
import { NormalizedAst } from '@eagleoutice/flowr/r-bridge/lang-4.x/ast/model/processing/decorate';

Check failure on line 9 in src/flowr/views/inline-values.ts

View workflow job for this annotation

GitHub Actions / lint

All imports in the declaration are only used as types. Use `import type`

Check failure on line 9 in src/flowr/views/inline-values.ts

View workflow job for this annotation

GitHub Actions / test

All imports in the declaration are only used as types. Use `import type`

Check failure on line 9 in src/flowr/views/inline-values.ts

View workflow job for this annotation

GitHub Actions / lint

All imports in the declaration are only used as types. Use `import type`

Check failure on line 9 in src/flowr/views/inline-values.ts

View workflow job for this annotation

GitHub Actions / test

All imports in the declaration are only used as types. Use `import type`

export function registerInlineHints(output: vscode.OutputChannel): vscode.Disposable {
return vscode.languages.registerInlayHintsProvider(
// only for r
{ scheme: 'file', language: 'r' },
new FlowrInlayHintsProvider(output)
)

Check failure on line 16 in src/flowr/views/inline-values.ts

View workflow job for this annotation

GitHub Actions / lint

Missing semicolon

Check failure on line 16 in src/flowr/views/inline-values.ts

View workflow job for this annotation

GitHub Actions / test

Missing semicolon

Check failure on line 16 in src/flowr/views/inline-values.ts

View workflow job for this annotation

GitHub Actions / lint

Missing semicolon

Check failure on line 16 in src/flowr/views/inline-values.ts

View workflow job for this annotation

GitHub Actions / test

Missing semicolon
}

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

Check failure on line 24 in src/flowr/views/inline-values.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected 'todo' comment: 'TODO: work with the server as well'

Check failure on line 24 in src/flowr/views/inline-values.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected 'todo' comment: 'TODO: work with the server as well'

Check failure on line 24 in src/flowr/views/inline-values.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected 'todo' comment: 'TODO: work with the server as well'

Check failure on line 24 in src/flowr/views/inline-values.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected 'todo' comment: 'TODO: work with the server as well'
// TODO: merge infrastructure with dependency viewer?

Check failure on line 25 in src/flowr/views/inline-values.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected 'todo' comment: 'TODO: merge infrastructure with...'

Check failure on line 25 in src/flowr/views/inline-values.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected 'todo' comment: 'TODO: merge infrastructure with...'

Check failure on line 25 in src/flowr/views/inline-values.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected 'todo' comment: 'TODO: merge infrastructure with...'

Check failure on line 25 in src/flowr/views/inline-values.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected 'todo' comment: 'TODO: merge infrastructure with...'
private graphInfo: DataflowGraph | undefined;
private normalizeInfo: NormalizedAst | 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> {
const active = vscode.window.activeTextEditor;
if(!active) {
return;
}
const content = active.document.getText();
if(content.trim() === this.lastEditorContent) {
return;
}
this.lastEditorContent = content.trim();


const session = await getFlowrSession();

const res = (await session.retrieveQuery(active.document, [{ type: 'dataflow' }, { type: 'normalized-ast' }]));
this.graphInfo = res.result.dataflow.graph;
this.normalizeInfo = res.result['normalized-ast'].normalized;

this.updateEvent.fire();
}

private collectAllVariables(): Set<NodeId> {
if(!this.graphInfo) {
return new Set();
}
const variables = new Set<NodeId>();
for(const [v,info] of this.graphInfo.vertices(true)) {
if(info.tag === VertexType.Use) {
variables.add(v);
}
}
return variables;
}

private getValuesForVariable(variable: NodeId): string[] {
if(!this.graphInfo || !this.normalizeInfo) {
return [];
}
const values = resolveIdToValue(variable, { graph: this.graphInfo, full: true, idMap: this.normalizeInfo.idMap });
return values?.map(unwrapRValue).filter(isNotUndefined) ?? [];
}

provideInlayHints(document: vscode.TextDocument, range: vscode.Range, token: vscode.CancellationToken): vscode.ProviderResult<vscode.InlayHint[]> {
if(!this.graphInfo || !this.normalizeInfo) {
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.normalizeInfo.idMap?.get(variable);
if(!loc?.location) {
continue;
}
const vals = values.join(' | ');
const position = new vscode.Position(loc.location[0] - 1, loc.location[1]);
results.push({
label: `: ${vals}`,
tooltip: 'Values: ' + vals,
kind: vscode.InlayHintKind.Parameter,
position,
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;
}