-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexternals.lua
More file actions
126 lines (109 loc) · 3.55 KB
/
Copy pathexternals.lua
File metadata and controls
126 lines (109 loc) · 3.55 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
local M = {}
local store = require("css-angular.store")
local extractor = require("css-angular.extractor")
local fetcher = require("css-angular.fetcher")
local utils = require("css-angular.utils")
local config = require("css-angular.config")
local function url_exists(url, list)
for _, link in ipairs(list) do
if link.url == url or link.path == url then
return true
end
end
return false
end
---@type fun(ctx: Ctx, link: Link, bufnr: number)
local extractDataFromLinks = function(ctx, link, bufnr)
local selectors = store.get(bufnr, "selectors") or {
ids = {},
classes = {},
}
if ctx.code == 0 then
local extracted_selectors = extractor.selectors(ctx.stdout, link.url)
selectors.classes = vim.list_extend(selectors.classes, extracted_selectors.classes)
selectors.ids = vim.list_extend(selectors.ids, extracted_selectors.ids)
link.fetched = true
link.available = true
store.set(bufnr, "selectors", selectors)
end
end
---@type fun(bufrn: number, hrefs: Externals , externals: Externals):Externals
local function remove_missing_hrefs(bufnr, hrefs, externals)
local updated_externals = {
cdn = {},
locals = {},
}
local selectors = store.get(bufnr, "selectors") or {
ids = {},
classes = {},
}
for type, external in pairs(externals) do
for _, link in ipairs(external) do
if url_exists(link.url or link.path, hrefs[type]) then
table.insert(updated_externals[type], link)
else
selectors.classes = vim.tbl_filter(function(class)
return class.source ~= (link.url or link.path)
end, selectors.classes)
selectors.ids = vim.tbl_filter(function(id)
return id.source ~= (link.url or link.path)
end, selectors.ids)
end
end
end
store.set(bufnr, "selectors", selectors)
return updated_externals
end
---@type fun(bufnr: number, hrefs: Externals)
M.init = function(bufnr, hrefs)
-- so this init fun extract hrefs on enter the buf or save,
-- checks does we already have this in store, and if we does
-- then it will skip fetching, if not then we will fetch
-- only new href that is added
local externals = store.get(bufnr, "externals") or {
cdn = {},
locals = {},
}
externals = remove_missing_hrefs(bufnr, hrefs, externals)
-- checking does the url already exist in the store
for type, external in pairs(hrefs) do
for _, v in ipairs(external) do
if not url_exists(v.url, externals[type]) then
table.insert(externals[type], v)
end
end
end
store.set(bufnr, "externals", externals) -- maybe this needs refactoring since we write the same stuff to store evrytime
-- looping over cdns and fetch them if they are not fetch if tehy are just skip
for _, link in pairs(externals.cdn) do
if not link.fetched then
local opts = {}
fetcher(link.url, opts, function(ctx)
extractDataFromLinks(ctx, link, bufnr)
end)
end
end
-- looping over the locals and read files and store selectors
for _, file in pairs(externals.locals) do
if not file.fetched then
utils.readFile(file.path, function(data)
if config.config.notify then
vim.schedule(function()
vim.notify(file.path, vim.log.levels.INFO, { title = "Fetching ..." })
end)
end
local extracted_selectors = extractor.selectors(data, file.path)
local selectors = store.get(bufnr, "selectors") or {
ids = {},
classes = {},
}
selectors.classes = vim.list_extend(selectors.classes, extracted_selectors.classes)
selectors.ids = vim.list_extend(selectors.ids, extracted_selectors.ids)
file.fetched = true
file.available = true
store.set(bufnr, "selectors", selectors)
end)
end
end
end
return M