Skip to content

Commit 127e453

Browse files
committed
fix: goto dependency
1 parent 55edca6 commit 127e453

File tree

2 files changed

+73
-19
lines changed

2 files changed

+73
-19
lines changed

lua/crates/popup/dependencies.lua

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ local M = {DepsContext = {}, DepsHistoryEntry = {}, }
1414

1515

1616

17+
1718
local DepsContext = M.DepsContext
1819
local api = require("crates.api")
1920
local async = require("crates.async")
@@ -28,12 +29,10 @@ local Version = types.Version
2829
local util = require("crates.util")
2930

3031
local goto_dep = async.wrap(function(ctx, line)
31-
local index = popup.item_index(line)
3232
local hist_entry = ctx.history[ctx.hist_idx]
33-
local deps = hist_entry.version.deps
3433

35-
if not deps or not deps[index] then return end
36-
local selected_dependency = deps[index]
34+
local selected_dependency = hist_entry.line_mapping[line]
35+
if not selected_dependency then return end
3736

3837

3938
hist_entry.line = line
@@ -154,6 +153,10 @@ function M.open_deps(ctx, crate_name, version, opts)
154153
local title = string.format(state.cfg.popup.text.title, crate_name .. " " .. version.num)
155154
local deps_width = 0
156155
local deps_text_index = {}
156+
local HlTextDepList = {}
157+
158+
159+
157160
local normal_deps_text = {}
158161
local build_deps_text = {}
159162
local dev_deps_text = {}
@@ -168,7 +171,7 @@ function M.open_deps(ctx, crate_name, version, opts)
168171
t.hl = state.cfg.popup.highlight.dependency
169172
end
170173

171-
local line = { t }
174+
local line = { t, dep = d }
172175
if d.kind == "normal" then
173176
table.insert(normal_deps_text, line)
174177
elseif d.kind == "build" then
@@ -199,25 +202,49 @@ function M.open_deps(ctx, crate_name, version, opts)
199202
end
200203

201204
local deps_text = {}
205+
local line_mapping = {}
206+
local line_idx = popup.TOP_OFFSET
202207
if #normal_deps_text > 0 then
203208
table.insert(deps_text, { { text = state.cfg.popup.text.normal_dependencies_title, hl = state.cfg.popup.highlight.normal_dependencies_title } })
204-
vim.list_extend(deps_text, normal_deps_text)
209+
line_idx = line_idx + 1
210+
211+
for _, t in ipairs(normal_deps_text) do
212+
table.insert(deps_text, t)
213+
line_mapping[line_idx] = t.dep
214+
line_idx = line_idx + 1
215+
end
205216
end
206217
if #build_deps_text > 0 then
207218
if #deps_text > 0 then
208219
table.insert(deps_text, {})
220+
line_idx = line_idx + 1
209221
end
210222
table.insert(deps_text, { { text = state.cfg.popup.text.build_dependencies_title, hl = state.cfg.popup.highlight.build_dependencies_title } })
211-
vim.list_extend(deps_text, build_deps_text)
223+
line_idx = line_idx + 1
224+
225+
for _, t in ipairs(build_deps_text) do
226+
table.insert(deps_text, t)
227+
line_mapping[line_idx] = t.dep
228+
line_idx = line_idx + 1
229+
end
212230
end
213231
if #dev_deps_text > 0 then
214232
if #deps_text > 0 then
215233
table.insert(deps_text, {})
234+
line_idx = line_idx + 1
216235
end
217236
table.insert(deps_text, { { text = state.cfg.popup.text.dev_dependencies_title, hl = state.cfg.popup.highlight.dev_dependencies_title } })
218-
vim.list_extend(deps_text, dev_deps_text)
237+
line_idx = line_idx + 1
238+
239+
for _, t in ipairs(dev_deps_text) do
240+
table.insert(deps_text, t)
241+
line_mapping[line_idx] = t.dep
242+
line_idx = line_idx + 1
243+
end
219244
end
220245

246+
ctx.history[ctx.hist_idx].line_mapping = line_mapping
247+
221248
local width = popup.win_width(title, deps_width + vers_width)
222249
local height = popup.win_height(deps_text)
223250

teal/crates/popup/dependencies.tl

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ local record M
1010
record DepsHistoryEntry
1111
crate_name: string
1212
version: Version
13+
line_mapping: {integer:Dependency}
1314
line: integer -- 0-indexed
1415
end
1516
end
@@ -28,12 +29,10 @@ local Version = types.Version
2829
local util = require("crates.util")
2930

3031
local goto_dep = async.wrap(function(ctx: DepsContext, line: integer)
31-
local index = popup.item_index(line)
3232
local hist_entry = ctx.history[ctx.hist_idx]
33-
local deps = hist_entry.version.deps as {Dependency}
3433

35-
if not deps or not deps[index] then return end
36-
local selected_dependency = deps[index]
34+
local selected_dependency = hist_entry.line_mapping[line]
35+
if not selected_dependency then return end
3736

3837
-- update current entry
3938
hist_entry.line = line
@@ -154,9 +153,13 @@ function M.open_deps(ctx: DepsContext, crate_name: string, version: Version, opt
154153
local title = string.format(state.cfg.popup.text.title, crate_name.." "..version.num)
155154
local deps_width = 0
156155
local deps_text_index: {{HighlightText}} = {}
157-
local normal_deps_text: {{HighlightText}} = {}
158-
local build_deps_text: {{HighlightText}} = {}
159-
local dev_deps_text: {{HighlightText}} = {}
156+
local record HlTextDepList
157+
{HighlightText}
158+
dep: Dependency
159+
end
160+
local normal_deps_text: {HlTextDepList} = {}
161+
local build_deps_text: {HlTextDepList} = {}
162+
local dev_deps_text: {HlTextDepList} = {}
160163

161164
for _,d in ipairs(deps) do
162165
local t: HighlightText = {}
@@ -168,7 +171,7 @@ function M.open_deps(ctx: DepsContext, crate_name: string, version: Version, opt
168171
t.hl = state.cfg.popup.highlight.dependency
169172
end
170173

171-
local line = { t }
174+
local line = { t, dep = d }
172175
if d.kind == "normal" then
173176
table.insert(normal_deps_text, line)
174177
elseif d.kind == "build" then
@@ -199,25 +202,49 @@ function M.open_deps(ctx: DepsContext, crate_name: string, version: Version, opt
199202
end
200203

201204
local deps_text: {{HighlightText}} = {}
205+
local line_mapping = {}
206+
local line_idx = popup.TOP_OFFSET
202207
if #normal_deps_text > 0 then
203208
table.insert(deps_text, {{ text = state.cfg.popup.text.normal_dependencies_title, hl = state.cfg.popup.highlight.normal_dependencies_title }})
204-
vim.list_extend(deps_text, normal_deps_text)
209+
line_idx = line_idx + 1
210+
211+
for _,t in ipairs(normal_deps_text) do
212+
table.insert(deps_text, t)
213+
line_mapping[line_idx] = t.dep
214+
line_idx = line_idx + 1
215+
end
205216
end
206217
if #build_deps_text > 0 then
207218
if #deps_text > 0 then
208219
table.insert(deps_text, {})
220+
line_idx = line_idx + 1
209221
end
210222
table.insert(deps_text, {{ text = state.cfg.popup.text.build_dependencies_title, hl = state.cfg.popup.highlight.build_dependencies_title }})
211-
vim.list_extend(deps_text, build_deps_text)
223+
line_idx = line_idx + 1
224+
225+
for _,t in ipairs(build_deps_text) do
226+
table.insert(deps_text, t)
227+
line_mapping[line_idx] = t.dep
228+
line_idx = line_idx + 1
229+
end
212230
end
213231
if #dev_deps_text > 0 then
214232
if #deps_text > 0 then
215233
table.insert(deps_text, {})
234+
line_idx = line_idx + 1
216235
end
217236
table.insert(deps_text, {{ text = state.cfg.popup.text.dev_dependencies_title, hl = state.cfg.popup.highlight.dev_dependencies_title }})
218-
vim.list_extend(deps_text, dev_deps_text)
237+
line_idx = line_idx + 1
238+
239+
for _,t in ipairs(dev_deps_text) do
240+
table.insert(deps_text, t)
241+
line_mapping[line_idx] = t.dep
242+
line_idx = line_idx + 1
243+
end
219244
end
220245

246+
ctx.history[ctx.hist_idx].line_mapping = line_mapping
247+
221248
local width = popup.win_width(title, deps_width + vers_width)
222249
local height = popup.win_height(deps_text)
223250

0 commit comments

Comments
 (0)