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

wip: working on resolve values #200

Draft
wants to merge 1 commit 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
5 changes: 4 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import { 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 @@ -77,6 +78,8 @@
updateDependencyView();
}));

context.subscriptions.push(registerInlineHints(outputChannel))

Check failure on line 81 in src/extension.ts

View workflow job for this annotation

GitHub Actions / lint

Missing semicolon

Check failure on line 81 in src/extension.ts

View workflow job for this annotation

GitHub Actions / test

Missing semicolon

Check failure on line 81 in src/extension.ts

View workflow job for this annotation

GitHub Actions / lint

Missing semicolon

Check failure on line 81 in src/extension.ts

View workflow job for this annotation

GitHub Actions / test

Missing semicolon

context.subscriptions.push(new vscode.Disposable(() => disposeDep()));
process.on('SIGINT', () => destroySession());

Expand Down Expand Up @@ -195,7 +198,7 @@
const config = getConfig();
const wasmRoot = getWasmRootPath();
// we don't want to *amend* here since updates to our extension config shouldn't add additional entries while keeping old ones (definitions etc.)
setConfig(deepMergeObject(defaultConfigOptions, {
setConfig(deepMergeObject(defaultConfigOptions, {
ignoreSourceCalls: config.get<boolean>(Settings.IgnoreSourceCalls, false),
solver: {
variables: config.get<VariableResolve>(Settings.SolverVariableHandling, VariableResolve.Alias),
Expand Down
144 changes: 144 additions & 0 deletions src/flowr/views/inline-values.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import * as vscode from 'vscode';
import { PipelineOutput } from '@eagleoutice/flowr/core/steps/pipeline/pipeline';

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 { TREE_SITTER_DATAFLOW_PIPELINE, createDataflowPipeline } from '@eagleoutice/flowr/core/steps/pipeline/default-pipelines';

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

View workflow job for this annotation

GitHub Actions / lint

Imports "TREE_SITTER_DATAFLOW_PIPELINE" are only used as type

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

View workflow job for this annotation

GitHub Actions / test

Imports "TREE_SITTER_DATAFLOW_PIPELINE" are only used as type

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

View workflow job for this annotation

GitHub Actions / lint

Imports "TREE_SITTER_DATAFLOW_PIPELINE" are only used as type

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

View workflow job for this annotation

GitHub Actions / test

Imports "TREE_SITTER_DATAFLOW_PIPELINE" are only used as type
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';

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 { 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';

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`
import { RNumberValue, RStringValue } from '@eagleoutice/flowr/r-bridge/lang-4.x/convert-values';

Check failure on line 10 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 10 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 10 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 10 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 17 in src/flowr/views/inline-values.ts

View workflow job for this annotation

GitHub Actions / lint

Missing semicolon

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

View workflow job for this annotation

GitHub Actions / test

Missing semicolon

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

View workflow job for this annotation

GitHub Actions / lint

Missing semicolon

Check failure on line 17 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 25 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 25 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 25 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 25 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 26 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 26 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 26 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 26 in src/flowr/views/inline-values.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected 'todo' comment: 'TODO: merge infrastructure with...'
private analysisInfo: PipelineOutput<typeof TREE_SITTER_DATAFLOW_PIPELINE> | undefined;
// TODO: on update event etc.

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

View workflow job for this annotation

GitHub Actions / lint

Unexpected 'todo' comment: 'TODO: on update event etc.'

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

View workflow job for this annotation

GitHub Actions / test

Unexpected 'todo' comment: 'TODO: on update event etc.'

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

View workflow job for this annotation

GitHub Actions / lint

Unexpected 'todo' comment: 'TODO: on update event etc.'

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

View workflow job for this annotation

GitHub Actions / test

Unexpected 'todo' comment: '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;
}
Loading