Skip to content

Commit a49d5d4

Browse files
fix(autocompletion): Fix insert link autocompletion
Closes #1035
1 parent 994bb0f commit a49d5d4

File tree

3 files changed

+37
-16
lines changed

3 files changed

+37
-16
lines changed

lua/orgmode/init.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,17 @@ function Org:init()
6565
self.capture = require('orgmode.capture'):new({
6666
files = self.files,
6767
})
68+
self.completion = require('orgmode.org.autocompletion'):new({ files = self.files, links = self.links })
6869
self.org_mappings = require('orgmode.org.mappings'):new({
6970
capture = self.capture,
7071
agenda = self.agenda,
7172
files = self.files,
7273
links = self.links,
74+
completion = self.completion,
7375
})
7476
self.clock = require('orgmode.clock'):new({
7577
files = self.files,
7678
})
77-
self.completion = require('orgmode.org.autocompletion'):new({ files = self.files, links = self.links })
7879
self.statusline_debounced = require('orgmode.utils').debounce('statusline', function()
7980
return self.clock:get_statusline()
8081
end, 300)

lua/orgmode/org/autocompletion/init.lua

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
---@field links OrgLinks
44
---@field private sources OrgCompletionSource[]
55
---@field private sources_by_name table<string, OrgCompletionSource>
6+
---@field private fuzzy_match? boolean does completeopt has fuzzy option
67
---@field menu string
78
local OrgCompletion = {
89
menu = '[Org]',
@@ -16,6 +17,7 @@ function OrgCompletion:new(opts)
1617
links = opts.links,
1718
sources = {},
1819
sources_by_name = {},
20+
fuzzy_match = vim.tbl_contains(vim.opt_local.completeopt:get(), 'fuzzy'),
1921
}, OrgCompletion)
2022
this:setup_builtin_sources()
2123
this:register_frameworks()
@@ -46,16 +48,7 @@ function OrgCompletion:complete(context)
4648
local results = {}
4749
context.base = context.base or ''
4850
if not context.matcher then
49-
context.matcher = function(value, pattern)
50-
pattern = pattern or ''
51-
if pattern == '' then
52-
return true
53-
end
54-
if context.fuzzy then
55-
return #vim.fn.matchfuzzy({ value }, pattern) > 0
56-
end
57-
return value:find('^' .. vim.pesc(pattern)) ~= nil
58-
end
51+
context.matcher = self:_build_matcher(context)
5952
end
6053
for _, source in ipairs(self.sources) do
6154
if source:get_start(context) then
@@ -103,12 +96,26 @@ function OrgCompletion:omnifunc(findstart, base)
10396

10497
self._context = self._context or { line = self:get_line() }
10598
self._context.base = base
106-
if vim.tbl_contains(vim.opt_local.completeopt:get(), 'fuzzy') then
107-
self._context.fuzzy = true
108-
end
99+
self._context.fuzzy = self.fuzzy_match
109100
return self:complete(self._context)
110101
end
111102

103+
---@private
104+
---@param context OrgCompletionContext
105+
---@return fun(value: string, pattern: string):boolean
106+
function OrgCompletion:_build_matcher(context)
107+
return function(value, pattern)
108+
pattern = pattern or ''
109+
if pattern == '' then
110+
return true
111+
end
112+
if context.fuzzy then
113+
return #vim.fn.matchfuzzy({ value }, pattern) > 0
114+
end
115+
return value:find('^' .. vim.pesc(pattern)) ~= nil
116+
end
117+
end
118+
112119
function OrgCompletion:get_line()
113120
local cursor = vim.api.nvim_win_get_cursor(0)
114121
return vim.api.nvim_get_current_line():sub(1, cursor[2])
@@ -123,4 +130,16 @@ function OrgCompletion:register_frameworks()
123130
require('orgmode.org.autocompletion.cmp')
124131
end
125132

133+
---@param arg_lead string
134+
---@return string[]
135+
function OrgCompletion:complete_links_from_input(arg_lead)
136+
local context = {
137+
base = arg_lead,
138+
fuzzy = self.fuzzy_match,
139+
}
140+
context.matcher = self:_build_matcher(context)
141+
142+
return self.links:autocomplete(context)
143+
end
144+
126145
return OrgCompletion

lua/orgmode/org/mappings.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ local Babel = require('orgmode.babel')
1616
local Promise = require('orgmode.utils.promise')
1717
local Input = require('orgmode.ui.input')
1818
local Footnote = require('orgmode.objects.footnote')
19-
local Range = require('orgmode.files.elements.range')
2019

2120
---@class OrgMappings
2221
---@field capture OrgCapture
2322
---@field agenda OrgAgenda
2423
---@field files OrgFiles
2524
---@field links OrgLinks
25+
---@field completion OrgCompletion
2626
local OrgMappings = {}
2727

2828
---@param data table
@@ -33,6 +33,7 @@ function OrgMappings:new(data)
3333
opts.agenda = data.agenda
3434
opts.files = data.files
3535
opts.links = data.links
36+
opts.completion = data.completion
3637
setmetatable(opts, self)
3738
self.__index = self
3839
return opts
@@ -781,7 +782,7 @@ end
781782
function OrgMappings:insert_link()
782783
local link = OrgHyperlink.at_cursor()
783784
return Input.open('Links: ', link and link.url:to_string() or '', function(arg_lead)
784-
return self.links:autocomplete(arg_lead)
785+
return self.completion:complete_links_from_input(arg_lead)
785786
end):next(function(link_location)
786787
if not link_location then
787788
return false

0 commit comments

Comments
 (0)