Skip to content

Commit a2052c1

Browse files
authored
Merge pull request #1379 from FAForever/add-color-support
add color support
2 parents a0128ff + eecbfae commit a2052c1

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

script/core/color.lua

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
local files = require "files"
2+
local guide = require "parser.guide"
3+
4+
local colorPattern = string.rep('%x', 8)
5+
---@param source parser.object
6+
---@return boolean
7+
local function isColor(source)
8+
---@type string
9+
local text = source[1]
10+
if text:len() ~= 8 then
11+
return false
12+
end
13+
return text:match(colorPattern)
14+
end
15+
16+
17+
---@param colorText string
18+
---@return Color
19+
local function textToColor(colorText)
20+
return {
21+
alpha = tonumber(colorText:sub(1, 2), 16) / 255,
22+
red = tonumber(colorText:sub(3, 4), 16) / 255,
23+
green = tonumber(colorText:sub(5, 6), 16) / 255,
24+
blue = tonumber(colorText:sub(7, 8), 16) / 255,
25+
}
26+
end
27+
28+
29+
---@param color Color
30+
---@return string
31+
local function colorToText(color)
32+
return (''
33+
.. string.format('%02x', math.tointeger(color.alpha * 255))
34+
.. string.format('%02x', math.tointeger(color.red * 255))
35+
.. string.format('%02x', math.tointeger(color.green * 255))
36+
.. string.format('%02x', math.tointeger(color.blue * 255))):upper()
37+
end
38+
39+
---@class Color
40+
---@field red number
41+
---@field green number
42+
---@field blue number
43+
---@field alpha number
44+
45+
---@class ColorValue
46+
---@field color Color
47+
---@field start integer
48+
---@field finish integer
49+
50+
---@async
51+
function colors(uri)
52+
local state = files.getState(uri)
53+
local text = files.getText(uri)
54+
if not state or not text then
55+
return nil
56+
end
57+
---@type ColorValue[]
58+
local colors = {}
59+
60+
guide.eachSource(state.ast, function (source) ---@async
61+
if source.type == 'string' and isColor(source) then
62+
---@type string
63+
local colorText = source[1]
64+
65+
colors[#colors+1] = {
66+
start = source.start + 1,
67+
finish = source.finish - 1,
68+
color = textToColor(colorText)
69+
}
70+
end
71+
end)
72+
return colors
73+
end
74+
return {
75+
colors = colors,
76+
colorToText = colorToText
77+
}

script/provider/provider.lua

+34
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,40 @@ m.register 'textDocument/foldingRange' {
10211021
end
10221022
}
10231023

1024+
m.register 'textDocument/documentColor' {
1025+
capability = {
1026+
colorProvider = true
1027+
},
1028+
---@async
1029+
function (params)
1030+
local color = require 'core.color'
1031+
local uri = files.getRealUri(params.textDocument.uri)
1032+
workspace.awaitReady(uri)
1033+
if not files.exists(uri) then
1034+
return nil
1035+
end
1036+
local colors = color.colors(uri)
1037+
if not colors then
1038+
return nil
1039+
end
1040+
local results = {}
1041+
for _, colorValue in ipairs(colors) do
1042+
results[#results+1] = {
1043+
range = converter.packRange(uri, colorValue.start, colorValue.finish),
1044+
color = colorValue.color
1045+
}
1046+
end
1047+
return results
1048+
end
1049+
}
1050+
1051+
m.register 'textDocument/colorPresentation' {
1052+
function (params)
1053+
local color = (require 'core.color').colorToText(params.color)
1054+
return {{label = color}}
1055+
end
1056+
}
1057+
10241058
m.register 'window/workDoneProgress/cancel' {
10251059
function (params)
10261060
log.debug('close proto(cancel):', params.token)

0 commit comments

Comments
 (0)