|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import * as vscode from 'vscode'; |
| 7 | +import { DocumentSelector } from '../configuration/documentSelector'; |
| 8 | +import { API } from '../tsServer/api'; |
| 9 | +import * as typeConverters from '../typeConverters'; |
| 10 | +import { ClientCapability, ITypeScriptServiceClient } from '../typescriptService'; |
| 11 | +import { conditionalRegistration, requireMinVersion, requireSomeCapability } from './util/dependentRegistration'; |
| 12 | + |
| 13 | +class JsxLinkedEditingSupport implements vscode.LinkedEditingRangeProvider { |
| 14 | + |
| 15 | + public static readonly minVersion = API.v510; |
| 16 | + |
| 17 | + public constructor( |
| 18 | + private readonly client: ITypeScriptServiceClient |
| 19 | + ) { } |
| 20 | + |
| 21 | + async provideLinkedEditingRanges(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken): Promise<vscode.LinkedEditingRanges | undefined> { |
| 22 | + const filepath = this.client.toOpenTsFilePath(document); |
| 23 | + if (!filepath) { |
| 24 | + return undefined; |
| 25 | + } |
| 26 | + |
| 27 | + const args = typeConverters.Position.toFileLocationRequestArgs(filepath, position); |
| 28 | + const response = await this.client.execute('linkedEditingRange', args, token); |
| 29 | + if (response.type !== 'response' || !response.body) { |
| 30 | + return undefined; |
| 31 | + } |
| 32 | + |
| 33 | + const wordPattern = response.body.wordPattern ? new RegExp(response.body.wordPattern) : undefined; |
| 34 | + return new vscode.LinkedEditingRanges(response.body.ranges.map(range => typeConverters.Range.fromTextSpan(range)), wordPattern); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +export function register( |
| 39 | + selector: DocumentSelector, |
| 40 | + client: ITypeScriptServiceClient |
| 41 | +) { |
| 42 | + return conditionalRegistration([ |
| 43 | + requireMinVersion(client, JsxLinkedEditingSupport.minVersion), |
| 44 | + requireSomeCapability(client, ClientCapability.Syntax), |
| 45 | + ], () => { |
| 46 | + return vscode.languages.registerLinkedEditingRangeProvider(selector.semantic, |
| 47 | + new JsxLinkedEditingSupport(client)); |
| 48 | + }); |
| 49 | +} |
0 commit comments