Skip to content

Commit

Permalink
feat: implement textDocument/selectionRange request (#642)
Browse files Browse the repository at this point in the history
  • Loading branch information
rchl committed Dec 22, 2022
1 parent e999487 commit a5598c6
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,7 @@ interface DefinitionSymbol {
- [x] textDocument/rangeFormatting
- [x] textDocument/references
- [x] textDocument/rename
- [x] textDocument/selectionRange
- [x] textDocument/signatureHelp
- [x] workspace/symbol
- [x] workspace/didChangeConfiguration
Expand Down
1 change: 1 addition & 0 deletions src/lsp-connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export function createLspConnection(options: LspConnectionOptions): lsp.Connecti
connection.onReferences(server.references.bind(server));
connection.onRenameRequest(server.rename.bind(server));
connection.onPrepareRename(server.prepareRename.bind(server));
connection.onSelectionRanges(server.selectionRanges.bind(server));
connection.onSignatureHelp(server.signatureHelp.bind(server));
connection.onWorkspaceSymbol(server.workspaceSymbol.bind(server));
connection.onFoldingRanges(server.foldingRanges.bind(server));
Expand Down
18 changes: 17 additions & 1 deletion src/lsp-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import API from './utils/api.js';
import { Logger, LogLevel, PrefixingLogger } from './utils/logger.js';
import { TspClient } from './tsp-client.js';
import { DiagnosticEventQueue } from './diagnostic-queue.js';
import { toDocumentHighlight, uriToPath, toSymbolKind, toLocation, pathToUri, toTextEdit, normalizePath } from './protocol-translation.js';
import { toDocumentHighlight, uriToPath, toSymbolKind, toLocation, toSelectionRange, pathToUri, toTextEdit, normalizePath } from './protocol-translation.js';
import { LspDocuments, LspDocument } from './document.js';
import { asCompletionItem, asResolvedCompletionItem, getCompletionTriggerCharacter } from './completion.js';
import { asSignatureHelp, toTsTriggerReason } from './hover.js';
Expand Down Expand Up @@ -217,6 +217,7 @@ export class LspServer {
inlayHintProvider: true,
renameProvider: prepareSupport ? { prepareProvider: true } : true,
referencesProvider: true,
selectionRangeProvider: true,
signatureHelpProvider: {
triggerCharacters: ['(', ',', '<'],
retriggerCharacters: [')'],
Expand Down Expand Up @@ -791,6 +792,21 @@ export class LspServer {
return [];
}

async selectionRanges(params: lsp.SelectionRangeParams): Promise<lsp.SelectionRange[] | null> {
const file = uriToPath(params.textDocument.uri);
if (!file) {
return null;
}
const response = await this.tspClient.request(CommandTypes.SelectionRange, {
file,
locations: params.positions.map(Position.toLocation),
});
if (response.type !== 'response' || !response.body) {
return null;
}
return response.body.map(toSelectionRange);
}

async signatureHelp(params: lsp.SignatureHelpParams): Promise<lsp.SignatureHelp | undefined> {
const file = uriToPath(params.textDocument.uri);
this.logger.log('signatureHelp', params, file);
Expand Down
9 changes: 8 additions & 1 deletion src/protocol-translation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import * as lsp from 'vscode-languageserver';
import vscodeUri from 'vscode-uri';
import { LspDocuments } from './document.js';
import { tslib, tsp, SupportedFeatures } from './ts-protocol.js';
import { Position } from './utils/typeConverters.js';
import { Position, Range } from './utils/typeConverters.js';

const RE_PATHSEP_WINDOWS = /\\/g;

Expand Down Expand Up @@ -160,6 +160,13 @@ function asRelatedInformation(info: tsp.DiagnosticRelatedInformation[] | undefin
return result;
}

export function toSelectionRange(range: tsp.SelectionRange): lsp.SelectionRange {
return lsp.SelectionRange.create(
Range.fromTextSpan(range.textSpan),
range.parent ? toSelectionRange(range.parent) : undefined,
);
}

export function toTextEdit(edit: tsp.CodeEdit): lsp.TextEdit {
return {
range: {
Expand Down
1 change: 1 addition & 0 deletions src/tsServer/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export interface TypeScriptRequestTypes {
[CommandTypes.Quickinfo]: [tsp.FileLocationRequestArgs, tsp.QuickInfoResponse];
[CommandTypes.References]: [tsp.FileLocationRequestArgs, tsp.ReferencesResponse];
[CommandTypes.Rename]: [tsp.RenameRequestArgs, tsp.RenameResponse];
[CommandTypes.SelectionRange]: [tsp.SelectionRangeRequestArgs, tsp.SelectionRangeResponse];
[CommandTypes.SignatureHelp]: [tsp.SignatureHelpRequestArgs, tsp.SignatureHelpResponse];
[CommandTypes.TypeDefinition]: [tsp.FileLocationRequestArgs, tsp.TypeDefinitionResponse];
[CommandTypes.UpdateOpen]: [tsp.UpdateOpenRequestArgs, tsp.Response];
Expand Down

0 comments on commit a5598c6

Please sign in to comment.