Skip to content

Commit

Permalink
fix(autocompletion): correctly autocomplete non-absolute paths
Browse files Browse the repository at this point in the history
  • Loading branch information
kristijanhusak committed Jan 12, 2025
1 parent 9ad2ff6 commit 7f975b1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
14 changes: 14 additions & 0 deletions lua/orgmode/org/links/types/headline_search.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local utils = require('orgmode.utils')
local fs = require('orgmode.utils.fs')
local OrgLinkUrl = require('orgmode.org.links.url')
local link_utils = require('orgmode.org.links.utils')

Expand Down Expand Up @@ -74,6 +75,19 @@ function OrgLinkHeadlineSearch:autocomplete(link)
f = f:gsub('^' .. opts.file_path, opts.link_url.path)
table.insert(valid_filenames, f)
end

local real_path = opts.link_url:get_real_path()

if not real_path then
local substitute_path = fs.substitute_path(opts.file_path)
if substitute_path then
local full_path = vim.fn.fnamemodify(substitute_path, ':p')
if f:find('^' .. full_path) then
f = f:gsub('^' .. full_path, opts.link_url.path)
table.insert(valid_filenames, f)
end
end
end
end

local prefix = opts.link_url:get_protocol() == 'file' and 'file:' or ''
Expand Down
12 changes: 5 additions & 7 deletions lua/orgmode/org/links/url.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,20 @@ end
---@return string | nil
function OrgLinkUrl:get_file_path()
if self.protocol == 'file' then
return self:_get_real_path()
return self:get_real_path() or self.path
end

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

if first_char == '/' then
return self:_get_real_path()
return self:get_real_path() or self.path
end

if
(first_char == '.' and (self.path:sub(1, 3) == '../' or self.path:sub(1, 2) == './'))
or (first_char == '~' and self.path:sub(2, 2) == '/')
then
return self:_get_real_path()
return self:get_real_path() or self.path
end

return nil
Expand Down Expand Up @@ -76,10 +76,8 @@ function OrgLinkUrl:get_id()
return self.path
end

---@private
---@return string
function OrgLinkUrl:_get_real_path()
return fs.get_real_path(self.path) or self.path
function OrgLinkUrl:get_real_path()
return fs.get_real_path(self.path) or nil
end

---@return string
Expand Down

0 comments on commit 7f975b1

Please sign in to comment.