|
| 1 | +import type * as CSS from 'vscode-css-languageservice' |
| 2 | +import type { TextDocument } from 'vscode-languageserver-textdocument' |
| 3 | +import type { |
| 4 | + LanguageServicePlugin, |
| 5 | + VirtualCode, |
| 6 | +} from '@volar/language-service' |
| 7 | +import { type Provide, create as baseCreate } from 'volar-service-css' |
| 8 | +import { URI } from 'vscode-uri' |
| 9 | +import { MpxVirtualCode } from '@mpxjs/language-core' |
| 10 | + |
| 11 | +export function create(): LanguageServicePlugin { |
| 12 | + const base = baseCreate({ |
| 13 | + scssDocumentSelector: ['scss', 'postcss'], |
| 14 | + }) |
| 15 | + |
| 16 | + return { |
| 17 | + ...base, |
| 18 | + name: 'mpx-css', |
| 19 | + create(context) { |
| 20 | + const baseInstance = base.create(context) |
| 21 | + const { |
| 22 | + 'css/languageService': getCssLs, |
| 23 | + 'css/stylesheet': getStylesheet, |
| 24 | + } = baseInstance.provide as Provide |
| 25 | + |
| 26 | + return { |
| 27 | + ...baseInstance, |
| 28 | + async provideDiagnostics(document, token) { |
| 29 | + let diagnostics = |
| 30 | + (await baseInstance.provideDiagnostics?.(document, token)) ?? [] |
| 31 | + if (document.languageId === 'postcss') { |
| 32 | + diagnostics = diagnostics.filter( |
| 33 | + diag => diag.code !== 'css-semicolonexpected', |
| 34 | + ) |
| 35 | + diagnostics = diagnostics.filter( |
| 36 | + diag => diag.code !== 'css-ruleorselectorexpected', |
| 37 | + ) |
| 38 | + diagnostics = diagnostics.filter( |
| 39 | + diag => diag.code !== 'unknownAtRules', |
| 40 | + ) |
| 41 | + } |
| 42 | + return diagnostics |
| 43 | + }, |
| 44 | + /** |
| 45 | + * If the editing position is within the virtual code and navigation is enabled, |
| 46 | + * skip the CSS renaming feature. |
| 47 | + */ |
| 48 | + provideRenameRange(document, position) { |
| 49 | + do { |
| 50 | + const uri = URI.parse(document.uri) |
| 51 | + const decoded = context.decodeEmbeddedDocumentUri(uri) |
| 52 | + const sourceScript = |
| 53 | + decoded && context.language.scripts.get(decoded[0]) |
| 54 | + const virtualCode = |
| 55 | + decoded && sourceScript?.generated?.embeddedCodes.get(decoded[1]) |
| 56 | + if ( |
| 57 | + !sourceScript?.generated || |
| 58 | + !virtualCode?.id.startsWith('style_') |
| 59 | + ) { |
| 60 | + break |
| 61 | + } |
| 62 | + |
| 63 | + const root = sourceScript.generated.root |
| 64 | + if (!(root instanceof MpxVirtualCode)) { |
| 65 | + break |
| 66 | + } |
| 67 | + |
| 68 | + const block = root.sfc.styles.find( |
| 69 | + style => style.name === decoded![1], |
| 70 | + ) |
| 71 | + if (!block) { |
| 72 | + break |
| 73 | + } |
| 74 | + |
| 75 | + let script: VirtualCode | undefined |
| 76 | + for (const [key, value] of sourceScript.generated.embeddedCodes) { |
| 77 | + if (key.startsWith('script_')) { |
| 78 | + script = value |
| 79 | + break |
| 80 | + } |
| 81 | + } |
| 82 | + if (!script) { |
| 83 | + break |
| 84 | + } |
| 85 | + |
| 86 | + const offset = document.offsetAt(position) + block.startTagEnd |
| 87 | + for (const { sourceOffsets, lengths, data } of script.mappings) { |
| 88 | + if ( |
| 89 | + !sourceOffsets.length || |
| 90 | + !data.navigation || |
| 91 | + (typeof data.navigation === 'object' && |
| 92 | + !data.navigation.shouldRename) |
| 93 | + ) { |
| 94 | + continue |
| 95 | + } |
| 96 | + |
| 97 | + const start = sourceOffsets[0] |
| 98 | + const end = sourceOffsets.at(-1)! + lengths.at(-1)! |
| 99 | + |
| 100 | + if (offset >= start && offset <= end) { |
| 101 | + return |
| 102 | + } |
| 103 | + } |
| 104 | + // eslint-disable-next-line no-constant-condition |
| 105 | + } while (0) |
| 106 | + |
| 107 | + return worker(document, (stylesheet, cssLs) => { |
| 108 | + return cssLs.prepareRename(document, position, stylesheet) |
| 109 | + }) |
| 110 | + }, |
| 111 | + } |
| 112 | + |
| 113 | + function worker<T>( |
| 114 | + document: TextDocument, |
| 115 | + callback: (stylesheet: CSS.Stylesheet, cssLs: CSS.LanguageService) => T, |
| 116 | + ) { |
| 117 | + const cssLs = getCssLs(document) |
| 118 | + if (!cssLs) { |
| 119 | + return |
| 120 | + } |
| 121 | + return callback(getStylesheet(document, cssLs), cssLs) |
| 122 | + } |
| 123 | + }, |
| 124 | + } |
| 125 | +} |
0 commit comments