-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
158 lines (138 loc) · 4.04 KB
/
Copy pathinit.lua
File metadata and controls
158 lines (138 loc) · 4.04 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
local Source = {}
local config = require("cmp.config")
local a = require("plenary.async")
local l = require("html-css.local")
local ts = vim.treesitter
local tsu = require("nvim-treesitter.ts_utils")
local parsers = require("nvim-treesitter.parsers")
local scan = require("plenary.scandir")
local rootDir = scan.scan_dir(".", {
hidden = true,
add_dirs = true,
depth = 1,
respect_gitignore = true,
search_pattern = function(entry)
local subEntry = entry:sub(3) -- remove ./
-- %f[%a]git%f[^%a] -- old regex for matching .git
return subEntry:match(".git$") or subEntry:match("package.json") -- if project contains .git folder or package.json its gonna work
end,
})
local function mrgtbls(t1, t2)
for _, v in ipairs(t2) do
table.insert(t1, v)
end
return t1
end
function Source:setup()
require("cmp").register_source(self.source_name, Source)
end
function Source:new()
self.source_name = "html-css"
self.isRemote = "^https?://"
self.remote_classes = {}
self.items = {}
self.ids = {}
self.href_links = {}
-- reading user config
self.user_config = config.get_source_config(self.source_name) or {}
self.option = self.user_config.option or {}
self.file_extensions = self.option.file_extensions or {}
self.style_sheets = self.option.style_sheets or {}
self.enable_on = self.option.enable_on or {}
-- Get the current working directory
local current_directory = vim.fn.getcwd()
-- Check if the current directory contains a .git folder
local git_folder_exists = vim.fn.isdirectory(current_directory .. "/.git")
-- if git_folder_exists == 1 then
if vim.tbl_count(rootDir) ~= 0 then
self.style_sheets = mrgtbls(self.style_sheets, self.href_links) -- merge lings together
-- read all local files on start
a.run(function()
l.read_local_files(self.file_extensions, function(classes, ids)
for _, class in ipairs(classes) do
table.insert(self.items, class)
end
for _, id in ipairs(ids) do
table.insert(self.ids, id)
end
end)
end)
end
return self
end
function Source:complete(_, callback)
if vim.tbl_count(rootDir) ~= 0 then
self.items = {}
self.ids = {}
-- read all local files on start
a.run(function()
l.read_local_files(self.file_extensions, function(classes, ids)
for _, class in ipairs(classes) do
table.insert(self.items, class)
end
for _, id in ipairs(ids) do
table.insert(self.ids, id)
end
end)
for _, class in ipairs(self.remote_classes) do
table.insert(self.items, class)
end
end)
if self.current_selector == "class" or self.current_selector == "className" then
callback({ items = self.items, isComplete = false })
else
if self.current_selector == "id" then
callback({ items = self.ids, isComplete = false })
end
end
end
end
function Source:is_available()
if not next(self.user_config) then
return false
end
local bufnr = vim.api.nvim_get_current_buf()
local parser = parsers.get_parser(bufnr)
local node_at_cursor = tsu.get_node_at_cursor()
if node_at_cursor == nil then
return
end
local current_node = node_at_cursor
local lang = parser:lang()
while current_node do
if lang == "html" or lang == "svelte" or lang == "vue" or lang == "angular" then
if current_node:type() == "attribute_name" then
local identifier_name = ts.get_node_text(current_node, 0)
if
identifier_name == "className"
or identifier_name == "class"
or identifier_name == "id"
or identifier_name == "[id]"
then
self.current_selector = identifier_name
return true
end
break
end
current_node = current_node:prev_named_sibling()
else
if current_node:type() == "jsx_attribute" then
if current_node:child(0):type() == "property_identifier" then
local identifier_name = ts.get_node_text(current_node:child(0), 0)
if
identifier_name == "className"
or identifier_name == "class"
or identifier_name == "id"
then
self.current_selector = identifier_name
return true
end
break
end
end
current_node = current_node:parent()
end
end
return false
end
return Source:new()