-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathblink.lua
More file actions
116 lines (102 loc) · 2.62 KB
/
Copy pathblink.lua
File metadata and controls
116 lines (102 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
local Job = require("plenary.job")
local async = require("blink.lib.task")
local config
local css_variables
---Include the trigger character when accepting a completion.
---@param context blink.cmp.Context
local function transform(items, context)
return vim.tbl_map(function(entry)
local text = vim.api.nvim_buf_get_text(
0,
context.cursor[1] - 1,
context.bounds.start_col - 3,
context.cursor[1] - 1,
context.cursor[2],
{}
)
local newText
if text[1]:match("%-%-.*") then
newText = "-" .. entry.label:gmatch("-*(.*)")()
end
return vim.tbl_deep_extend("force", entry, {
kind = require("blink.cmp.types").CompletionItemKind.Variable,
textEdit = {
newText = newText or entry.label,
range = {
start = { line = context.cursor[1] - 1, character = context.bounds.start_col - 2 },
["end"] = { line = context.cursor[1] - 1, character = context.cursor[2] },
},
},
})
end, items)
end
---@type blink.cmp.Source
local M = {}
function M.new(opts)
local self = setmetatable({}, { __index = M })
config = vim.tbl_deep_extend("keep", opts or {}, require("css-vars.default_config"))
if css_variables then
return self
end
local args = {
"-e",
"[^\\w](--[^:)]*):([^;]+);",
"-r",
"'$1' '$2'",
"-o",
"--no-filename",
}
-- Only search in files that are listed in the "search_extensions" config.
for _, extension in pairs(config.search_extensions) do
table.insert(args, "-g")
table.insert(args, "*" .. extension)
end
table.insert(args, vim.uv.cwd())
Job:new({
command = "rg",
args = args,
env = { PATH = vim.env.PATH },
on_exit = function(j)
local result = j:result()
local items = {}
local processed = {}
for _, item in pairs(result) do
local css_var, css_value = item:match("^'%-(.-)' '(.-)'")
if not processed[css_var] then
processed[css_var] = true
table.insert(items, {
filterText = css_var,
label = "-" .. css_var,
documentation = css_value,
})
end
end
css_variables = items
end,
}):start()
return self
end
---@param context blink.cmp.Context
function M:get_completions(context, callback)
local task = async.new(function()
local is_char_trigger = vim.list_contains(
self:get_trigger_characters(),
context.line:sub(context.bounds.start_col - 1, context.bounds.start_col - 1)
)
if css_variables then
callback({
is_incomplete_forward = true,
is_incomplete_backward = true,
items = is_char_trigger and transform(css_variables, context) or {},
context = context,
})
end
end)
return function()
task:cancel()
end
end
function M:get_trigger_characters()
return { "-" }
end
return M