|
| 1 | +local methods = vim.lsp.protocol.Methods |
| 2 | +local OrgLspHandlers = {} |
| 3 | + |
| 4 | +local HEADLINE_KIND = vim.lsp.protocol.SymbolKind.Struct |
| 5 | + |
| 6 | +---@param headline OrgHeadline |
| 7 | +local function get_headline_symbol(headline) |
| 8 | + ---@cast headline OrgHeadline |
| 9 | + local range = headline:get_range():to_lsp() |
| 10 | + local result = { |
| 11 | + name = headline:get_title(), |
| 12 | + kind = HEADLINE_KIND, |
| 13 | + range = range, |
| 14 | + selectionRange = range, |
| 15 | + } |
| 16 | + local child_headlines = headline:get_child_headlines() |
| 17 | + if #child_headlines > 0 then |
| 18 | + result.children = vim.tbl_map(get_headline_symbol, child_headlines) |
| 19 | + end |
| 20 | + return result |
| 21 | +end |
| 22 | + |
| 23 | +OrgLspHandlers[methods.textDocument_documentSymbol] = function(params) |
| 24 | + local filename = vim.uri_to_fname(params.textDocument.uri) |
| 25 | + local orgfile = require('orgmode').files:load_file_sync(filename) |
| 26 | + if not orgfile then |
| 27 | + return {} |
| 28 | + end |
| 29 | + |
| 30 | + return vim.tbl_map(get_headline_symbol, orgfile:get_top_level_headlines()) |
| 31 | +end |
| 32 | + |
| 33 | +OrgLspHandlers[methods.workspace_symbol] = function(params) |
| 34 | + local results = {} |
| 35 | + local headlines = require('orgmode').files:find_headlines_matching_search_term(params.query or '', false, false) |
| 36 | + for _, headline in pairs(headlines) do |
| 37 | + table.insert(results, { |
| 38 | + name = headline:get_title(), |
| 39 | + kind = HEADLINE_KIND, |
| 40 | + location = { |
| 41 | + uri = vim.uri_from_fname(headline.file.filename), |
| 42 | + range = headline:get_range():to_lsp(), |
| 43 | + }, |
| 44 | + }) |
| 45 | + end |
| 46 | + |
| 47 | + return results |
| 48 | +end |
| 49 | + |
| 50 | +OrgLspHandlers[methods.textDocument_completion] = function(params) |
| 51 | + local line = vim.api |
| 52 | + .nvim_buf_get_lines(vim.uri_to_bufnr(params.textDocument.uri), params.position.line, params.position.line + 1, false)[1] |
| 53 | + :sub(1, params.position.character) |
| 54 | + |
| 55 | + local org = require('orgmode') |
| 56 | + local offset = org.completion:get_start({ line = line }) + 1 |
| 57 | + local base = string.sub(line, offset) |
| 58 | + |
| 59 | + local completion = org.completion:complete({ |
| 60 | + line = line, |
| 61 | + base = base, |
| 62 | + fuzzy = true, |
| 63 | + }) |
| 64 | + |
| 65 | + local results = vim.tbl_map(function(item) |
| 66 | + return { |
| 67 | + label = item.word, |
| 68 | + labelDetails = item.menu and { description = item.menu } or nil, |
| 69 | + } |
| 70 | + end, completion) |
| 71 | + |
| 72 | + return { |
| 73 | + isIncomplete = true, |
| 74 | + items = results, |
| 75 | + } |
| 76 | +end |
| 77 | + |
| 78 | +return OrgLspHandlers |
0 commit comments