diff --git a/src/documentSymbols.ts b/src/documentSymbols.ts new file mode 100644 index 000000000..f4b72a74b --- /dev/null +++ b/src/documentSymbols.ts @@ -0,0 +1,23 @@ +'use strict'; + +import { + CancellationToken, + DocumentSymbol, + DocumentSymbolParams, + DocumentSymbolRequest, + LanguageClient, + SymbolInformation +} from "vscode-languageclient"; + +type DocumentSymbolsResponse = DocumentSymbol[] | SymbolInformation[] | null; + +export type getDocumentSymbolsCommand = (params: DocumentSymbolParams, token?: CancellationToken) => Promise; + +export function getDocumentSymbolsProvider(languageClient: LanguageClient): getDocumentSymbolsCommand { + return async (params: DocumentSymbolParams, token?: CancellationToken): Promise => { + if (token !== undefined) { + return languageClient.sendRequest(DocumentSymbolRequest.type, params, token); + } + return languageClient.sendRequest(DocumentSymbolRequest.type, params); + }; +} diff --git a/src/extension.api.ts b/src/extension.api.ts index f47c4e835..68ce3a812 100644 --- a/src/extension.api.ts +++ b/src/extension.api.ts @@ -1,3 +1,4 @@ +import { getDocumentSymbolsCommand } from './documentSymbols'; import { RequirementsData } from './requirements'; import { TextDocumentPositionParams } from 'vscode-languageclient'; import { CancellationToken, Command, ProviderResult } from 'vscode'; @@ -5,9 +6,12 @@ import { CancellationToken, Command, ProviderResult } from 'vscode'; export type provideHoverCommandFn = (params: TextDocumentPositionParams, token: CancellationToken) => ProviderResult; export type registerHoverCommand = (callback: provideHoverCommandFn) => void; +export const ExtensionApiVersion = '0.3'; + export interface ExtensionAPI { readonly apiVersion: string; readonly javaRequirement: RequirementsData; readonly status: "Started" | "Error"; readonly registerHoverCommand: registerHoverCommand; + readonly getDocumentSymbols: getDocumentSymbolsCommand; } diff --git a/src/extension.ts b/src/extension.ts index e2ab74295..05088bb6f 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -7,13 +7,14 @@ import { workspace, extensions, ExtensionContext, window, StatusBarAlignment, co import { ExecuteCommandParams, ExecuteCommandRequest, LanguageClient, LanguageClientOptions, RevealOutputChannelOn, Position as LSPosition, Location as LSLocation, StreamInfo, VersionedTextDocumentIdentifier, ErrorHandler, Message, ErrorAction, CloseAction, InitializationFailedHandler } from 'vscode-languageclient'; import { onExtensionChange, collectJavaExtensions } from './plugin'; import { prepareExecutable, awaitServerConnection } from './javaServerStarter'; +import { getDocumentSymbolsCommand, getDocumentSymbolsProvider } from './documentSymbols'; import * as requirements from './requirements'; import { Commands } from './commands'; import { StatusNotification, ClassFileContentsRequest, ProjectConfigurationUpdateRequest, MessageType, ActionableNotification, FeatureStatus, CompileWorkspaceRequest, CompileWorkspaceStatus, ProgressReportNotification, ExecuteClientCommandRequest, SendNotificationRequest, SourceAttachmentRequest, SourceAttachmentResult, SourceAttachmentAttribute } from './protocol'; -import { ExtensionAPI } from './extension.api'; +import { ExtensionAPI, ExtensionApiVersion } from './extension.api'; import * as buildpath from './buildpath'; import * as hoverAction from './hoverAction'; import * as sourceAction from './sourceAction'; @@ -207,6 +208,7 @@ export function activate(context: ExtensionContext): Promise { languageClient = new LanguageClient('java', extensionName, serverOptions, clientOptions); languageClient.registerProposedFeatures(); const registerHoverCommand = hoverAction.registerClientHoverProvider(languageClient, context); + const getDocumentSymbols: getDocumentSymbolsCommand = getDocumentSymbolsProvider(languageClient); languageClient.onReady().then(() => { languageClient.onNotification(StatusNotification.type, (report) => { @@ -217,10 +219,11 @@ export function activate(context: ExtensionContext): Promise { lastStatus = item.text; commands.executeCommand('setContext', 'javaLSReady', true); resolve({ - apiVersion: '0.2', + apiVersion: ExtensionApiVersion, javaRequirement: requirements, status: report.type, registerHoverCommand, + getDocumentSymbols }); break; case 'Error': @@ -229,10 +232,11 @@ export function activate(context: ExtensionContext): Promise { p.report({ message: 'Finished with Error' }); toggleItem(window.activeTextEditor, item); resolve({ - apiVersion: '0.2', + apiVersion: ExtensionApiVersion, javaRequirement: requirements, status: report.type, registerHoverCommand, + getDocumentSymbols }); break; case 'Starting': diff --git a/src/protocol.ts b/src/protocol.ts index 4672501ac..28da7d484 100644 --- a/src/protocol.ts +++ b/src/protocol.ts @@ -1,6 +1,18 @@ 'use strict'; -import { RequestType, NotificationType, TextDocumentIdentifier, ExecuteCommandParams, CodeActionParams, WorkspaceEdit, FormattingOptions, WorkspaceSymbolParams, SymbolInformation, TextDocumentPositionParams, Location } from 'vscode-languageclient'; +import { + CodeActionParams, + ExecuteCommandParams, + FormattingOptions, + Location, + NotificationType, + RequestType, + SymbolInformation, + TextDocumentIdentifier, + TextDocumentPositionParams, + WorkspaceEdit, + WorkspaceSymbolParams, +} from 'vscode-languageclient'; import { Command, Range } from 'vscode'; /**