mirror of
https://github.com/fluencelabs/aqua-vscode
synced 2025-03-16 06:10:51 +00:00
Go-to-definition for extension (#15)
This commit is contained in:
parent
f2866f8075
commit
620534b6ec
23
server/package-lock.json
generated
23
server/package-lock.json
generated
@ -8,7 +8,7 @@
|
||||
"name": "aqua-ls-server",
|
||||
"version": "0.0.1",
|
||||
"dependencies": {
|
||||
"@fluencelabs/aqua-language-server-api": "0.0.4",
|
||||
"@fluencelabs/aqua-language-server-api": "0.7.2-314",
|
||||
"global-dirs": "^3.0.0",
|
||||
"vscode-languageserver": "^7.0.0",
|
||||
"vscode-languageserver-textdocument": "^1.0.4"
|
||||
@ -17,10 +17,19 @@
|
||||
"node": "*"
|
||||
}
|
||||
},
|
||||
"../../aqua/language-server": {
|
||||
"extraneous": true
|
||||
},
|
||||
"../../aqua/language-server-npm": {
|
||||
"name": "@fluencelabs/aqua-language-server-api",
|
||||
"version": "0.0.3",
|
||||
"extraneous": true,
|
||||
"license": "Apache-2.0"
|
||||
},
|
||||
"node_modules/@fluencelabs/aqua-language-server-api": {
|
||||
"version": "0.0.4",
|
||||
"resolved": "https://registry.npmjs.org/@fluencelabs/aqua-language-server-api/-/aqua-language-server-api-0.0.4.tgz",
|
||||
"integrity": "sha512-K/CTsXfSD/kw2eJ7Fb+J+63PE7CQdU37Iv9DzmkgEngc07ClY7TzWnc/EeLB2pjqcxOZJ05qJuOSZKHEyOoIPg=="
|
||||
"version": "0.7.2-314",
|
||||
"resolved": "https://registry.npmjs.org/@fluencelabs/aqua-language-server-api/-/aqua-language-server-api-0.7.2-314.tgz",
|
||||
"integrity": "sha512-wKKPvdYfmqOQ7HafCXwXIi4z5QYrla54PdvXNA1HYFzEJZssxdr07tSeV+U+Kfq8t+R8EU93OATuWTlxdTKVng=="
|
||||
},
|
||||
"node_modules/global-dirs": {
|
||||
"version": "3.0.0",
|
||||
@ -85,9 +94,9 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@fluencelabs/aqua-language-server-api": {
|
||||
"version": "0.0.4",
|
||||
"resolved": "https://registry.npmjs.org/@fluencelabs/aqua-language-server-api/-/aqua-language-server-api-0.0.4.tgz",
|
||||
"integrity": "sha512-K/CTsXfSD/kw2eJ7Fb+J+63PE7CQdU37Iv9DzmkgEngc07ClY7TzWnc/EeLB2pjqcxOZJ05qJuOSZKHEyOoIPg=="
|
||||
"version": "0.7.2-314",
|
||||
"resolved": "https://registry.npmjs.org/@fluencelabs/aqua-language-server-api/-/aqua-language-server-api-0.7.2-314.tgz",
|
||||
"integrity": "sha512-wKKPvdYfmqOQ7HafCXwXIi4z5QYrla54PdvXNA1HYFzEJZssxdr07tSeV+U+Kfq8t+R8EU93OATuWTlxdTKVng=="
|
||||
},
|
||||
"global-dirs": {
|
||||
"version": "3.0.0",
|
||||
|
@ -11,10 +11,10 @@
|
||||
"url": "https://github.com/fluencelabs/aqua"
|
||||
},
|
||||
"dependencies": {
|
||||
"vscode-languageserver": "^7.0.0",
|
||||
"vscode-languageserver-textdocument": "^1.0.4",
|
||||
"@fluencelabs/aqua-language-server-api": "0.7.2-314",
|
||||
"global-dirs": "^3.0.0",
|
||||
"@fluencelabs/aqua-language-server-api": "0.0.4"
|
||||
"vscode-languageserver": "^7.0.0",
|
||||
"vscode-languageserver-textdocument": "^1.0.4"
|
||||
},
|
||||
"scripts": {
|
||||
"get-root": "npm root --globall"
|
||||
|
@ -11,6 +11,8 @@ import type { WorkspaceFolder } from 'vscode-languageserver-protocol';
|
||||
|
||||
import { TextDocument } from 'vscode-languageserver-textdocument';
|
||||
import { compileAqua } from './validation';
|
||||
import type { DefinitionParams, Location } from 'vscode-languageserver';
|
||||
import type { TokenLink } from '@fluencelabs/aqua-language-server-api/aqua-lsp-api';
|
||||
|
||||
// Create a connection for the server, using Node's IPC as a transport.
|
||||
// Also include all preview / proposed LSP features.
|
||||
@ -27,6 +29,12 @@ export interface Settings {
|
||||
imports: string[];
|
||||
}
|
||||
|
||||
function searchDefinition(offset: number, name: string, locations: TokenLink[]): TokenLink | undefined {
|
||||
return locations.find((token) => {
|
||||
return token.current.name == name && token.current.start <= offset && token.current.end >= offset;
|
||||
});
|
||||
}
|
||||
|
||||
// The global settings, used when the `workspace/configuration` request is not supported by the client.
|
||||
// Please note that this is not the case when using this server with the client provided in this example
|
||||
// but could happen with other clients.
|
||||
@ -36,6 +44,36 @@ let globalSettings: Settings = defaultSettings;
|
||||
// Cache the settings of all open documents
|
||||
const documentSettings: Map<string, Thenable<Settings>> = new Map();
|
||||
|
||||
let currentLocations: TokenLink[] = [];
|
||||
|
||||
async function onDefinition({ textDocument, position }: DefinitionParams): Promise<Location[]> {
|
||||
const doc = documents.get(textDocument.uri);
|
||||
if (doc) {
|
||||
const offset = doc.offsetAt(position);
|
||||
const token = searchDefinition(offset, doc.uri.replace('file://', ''), currentLocations);
|
||||
connection.console.log('find token: ' + JSON.stringify(token));
|
||||
if (token) {
|
||||
const definition = token.definition;
|
||||
const defDoc = documents.get('file://' + definition.name);
|
||||
if (defDoc) {
|
||||
return [
|
||||
{
|
||||
uri: defDoc.uri,
|
||||
range: {
|
||||
start: defDoc.positionAt(definition.start),
|
||||
end: defDoc.positionAt(definition.end),
|
||||
},
|
||||
},
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
connection.onDefinition(onDefinition);
|
||||
|
||||
connection.onDidChangeConfiguration((change) => {
|
||||
connection.console.log(change.settings);
|
||||
|
||||
@ -78,11 +116,8 @@ connection.onInitialize((params: InitializeParams) => {
|
||||
|
||||
const result: InitializeResult = {
|
||||
capabilities: {
|
||||
textDocumentSync: TextDocumentSyncKind.Incremental,
|
||||
// Tell the client that this server supports code completion.
|
||||
completionProvider: {
|
||||
resolveProvider: true,
|
||||
},
|
||||
textDocumentSync: TextDocumentSyncKind.Full,
|
||||
definitionProvider: true,
|
||||
},
|
||||
};
|
||||
if (hasWorkspaceFolderCapability) {
|
||||
@ -113,7 +148,9 @@ documents.onDidOpen(async (change) => {
|
||||
async function validateDocument(textDocument: TextDocument): Promise<void> {
|
||||
const settings = await getDocumentSettings(textDocument.uri);
|
||||
|
||||
const diagnostics = await compileAqua(settings, textDocument, folders);
|
||||
const [diagnostics, locations] = await compileAqua(settings, textDocument, folders);
|
||||
|
||||
currentLocations = locations;
|
||||
|
||||
// Send the computed diagnostics to VSCode.
|
||||
connection.sendDiagnostics({ uri: textDocument.uri, diagnostics });
|
||||
|
@ -1,5 +1,5 @@
|
||||
import type { TextDocument } from 'vscode-languageserver-textdocument';
|
||||
import { AquaLSP } from '@fluencelabs/aqua-language-server-api/aqua-lsp-api';
|
||||
import { AquaLSP, ErrorInfo, TokenLink } from '@fluencelabs/aqua-language-server-api/aqua-lsp-api';
|
||||
import { Diagnostic, DiagnosticSeverity } from 'vscode-languageserver/node';
|
||||
import type { Settings } from './server';
|
||||
import type { WorkspaceFolder } from 'vscode-languageserver-protocol';
|
||||
@ -8,7 +8,7 @@ export async function compileAqua(
|
||||
settings: Settings,
|
||||
textDocument: TextDocument,
|
||||
folders: WorkspaceFolder[],
|
||||
): Promise<Diagnostic[]> {
|
||||
): Promise<[Diagnostic[], TokenLink[]]> {
|
||||
const uri = textDocument.uri.replace('file://', '');
|
||||
|
||||
let imports: string[] = [];
|
||||
@ -23,13 +23,13 @@ export async function compileAqua(
|
||||
}
|
||||
|
||||
// compile aqua and get possible errors
|
||||
const errors = await AquaLSP.compile(uri, imports);
|
||||
const result = await AquaLSP.compile(uri, imports);
|
||||
|
||||
const diagnostics: Diagnostic[] = [];
|
||||
|
||||
if (errors) {
|
||||
if (result.errors) {
|
||||
// Add all errors to Diagnostic
|
||||
errors.forEach((err) => {
|
||||
result.errors.forEach((err: ErrorInfo) => {
|
||||
const diagnostic: Diagnostic = {
|
||||
severity: DiagnosticSeverity.Error,
|
||||
range: {
|
||||
@ -55,5 +55,5 @@ export async function compileAqua(
|
||||
});
|
||||
}
|
||||
|
||||
return diagnostics;
|
||||
return [diagnostics, result.locations];
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user