Skip to content

Commit

Permalink
Add an alternate way to enable abbreviations in other filetypes / buf…
Browse files Browse the repository at this point in the history
…fers.
  • Loading branch information
Julian committed Sep 4, 2024
1 parent d89d027 commit 3548214
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
38 changes: 26 additions & 12 deletions lua/lean/abbreviations.lua
Original file line number Diff line number Diff line change
Expand Up @@ -256,32 +256,46 @@ function abbreviations.convert()
vim.api.nvim_win_set_cursor(0, { row1 + 1, col1 + new_cursor_col_shift })
end

function abbreviations.enable(pattern, opts)
opts = vim.tbl_extend('keep', opts or {}, { leader = '\\', extra = {} })

abbreviations.leader = opts.leader

abbreviations.abbreviations = abbreviations.load()
for from, to in pairs(opts.extra) do
abbreviations.abbreviations[from] = to
end
---Enable abbreviation expansion in the current buffer.
---@param bufnr? number the buffer to attach to, defaulting to the current one
function abbreviations.attach(bufnr)
local augroup = vim.api.nvim_create_augroup('LeanExpandAbbreviations', { clear = false })

local augroup = vim.api.nvim_create_augroup('LeanAbbreviations', { clear = false })
for event, callback in pairs {
InsertCharPre = insert_char_pre,
InsertLeave = abbreviations.convert,
BufLeave = abbreviations.convert,
} do
vim.api.nvim_create_autocmd(event, {
group = augroup,
pattern = pattern,
buffer = bufnr or 0,
callback = callback,
})
end
end

function abbreviations.enable(opts)
opts = vim.tbl_extend('keep', opts or {}, { leader = '\\', extra = {} })

abbreviations.leader = opts.leader
abbreviations.abbreviations = abbreviations.load()
for from, to in pairs(opts.extra) do
abbreviations.abbreviations[from] = to
end

vim.cmd [[hi def leanAbbreviationMark cterm=underline gui=underline guisp=Gray]]

local augroup = vim.api.nvim_create_augroup('LeanAbbreviations', {})
vim.api.nvim_create_autocmd('Filetype', {
group = augroup,
pattern = { 'lean' },
callback = function(_)
abbreviations.attach()
end,
})

vim.api.nvim_create_autocmd('CmdwinEnter', { group = augroup, callback = cmdwin_enter })
vim.api.nvim_create_autocmd('CmdwinLeave', { group = augroup, callback = cmdwin_leave })
vim.cmd [[hi def leanAbbreviationMark cterm=underline gui=underline guisp=Gray]]
end

return abbreviations
2 changes: 1 addition & 1 deletion lua/lean/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function lean.setup(opts)

opts.abbreviations = opts.abbreviations or {}
if opts.abbreviations.enable ~= false then
require('lean.abbreviations').enable('*.lean', opts.abbreviations)
require('lean.abbreviations').enable(opts.abbreviations)
end

opts.infoview = opts.infoview or {}
Expand Down
2 changes: 1 addition & 1 deletion spec/abbreviations/abbreviations_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('unicode abbreviation expansion', function()
assert.contents.are [[\a]]
vim.cmd.normal 'dd'

require('lean.abbreviations').enable '*.txt'
require('lean.abbreviations').attach()

helpers.insert [[\a]]
assert.contents.are [[α]]
Expand Down

0 comments on commit 3548214

Please sign in to comment.