Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 47c3d92

Browse files
PannenetsFfanyunqian
and
fanyunqian
authored
feat(core): enhance org-insert-link, add completion for ~/ prefix. (#749)
* feat(core): enhance org-insert-link, add completion for relative path 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 * test: add unit test for relative dirs --------- Co-authored-by: fanyunqian <fanyunqian@sensetime.com>
1 parent 7a256b5 commit 47c3d92

File tree

5 files changed

+78
-13
lines changed

5 files changed

+78
-13
lines changed

lua/orgmode/org/hyperlinks/init.lua

+4-11
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,13 @@ function Hyperlinks.find_by_filepath(url)
2424
return {}
2525
end
2626
--TODO integrate with orgmode.utils.fs or orgmode.objects.url
27-
local file_base_no_start_path = vim.pesc(file_base:gsub('^%./', '') .. '')
28-
local is_relative_path = file_base:match('^%./')
29-
local current_file_directory = vim.pesc(fs.get_current_file_dir())
3027
local valid_filenames = {}
3128
for _, f in ipairs(filenames) do
32-
if is_relative_path then
33-
local match = f:match('^' .. current_file_directory .. '/(' .. file_base_no_start_path .. '.*%.org)$')
34-
if match then
35-
table.insert(valid_filenames, './' .. match)
36-
end
37-
else
38-
if f:find('^' .. file_base) then
39-
table.insert(valid_filenames, f)
29+
if f:find('^' .. file_base) then
30+
if url.realpath then
31+
f = f:gsub(file_base, url.path)
4032
end
33+
table.insert(valid_filenames, f)
4134
end
4235
end
4336

lua/orgmode/org/hyperlinks/url.lua

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
local fs = require('orgmode.utils.fs')
12
---@alias OrgUrlPathType 'file' | 'headline' | 'custom-id' | 'id' | 'external-url' | 'plain' | nil
23
---@alias OrgUrlTargetType 'headline' | 'custom-id' | 'line-number' | 'unknown' | nil
34

@@ -116,7 +117,7 @@ function Url:get_file()
116117
if not self:is_file() then
117118
return nil
118119
end
119-
return self.path
120+
return self.realpath or self.path
120121
end
121122

122123
---@return string | nil
@@ -246,8 +247,12 @@ function Url:_parse_path_type()
246247
return
247248
end
248249

249-
if first_char == '.' and (self.path:sub(1, 3) == '../' or self.path:sub(1, 2) == './') then
250+
if
251+
(first_char == '.' and (self.path:sub(1, 3) == '../' or self.path:sub(1, 2) == './'))
252+
or (first_char == '~' and self.path:sub(2, 2) == '/')
253+
then
250254
self.path_type = 'file'
255+
self.realpath = fs.get_real_path(self.path) or self.path
251256
return
252257
end
253258

lua/orgmode/utils/fs.lua

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ function M.get_real_path(filepath)
3030
return false
3131
end
3232
local real = vim.loop.fs_realpath(substituted)
33+
if filepath:sub(-1, -1) == '/' then
34+
-- make sure if filepath gets a trailing slash, the realpath gets one, too.
35+
real = real .. '/'
36+
end
3337
return real or false
3438
end
3539

tests/plenary/helpers.lua

+30
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,38 @@ local function create_file_instance(lines, filename)
3535
return file
3636
end
3737

38+
---@return table
39+
local function create_agenda_files(filenames, contents)
40+
-- NOTE: content is only 1 line for 1 file
41+
local temp_fname = vim.fn.tempname()
42+
local temp_dir = vim.fn.fnamemodify(temp_fname, ':p:h')
43+
-- clear temp dir
44+
vim.fn.delete(temp_dir .. '/*', 'rf')
45+
local files = {}
46+
local agenda_files = {}
47+
for i, filename in ipairs(filenames) do
48+
local fname = temp_dir .. '/' .. filename
49+
fname = vim.fn.fnamemodify(fname, ':p')
50+
if fname then
51+
local dir = vim.fn.fnamemodify(fname, ':p:h')
52+
vim.fn.mkdir(dir, 'p')
53+
vim.fn.writefile({ contents[i] }, fname)
54+
files[filename] = fname
55+
table.insert(agenda_files, fname)
56+
end
57+
end
58+
local cfg = vim.tbl_extend('force', {
59+
org_agenda_files = agenda_files,
60+
}, {})
61+
local org = orgmode.setup(cfg)
62+
org:init()
63+
return files
64+
end
65+
3866
return {
67+
load_file = load_file,
3968
create_file = create_file,
4069
create_file_instance = create_file_instance,
4170
create_agenda_file = create_agenda_file,
71+
create_agenda_files = create_agenda_files,
4272
}

tests/plenary/org/autocompletion_spec.lua

+33
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,39 @@ describe('Autocompletion', function()
339339
{ menu = '[Org]', word = 'Title without anchor' },
340340
}, result)
341341
end)
342+
343+
it('should work on relative paths', function()
344+
local files = helpers.create_agenda_files({
345+
'a.org',
346+
'b/c.org',
347+
}, {
348+
'[[./ ',
349+
'[[../ ',
350+
})
351+
helpers.load_file(files['b/c.org'])
352+
vim.fn.cursor({ 1, 6 })
353+
354+
assert.are.same({
355+
{ menu = '[Org]', word = '../a.org' },
356+
{ menu = '[Org]', word = '../b/c.org' },
357+
}, org.completion:omnifunc(0, '../'))
358+
end)
359+
it('should work on relative paths', function()
360+
local files = helpers.create_agenda_files({
361+
'a.org',
362+
'b/c.org',
363+
}, {
364+
'[[./ ',
365+
'[[../ ',
366+
})
367+
helpers.load_file(files['a.org'])
368+
vim.fn.cursor({ 1, 5 })
369+
370+
assert.are.same({
371+
{ menu = '[Org]', word = './a.org' },
372+
{ menu = '[Org]', word = './b/c.org' },
373+
}, org.completion:omnifunc(0, './'))
374+
end)
342375
end)
343376
end)
344377
end)

0 commit comments

Comments
 (0)