Skip to content

Commit

Permalink
feat(core): enhance org-insert-link, add completion for relative path…
Browse files Browse the repository at this point in the history
… or ~/

_squash: keep Url.path, and add a realpath filed

fix: make sure realpath and path's ends are matched

e.g.
  /path/to/me ../to/me
  /path/to/me/ ../to/me/

refactor: remove unused flag

refactor: add comments on trailing slash

_fix
  • Loading branch information
PannenetsF authored and fanyunqian committed Jun 15, 2024
1 parent cc51c19 commit 091ea76
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
15 changes: 4 additions & 11 deletions lua/orgmode/org/hyperlinks/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,13 @@ function Hyperlinks.find_by_filepath(url)
return {}
end
--TODO integrate with orgmode.utils.fs or orgmode.objects.url
local file_base_no_start_path = vim.pesc(file_base:gsub('^%./', '') .. '')
local is_relative_path = file_base:match('^%./')
local current_file_directory = vim.pesc(fs.get_current_file_dir())
local valid_filenames = {}
for _, f in ipairs(filenames) do
if is_relative_path then
local match = f:match('^' .. current_file_directory .. '/(' .. file_base_no_start_path .. '.*%.org)$')
if match then
table.insert(valid_filenames, './' .. match)
end
else
if f:find('^' .. file_base) then
table.insert(valid_filenames, f)
if f:find('^' .. file_base) then
if url.realpath then
f = f:gsub(file_base, url.path)
end
table.insert(valid_filenames, f)
end
end

Expand Down
9 changes: 7 additions & 2 deletions lua/orgmode/org/hyperlinks/url.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local fs = require('orgmode.utils.fs')
---@alias OrgUrlPathType 'file' | 'headline' | 'custom-id' | 'id' | 'external-url' | 'plain' | nil
---@alias OrgUrlTargetType 'headline' | 'custom-id' | 'line-number' | 'unknown' | nil

Expand Down Expand Up @@ -116,7 +117,7 @@ function Url:get_file()
if not self:is_file() then
return nil
end
return self.path
return self.realpath or self.path
end

---@return string | nil
Expand Down Expand Up @@ -246,8 +247,12 @@ function Url:_parse_path_type()
return
end

if first_char == '.' and (self.path:sub(1, 3) == '../' or self.path:sub(1, 2) == './') then
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
self.path_type = 'file'
self.realpath = fs.get_real_path(self.path) or self.path
return
end

Expand Down
4 changes: 4 additions & 0 deletions lua/orgmode/utils/fs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ function M.get_real_path(filepath)
return false
end
local real = vim.loop.fs_realpath(substituted)
if filepath:sub(-1, -1) == '/' then
-- make sure if filepath gets a trailing slash, the realpath gets one, too.
real = real .. '/'
end
return real or false
end

Expand Down

0 comments on commit 091ea76

Please sign in to comment.