From 2d4395fbdb38af2474c7c70abd68b965860b706f Mon Sep 17 00:00:00 2001 From: Matt Bierner Date: Mon, 6 Mar 2023 10:19:25 -0800 Subject: [PATCH 1/4] Add stubs jsx linked editing For https://github.com/microsoft/TypeScript/issues/51832 --- .../src/languageFeatures/jsxLinkedEditing.ts | 49 +++++++++++++++++++ .../src/languageProvider.ts | 1 + .../src/tsServer/api.ts | 1 + .../src/tsServer/protocol/protocol.d.ts | 11 +++++ .../src/typescriptService.ts | 1 + 5 files changed, 63 insertions(+) create mode 100644 extensions/typescript-language-features/src/languageFeatures/jsxLinkedEditing.ts diff --git a/extensions/typescript-language-features/src/languageFeatures/jsxLinkedEditing.ts b/extensions/typescript-language-features/src/languageFeatures/jsxLinkedEditing.ts new file mode 100644 index 0000000000000..618acc3d87ffb --- /dev/null +++ b/extensions/typescript-language-features/src/languageFeatures/jsxLinkedEditing.ts @@ -0,0 +1,49 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +import * as vscode from 'vscode'; +import { ClientCapability, ITypeScriptServiceClient } from '../typescriptService'; +import { conditionalRegistration, requireMinVersion, requireSomeCapability } from '../utils/dependentRegistration'; +import { DocumentSelector } from '../utils/documentSelector'; +import * as typeConverters from '../utils/typeConverters'; +import API from '../utils/api'; + +class JsxLinkedEditingSupport implements vscode.LinkedEditingRangeProvider { + + public static readonly minVersion = API.v510; + + public constructor( + private readonly client: ITypeScriptServiceClient + ) { } + + async provideLinkedEditingRanges(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken): Promise { + const filepath = this.client.toOpenTsFilePath(document); + if (!filepath) { + return undefined; + } + + const args = typeConverters.Position.toFileLocationRequestArgs(filepath, position); + const response = await this.client.execute('jsxLinkedEdit', args, token); + if (response.type !== 'response' || !response.body) { + return undefined; + } + + const wordPattern = response.body.wordPattern ? new RegExp(response.body.wordPattern) : undefined; + return new vscode.LinkedEditingRanges(response.body.ranges.map(range => typeConverters.Range.fromTextSpan(range)), wordPattern); + } +} + +export function register( + selector: DocumentSelector, + client: ITypeScriptServiceClient +) { + return conditionalRegistration([ + requireMinVersion(client, JsxLinkedEditingSupport.minVersion), + requireSomeCapability(client, ClientCapability.Syntax), + ], () => { + return vscode.languages.registerLinkedEditingRangeProvider(selector.semantic, + new JsxLinkedEditingSupport(client)); + }); +} diff --git a/extensions/typescript-language-features/src/languageProvider.ts b/extensions/typescript-language-features/src/languageProvider.ts index a9ef3aea5a538..a50443d0dd996 100644 --- a/extensions/typescript-language-features/src/languageProvider.ts +++ b/extensions/typescript-language-features/src/languageProvider.ts @@ -75,6 +75,7 @@ export default class LanguageProvider extends Disposable { import('./languageFeatures/implementations').then(provider => this._register(provider.register(selector, this.client))), import('./languageFeatures/inlayHints').then(provider => this._register(provider.register(selector, this.description, this.client, this.fileConfigurationManager))), import('./languageFeatures/jsDocCompletions').then(provider => this._register(provider.register(selector, this.description, this.client, this.fileConfigurationManager))), + import('./languageFeatures/jsxLinkedEditing').then(provider => this._register(provider.register(selector, this.client))), import('./languageFeatures/organizeImports').then(provider => this._register(provider.register(selector, this.client, this.commandManager, this.fileConfigurationManager, this.telemetryReporter))), import('./languageFeatures/quickFix').then(provider => this._register(provider.register(selector, this.client, this.fileConfigurationManager, this.commandManager, this.client.diagnosticsManager, this.telemetryReporter))), import('./languageFeatures/refactor').then(provider => this._register(provider.register(selector, this.client, this.fileConfigurationManager, this.commandManager, this.telemetryReporter))), diff --git a/extensions/typescript-language-features/src/tsServer/api.ts b/extensions/typescript-language-features/src/tsServer/api.ts index 440c0b8fec44a..88156ee86641c 100644 --- a/extensions/typescript-language-features/src/tsServer/api.ts +++ b/extensions/typescript-language-features/src/tsServer/api.ts @@ -34,6 +34,7 @@ export class API { public static readonly v470 = API.fromSimpleString('4.7.0'); public static readonly v480 = API.fromSimpleString('4.8.0'); public static readonly v490 = API.fromSimpleString('4.9.0'); + public static readonly v510 = API.fromSimpleString('5.1.0'); public static fromVersionString(versionString: string): API { let version = semver.valid(versionString); diff --git a/extensions/typescript-language-features/src/tsServer/protocol/protocol.d.ts b/extensions/typescript-language-features/src/tsServer/protocol/protocol.d.ts index 0018bc10451df..d936a30e242ba 100644 --- a/extensions/typescript-language-features/src/tsServer/protocol/protocol.d.ts +++ b/extensions/typescript-language-features/src/tsServer/protocol/protocol.d.ts @@ -51,5 +51,16 @@ declare module 'typescript/lib/tsserverlibrary' { action: string; filepath: string; }; + + interface LinkedEditingRanges { + ranges: TextSpan[]; + wordPattern?: string; + } + + interface JsxLinkedEditRequest extends FileLocationRequest { } + + interface JsxLinkedEditResponse extends Response { + body?: LinkedEditingRanges; + } } } diff --git a/extensions/typescript-language-features/src/typescriptService.ts b/extensions/typescript-language-features/src/typescriptService.ts index 1a79016273c28..59061bb062c2c 100644 --- a/extensions/typescript-language-features/src/typescriptService.ts +++ b/extensions/typescript-language-features/src/typescriptService.ts @@ -76,6 +76,7 @@ interface StandardTsServerRequests { 'findSourceDefinition': [Proto.FileLocationRequestArgs, Proto.DefinitionResponse]; 'getMoveToRefactoringFileSuggestions': [Proto.GetMoveToRefactoringFileSuggestionsRequestArgs, Proto.GetMoveToRefactoringFileSuggestionsResponse]; 'getEditsForMoveToFileRefactor': [Proto.GetEditsForMoveToFileRefactorRequestArgs, Proto.GetEditsForMoveToFileRefactorResponse]; + 'jsxLinkedEdit': [Proto.FileLocationRequestArgs, Proto.JsxLinkedEditResponse]; } interface NoResponseTsServerRequests { From ee308dc7b287e041db6b8ec7ea9103948218b84f Mon Sep 17 00:00:00 2001 From: Matt Bierner Date: Mon, 3 Apr 2023 11:22:43 -0700 Subject: [PATCH 2/4] Update for new TS changes --- .../src/languageFeatures/jsxLinkedEditing.ts | 12 ++++++------ .../src/tsServer/protocol/protocol.d.ts | 10 ++++++---- .../src/tsServer/server.ts | 1 + .../src/typescriptService.ts | 2 +- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/extensions/typescript-language-features/src/languageFeatures/jsxLinkedEditing.ts b/extensions/typescript-language-features/src/languageFeatures/jsxLinkedEditing.ts index 618acc3d87ffb..1824624e51bb3 100644 --- a/extensions/typescript-language-features/src/languageFeatures/jsxLinkedEditing.ts +++ b/extensions/typescript-language-features/src/languageFeatures/jsxLinkedEditing.ts @@ -4,11 +4,11 @@ *--------------------------------------------------------------------------------------------*/ import * as vscode from 'vscode'; +import { DocumentSelector } from '../configuration/documentSelector'; +import { API } from '../tsServer/api'; +import * as typeConverters from '../typeConverters'; import { ClientCapability, ITypeScriptServiceClient } from '../typescriptService'; -import { conditionalRegistration, requireMinVersion, requireSomeCapability } from '../utils/dependentRegistration'; -import { DocumentSelector } from '../utils/documentSelector'; -import * as typeConverters from '../utils/typeConverters'; -import API from '../utils/api'; +import { conditionalRegistration, requireMinVersion, requireSomeCapability } from './util/dependentRegistration'; class JsxLinkedEditingSupport implements vscode.LinkedEditingRangeProvider { @@ -25,12 +25,12 @@ class JsxLinkedEditingSupport implements vscode.LinkedEditingRangeProvider { } const args = typeConverters.Position.toFileLocationRequestArgs(filepath, position); - const response = await this.client.execute('jsxLinkedEdit', args, token); + const response = await this.client.execute('LinkedEditing', args, token); if (response.type !== 'response' || !response.body) { return undefined; } - const wordPattern = response.body.wordPattern ? new RegExp(response.body.wordPattern) : undefined; + const wordPattern = undefined;//response.body.wordPattern ? new RegExp(response.body.wordPattern) : undefined; return new vscode.LinkedEditingRanges(response.body.ranges.map(range => typeConverters.Range.fromTextSpan(range)), wordPattern); } } diff --git a/extensions/typescript-language-features/src/tsServer/protocol/protocol.d.ts b/extensions/typescript-language-features/src/tsServer/protocol/protocol.d.ts index d936a30e242ba..09b39aba577fe 100644 --- a/extensions/typescript-language-features/src/tsServer/protocol/protocol.d.ts +++ b/extensions/typescript-language-features/src/tsServer/protocol/protocol.d.ts @@ -52,15 +52,17 @@ declare module 'typescript/lib/tsserverlibrary' { filepath: string; }; + interface LinkedEditingRequest extends FileLocationRequest { + readonly command: 'LinkedEditing'; + } + interface LinkedEditingRanges { ranges: TextSpan[]; wordPattern?: string; } - interface JsxLinkedEditRequest extends FileLocationRequest { } - - interface JsxLinkedEditResponse extends Response { - body?: LinkedEditingRanges; + interface LinkedEditingResponse extends Response { + readonly body: LinkedEditingRanges; } } } diff --git a/extensions/typescript-language-features/src/tsServer/server.ts b/extensions/typescript-language-features/src/tsServer/server.ts index d1dc0233e96b0..33f3701c8b42f 100644 --- a/extensions/typescript-language-features/src/tsServer/server.ts +++ b/extensions/typescript-language-features/src/tsServer/server.ts @@ -495,6 +495,7 @@ export class SyntaxRoutingTsServer extends Disposable implements ITypeScriptServ 'format', 'formatonkey', 'docCommentTemplate', + 'LinkedEditing' ]); /** diff --git a/extensions/typescript-language-features/src/typescriptService.ts b/extensions/typescript-language-features/src/typescriptService.ts index 59061bb062c2c..5407a2dba7f2e 100644 --- a/extensions/typescript-language-features/src/typescriptService.ts +++ b/extensions/typescript-language-features/src/typescriptService.ts @@ -76,7 +76,7 @@ interface StandardTsServerRequests { 'findSourceDefinition': [Proto.FileLocationRequestArgs, Proto.DefinitionResponse]; 'getMoveToRefactoringFileSuggestions': [Proto.GetMoveToRefactoringFileSuggestionsRequestArgs, Proto.GetMoveToRefactoringFileSuggestionsResponse]; 'getEditsForMoveToFileRefactor': [Proto.GetEditsForMoveToFileRefactorRequestArgs, Proto.GetEditsForMoveToFileRefactorResponse]; - 'jsxLinkedEdit': [Proto.FileLocationRequestArgs, Proto.JsxLinkedEditResponse]; + 'LinkedEditing': [Proto.FileLocationRequestArgs, Proto.LinkedEditingResponse]; } interface NoResponseTsServerRequests { From 91b6311979bdbccb0b45e32b343d8d2f9c0e5568 Mon Sep 17 00:00:00 2001 From: Matt Bierner Date: Mon, 10 Apr 2023 10:14:51 -0700 Subject: [PATCH 3/4] Update to finalized protocol --- .../src/languageFeatures/jsxLinkedEditing.ts | 4 ++-- .../src/tsServer/protocol/protocol.d.ts | 10 +++------- .../src/typescriptService.ts | 2 +- 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/extensions/typescript-language-features/src/languageFeatures/jsxLinkedEditing.ts b/extensions/typescript-language-features/src/languageFeatures/jsxLinkedEditing.ts index 1824624e51bb3..4c0c999e60b9a 100644 --- a/extensions/typescript-language-features/src/languageFeatures/jsxLinkedEditing.ts +++ b/extensions/typescript-language-features/src/languageFeatures/jsxLinkedEditing.ts @@ -25,12 +25,12 @@ class JsxLinkedEditingSupport implements vscode.LinkedEditingRangeProvider { } const args = typeConverters.Position.toFileLocationRequestArgs(filepath, position); - const response = await this.client.execute('LinkedEditing', args, token); + const response = await this.client.execute('linkedEditingRange', args, token); if (response.type !== 'response' || !response.body) { return undefined; } - const wordPattern = undefined;//response.body.wordPattern ? new RegExp(response.body.wordPattern) : undefined; + const wordPattern = response.body.wordPattern ? new RegExp(response.body.wordPattern) : undefined; return new vscode.LinkedEditingRanges(response.body.ranges.map(range => typeConverters.Range.fromTextSpan(range)), wordPattern); } } diff --git a/extensions/typescript-language-features/src/tsServer/protocol/protocol.d.ts b/extensions/typescript-language-features/src/tsServer/protocol/protocol.d.ts index 09b39aba577fe..dfcacb777a94c 100644 --- a/extensions/typescript-language-features/src/tsServer/protocol/protocol.d.ts +++ b/extensions/typescript-language-features/src/tsServer/protocol/protocol.d.ts @@ -52,17 +52,13 @@ declare module 'typescript/lib/tsserverlibrary' { filepath: string; }; - interface LinkedEditingRequest extends FileLocationRequest { - readonly command: 'LinkedEditing'; - } - - interface LinkedEditingRanges { + interface LinkedEditingRangesBody { ranges: TextSpan[]; wordPattern?: string; } - interface LinkedEditingResponse extends Response { - readonly body: LinkedEditingRanges; + interface LinkedEditingRangeResponse extends Response { + readonly body: LinkedEditingRangesBody; } } } diff --git a/extensions/typescript-language-features/src/typescriptService.ts b/extensions/typescript-language-features/src/typescriptService.ts index 5407a2dba7f2e..1050fc2e2a484 100644 --- a/extensions/typescript-language-features/src/typescriptService.ts +++ b/extensions/typescript-language-features/src/typescriptService.ts @@ -76,7 +76,7 @@ interface StandardTsServerRequests { 'findSourceDefinition': [Proto.FileLocationRequestArgs, Proto.DefinitionResponse]; 'getMoveToRefactoringFileSuggestions': [Proto.GetMoveToRefactoringFileSuggestionsRequestArgs, Proto.GetMoveToRefactoringFileSuggestionsResponse]; 'getEditsForMoveToFileRefactor': [Proto.GetEditsForMoveToFileRefactorRequestArgs, Proto.GetEditsForMoveToFileRefactorResponse]; - 'LinkedEditing': [Proto.FileLocationRequestArgs, Proto.LinkedEditingResponse]; + 'linkedEditingRange': [Proto.FileLocationRequestArgs, Proto.LinkedEditingRangeResponse]; } interface NoResponseTsServerRequests { From 89fc9dc9554644b45120394998c0e87ee2e33c0f Mon Sep 17 00:00:00 2001 From: Matt Bierner Date: Mon, 10 Apr 2023 15:14:55 -0700 Subject: [PATCH 4/4] Update request name --- extensions/typescript-language-features/src/tsServer/server.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extensions/typescript-language-features/src/tsServer/server.ts b/extensions/typescript-language-features/src/tsServer/server.ts index 33f3701c8b42f..421c5f5d8e9a6 100644 --- a/extensions/typescript-language-features/src/tsServer/server.ts +++ b/extensions/typescript-language-features/src/tsServer/server.ts @@ -495,7 +495,7 @@ export class SyntaxRoutingTsServer extends Disposable implements ITypeScriptServ 'format', 'formatonkey', 'docCommentTemplate', - 'LinkedEditing' + 'linkedEditingRange' ]); /**