Skip to content

Commit

Permalink
Show inferred captures
Browse files Browse the repository at this point in the history
  • Loading branch information
b-studios committed Nov 19, 2022
1 parent ed31f07 commit cd4deb1
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 5 deletions.
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"type": "git",
"url": "https://github.com/effekt-lang/effekt-vscode.git"
},
"version": "0.1.5",
"version": "0.1.6",
"publisher": "b-studios",
"engines": {
"vscode": "^1.30.0"
Expand Down Expand Up @@ -81,6 +81,11 @@
"default": true,
"description": "Display additional information when hovering over identifier."
},
"effekt.showCaptures": {
"type": "boolean",
"default": false,
"description": "Show additional information about resources used by a function."
},
"effekt.showIR": {
"type": "string",
"default": "none",
Expand Down
57 changes: 53 additions & 4 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

import { ExtensionContext, workspace, window, Range, DecorationOptions } from 'vscode';
import { LanguageClient, LanguageClientOptions, ServerOptions } from 'vscode-languageclient';
import { ExtensionContext, workspace, window, Range, DecorationOptions, Location } from 'vscode';
import { LanguageClient, LanguageClientOptions, ServerOptions, ExecuteCommandRequest } from 'vscode-languageclient';
import { Monto } from './monto';
import { platform } from 'os';

Expand Down Expand Up @@ -89,22 +89,67 @@ export function activate(context: ExtensionContext) {
dark: { backgroundColor: "rgba(255,255,255,0.05)" }
})

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

// based on https://github.com/microsoft/vscode-extension-samples/blob/master/decorator-sample/src/extension.ts
let timeout: NodeJS.Timer;
let editor = window.activeTextEditor

function scheduleDecorations() {
if (timeout) { clearTimeout(timeout) }
timeout = setTimeout(decorate, 50);
timeout = setTimeout(updateHoles, 50);
}

function updateCaptures() {

if (!editor) { return; }

if (!config.get<boolean>("showCaptures")) { return; }

client.sendRequest(ExecuteCommandRequest.type, { command: "inferredCaptures", arguments: [{
uri: editor.document.uri.toString()
}]}).then(
(result : [{ location: Location, captureText: string }]) => {
if (!editor) { return; }

let captureAnnotations: DecorationOptions[] = []

console.log(result)

if (result == null) return;

result.forEach(response => {
if (!editor) { return; }
const loc = response.location
if (loc.uri != editor.document.uri) return;

captureAnnotations.push({
range: loc.range,
renderOptions: {
before: {
contentText: response.captureText,
backgroundColor: "rgba(170,210,255,0.3)",
color: "rgba(50,50,50,0.5)",
fontStyle: "italic",
margin: "0 0.5em 0 0.5em"
}
}
})
})

if (!editor) { return; }
return editor.setDecorations(captureDecoration, captureAnnotations)
}
);
}

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

/**
* TODO clean this up -- ideally move it to the language server
*/
function decorate() {
function updateHoles() {
if (!editor) { return; }

const text = editor.document.getText()
Expand Down Expand Up @@ -137,6 +182,10 @@ export function activate(context: ExtensionContext) {
}
}, null, context.subscriptions);

workspace.onDidSaveTextDocument(ev => {
setTimeout(updateCaptures, 50)
})

scheduleDecorations();

context.subscriptions.push(client.start());
Expand Down

0 comments on commit cd4deb1

Please sign in to comment.