Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

specify file extension for abbreviations #348

Merged
merged 2 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lua/lean/abbreviations.lua
Original file line number Diff line number Diff line change
Expand Up @@ -256,26 +256,27 @@ function abbreviations.convert()
vim.api.nvim_win_set_cursor(0, { row1 + 1, col1 + new_cursor_col_shift })
end

function abbreviations.enable(opts)
function abbreviations.enable(pattern, opts)
abbreviations.leader = opts.leader or '\\'

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

local augroup = vim.api.nvim_create_augroup('LeanAbbreviations', {})
local augroup = vim.api.nvim_create_augroup('LeanAbbreviations' .. pattern, {})
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 = { '*.lean' },
pattern = pattern,
callback = callback,
})
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]]
Expand Down
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(opts.abbreviations)
require('lean.abbreviations').enable('*.lean', opts.abbreviations)
end

opts.infoview = opts.infoview or {}
Expand Down
9 changes: 9 additions & 0 deletions spec/abbreviations/abbreviations_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ local function wait_for_expansion()
end

require('lean').setup {}
require('lean.abbreviations').enable('*.txt', {})

describe('unicode abbreviation expansion', function()
it(
Expand All @@ -20,6 +21,14 @@ describe('unicode abbreviation expansion', function()
end)
)

it(
'autoexpands abbreviations',
helpers.clean_buffer(function()
helpers.insert [[\a]]
assert.contents.are [[α]]
end, nil, 'txt')
)

describe('explicit triggers', function()
it(
'inserts a space on <Space>',
Expand Down
8 changes: 4 additions & 4 deletions spec/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@ end

-- Even though we can delete a buffer, so should be able to reuse names,
-- we do this to ensure if a test fails, future ones still get new "files".
local function set_unique_name_so_we_always_have_a_separate_fake_file(bufnr)
local function set_unique_name_so_we_always_have_a_separate_fake_file(bufnr, ext)
local counter = helpers._clean_buffer_counter
helpers._clean_buffer_counter = helpers._clean_buffer_counter + 1
local unique_name = string.format('%s/unittest-%d.lean', fixtures.project.path, counter)
local unique_name = string.format('%s/unittest-%d.%s', fixtures.project.path, counter, ext)
vim.api.nvim_buf_set_name(bufnr, unique_name)
end

Expand All @@ -129,7 +129,7 @@ end
-- Waits for the LSP to be ready before proceeding with a given callback.
--
-- Yes c(lean) may be a double entendre, and no I don't feel bad.
function helpers.clean_buffer(contents, callback)
function helpers.clean_buffer(contents, callback, ext)
local lines

-- Support a 1-arg version where we assume the contents is an empty buffer.
Expand All @@ -142,7 +142,7 @@ function helpers.clean_buffer(contents, callback)

return function()
local bufnr = vim.api.nvim_create_buf(false, false)
set_unique_name_so_we_always_have_a_separate_fake_file(bufnr)
set_unique_name_so_we_always_have_a_separate_fake_file(bufnr, ext or 'lean')
-- apparently necessary to trigger BufWinEnter
vim.api.nvim_set_current_buf(bufnr)
vim.bo.bufhidden = 'hide'
Expand Down