add intellitab
This commit is contained in:
parent
ce2bb5fb5d
commit
7da9807f23
2 changed files with 51 additions and 49 deletions
|
@ -57,3 +57,5 @@ vim.keymap.set({ "n", "v" }, "<leader>cbf", function()
|
||||||
vim.b[0].disable_autoformat = true
|
vim.b[0].disable_autoformat = true
|
||||||
end
|
end
|
||||||
end, { desc = "Disable autoformat on save buffer" })
|
end, { desc = "Disable autoformat on save buffer" })
|
||||||
|
|
||||||
|
vim.keymap.set("i", "<Tab>", require("scripts.intellitab").indent)
|
||||||
|
|
|
@ -7,66 +7,66 @@ local v = vim.api
|
||||||
local treesitter = require("nvim-treesitter")
|
local treesitter = require("nvim-treesitter")
|
||||||
|
|
||||||
local function get_line_indent(line, sw)
|
local function get_line_indent(line, sw)
|
||||||
local indent = 0
|
local indent = 0
|
||||||
|
|
||||||
for c in line:gmatch("%s") do
|
for c in line:gmatch("%s") do
|
||||||
if c == "\t" then
|
if c == "\t" then
|
||||||
indent = indent + sw
|
indent = indent + sw
|
||||||
else
|
else
|
||||||
indent = indent + 1
|
indent = indent + 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return indent
|
return indent
|
||||||
end
|
end
|
||||||
|
|
||||||
local function indent()
|
local function indent()
|
||||||
local cursor = v.nvim_win_get_cursor(0)
|
local cursor = v.nvim_win_get_cursor(0)
|
||||||
local line = v.nvim_buf_get_lines(0, cursor[1] - 1, cursor[1], false)[1]
|
local line = v.nvim_buf_get_lines(0, cursor[1] - 1, cursor[1], false)[1]
|
||||||
|
|
||||||
local indentexpr = v.nvim_buf_get_option(0, "indentexpr")
|
local indentexpr = v.nvim_get_option_value("indentexpr", { buf = 0 })
|
||||||
local expand = v.nvim_buf_get_option(0, "expandtab")
|
local expand = v.nvim_get_option_value("expandtab", { buf = 0 })
|
||||||
local shiftwidth = v.nvim_eval("shiftwidth()")
|
local shiftwidth = v.nvim_eval("shiftwidth()")
|
||||||
local tab_char = v.nvim_replace_termcodes("<Tab>", true, true, true)
|
local tab_char = v.nvim_replace_termcodes("<Tab>", true, true, true)
|
||||||
local indent_goal
|
local indent_goal
|
||||||
|
|
||||||
if treesitter ~= nil and treesitter.get_indent ~= nil then
|
if treesitter ~= nil and treesitter.get_indent ~= nil then
|
||||||
indent_goal = treesitter.get_indent(line)
|
indent_goal = treesitter.get_indent(line)
|
||||||
end
|
end
|
||||||
|
|
||||||
if indent_goal == nil or indent_goal < 0 then
|
if indent_goal == nil or indent_goal < 0 then
|
||||||
if indentexpr ~= "" then
|
if indentexpr ~= "" then
|
||||||
indent_goal = v.nvim_eval(indentexpr)
|
indent_goal = v.nvim_eval(indentexpr)
|
||||||
elseif v.nvim_buf_get_option(0, "cindent") then
|
elseif v.nvim_get_option_value("cindent", { buf = 0 }) then
|
||||||
indent_goal = v.nvim_call_function("cindent", { cursor[1] })
|
indent_goal = v.nvim_call_function("cindent", { cursor[1] })
|
||||||
else
|
else
|
||||||
indent_goal = v.nvim_call_function("indent", { cursor[1] })
|
indent_goal = v.nvim_call_function("indent", { cursor[1] })
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if indent_goal == -1 and cursor[1] ~= 1 then
|
if indent_goal == -1 and cursor[1] ~= 1 then
|
||||||
local prev_line = v.nvim_buf_get_lines(0, cursor[1] - 2, cursor[1] - 1, false)[1]
|
local prev_line = v.nvim_buf_get_lines(0, cursor[1] - 2, cursor[1] - 1, false)[1]
|
||||||
indent_goal = get_line_indent(prev_line, shiftwidth)
|
indent_goal = get_line_indent(prev_line, shiftwidth)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Reset the cursor, since the functions are free to move it
|
-- Reset the cursor, since the functions are free to move it
|
||||||
v.nvim_win_set_cursor(0, cursor)
|
v.nvim_win_set_cursor(0, cursor)
|
||||||
|
|
||||||
if cursor[2] == 0 and line == "" and indent_goal ~= nil and indent_goal > 0 then
|
if cursor[2] == 0 and line == "" and indent_goal ~= nil and indent_goal > 0 then
|
||||||
local i = 0
|
local i = 0
|
||||||
while i < indent_goal do
|
while i < indent_goal do
|
||||||
if expand then
|
if expand then
|
||||||
v.nvim_feedkeys(" ", "n", true)
|
v.nvim_feedkeys(" ", "n", true)
|
||||||
i = i + 1
|
i = i + 1
|
||||||
else
|
else
|
||||||
v.nvim_feedkeys(tab_char, "n", true)
|
v.nvim_feedkeys(tab_char, "n", true)
|
||||||
i = i + shiftwidth
|
i = i + shiftwidth
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
print(i, indent_goal)
|
print(i, indent_goal)
|
||||||
else
|
else
|
||||||
v.nvim_feedkeys(tab_char, "n", true)
|
v.nvim_feedkeys(tab_char, "n", true)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return { indent = indent }
|
return { indent = indent }
|
||||||
|
|
Loading…
Reference in a new issue