From 35a37a005127063703a06c6aca122b118e67c075 Mon Sep 17 00:00:00 2001 From: MrZ_26 <1046101471@qq.com> Date: Sat, 7 Jun 2025 11:07:57 +0800 Subject: [PATCH 1/3] feat: support rgb6 format --- script/core/color.lua | 62 ++++++++++++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 22 deletions(-) diff --git a/script/core/color.lua b/script/core/color.lua index a189234a7..32fadb646 100644 --- a/script/core/color.lua +++ b/script/core/color.lua @@ -1,28 +1,33 @@ local files = require "files" local guide = require "parser.guide" -local colorPattern = string.rep('%x', 8) -local hex6Pattern = string.format("^#%s", string.rep('%x', 6)) +---@enum (key) ColorMode +local colorPattern = { + argb8 = "^%x%x%x%x%x%x%x%x$", + hexrgb6 = "^#%x%x%x%x%x%x$", + rgb6 = "^%x%x%x%x%x%x$", +} + ---@param source parser.object ----@return boolean -local function isColor(source) +---@return ColorMode | false +local function getColorMode(source) ---@type string local text = source[1] - if text:len() == 8 then - return text:match(colorPattern) - end - if text:len() == 7 then - return text:match(hex6Pattern) + for k,v in next,colorPattern do + if text:match(v) then + return k + end end return false end +local textToColor = {} ---@param colorText string ---@return Color -local function textToColor(colorText) +function textToColor.argb8(colorText) return { alpha = tonumber(colorText:sub(1, 2), 16) / 255, red = tonumber(colorText:sub(3, 4), 16) / 255, @@ -33,15 +38,26 @@ end ---@param colorText string ---@return Color -local function hexTextToColor(colorText) +function textToColor.hexrgb6(colorText) return { - alpha = 255, + alpha = 1, red = tonumber(colorText:sub(2, 3), 16) / 255, green = tonumber(colorText:sub(4, 5), 16) / 255, blue = tonumber(colorText:sub(6, 7), 16) / 255, } end +---@param colorText string +---@return Color +function textToColor.rgb6(colorText) + return { + alpha = 1, + red = tonumber(colorText:sub(1, 2), 16) / 255, + green = tonumber(colorText:sub(3, 4), 16) / 255, + blue = tonumber(colorText:sub(5, 6), 16) / 255, + } +end + ---@param color Color ---@return string local function colorToText(color) @@ -75,17 +91,19 @@ local function colors(uri) local colorValues = {} guide.eachSource(state.ast, function (source) ---@async - if source.type == 'string' and isColor(source) then - ---@type string - local colorText = source[1] - - local color = colorText:match(colorPattern) and textToColor(colorText) or hexTextToColor(colorText) + if source.type == 'string' then + local colorMode = getColorMode(source) + if colorMode then + ---@type string + local colorText = source[1] + local color = textToColor[colorMode](colorText) - colorValues[#colorValues+1] = { - start = source.start + 1, - finish = source.finish - 1, - color = color - } + colorValues[#colorValues+1] = { + start = source.start + 1, + finish = source.finish - 1, + color = color, + } + end end end) return colorValues From c5f7e880a83e26518ec76af6c865c610a8e2dcda Mon Sep 17 00:00:00 2001 From: MrZ_26 <1046101471@qq.com> Date: Sun, 8 Jun 2025 06:46:20 +0800 Subject: [PATCH 2/3] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit in next,t改in pairs(t),写习惯了 Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- script/core/color.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/script/core/color.lua b/script/core/color.lua index 32fadb646..97a3b3c65 100644 --- a/script/core/color.lua +++ b/script/core/color.lua @@ -14,7 +14,7 @@ local function getColorMode(source) ---@type string local text = source[1] - for k,v in next,colorPattern do + for k,v in pairs(colorPattern) do if text:match(v) then return k end From c91cb8d63e19f0ccd63af42c745afb97c286ee87 Mon Sep 17 00:00:00 2001 From: MrZ_26 <1046101471@qq.com> Date: Sun, 8 Jun 2025 06:49:26 +0800 Subject: [PATCH 3/3] update changelog.md --- changelog.md | 1 + 1 file changed, 1 insertion(+) diff --git a/changelog.md b/changelog.md index d929b7919..dc1ceb94f 100644 --- a/changelog.md +++ b/changelog.md @@ -4,6 +4,7 @@ * `FIX` cannot debug in Linux due to lua-debug expecting host process to have lua54 symbols available * `FIX` support hex color codes with `#` in `textDocument/documentColor` +* `FIX` support 6-digit hex color codes in `textDocument/documentColor` ## 3.14.0 `2025-4-7`