Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions script/core/color.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
local files = require "files"
local guide = require "parser.guide"

local colorPattern = string.rep('%x', 8)
---@param source parser.object
---@return boolean
local function isColor(source)
---@type string
local text = source[1]
if text:len() ~= 8 then
return false
end
return text:match(colorPattern)
end


---@param colorText string
---@return Color
local function textToColor(colorText)
return {
alpha = tonumber(colorText:sub(1, 2), 16) / 255,
red = tonumber(colorText:sub(3, 4), 16) / 255,
green = tonumber(colorText:sub(5, 6), 16) / 255,
blue = tonumber(colorText:sub(7, 8), 16) / 255,
}
end


---@param color Color
---@return string
local function colorToText(color)
return (''
.. string.format('%02x', math.tointeger(color.alpha * 255))
.. string.format('%02x', math.tointeger(color.red * 255))
.. string.format('%02x', math.tointeger(color.green * 255))
.. string.format('%02x', math.tointeger(color.blue * 255))):upper()
end

---@class Color
---@field red number
---@field green number
---@field blue number
---@field alpha number

---@class ColorValue
---@field color Color
---@field start integer
---@field finish integer

---@async
function colors(uri)
local state = files.getState(uri)
local text = files.getText(uri)
if not state or not text then
return nil
end
---@type ColorValue[]
local colors = {}

guide.eachSource(state.ast, function (source) ---@async
if source.type == 'string' and isColor(source) then
---@type string
local colorText = source[1]

colors[#colors+1] = {
start = source.start + 1,
finish = source.finish - 1,
color = textToColor(colorText)
}
end
end)
return colors
end
return {
colors = colors,
colorToText = colorToText
}
34 changes: 34 additions & 0 deletions script/provider/provider.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,40 @@ m.register 'textDocument/foldingRange' {
end
}

m.register 'textDocument/documentColor' {
capability = {
colorProvider = true
},
---@async
function (params)
local color = require 'core.color'
local uri = files.getRealUri(params.textDocument.uri)
workspace.awaitReady(uri)
if not files.exists(uri) then
return nil
end
local colors = color.colors(uri)
if not colors then
return nil
end
local results = {}
for _, colorValue in ipairs(colors) do
results[#results+1] = {
range = converter.packRange(uri, colorValue.start, colorValue.finish),
color = colorValue.color
}
end
return results
end
}

m.register 'textDocument/colorPresentation' {
function (params)
local color = (require 'core.color').colorToText(params.color)
return {{label = color}}
end
}

m.register 'window/workDoneProgress/cancel' {
function (params)
log.debug('close proto(cancel):', params.token)
Expand Down