Skip to content

Commit 0800ec0

Browse files
committed
feat: review
1 parent df04309 commit 0800ec0

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

inspect-extension/components/stylus/basic01.mpx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,12 @@
5151
// 注释
5252
display flex // 注释
5353
background-color #f0f0f0
54-
color #ecc721
54+
color: #ecc721;
5555
color red
5656

5757
#fff
5858
background #1673d7
59+
background#1673d7
5960

6061
#bbb #ccc
6162
border 1px solid #c72121

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

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ import {
1212
isValue,
1313
} from '../utils/stylus'
1414

15-
// Examples matched: "color #fff", "color: #fff;", "color:#fff", " background #aaa"
16-
// Examples not matched: "#fff" at line start, "#id", selectors like "a #abc", "#bbb #ccc" (second #)
15+
// Match Stylus property values containing hex colors
16+
// Examples matched: "color #fff", "color: #fff", "background #aaa", "border-color:#333"
17+
// Examples not matched: "#fff" at line start, selectors like "a #abc", "#bbb #ccc"
1718
const stylusColorRegex =
18-
/(?<!^)(?<!^\s*)(?<!#[0-9a-fA-F]{0,6}\s)(?<=:| )#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})(?![0-9a-fA-F])/gm
19+
/^(\s*)([a-zA-Z][\w-]*)\s*:?\s+(.*?#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})(?![0-9a-fA-F]).*?)$/gm
1920

2021
export function create(): LanguageServicePlugin {
2122
const cssBuiltinData = CSS.getDefaultCSSDataProvider()
@@ -176,12 +177,23 @@ function parseStylusColors(document: TextDocument): CSS.ColorInformation[] {
176177
const text = document.getText()
177178
let match: RegExpExecArray | null
178179
while ((match = stylusColorRegex.exec(text))) {
179-
const start = document.positionAt(match.index)
180-
const end = document.positionAt(match.index + match[0].length)
181-
const hex = match[1]
182-
let r = 0,
183-
g = 0,
184-
b = 0
180+
const fullMatch = match[0]
181+
const valueWithColor = match[3] // The part containing the hex color
182+
const hex = match[4] // The captured hex digits
183+
184+
// Find the position of the # in the value part
185+
const colorIndex = valueWithColor.indexOf('#')
186+
if (colorIndex === -1) continue
187+
188+
// Calculate the absolute position in the document
189+
const lineStartIndex = match.index
190+
const colorStartIndex =
191+
lineStartIndex + fullMatch.indexOf(valueWithColor) + colorIndex
192+
const colorEndIndex = colorStartIndex + 1 + hex.length // +1 for the #
193+
const start = document.positionAt(colorStartIndex)
194+
const end = document.positionAt(colorEndIndex)
195+
let [r, g, b] = [0, 0, 0]
196+
185197
if (hex.length === 3) {
186198
r = parseInt(hex[0] + hex[0], 16)
187199
g = parseInt(hex[1] + hex[1], 16)

0 commit comments

Comments
 (0)