Skip to content

Commit

Permalink
Go to declaration service (#734)
Browse files Browse the repository at this point in the history
* Go to declaration service

* Removed tests and implementation
  • Loading branch information
gfontorbe committed Nov 9, 2022
1 parent 62c127c commit 824f098
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
21 changes: 21 additions & 0 deletions packages/langium/src/lsp/declaration-provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/******************************************************************************
* Copyright 2022 TypeFox GmbH
* This program and the accompanying materials are made available under the
* terms of the MIT License, which is available in the project root.
******************************************************************************/

import { CancellationToken, DeclarationParams, LocationLink } from 'vscode-languageserver';
import { MaybePromise } from '../utils/promise-util';
import { LangiumDocument } from '../workspace/documents';

/**
* Language-specific service for handling go to declaration requests
*/
export interface DeclarationProvider {
/**
* Handle a go to declaration request.
* @throws `OperationCancelled` if cancellation is detected during execution
* @throws `ResponseError` if an error is detected that should be sent as response to the client
*/
getDeclaration(document: LangiumDocument, params: DeclarationParams, cancelToken?: CancellationToken): MaybePromise<LocationLink[] | undefined>
}
12 changes: 11 additions & 1 deletion packages/langium/src/lsp/language-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export class DefaultLanguageServer implements LanguageServer {
const hasRenameProvider = this.hasService(e => e.lsp.RenameProvider);
const hasCallHierarchyProvider = this.hasService(e => e.lsp.CallHierarchyProvider);
const codeLensProvider = this.services.lsp.CodeLensProvider;
const hasDeclarationProvider = this.hasService(e => e.lsp.DeclarationProvider);

const result: InitializeResult = {
capabilities: {
Expand Down Expand Up @@ -125,7 +126,8 @@ export class DefaultLanguageServer implements LanguageServer {
: undefined,
codeLensProvider: codeLensProvider
? { resolveProvider: !!codeLensProvider.resolveCodeLens }
: undefined
: undefined,
declarationProvider: hasDeclarationProvider
}
};

Expand Down Expand Up @@ -165,6 +167,7 @@ export function startLanguageServer(services: LangiumSharedServices): void {
addCodeLensHandler(connection, services);
addDocumentLinkHandler(connection, services);
addConfigurationChangeHandler(connection, services);
addGoToDeclarationHandler(connection, services);

connection.onInitialize(params => {
return services.lsp.LanguageServer.initialize(params);
Expand Down Expand Up @@ -268,6 +271,13 @@ export function addGoToImplementationHandler(connection: Connection, services: L
));
}

export function addGoToDeclarationHandler(connection: Connection, services: LangiumSharedServices) {
connection.onDeclaration(createRequestHandler(
(services, document, params, cancelToken) => services.lsp.DeclarationProvider?.getDeclaration(document, params, cancelToken),
services
));
}

export function addDocumentHighlightsHandler(connection: Connection, services: LangiumSharedServices): void {
connection.onDocumentHighlight(createRequestHandler(
(services, document, params, cancelToken) => services.lsp.DocumentHighlightProvider?.getDocumentHighlight(document, params, cancelToken),
Expand Down
2 changes: 2 additions & 0 deletions packages/langium/src/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import type { ImplementationProvider } from './lsp/implementation-provider';
import type { CallHierarchyProvider } from './lsp/call-hierarchy-provider';
import type { DocumentLinkProvider } from './lsp/document-link-provider';
import type { CodeLensProvider } from './lsp/code-lens-provider';
import type { DeclarationProvider } from './lsp/declaration-provider';

/**
* The services generated by `langium-cli` for a single language. These are derived from the
Expand Down Expand Up @@ -85,6 +86,7 @@ export type LangiumLspServices = {
Formatter?: Formatter
SignatureHelp?: SignatureHelpProvider
CallHierarchyProvider?: CallHierarchyProvider
DeclarationProvider?: DeclarationProvider
}

/**
Expand Down

0 comments on commit 824f098

Please sign in to comment.