Skip to content

Commit 7a16220

Browse files
author
zhanjuntao
committed
feat: 增强正则逻辑,处理命中id选择器情况
1 parent 4809101 commit 7a16220

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

inspect-extension/components/script-json/components/list.mpx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@ createComponent({
1515
</script>
1616

1717
<style lang="stylus">
18-
.list
19-
background-color #ccc
18+
#fff
19+
background #aaa
20+
#bbb #ccc
21+
border 1px solid #683434
22+
background #333
2023
</style>
2124

2225
<script type="application/json">

packages/language-service/src/plugins/mpx-sfc-style-css.ts

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import type * as CSS from 'vscode-css-languageservice'
1+
import * as CSS from 'vscode-css-languageservice'
22
import type { TextDocument } from 'vscode-languageserver-textdocument'
33
import {
4-
type Color,
54
type LanguageServicePlugin,
6-
TextEdit,
75
type VirtualCode,
86
} from '@volar/language-service'
97
import { type Provide, create as baseCreate } from 'volar-service-css'
@@ -13,13 +11,23 @@ import { MpxVirtualCode } from '@mpxjs/language-core'
1311
function parseStylusColors(document: TextDocument): CSS.ColorInformation[] {
1412
const colors: CSS.ColorInformation[] = []
1513
const text = document.getText()
16-
17-
const regex = /#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})\b/g
14+
const regex = /(?:^|[\s:,(])#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})(?![\w-])/g
1815
let match: RegExpExecArray | null
16+
1917
while ((match = regex.exec(text))) {
20-
const start = document.positionAt(match.index)
21-
const end = document.positionAt(match.index + match[0].length)
2218
const hex = match[1]
19+
20+
// 精确 range:找到 #hex 的起始位置
21+
const colorStartIndex = match.index + match[0].lastIndexOf(hex)
22+
const start = document.positionAt(colorStartIndex - 1) // 包含 #
23+
const end = document.positionAt(colorStartIndex - 1 + hex.length + 1)
24+
25+
// 获取当前行,排除 ID 选择器
26+
const lineStart = text.lastIndexOf('\n', match.index) + 1
27+
const line = text.slice(lineStart, text.indexOf('\n', match.index))
28+
if (/^\s*#\w/.test(line)) continue
29+
30+
// 转换颜色
2331
let r = 0,
2432
g = 0,
2533
b = 0
@@ -32,13 +40,11 @@ function parseStylusColors(document: TextDocument): CSS.ColorInformation[] {
3240
g = parseInt(hex.slice(2, 4), 16)
3341
b = parseInt(hex.slice(4, 6), 16)
3442
}
35-
const color: Color = {
36-
red: r / 255,
37-
green: g / 255,
38-
blue: b / 255,
39-
alpha: 1,
40-
}
41-
colors.push({ range: { start, end }, color })
43+
44+
colors.push({
45+
range: { start, end },
46+
color: { red: r / 255, green: g / 255, blue: b / 255, alpha: 1 },
47+
})
4248
}
4349

4450
return colors
@@ -53,10 +59,9 @@ function parseStylusColorPresentation(
5359
const g = Math.round(color.green * 255)
5460
const b = Math.round(color.blue * 255)
5561
const hex = `#${r.toString(16).padStart(2, '0')}${g.toString(16).padStart(2, '0')}${b.toString(16).padStart(2, '0')}`
56-
5762
colors.push({
5863
label: hex,
59-
textEdit: TextEdit.replace(range, hex),
64+
textEdit: CSS.TextEdit.replace(range, hex),
6065
})
6166
return colors
6267
}

0 commit comments

Comments
 (0)