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] Use inlay hints to render captures #34

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
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
76 changes: 68 additions & 8 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,51 @@ class EffektRunCodeLensProvider implements vscode.CodeLensProvider {
}
}

let outputChannel = vscode.window.createOutputChannel("Effekt DEBUG");

function log(message: string) {
outputChannel.appendLine(message);
}
class EffektCapturesProvider implements vscode.InlayHintsProvider {
public async provideInlayHints(document: vscode.TextDocument, range: vscode.Range): Promise<vscode.InlayHint[]> {
log("Inlay hints requested for: " + document.uri.toString() + " & range: " + JSON.stringify(range));

const hints: vscode.InlayHint[] = [];

try {
const result = await client.sendRequest(ExecuteCommandRequest.type, {
command: "inferredCaptures",
arguments: [{ uri: document.uri.toString() }]
}) as { location: vscode.Location, captureText: string }[];

log("Inlay hints result: " + JSON.stringify(result));

if (!result) {
log("No results returned.");
return hints;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sometimes the backend just returns null, no idea why.


for (const response of result) {
log("processing a single response: " + JSON.stringify(response))
if (response.location.uri.toString() === document.uri.toString()) {
log("... URI correct!")
const position = response.location.range.start;
const hint = new vscode.InlayHint(position, response.captureText, vscode.InlayHintKind.Type);
hint.paddingRight = true;
hint.paddingLeft = false;
hints.push(hint);
}
}

} catch (error) {
log("Error during inlay hints request: " + JSON.stringify(error));
vscode.window.showErrorMessage("An error occurred while fetching inlay hints.");
}

return hints;
}
}

export async function activate(context: vscode.ExtensionContext) {
effektManager = new EffektManager();

Expand All @@ -84,6 +129,8 @@ export async function activate(context: vscode.ExtensionContext) {

registerCommands(context);

const config = vscode.workspace.getConfiguration("effekt");

// Register the CodeLens provider
context.subscriptions.push(
vscode.languages.registerCodeLensProvider(
Expand All @@ -96,6 +143,21 @@ export async function activate(context: vscode.ExtensionContext) {
)
);

// Conditionally register the Captures provider
//if (config.get<boolean>("showCaptures")) {
context.subscriptions.push(
vscode.languages.registerInlayHintsProvider(
{ language: 'effekt', scheme: 'file' },
new EffektCapturesProvider()
),
vscode.languages.registerInlayHintsProvider(
{ language: 'literate effekt', scheme: 'file' },
new EffektCapturesProvider()
)
);
//}


// Clean up REPL when closed
context.subscriptions.push(
vscode.window.onDidCloseTerminal(terminal => {
Expand All @@ -105,8 +167,6 @@ export async function activate(context: vscode.ExtensionContext) {
})
);

const config = vscode.workspace.getConfiguration("effekt");

let serverOptions: ServerOptions;

if (config.get<boolean>("debug")) {
Expand Down Expand Up @@ -177,7 +237,7 @@ export async function activate(context: vscode.ExtensionContext) {
})

// the decorations themselves don't have styles. Only the added before-elements.
const captureDecoration = vscode.window.createTextEditorDecorationType({})
// const captureDecoration = vscode.window.createTextEditorDecorationType({})

// based on https://github.com/microsoft/vscode-extension-samples/blob/master/decorator-sample/src/extension.ts
let timeout: NodeJS.Timeout;
Expand All @@ -188,7 +248,7 @@ export async function activate(context: vscode.ExtensionContext) {
timeout = setTimeout(updateHoles, 50);
}

function updateCaptures() {
/*function updateCaptures() {

if (!editor) { return; }

Expand Down Expand Up @@ -227,7 +287,7 @@ export async function activate(context: vscode.ExtensionContext) {
return editor.setDecorations(captureDecoration, captureAnnotations)
}
);
}
}*/

const holeRegex = /<>|<{|}>/g

Expand Down Expand Up @@ -267,9 +327,9 @@ export async function activate(context: vscode.ExtensionContext) {
}
}, null, context.subscriptions);

vscode.workspace.onDidSaveTextDocument(ev => {
setTimeout(updateCaptures, 50)
})
//vscode.workspace.onDidSaveTextDocument(ev => {
// setTimeout(updateCaptures, 50)
//})
jiribenes marked this conversation as resolved.
Show resolved Hide resolved

scheduleDecorations();

Expand Down