This repository has been archived by the owner on Nov 11, 2021. It is now read-only.
/
init.lua
169 lines (157 loc) · 5.37 KB
/
init.lua
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
159
160
161
162
163
164
165
166
167
168
169
local api = require("dependency_assist.dart.pubspec_api")
local formatter = require("dependency_assist.dart.formatter")
local extension = "dart"
local dependency_file = "pubspec.yaml"
local dev_block = "devDependencies:"
local dependency_block = "dependencies:"
local filetypes = { "dart", "yaml" }
local dart = {
api = api,
formatter = formatter,
filetypes = filetypes,
extension = extension,
filename = dependency_file,
}
--- @param name string
--- @param version string
--- @param line string
local function is_matching_pkg(name, version, line)
local helpers = require("dependency_assist.utils.helpers")
if helpers.any(helpers.is_empty, line, name, version) then
return false
end
local escaped = helpers.escape_pattern(version)
return line:gsub('"', ""):match(name .. ": " .. escaped)
end
--- This function determines position of dependencies by searching through
--- the relevant buffer content. Ideally it should only check the relevant sections
--- @param deps table
--- @param lines table
--- @param callback function
local function report_outdated_packages(deps, lines, callback)
if deps and not vim.tbl_isempty(deps) then
dart.api.check_outdated_packages(deps, function(pkg)
local lnum
local is_string = type(pkg.previous) == "string"
for idx, line in ipairs(lines) do
if is_string and is_matching_pkg(pkg.name, pkg.previous, line) then
lnum = idx - 1
end
end
if lnum then
callback(lnum, pkg.latest)
end
end)
end
end
local function parse_pubspec(buf_id, should_truncate)
should_truncate = should_truncate ~= nil and should_truncate or true
local lines = vim.api.nvim_buf_get_lines(buf_id, 0, -1, false)
if #lines > 0 then
local buffer_text = ""
-- NOTE: filter out blank lines otherwise the parser fails
local dependency_section_seen = false
for _, line in ipairs(lines) do
-- Filter out all lines that don't relate to our dependencies
-- unless we have specified that we should not truncate the lines
if line:match(dependency_block) or line:match(dev_block) then
dependency_section_seen = true
end
if line ~= "" and (not should_truncate or dependency_section_seen) then
buffer_text = buffer_text .. "\n" .. line
end
end
-- parsing the buffer text should NOT throw an error just fail silently for now
if buffer_text == "" then
return
end
local yaml = require("dependency_assist.yaml")
local success, parsed_lines = pcall(yaml.eval, buffer_text)
if not success then
return
end
return parsed_lines, lines
end
end
--- @param buf_id number
--- @param callback function
function dart.show_versions(buf_id, callback)
local output, lines = parse_pubspec(buf_id)
local dependencies = output and output.dependencies or {}
local dev_dependencies = output and output.dev_dependencies or {}
local deps = vim.tbl_extend("force", dependencies, dev_dependencies)
report_outdated_packages(deps, lines, callback)
end
--- @param dependencies string[]
--- @param is_dev boolean
function dart.insert_dependencies(dependencies, is_dev)
local buf_id = vim.api.nvim_get_current_buf()
local parsed_lines, lines = parse_pubspec(buf_id, false)
local data = is_dev and parsed_lines.dev_dependencies or parsed_lines.dependencies
local length = vim.tbl_count(data)
local index = 1
local last_inserted
-- search through our dev or main dependencies and find the last one
-- alphabetically. We don't have another way to search since trying
-- to use the file structure is brittle and the yaml parser doesn't
-- give us back a line number
for k, v in pairs(data) do
if index == length then
last_inserted = { name = k, version = v }
end
index = index + 1
end
local lnum
-- Try to match the "last inserted" dependency i.e. the alphabetically
-- furthest with a line number
for idx, line in ipairs(lines) do
local is_match = is_matching_pkg(last_inserted.name, last_inserted.version, line)
if is_match then
lnum = idx - 1
end
end
local helpers = require("dependency_assist.utils.helpers")
if lnum then
helpers.insert_beneath(lnum, dependencies)
else
helpers.echomsg("Couldn't find the last inserted dependency")
end
end
--- Find the path of the project's pubspec.yaml
--- @param buf_id number
function dart.find_dependency_file(buf_id)
local helpers = require("dependency_assist.utils.helpers")
return helpers.find(buf_id, dependency_file)
end
function dart.process_search_results(results)
local selected = {}
for input, result in pairs(results) do
local match
local score
for _, pkg in ipairs(result.packages) do
local ld = require("dependency_assist.utils.levenshtein_distance")
local distance = ld.levenshtein(input, pkg.package)
if score == nil or distance < score then
score = distance
match = pkg.package
end
end
if match then
table.insert(selected, match)
end
end
return selected
end
function dart.get_packages(packages, callback)
dart.api.get_packages(packages, function(data)
local all_latest = {}
local all_versions = {}
for _, result in pairs(data) do
local versions, name = dart.formatter.format_package_details(result)
table.insert(all_latest, versions[1])
all_versions[name] = versions
end
callback(all_latest, all_versions)
end)
end
return dart