Skip to content

Commit c4eeb3d

Browse files
feat(links): Add links position when using get_links()
1 parent 261c987 commit c4eeb3d

File tree

4 files changed

+56
-7
lines changed

4 files changed

+56
-7
lines changed

lua/orgmode/files/elements/range.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
---@field end_col number
66
local Range = {}
77

8-
---@param data table
8+
---@param data { start_line?: number, end_line?: number, start_col?: number, end_col?: number }
99
function Range:new(data)
1010
local opts = {}
1111
opts.start_line = data.start_line

lua/orgmode/files/file.lua

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,8 +606,13 @@ function OrgFile:get_links()
606606
]])
607607

608608
local links = {}
609+
local processed_lines = {}
609610
for _, match in ts_query:iter_captures(self.root, self:_get_source()) do
610-
vim.list_extend(links, Link.all_from_line(self:get_node_text(match)))
611+
local line = match:start()
612+
if not processed_lines[line] then
613+
vim.list_extend(links, Link.all_from_line(self.lines[line + 1], line + 1))
614+
processed_lines[line] = true
615+
end
611616
end
612617
return links
613618
end

lua/orgmode/org/hyperlinks/link.lua

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
local Url = require('orgmode.org.hyperlinks.url')
2+
local Range = require('orgmode.files.elements.range')
23

34
---@class OrgLink
45
---@field url OrgUrl
56
---@field desc string | nil
7+
---@field range? OrgRange
68
local Link = {}
79

810
local pattern = '%[%[([^%]]+.-)%]%]'
911

1012
---@param str string
13+
---@param range? OrgRange
1114
---@return OrgLink
12-
function Link:new(str)
15+
function Link:new(str, range)
1316
local this = setmetatable({}, { __index = Link })
1417
local parts = vim.split(str, '][', { plain = true })
1518
this.url = Url:new(parts[1] or '')
1619
this.desc = parts[2]
20+
this.range = range
1721
return this
1822
end
1923

@@ -50,13 +54,16 @@ function Link.at_pos(line, pos)
5054
return Link:new(found_link), position
5155
end
5256

53-
function Link.all_from_line(line)
57+
function Link.all_from_line(line, line_number)
5458
local links = {}
5559
for link in line:gmatch(pattern) do
5660
local start_from = #links > 0 and links[#links].to or nil
5761
local from, to = line:find(pattern, start_from)
5862
if from and to then
59-
table.insert(links, Link:new(link))
63+
local range = Range.from_line(line_number)
64+
range.start_col = from
65+
range.end_col = to
66+
table.insert(links, Link:new(link, range))
6067
end
6168
end
6269

tests/plenary/files/file_spec.lua

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local OrgFile = require('orgmode.files.file')
22
local config = require('orgmode.config')
3+
local Range = require('orgmode.files.elements.range')
34

45
describe('OrgFile', function()
56
---@return OrgFile
@@ -748,23 +749,59 @@ describe('OrgFile', function()
748749
' - list item link to [[https://duckduckgo.com][duck]]',
749750
'',
750751
' :LOGBOOK:',
751-
' :TEST: And link in drawer [[https://github.com][github]]',
752+
' :TEST: And link in drawer [[https://github.com][github link]]',
752753
' :END:',
753754
})
754755
local links = file:get_links()
755756

756757
assert.are.same(4, #links)
757758
assert.are.same('https://google.com', links[1].url:to_string())
759+
assert.are.same(
760+
Range:new({
761+
start_line = 1,
762+
end_line = 1,
763+
start_col = 11,
764+
end_col = 32,
765+
}),
766+
links[1].range
767+
)
758768
assert.is.Nil(links[1].desc)
759769

760770
assert.are.same('./some-file.org', links[2].url:to_string())
761771
assert.is.Nil(links[2].desc)
772+
assert.are.same(
773+
Range:new({
774+
start_line = 3,
775+
end_line = 3,
776+
start_col = 30,
777+
end_col = 48,
778+
}),
779+
links[2].range
780+
)
762781

763782
assert.are.same('https://duckduckgo.com', links[3].url:to_string())
764783
assert.are.same('duck', links[3].desc)
784+
assert.are.same(
785+
Range:new({
786+
start_line = 4,
787+
end_line = 4,
788+
start_col = 23,
789+
end_col = 54,
790+
}),
791+
links[3].range
792+
)
765793

766794
assert.are.same('https://github.com', links[4].url:to_string())
767-
assert.are.same('github', links[4].desc)
795+
assert.are.same('github link', links[4].desc)
796+
assert.are.same(
797+
Range:new({
798+
start_line = 7,
799+
end_line = 7,
800+
start_col = 29,
801+
end_col = 63,
802+
}),
803+
links[4].range
804+
)
768805
end)
769806

770807
it('should get file level property', function()

0 commit comments

Comments
 (0)