Added orgmode completion as cmp source

This commit is contained in:
2024-09-26 10:05:35 +02:00
parent 54da15cc25
commit eeb62a1072
2 changed files with 70 additions and 69 deletions

View File

@@ -1,80 +1,81 @@
local has_words_before = function() local has_words_before = function()
unpack = unpack or table.unpack unpack = unpack or table.unpack
local line, col = unpack(vim.api.nvim_win_get_cursor(0)) local line, col = unpack(vim.api.nvim_win_get_cursor(0))
return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil
end end
local luasnip = require("luasnip") local luasnip = require("luasnip")
local cmp = require("cmp") local cmp = require("cmp")
cmp.setup({ cmp.setup({
snippet = { snippet = {
-- REQUIRED - you must specify a snippet engine -- REQUIRED - you must specify a snippet engine
expand = function(args) expand = function(args)
require('luasnip').lsp_expand(args.body) -- For `luasnip` users. require('luasnip').lsp_expand(args.body) -- For `luasnip` users.
end, end,
}, },
mapping = cmp.mapping.preset.insert({ mapping = cmp.mapping.preset.insert({
-- Use <C-b/f> to scroll the docs -- Use <C-b/f> to scroll the docs
['<C-b>'] = cmp.mapping.scroll_docs(-4), ['<C-b>'] = cmp.mapping.scroll_docs(-4),
['<C-f>'] = cmp.mapping.scroll_docs(4), ['<C-f>'] = cmp.mapping.scroll_docs(4),
-- Use <C-k/j> to switch in items -- Use <C-k/j> to switch in items
['<C-k>'] = cmp.mapping.select_prev_item(), ['<C-k>'] = cmp.mapping.select_prev_item(),
['<C-j>'] = cmp.mapping.select_next_item(), ['<C-j>'] = cmp.mapping.select_next_item(),
-- Use <CR>(Enter) to confirm selection -- Use <CR>(Enter) to confirm selection
-- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items. -- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items.
['<CR>'] = cmp.mapping.confirm({ select = true }), ['<CR>'] = cmp.mapping.confirm({ select = true }),
-- A super tab -- A super tab
-- sourc: https://github.com/hrsh7th/nvim-cmp/wiki/Example-mappings#luasnip -- sourc: https://github.com/hrsh7th/nvim-cmp/wiki/Example-mappings#luasnip
["<Tab>"] = cmp.mapping(function(fallback) ["<Tab>"] = cmp.mapping(function(fallback)
-- Hint: if the completion menu is visible select next one -- Hint: if the completion menu is visible select next one
if cmp.visible() then if cmp.visible() then
cmp.select_next_item() cmp.select_next_item()
elseif has_words_before() then elseif has_words_before() then
cmp.complete() cmp.complete()
else else
fallback() fallback()
end end
end, { "i", "s" }), -- i - insert mode; s - select mode end, { "i", "s" }), -- i - insert mode; s - select mode
["<S-Tab>"] = cmp.mapping(function(fallback) ["<S-Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then if cmp.visible() then
cmp.select_prev_item() cmp.select_prev_item()
elseif luasnip.jumpable(-1) then elseif luasnip.jumpable(-1) then
luasnip.jump(-1) luasnip.jump(-1)
else else
fallback() fallback()
end end
end, { "i", "s" }), end, { "i", "s" }),
}), }),
-- Let's configure the item's appearance -- Let's configure the item's appearance
-- source: https://github.com/hrsh7th/nvim-cmp/wiki/Menu-Appearance -- source: https://github.com/hrsh7th/nvim-cmp/wiki/Menu-Appearance
formatting = { formatting = {
-- Set order from left to right -- Set order from left to right
-- kind: single letter indicating the type of completion -- kind: single letter indicating the type of completion
-- abbr: abbreviation of "word"; when not empty it is used in the menu instead of "word" -- abbr: abbreviation of "word"; when not empty it is used in the menu instead of "word"
-- menu: extra text for the popup menu, displayed after "word" or "abbr" -- menu: extra text for the popup menu, displayed after "word" or "abbr"
fields = { 'abbr', 'menu' }, fields = { 'abbr', 'menu' },
-- customize the appearance of the completion menu -- customize the appearance of the completion menu
format = function(entry, vim_item) format = function(entry, vim_item)
vim_item.menu = ({ vim_item.menu = ({
nvim_lsp = '[Lsp]', nvim_lsp = '[Lsp]',
luasnip = '[Luasnip]', luasnip = '[Luasnip]',
buffer = '[File]', buffer = '[File]',
path = '[Path]', path = '[Path]',
})[entry.source.name] })[entry.source.name]
return vim_item return vim_item
end, end,
}, },
-- Set source precedence -- Set source precedence
sources = cmp.config.sources({ sources = cmp.config.sources({
{ name = 'nvim_lsp' }, -- For nvim-lsp { name = 'nvim_lsp' }, -- For nvim-lsp
{ name = 'luasnip' }, -- For luasnip user { name = 'luasnip' }, -- For luasnip user
{ name = 'buffer' }, -- For buffer word completion { name = 'buffer' }, -- For buffer word completion
{ name = 'path' }, -- For path completion { name = 'path' }, -- For path completion
{ name = 'rpncalc' }, -- for RPN calculations { name = 'rpncalc' }, -- for RPN calculations
}) { name = 'orgmode' }, -- for nvim-orgmode
})
}) })

View File

@@ -234,7 +234,7 @@ require("lazy").setup({
"PhilRunninger/cmp-rpncalc", "PhilRunninger/cmp-rpncalc",
}, },
-- comfortable table editing -- comfortable table editing, esp. used in orgmode
{ 'dhruvasagar/vim-table-mode' }, { 'dhruvasagar/vim-table-mode' },
-- TODO, WARN, HACK, PERF, NOTE, TEST and others highlighting and searching -- TODO, WARN, HACK, PERF, NOTE, TEST and others highlighting and searching