|
1 |
| -local function register() |
2 |
| - require('orgmode.org.autocompletion.compe') |
| 1 | +---@class OrgCompletion |
| 2 | +---@field files OrgFiles |
| 3 | +---@field private sources OrgCompletionSource[] |
| 4 | +---@field private sources_by_name table<string, OrgCompletionSource> |
| 5 | +---@field menu string |
| 6 | +local OrgCompletion = { |
| 7 | + menu = '[Org]', |
| 8 | +} |
| 9 | +OrgCompletion.__index = OrgCompletion |
| 10 | + |
| 11 | +---@param opts { files: OrgFiles } |
| 12 | +function OrgCompletion:new(opts) |
| 13 | + local this = setmetatable({ |
| 14 | + files = opts.files, |
| 15 | + sources = {}, |
| 16 | + sources_by_name = {}, |
| 17 | + }, OrgCompletion) |
| 18 | + this:setup_builtin_sources() |
| 19 | + this:register_frameworks() |
| 20 | + return this |
| 21 | +end |
| 22 | + |
| 23 | +function OrgCompletion:setup_builtin_sources() |
| 24 | + self:add_source(require('orgmode.org.autocompletion.sources.todo_keywords'):new()) |
| 25 | + self:add_source(require('orgmode.org.autocompletion.sources.tags'):new({ completion = self })) |
| 26 | + self:add_source(require('orgmode.org.autocompletion.sources.plan'):new({ completion = self })) |
| 27 | + self:add_source(require('orgmode.org.autocompletion.sources.directives'):new()) |
| 28 | + self:add_source(require('orgmode.org.autocompletion.sources.properties'):new({ completion = self })) |
| 29 | + self:add_source(require('orgmode.org.autocompletion.sources.hyperlinks'):new({ completion = self })) |
| 30 | +end |
| 31 | + |
| 32 | +---@param source OrgCompletionSource |
| 33 | +function OrgCompletion:add_source(source) |
| 34 | + if self.sources_by_name[source:get_name()] then |
| 35 | + error('Completion source ' .. source:get_name() .. ' already exists') |
| 36 | + end |
| 37 | + self.sources_by_name[source:get_name()] = source |
| 38 | + table.insert(self.sources, source) |
| 39 | +end |
| 40 | + |
| 41 | +---@param context OrgCompletionContext |
| 42 | +---@return OrgCompletionItem |
| 43 | +function OrgCompletion:complete(context) |
| 44 | + local results = {} |
| 45 | + for _, source in ipairs(self.sources) do |
| 46 | + if source:get_start(context) then |
| 47 | + vim.list_extend(results, self:_get_valid_results(source:get_results(context), context)) |
| 48 | + end |
| 49 | + end |
| 50 | + |
| 51 | + return results |
| 52 | +end |
| 53 | + |
| 54 | +function OrgCompletion:_get_valid_results(results, context) |
| 55 | + local base = context.base or '' |
| 56 | + |
| 57 | + local valid_results = {} |
| 58 | + for _, item in ipairs(results) do |
| 59 | + if base == '' or item:find('^' .. vim.pesc(base)) then |
| 60 | + table.insert(valid_results, { |
| 61 | + word = item, |
| 62 | + menu = self.menu, |
| 63 | + }) |
| 64 | + end |
| 65 | + end |
| 66 | + |
| 67 | + return valid_results |
| 68 | +end |
| 69 | + |
| 70 | +---@param context OrgCompletionContext |
| 71 | +function OrgCompletion:get_start(context) |
| 72 | + for _, source in ipairs(self.sources) do |
| 73 | + local start = source:get_start(context) |
| 74 | + if start then |
| 75 | + return start |
| 76 | + end |
| 77 | + end |
| 78 | + |
| 79 | + return -1 |
| 80 | +end |
| 81 | + |
| 82 | +function OrgCompletion:omnifunc(findstart, base) |
| 83 | + if findstart == 1 then |
| 84 | + self._context = { line = self:get_line() } |
| 85 | + return self:get_start(self._context) |
| 86 | + end |
| 87 | + |
| 88 | + self._context = self._context or { line = self:get_line() } |
| 89 | + self._context.base = base |
| 90 | + return self:complete(self._context) |
| 91 | +end |
| 92 | + |
| 93 | +function OrgCompletion:get_line() |
| 94 | + local cursor = vim.api.nvim_win_get_cursor(0) |
| 95 | + return vim.api.nvim_get_current_line():sub(1, cursor[2]) |
| 96 | +end |
| 97 | + |
| 98 | +---@param line string |
| 99 | +function OrgCompletion:is_headline_line(line) |
| 100 | + return line:find([[^%*+%s+]]) ~= nil |
| 101 | +end |
| 102 | + |
| 103 | +function OrgCompletion:register_frameworks() |
3 | 104 | require('orgmode.org.autocompletion.cmp')
|
4 | 105 | end
|
5 | 106 |
|
6 |
| -return { |
7 |
| - register = register, |
8 |
| -} |
| 107 | +return OrgCompletion |
0 commit comments