Skip to content

Commit 5fc35b6

Browse files
authored
fix(language-core): skip css references for position within virtual code with navigation: true (#5378)
1 parent 7aa44b7 commit 5fc35b6

File tree

1 file changed

+67
-49
lines changed
  • packages/language-service/lib/plugins

1 file changed

+67
-49
lines changed

packages/language-service/lib/plugins/css.ts

Lines changed: 67 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -28,66 +28,84 @@ export function create(): LanguageServicePlugin {
2828
return diagnostics;
2929
},
3030
/**
31-
* If the editing position is within the virtual code and navigation is enabled,
32-
* skip the CSS renaming feature.
31+
* If the position is within the virtual code and navigation is enabled,
32+
* skip the CSS navigation feature.
3333
*/
34+
provideReferences(document, position) {
35+
if (isWithinNavigationVirtualCode(document, position)) {
36+
return;
37+
}
38+
return worker(document, (stylesheet, cssLs) => {
39+
return cssLs.findReferences(document, position, stylesheet);
40+
});
41+
},
3442
provideRenameRange(document, position) {
35-
do {
36-
const uri = URI.parse(document.uri);
37-
const decoded = context.decodeEmbeddedDocumentUri(uri);
38-
const sourceScript = decoded && context.language.scripts.get(decoded[0]);
39-
const virtualCode = decoded && sourceScript?.generated?.embeddedCodes.get(decoded[1]);
40-
if (!sourceScript?.generated || !virtualCode?.id.startsWith('style_')) {
41-
break;
42-
}
43+
if (isWithinNavigationVirtualCode(document, position)) {
44+
return;
45+
}
46+
return worker(document, (stylesheet, cssLs) => {
47+
return cssLs.prepareRename(document, position, stylesheet);
48+
});
49+
}
50+
};
4351

44-
const root = sourceScript.generated.root;
45-
if (!(root instanceof VueVirtualCode)) {
46-
break;
47-
}
52+
function isWithinNavigationVirtualCode(
53+
document: TextDocument,
54+
position: css.Position
55+
) {
56+
const uri = URI.parse(document.uri);
57+
const decoded = context.decodeEmbeddedDocumentUri(uri);
58+
const sourceScript = decoded && context.language.scripts.get(decoded[0]);
59+
const virtualCode = decoded && sourceScript?.generated?.embeddedCodes.get(decoded[1]);
60+
if (!sourceScript?.generated || !virtualCode?.id.startsWith('style_')) {
61+
return false;
62+
}
4863

49-
const block = root.sfc.styles.find(style => style.name === decoded![1]);
50-
if (!block) {
51-
break;
52-
}
64+
const root = sourceScript.generated.root;
65+
if (!(root instanceof VueVirtualCode)) {
66+
return false;
67+
}
5368

54-
let script: VirtualCode | undefined;
55-
for (const [key, value] of sourceScript.generated.embeddedCodes) {
56-
if (key.startsWith('script_')) {
57-
script = value;
58-
break;
59-
}
60-
}
61-
if (!script) {
62-
break;
63-
}
69+
const block = root.sfc.styles.find(style => style.name === decoded![1]);
70+
if (!block) {
71+
return false;
72+
}
6473

65-
const offset = document.offsetAt(position) + block.startTagEnd;
66-
for (const { sourceOffsets, lengths, data } of script.mappings) {
67-
if (
68-
!sourceOffsets.length
69-
|| !data.navigation
70-
|| typeof data.navigation === 'object' && !data.navigation.shouldRename
71-
) {
72-
continue;
73-
}
74+
let script: VirtualCode | undefined;
75+
for (const [key, value] of sourceScript.generated.embeddedCodes) {
76+
if (key.startsWith('script_')) {
77+
script = value;
78+
break;
79+
}
80+
}
81+
if (!script) {
82+
return false;
83+
}
7484

75-
const start = sourceOffsets[0];
76-
const end = sourceOffsets.at(-1)! + lengths.at(-1)!;
85+
const offset = document.offsetAt(position) + block.startTagEnd;
86+
for (const { sourceOffsets, lengths, data } of script.mappings) {
87+
if (
88+
!sourceOffsets.length
89+
|| !data.navigation
90+
|| typeof data.navigation === 'object' && !data.navigation.shouldRename
91+
) {
92+
continue;
93+
}
7794

78-
if (offset >= start && offset <= end) {
79-
return;
80-
}
81-
}
82-
} while (0);
95+
const start = sourceOffsets[0];
96+
const end = sourceOffsets.at(-1)! + lengths.at(-1)!;
8397

84-
return worker(document, (stylesheet, cssLs) => {
85-
return cssLs.prepareRename(document, position, stylesheet);
86-
});
98+
if (offset >= start && offset <= end) {
99+
return true;
100+
}
87101
}
88-
};
102+
return false;
103+
}
89104

90-
function worker<T>(document: TextDocument, callback: (stylesheet: css.Stylesheet, cssLs: css.LanguageService) => T) {
105+
function worker<T>(
106+
document: TextDocument,
107+
callback: (stylesheet: css.Stylesheet, cssLs: css.LanguageService) => T
108+
) {
91109
const cssLs = getCssLs(document);
92110
if (!cssLs) {
93111
return;

0 commit comments

Comments
 (0)