Skip to content

Commit

Permalink
[Feat] add auto command to support auto pair
Browse files Browse the repository at this point in the history
  • Loading branch information
mistricky committed Feb 11, 2024
1 parent ec98883 commit f432370
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 15 deletions.
1 change: 1 addition & 0 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ require("neovide-options")
require("plugins")
require("theme")
require("kitty-configs")
require("autocmds")
1 change: 1 addition & 0 deletions lua/autocmds/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require("autocmds.insert-char-pre")
26 changes: 26 additions & 0 deletions lua/autocmds/insert-char-pre/auto-pair.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
local list_utils = require("utils.list")
local AUTO_PAIR_SYMBOL = require("constants.auto-pair")

local function match_auto_pair_symbol(predicate)
return list_utils.find(AUTO_PAIR_SYMBOL, function(pair)
local left, right = table.unpack(pair)

predicate(left, right)
end)
end

return function(char)
local pair = list_utils.find(AUTO_PAIR_SYMBOL, function(pair)
local left, _ = table.unpack(pair)

return left == char
end)

if pair == nil then
return
end

vim.schedule(function()
vim.api.nvim_put({ pair[2] }, "", true, true)
end)
end
7 changes: 7 additions & 0 deletions lua/autocmds/insert-char-pre/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
local auto_pair = require("autocmds.insert-char-pre.auto-pair")

vim.api.nvim_create_autocmd({ "InsertCharPre" }, {
callback = function()
auto_pair(vim.v.char)
end,
})
8 changes: 8 additions & 0 deletions lua/constants/auto-pair.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
return {
{ '"', '"' },
{ "'", "'" },
{ "{", "}" },
{ "[", "]" },
{ "(", ")" },
{ "<", ">" },
}
3 changes: 3 additions & 0 deletions lua/constants/lazy.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
return {
MAX_PRIORITY = 1000,
}
6 changes: 3 additions & 3 deletions lua/plugins/init.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local constants = require("utils.constants")
local options = require("plugins.lazy-options")
local lazy_constants = require("constants.lazy")

require("plugins.lazy-install")

Expand All @@ -14,7 +14,7 @@ require("lazy").setup({
"nvim-tree/nvim-web-devicons",
{
"olimorris/onedarkpro.nvim",
priority = constants.MAX_PRIORITY,
priority = lazy_constants.MAX_PRIORITY,
},
{
"willothy/veil.nvim",
Expand Down Expand Up @@ -69,7 +69,7 @@ require("lazy").setup({
{
"mrjones2014/legendary.nvim",
version = "v2.1.0",
priority = constants.MAX_PRIORITY,
priority = lazy_constants.MAX_PRIORITY,
lazy = false,
},
{
Expand Down
3 changes: 0 additions & 3 deletions lua/utils/constants.lua

This file was deleted.

11 changes: 2 additions & 9 deletions lua/utils/edit.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local string_utils = require("utils.string")
local AUTO_PAIR_SYMBOLS = require("constants.auto-pair")
local edit_utils = {}

local function find_last_string_pos_by_pattern(target_str, pattern)
Expand Down Expand Up @@ -61,18 +62,10 @@ function edit_utils.move_cursor_to_nearest_symbol()
end

function edit_utils.escape_in_symbol()
local matched_symbols = {
{ '"', '"' },
{ "'", "'" },
{ "{", "}" },
{ "[", "]" },
{ "(", ")" },
{ "<", ">" },
}
local cursor_pos = vim.api.nvim_win_get_cursor(0)
local current_line_content = vim.api.nvim_get_current_line()

for _, symbol in ipairs(matched_symbols) do
for _, symbol in ipairs(AUTO_PAIR_SYMBOLS) do
if
string_utils.char_at(current_line_content, cursor_pos[2]) == symbol[2]
and string_utils.char_at(current_line_content, cursor_pos[2] - 1) == symbol[1]
Expand Down
23 changes: 23 additions & 0 deletions lua/utils/list.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
local list_utils = {}

function list_utils.find(list, predicate)
for _, value in ipairs(list) do
if predicate(value) then
return value
end
end

return nil
end

function list_utils.some(list, predicate)
return list_utils.find(list, predicate) ~= nil
end

function list_utils.includes(list, value)
return list_utils.find(list, function(item)
return item == value
end) ~= nil
end

return list_utils

0 comments on commit f432370

Please sign in to comment.