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

Add jsx linked editing #176279

Merged
merged 4 commits into from
Apr 10, 2023
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
Original file line number Diff line number Diff line change
@@ -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 { DocumentSelector } from '../configuration/documentSelector';
import { API } from '../tsServer/api';
import * as typeConverters from '../typeConverters';
import { ClientCapability, ITypeScriptServiceClient } from '../typescriptService';
import { conditionalRegistration, requireMinVersion, requireSomeCapability } from './util/dependentRegistration';

class JsxLinkedEditingSupport implements vscode.LinkedEditingRangeProvider {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit - you currently don't limit this to any JSX-specific file, so maybe we rename this to LinkedEditingSupport and just leave a comment about the main use-case being JSX tags?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Will follow up with these changes in a new PR


public static readonly minVersion = API.v510;

public constructor(
private readonly client: ITypeScriptServiceClient
) { }

async provideLinkedEditingRanges(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken): Promise<vscode.LinkedEditingRanges | undefined> {
const filepath = this.client.toOpenTsFilePath(document);
if (!filepath) {
return undefined;
}

const args = typeConverters.Position.toFileLocationRequestArgs(filepath, position);
const response = await this.client.execute('linkedEditingRange', 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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be selector.syntax?

new JsxLinkedEditingSupport(client));
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -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))),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,14 @@ declare module 'typescript/lib/tsserverlibrary' {
action: string;
filepath: string;
};

interface LinkedEditingRangesBody {
ranges: TextSpan[];
wordPattern?: string;
}

interface LinkedEditingRangeResponse extends Response {
readonly body: LinkedEditingRangesBody;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,7 @@ export class SyntaxRoutingTsServer extends Disposable implements ITypeScriptServ
'format',
'formatonkey',
'docCommentTemplate',
'linkedEditingRange'
]);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ interface StandardTsServerRequests {
'findSourceDefinition': [Proto.FileLocationRequestArgs, Proto.DefinitionResponse];
'getMoveToRefactoringFileSuggestions': [Proto.GetMoveToRefactoringFileSuggestionsRequestArgs, Proto.GetMoveToRefactoringFileSuggestionsResponse];
'getEditsForMoveToFileRefactor': [Proto.GetEditsForMoveToFileRefactorRequestArgs, Proto.GetEditsForMoveToFileRefactorResponse];
'linkedEditingRange': [Proto.FileLocationRequestArgs, Proto.LinkedEditingRangeResponse];
}

interface NoResponseTsServerRequests {
Expand Down