Skip to content

Commit

Permalink
indexing + enrichment work
Browse files Browse the repository at this point in the history
  • Loading branch information
warfaj committed Sep 27, 2023
1 parent 190c47c commit a42c843
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 14 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"Programming Languages"
],
"activationEvents": [
"onLanguage:pax"
"onLanguage:pax",
"onLanguage:rust"
],
"main": "./out/extension.js",
"contributes": {
Expand Down
86 changes: 86 additions & 0 deletions src/enrichmentProxy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { commands, Hover, LocationLink, Uri, Position } from "vscode";
import { SymbolData } from "./extension";

enum EnrichmentType {
DEFINITION = "getDefinition",
HOVER = "getHover"
}

interface EnrichmentData {
[EnrichmentType.DEFINITION]: Map<number, LocationLink[]>,
[EnrichmentType.HOVER]: Map<number, Hover[]>
}


export interface EnrichParams {
symbol: SymbolData;
originatingPaxFile: string;
}


interface EnrichmentResult {
[EnrichmentType.DEFINITION]?: number,
[EnrichmentType.HOVER]?: number
}

class EnrichmentProxy {

private fileMap: Map<string, EnrichmentData>;
private idCounter: number;

constructor() {
this.fileMap = new Map<string, EnrichmentData>();
this.idCounter = 0;
}

async enrich(params: EnrichParams): Promise<EnrichmentResult> {
const uri = Uri.file(params.symbol.uri);
const respDefinition: LocationLink[] = await commands.executeCommand('vscode.executeDefinitionProvider', uri, params.symbol.position);
const respHover: Hover[] = await commands.executeCommand('vscode.executeHoverProvider', uri, params.symbol.position);

if (!this.fileMap.has(params.originatingPaxFile)) {
this.fileMap.set(params.originatingPaxFile, {
[EnrichmentType.DEFINITION]: new Map<number, LocationLink[]>(),
[EnrichmentType.HOVER]: new Map<number, Hover[]>()
});
}

const fileData = this.fileMap.get(params.originatingPaxFile);
const definitionId = this.storeData(fileData![EnrichmentType.DEFINITION], respDefinition);
const hoverId = this.storeData(fileData![EnrichmentType.HOVER], respHover);

return {
[EnrichmentType.DEFINITION]: definitionId,
[EnrichmentType.HOVER]: hoverId
};
}


getEnrichmentData(originatingPaxFile: string, type: EnrichmentType, id: number): LocationLink[] | Hover[] | null {
const fileData = this.fileMap.get(originatingPaxFile);
if (!fileData) {
return null;
}
return fileData[type].get(id) || null;
}

clearDataForFile(originatingPaxFile: string): void {
this.fileMap.delete(originatingPaxFile);
}

clearAllData(): void {
this.fileMap.clear();
}

private storeData<T>(dataMap: Map<number, T>, data: T): number {
const id = this.getUniqueId();
dataMap.set(id, data);
return id;
}

private getUniqueId(): number {
return this.idCounter++;
}
}

export default EnrichmentProxy;
84 changes: 71 additions & 13 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,55 @@
import * as path from 'path';
import { workspace, ExtensionContext, window, languages, Position, CancellationToken, CompletionItemProvider, ProviderResult, CompletionItem, CompletionList, DefinitionProvider, Definition, TextDocument, TextDocumentChangeEvent, Uri, TextEdit } from 'vscode';
import { workspace, ExtensionContext, window, languages, Position, CancellationToken, CompletionItemProvider, ProviderResult, CompletionItem, CompletionList, DefinitionProvider, Definition, TextDocument, TextDocumentChangeEvent, Uri, TextEdit, Location, LocationLink, commands, Range, Hover } from 'vscode';

import {
LanguageClient,
LanguageClientOptions,
RequestType,
ServerOptions,
} from 'vscode-languageclient/node';
import EnrichmentProxy, { EnrichParams } from './enrichmentProxy';


export interface SymbolData {
uri: string,
position: Position,
}

interface SymbolLocationParams {
symbol: SymbolData;
}


const GetDefinitionRequest = 'pax/getDefinition';
const GetHoverRequest = 'pax/getHover';
const EnrichmentRequest = 'pax/enrich';

let client: LanguageClient;
let enrichmentProxy: EnrichmentProxy;

export function activate(context: ExtensionContext) {

console.log('Congratulations, your extension "pax" is now active!');
console.log('Your extension "pax" is now active!');

let serverOptions: ServerOptions = {
run: {
command: path.resolve(__dirname, '../../target/debug/pax-language-server')
command: path.resolve(__dirname, '../../pax/pax-language-server/target/debug/pax-language-server')
},
debug: {
command: path.resolve(__dirname, '../../target/debug/pax-language-server'),
command: path.resolve(__dirname, '../../pax/pax-language-server/target/debug/pax-language-server'),
}
};

let outputChannel = window.createOutputChannel('Pax Language Server');

let clientOptions: LanguageClientOptions = {
documentSelector: [{ scheme: 'file', language: 'pax' }],
synchronize: {
fileEvents: workspace.createFileSystemWatcher('**/*.pax'),
},
documentSelector: [
{ scheme: 'file', language: 'pax' },
{ scheme: 'file', language: 'rust' }
],
synchronize: {
fileEvents: workspace.createFileSystemWatcher('{**/*.pax,**/*.rs}'),
},
outputChannel: outputChannel,
revealOutputChannelOn: 1,
initializationOptions: {
Expand All @@ -43,7 +64,39 @@ export function activate(context: ExtensionContext) {
clientOptions
);

enrichmentProxy = new EnrichmentProxy();

client.onReady().then(() => {

client.onRequest(GetDefinitionRequest, async (params: SymbolLocationParams) => {
const uri = Uri.file(params.symbol.uri);
const resp: LocationLink[] = await commands.executeCommand('vscode.executeDefinitionProvider', uri, params.symbol.position);
let locations = [];
if (resp.length > 0) {
locations.push({
targetUri: resp[0].targetUri.toString(),
targetRange: resp[0].targetRange,
targetSelectionRange: resp[0].targetSelectionRange,
originSelectionRange: resp[0].originSelectionRange,
});
}
return {
locations
};
});

client.onRequest(GetHoverRequest, async (params: SymbolLocationParams) => {
const uri = Uri.file(params.symbol.uri);
const resp: Hover[] = await commands.executeCommand('vscode.executeHoverProvider', uri, params.symbol.position);
return resp
});

client.onRequest(EnrichmentRequest, async (params: EnrichParams) => {
const enrichmentResult = await enrichmentProxy.enrich(params);
return enrichmentResult;
});


workspace.onDidOpenTextDocument(sendDocumentOpen);
workspace.onDidCloseTextDocument(sendDocumentClose);
workspace.onDidSaveTextDocument(sendDocumentSave);
Expand All @@ -52,20 +105,25 @@ export function activate(context: ExtensionContext) {
languages.registerDefinitionProvider({ scheme: 'file', language: 'pax' }, new PaxDefinitionProvider());
});



context.subscriptions.push(client.start());
}

function sendDocumentOpen(document: TextDocument) {
if(document.languageId === 'pax') {
function delay(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms));
}


async function sendDocumentOpen(document: TextDocument) {
client.sendNotification('textDocument/didOpen', {
textDocument: {
uri: document.uri.toString(),
languageId: 'pax',
languageId: document.languageId,
version: document.version,
text: document.getText()
}
});
}
}

function sendDocumentClose(document: TextDocument) {
Expand All @@ -86,7 +144,6 @@ function sendDocumentSave(document: TextDocument) {
}

function sendDocumentChange(event: TextDocumentChangeEvent) {
console.log("sending change");
client.sendNotification('textDocument/didChange', {
textDocument: {
uri: event.document.uri.toString(),
Expand Down Expand Up @@ -120,3 +177,4 @@ export function deactivate(): Thenable<void> | undefined {
}
return client.stop();
}

0 comments on commit a42c843

Please sign in to comment.