Skip to content

Commit 7f975b1

Browse files
fix(autocompletion): correctly autocomplete non-absolute paths
1 parent 9ad2ff6 commit 7f975b1

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

lua/orgmode/org/links/types/headline_search.lua

+14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local utils = require('orgmode.utils')
2+
local fs = require('orgmode.utils.fs')
23
local OrgLinkUrl = require('orgmode.org.links.url')
34
local link_utils = require('orgmode.org.links.utils')
45

@@ -74,6 +75,19 @@ function OrgLinkHeadlineSearch:autocomplete(link)
7475
f = f:gsub('^' .. opts.file_path, opts.link_url.path)
7576
table.insert(valid_filenames, f)
7677
end
78+
79+
local real_path = opts.link_url:get_real_path()
80+
81+
if not real_path then
82+
local substitute_path = fs.substitute_path(opts.file_path)
83+
if substitute_path then
84+
local full_path = vim.fn.fnamemodify(substitute_path, ':p')
85+
if f:find('^' .. full_path) then
86+
f = f:gsub('^' .. full_path, opts.link_url.path)
87+
table.insert(valid_filenames, f)
88+
end
89+
end
90+
end
7791
end
7892

7993
local prefix = opts.link_url:get_protocol() == 'file' and 'file:' or ''

lua/orgmode/org/links/url.lua

+5-7
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,20 @@ end
2020
---@return string | nil
2121
function OrgLinkUrl:get_file_path()
2222
if self.protocol == 'file' then
23-
return self:_get_real_path()
23+
return self:get_real_path() or self.path
2424
end
2525

2626
local first_char = self.path:sub(1, 1)
2727

2828
if first_char == '/' then
29-
return self:_get_real_path()
29+
return self:get_real_path() or self.path
3030
end
3131

3232
if
3333
(first_char == '.' and (self.path:sub(1, 3) == '../' or self.path:sub(1, 2) == './'))
3434
or (first_char == '~' and self.path:sub(2, 2) == '/')
3535
then
36-
return self:_get_real_path()
36+
return self:get_real_path() or self.path
3737
end
3838

3939
return nil
@@ -76,10 +76,8 @@ function OrgLinkUrl:get_id()
7676
return self.path
7777
end
7878

79-
---@private
80-
---@return string
81-
function OrgLinkUrl:_get_real_path()
82-
return fs.get_real_path(self.path) or self.path
79+
function OrgLinkUrl:get_real_path()
80+
return fs.get_real_path(self.path) or nil
8381
end
8482

8583
---@return string

0 commit comments

Comments
 (0)