-
-
Notifications
You must be signed in to change notification settings - Fork 377
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(language-service): re-support scoped class links in template (#4357
- Loading branch information
1 parent
e50c882
commit b23e5ba
Showing
9 changed files
with
114 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
packages/language-service/lib/plugins/vue-document-links.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import type { LanguageServicePlugin, LanguageServicePluginInstance } from '@volar/language-service'; | ||
import { Sfc, VueVirtualCode } from '@vue/language-core'; | ||
import { tsCodegen } from '@vue/language-core/lib/plugins/vue-tsx'; | ||
import type * as vscode from 'vscode-languageserver-protocol'; | ||
|
||
export function create(): LanguageServicePlugin { | ||
return { | ||
name: 'vue-document-links', | ||
create(context): LanguageServicePluginInstance { | ||
return { | ||
provideDocumentLinks(document) { | ||
|
||
const decoded = context.decodeEmbeddedDocumentUri(document.uri); | ||
const sourceScript = decoded && context.language.scripts.get(decoded[0]); | ||
const virtualCode = decoded && sourceScript?.generated?.embeddedCodes.get(decoded[1]); | ||
|
||
if (sourceScript?.generated?.root instanceof VueVirtualCode && virtualCode?.id === 'template') { | ||
|
||
const result: vscode.DocumentLink[] = []; | ||
const codegen = tsCodegen.get(sourceScript.generated.root.sfc); | ||
const scopedClasses = codegen?.generatedTemplate()?.scopedClasses ?? []; | ||
const styleClasses = new Map<string, { | ||
index: number; | ||
style: Sfc['styles'][number]; | ||
classOffset: number; | ||
}[]>(); | ||
const option = sourceScript.generated.root.vueCompilerOptions.experimentalResolveStyleCssClasses; | ||
|
||
for (let i = 0; i < sourceScript.generated.root.sfc.styles.length; i++) { | ||
const style = sourceScript.generated.root.sfc.styles[i]; | ||
if (option === 'always' || (option === 'scoped' && style.scoped)) { | ||
for (const className of style.classNames) { | ||
if (!styleClasses.has(className.text.substring(1))) { | ||
styleClasses.set(className.text.substring(1), []); | ||
} | ||
styleClasses.get(className.text.substring(1))!.push({ | ||
index: i, | ||
style, | ||
classOffset: className.offset, | ||
}); | ||
} | ||
} | ||
} | ||
|
||
for (const { className, offset } of scopedClasses) { | ||
const styles = styleClasses.get(className); | ||
if (styles) { | ||
for (const style of styles) { | ||
const styleDocumentUri = context.encodeEmbeddedDocumentUri(decoded![0], 'style_' + style.index); | ||
const styleVirtualCode = sourceScript.generated.embeddedCodes.get('style_' + style.index); | ||
if (!styleVirtualCode) { | ||
continue; | ||
} | ||
const styleDocument = context.documents.get(styleDocumentUri, styleVirtualCode.languageId, styleVirtualCode.snapshot); | ||
const start = styleDocument.positionAt(style.classOffset); | ||
const end = styleDocument.positionAt(style.classOffset + className.length + 1); | ||
result.push({ | ||
range: { | ||
start: document.positionAt(offset), | ||
end: document.positionAt(offset + className.length), | ||
}, | ||
target: context.encodeEmbeddedDocumentUri(decoded![0], 'style_' + style.index) + `#L${start.line + 1},${start.character + 1}-L${end.line + 1},${end.character + 1}`, | ||
}); | ||
} | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
}, | ||
}; | ||
}, | ||
}; | ||
} |