Skip to content

Commit 20b2ce2

Browse files
Add support for relative file path completion with file hyperlinks. Closes #405
1 parent 173d564 commit 20b2ce2

File tree

4 files changed

+35
-14
lines changed

4 files changed

+35
-14
lines changed

lua/orgmode/org/autocompletion/cmp.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function Source:is_available()
2121
end
2222

2323
function Source:get_trigger_characters(_)
24-
return { '#', '+', ':', '*' }
24+
return { '#', '+', ':', '*', '.', '/' }
2525
end
2626

2727
function Source:complete(params, callback)

lua/orgmode/org/hyperlinks.lua

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ local utils = require('orgmode.utils')
33
local Hyperlinks = {}
44

55
local function get_file_from_context(ctx)
6-
return (
7-
ctx.hyperlinks and ctx.hyperlinks.filepath and Files.get(ctx.hyperlinks.filepath, true)
8-
or Files.get_current_file()
9-
)
6+
return (ctx.hyperlinks and ctx.hyperlinks.filepath and Files.get(ctx.hyperlinks.filepath) or Files.get_current_file())
107
end
118

129
local function update_hyperlink_ctx(ctx)
@@ -22,7 +19,9 @@ local function update_hyperlink_ctx(ctx)
2219
}
2320

2421
local file_match = ctx.line:match('file:(.-)::')
25-
file_match = file_match and vim.fn.fnamemodify(file_match, ':p') or file_match
22+
if file_match then
23+
file_match = Hyperlinks.get_file_real_path(file_match)
24+
end
2625

2726
if file_match and Files.get(file_match) then
2827
hyperlinks_ctx.filepath = Files.get(file_match).filename
@@ -41,17 +40,28 @@ end
4140
function Hyperlinks.find_by_filepath(ctx)
4241
local filenames = Files.filenames()
4342
local file_base = ctx.base:gsub('^file:', '')
44-
if vim.trim(file_base) ~= '' then
45-
filenames = vim.tbl_filter(function(f)
46-
return f:find('^' .. file_base)
47-
end, filenames)
43+
local file_base_no_start_path = file_base:gsub('^%./', '') .. ''
44+
local is_relative_path = file_base:match('^%./')
45+
local current_file_directory = vim.fn.fnamemodify(utils.current_file_path(), ':p:h')
46+
local valid_filenames = {}
47+
for _, f in ipairs(filenames) do
48+
if is_relative_path then
49+
local match = f:match('^' .. current_file_directory .. '/(' .. file_base_no_start_path .. '[^/]*%.org)$')
50+
if match then
51+
table.insert(valid_filenames, './' .. match)
52+
end
53+
else
54+
if f:find('^' .. file_base) then
55+
table.insert(valid_filenames, f)
56+
end
57+
end
4858
end
4959

5060
-- Outer checks already filter cases where `ctx.skip_add_prefix` is truthy,
5161
-- so no need to check it here
5262
return vim.tbl_map(function(path)
5363
return 'file:' .. path
54-
end, filenames)
64+
end, valid_filenames)
5565
end
5666

5767
function Hyperlinks.find_by_custom_id_property(ctx)
@@ -135,4 +145,14 @@ function Hyperlinks.find_matching_links(ctx)
135145
return all
136146
end
137147

148+
function Hyperlinks.get_file_real_path(url_path)
149+
local path = url_path
150+
path = path:gsub('^file:', '')
151+
if path:match('^/') then
152+
return path
153+
end
154+
path = path:gsub('^./', '')
155+
return vim.fn.fnamemodify(utils.current_file_path(), ':p:h') .. '/' .. path
156+
end
157+
138158
return Hyperlinks

lua/orgmode/org/mappings.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,7 @@ function OrgMappings:open_at_point()
653653
if not link then
654654
return
655655
end
656+
656657
local parts = vim.split(link, '][', true)
657658
local url = parts[1]
658659
local link_ctx = { base = url, skip_add_prefix = true }
@@ -661,15 +662,15 @@ function OrgMappings:open_at_point()
661662
parts = vim.split(url, ' +', true)
662663
url = parts[1]
663664
local line_number = parts[2]
664-
vim.cmd(string.format('edit +%s %s', line_number, url:sub(6)))
665+
vim.cmd(string.format('edit +%s %s', line_number, Hyperlinks.get_file_real_path(url)))
665666
vim.cmd([[normal! zv]])
666667
return
667668
end
668669

669670
if url:find('^file:(.-)::') then
670671
link_ctx.line = url
671672
else
672-
vim.cmd(string.format('edit %s', url:sub(6)))
673+
vim.cmd(string.format('edit %s', Hyperlinks.get_file_real_path(url)))
673674
vim.cmd([[normal! zv]])
674675
return
675676
end

lua/orgmode/parser/file.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ end
164164

165165
function File:refresh()
166166
local bufnr = vim.fn.bufnr(self.filename)
167-
if bufnr < 0 then
167+
if bufnr < 0 or vim.fn.buflisted(bufnr) == 0 then
168168
return self
169169
end
170170
local changed = self.changedtick ~= vim.api.nvim_buf_get_var(bufnr, 'changedtick')

0 commit comments

Comments
 (0)