Skip to content

Commit

Permalink
feat: support document highlights cross <script>, <template>
Browse files Browse the repository at this point in the history
close #462
  • Loading branch information
johnsoncodehk committed Aug 10, 2022
1 parent 9237d8b commit 5d994ad
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/vue-language-core/src/generators/script.ts
Expand Up @@ -125,7 +125,7 @@ export function generate(
}

return {
codeGen: codeGen,
codeGen,
teleports,
};

Expand Down
Expand Up @@ -2,6 +2,8 @@ import * as vscode from 'vscode-languageserver-protocol';
import type { LanguageServiceRuntimeContext } from '../types';
import * as shared from '@volar/shared';
import { languageFeatureWorker } from '../utils/featureWorkers';
import { TextDocument } from 'vscode-languageserver-textdocument';
import * as dedupe from '../utils/dedupe';

export function register(context: LanguageServiceRuntimeContext) {

Expand All @@ -15,12 +17,63 @@ export function register(context: LanguageServiceRuntimeContext) {
for (const [mappedRange] of sourceMap.getMappedRanges(
position,
position,
data => !!data.capabilities.semanticTokens,
data => !!data.capabilities.references,
)) {
yield mappedRange.start;
}
},
(plugin, document, position) => plugin.findDocumentHighlights?.(document, position),
async (plugin, document, position, sourceMap, vueDocument) => {

const recursiveChecker = dedupe.createLocationSet();
const result: vscode.DocumentHighlight[] = [];

await withTeleports(document, position);

return result;

async function withTeleports(document: TextDocument, position: vscode.Position) {

if (!plugin.findDocumentHighlights)
return;

if (recursiveChecker.has({ uri: document.uri, range: { start: position, end: position } }))
return;

recursiveChecker.add({ uri: document.uri, range: { start: position, end: position } });

const references = await plugin.findDocumentHighlights(document, position) ?? [];

for (const reference of references) {

let foundTeleport = false;

recursiveChecker.add({ uri: document.uri, range: { start: reference.range.start, end: reference.range.start } });

const teleport = context.vueDocuments.teleportfromEmbeddedDocumentUri(document.uri);

if (teleport) {

for (const [teleRange] of teleport.findTeleports(
reference.range.start,
reference.range.end,
sideData => !!sideData.capabilities.references,
)) {

if (recursiveChecker.has({ uri: teleport.document.uri, range: { start: teleRange.start, end: teleRange.start } }))
continue;

foundTeleport = true;

await withTeleports(teleport.document, teleRange.start);
}
}

if (!foundTeleport) {
result.push(reference);
}
}
}
},
(data, sourceMap) => data.map(highlisht => {

if (!sourceMap)
Expand Down
Expand Up @@ -74,7 +74,7 @@ export function register(context: LanguageServiceRuntimeContext) {
for (const [teleRange, sideData] of teleport.findTeleports(
textEdit.range.start,
textEdit.range.end,
sideData => !!sideData.capabilities.references,
sideData => !!sideData.capabilities.rename,
)) {

if (recursiveChecker.has({ uri: teleport.document.uri, range: { start: teleRange.start, end: teleRange.start } }))
Expand Down

0 comments on commit 5d994ad

Please sign in to comment.