Skip to content

Commit 759e8fb

Browse files
authored
Merge pull request #3169 from seblyng/document-color
feat: support hex color with `#`
2 parents 606ea70 + dc5acf1 commit 759e8fb

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

changelog.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Unreleased
44
<!-- Add all new changes here. They will be moved under a version at release -->
55
* `FIX` cannot debug in Linux due to lua-debug expecting host process to have lua54 symbols available
6+
* `FIX` support hex color codes with `#` in `textDocument/documentColor`
67

78
## 3.14.0
89
`2025-4-7`

script/core/color.lua

+22-4
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@ local files = require "files"
22
local guide = require "parser.guide"
33

44
local colorPattern = string.rep('%x', 8)
5+
local hex6Pattern = string.format("^#%s", string.rep('%x', 6))
56
---@param source parser.object
67
---@return boolean
78
local function isColor(source)
89
---@type string
910
local text = source[1]
10-
if text:len() ~= 8 then
11-
return false
11+
if text:len() == 8 then
12+
return text:match(colorPattern)
1213
end
13-
return text:match(colorPattern)
14+
15+
if text:len() == 7 then
16+
return text:match(hex6Pattern)
17+
end
18+
19+
return false
1420
end
1521

1622

@@ -25,6 +31,16 @@ local function textToColor(colorText)
2531
}
2632
end
2733

34+
---@param colorText string
35+
---@return Color
36+
local function hexTextToColor(colorText)
37+
return {
38+
alpha = 255,
39+
red = tonumber(colorText:sub(2, 3), 16) / 255,
40+
green = tonumber(colorText:sub(4, 5), 16) / 255,
41+
blue = tonumber(colorText:sub(6, 7), 16) / 255,
42+
}
43+
end
2844

2945
---@param color Color
3046
---@return string
@@ -63,10 +79,12 @@ local function colors(uri)
6379
---@type string
6480
local colorText = source[1]
6581

82+
local color = colorText:match(colorPattern) and textToColor(colorText) or hexTextToColor(colorText)
83+
6684
colorValues[#colorValues+1] = {
6785
start = source.start + 1,
6886
finish = source.finish - 1,
69-
color = textToColor(colorText)
87+
color = color
7088
}
7189
end
7290
end)

0 commit comments

Comments
 (0)