Basic LSP Usage

This commit is contained in:
mp 2024-11-14 18:08:52 -08:00
parent 19a8d36e07
commit a765d738e1
3 changed files with 74 additions and 1 deletions

View file

@ -21,6 +21,10 @@
"properties": {}
},
"commands": [
{
"command": "typeInspector.inspect",
"title": "Inspect Types of All Variables"
},
{
"command": "void.ctrl+l",
"title": "Show Sidebar"
@ -158,4 +162,4 @@
"dependencies": {
"lru-cache": "^11.0.2"
}
}
}

View file

@ -0,0 +1,63 @@
import * as vscode from 'vscode';
const legend = new vscode.SemanticTokensLegend([], []);
export async function getFunctionTokens() {
const editor = vscode.window.activeTextEditor;
if (!editor) return;
const document = editor.document;
const tokens = await vscode.commands.executeCommand<vscode.SemanticTokens>(
'vscode.provideDocumentSemanticTokens',
document.uri
);
if (!tokens) {
console.error('No tokens found');
return [];
}
const allTokens = decodeTokens(tokens, document);
console.log("Tokens:", allTokens);
return allTokens;
}
function decodeTokens(tokens: vscode.SemanticTokens, document: vscode.TextDocument) {
const data = tokens.data;
const decodedTokens = [];
let line = 0;
let character = 0;
for (let i = 0; i < data.length; i += 5) {
const deltaLine = data[i];
const deltaStartChar = data[i + 1];
const length = data[i + 2];
const tokenTypeIdx = data[i + 3];
const tokenModifierIdx = data[i + 4];
line += deltaLine;
character = deltaLine === 0 ? character + deltaStartChar : deltaStartChar;
const type = legend.tokenTypes[tokenTypeIdx] || `(${tokenTypeIdx})`;
const modifier = legend.tokenModifiers[tokenModifierIdx] || `(${tokenModifierIdx})`;
const tokenRange = new vscode.Range(line, character, line, character + length);
const tokenText = document.getText(tokenRange);
decodedTokens.push({
line,
startCharacter: character,
length,
type,
modifier,
text: tokenText,
});
console.log(`Token: '${tokenText}' | Type: ${type} | Modifier: ${modifier} | Line: ${line}, Character: ${character}`);
}
return decodedTokens;
}

View file

@ -10,6 +10,7 @@ import { readFileContentOfUri } from './extensionLib/readFileContentOfUri';
import { SidebarWebviewProvider } from './providers/SidebarWebviewProvider';
import { CtrlKWebviewProvider } from './providers/CtrlKWebviewProvider';
import { AutocompleteProvider } from './AutcompleteProvider';
import { getFunctionTokens } from '../common/LangaugeServer/findFunctions';
// // this comes from vscode.proposed.editorInsets.d.ts
// declare module 'vscode' {
@ -191,6 +192,11 @@ export function activate(context: vscode.ExtensionContext) {
// setupAutocomplete({ voidConfig, abortRef })
// 7. Language Server
let disposable = vscode.commands.registerCommand('typeInspector.inspect', getFunctionTokens);
context.subscriptions.push(disposable);
// Gets called when user presses ctrl + k (mounts ctrl+k-style codelens)
// TODO need to build this