Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Go to declaration service #734

Merged
merged 2 commits into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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