Skip to content

Commit

Permalink
feat: prepare linked editing feature
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Dec 10, 2020
1 parent 12bdfb7 commit ec3aa66
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 2 deletions.
14 changes: 14 additions & 0 deletions packages/client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
import {
TagCloseRequest,
TagEditRequest,
// LinkedEditingRangeRequest,
VerifyAllScriptsRequest,
FormatAllScriptsRequest,
WriteVirtualFilesRequest,
Expand All @@ -38,6 +39,19 @@ export async function activate(context: vscode.ExtensionContext) {
await apiClient.onReady();
await docClient.onReady();

// context.subscriptions.push(vscode.languages.registerOnTypeRenameProvider({ language: 'vue' }, {
// async provideOnTypeRenameRanges(document, position) {
// const param = apiClient.code2ProtocolConverter.asTextDocumentPositionParams(document, position);
// return apiClient.sendRequest(OnTypeRenameRequest.type, param).then(response => {
// if (response) {
// return {
// ranges: response.map(r => apiClient.protocol2CodeConverter.asRange(r))
// };
// }
// return undefined;
// });
// }
// }));
context.subscriptions.push(activateTagClosing(tagRequestor, { vue: true }, 'html.autoClosingTags'));
context.subscriptions.push(activateTagEditing(tagEditRequestor, { vue: true }, 'volar.html.autoEditingTags'));
context.subscriptions.push(vscode.commands.registerCommand('volar.action.restartServer', () => {
Expand Down
6 changes: 6 additions & 0 deletions packages/server/src/apiServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
GetFormattingSourceMapsRequest,
uriToFsPath,
RestartServerNotification,
LinkedEditingRangeRequest,
} from '@volar/shared';
import * as upath from 'upath';

Expand Down Expand Up @@ -137,6 +138,11 @@ function initLanguageService(rootPath: string) {
if (!document) return;
return host.best(document.uri)?.doAutoClose(document, handler.position);
});
connection.onRequest(LinkedEditingRangeRequest.type, handler => {
const document = documents.get(handler.textDocument.uri);
if (!document) return;
return host.best(document.uri)?.findLinkedEditingRanges(document, handler.position);
});
connection.onRequest(TagEditRequest.type, handler => {
const document = documents.get(handler.textDocument.uri);
if (!document) return;
Expand Down
3 changes: 3 additions & 0 deletions packages/shared/src/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ export namespace D3Request {
export namespace TagCloseRequest {
export const type: RequestType<TextDocumentPositionParams, string | null | undefined, any> = new RequestType('html/tag');
}
export namespace LinkedEditingRangeRequest {
export const type: RequestType<TextDocumentPositionParams, Range[] | null, any> = new RequestType('html/onTypeRename');
}
export namespace TagEditRequest {
export const type: RequestType<{
textDocument: TextDocumentIdentifier,
Expand Down
6 changes: 4 additions & 2 deletions packages/vscode-vue-languageservice/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import * as codeLens from './services/codeLens';
import * as codeLensResolve from './services/codeLensResolve';
import * as executeCommand from './services/executeCommand';
import * as callHierarchy from './services/callHierarchy';
import * as linkedEditingRanges from './services/linkedEditingRanges';
import * as d3 from './services/d3';
import { CompletionItem } from 'vscode-css-languageservice';

Expand Down Expand Up @@ -224,17 +225,18 @@ export function createLanguageService(vueHost: ts.LanguageServiceHost) {
doCompletionResolve: apiHook(completionResolve.register(sourceFiles, tsLanguageService), false),
doAutoClose: apiHook(autoClose.register(sourceFiles), false),
doAutoEditTag: apiHook(autoEditTag.register(sourceFiles), false),
doCodeLensResolve: apiHook(codeLensResolve.register(sourceFiles, tsLanguageService), false),
getEmbeddedDocument: apiHook(embeddedDocument.register(sourceFiles), false),
getSignatureHelp: apiHook(signatureHelp.register(sourceFiles, tsLanguageService), false),
getSelectionRanges: apiHook(selectionRanges.register(sourceFiles, tsLanguageService), false),
getColorPresentations: apiHook(colorPresentations.register(sourceFiles), false),
getCodeLens: apiHook(codeLens.register(sourceFiles), false),
doCodeLensResolve: apiHook(codeLensResolve.register(sourceFiles, tsLanguageService), false),
getFoldingRanges: apiHook(foldingRanges.register(sourceFiles, tsLanguageService), false),
findDocumentHighlights: apiHook(documentHighlight.register(sourceFiles, tsLanguageService), false),
findDocumentSymbols: apiHook(documentSymbol.register(sourceFiles, tsLanguageService), false),
findDocumentLinks: apiHook(documentLink.register(sourceFiles, vueHost), false),
findDocumentColors: apiHook(documentColor.register(sourceFiles), false),
getFoldingRanges: apiHook(foldingRanges.register(sourceFiles, tsLanguageService), false),
findLinkedEditingRanges: apiHook(linkedEditingRanges.register(sourceFiles), false),
dispose: tsLanguageService.dispose,
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {
TextDocument,
Position,
} from 'vscode-languageserver/node';
import { SourceFile } from '../sourceFiles';
import * as globalServices from '../globalServices';

export function register(sourceFiles: Map<string, SourceFile>) {
return (document: TextDocument, position: Position) => {
const sourceFile = sourceFiles.get(document.uri);
if (!sourceFile) return;

const htmlResult = getHtmlResult(sourceFile);
if (htmlResult) return htmlResult;

function getHtmlResult(sourceFile: SourceFile) {
const vueHtmlDoc = sourceFile.getVueHtmlDocument();
return globalServices.html.findLinkedEditingRanges(document, position, vueHtmlDoc);
}
}
}

0 comments on commit ec3aa66

Please sign in to comment.