2024-10-09 17:13:16 -07:00
|
|
|
-- `intellitab.nvim` by Pedro Alves (https://github.com/pta2002)
|
|
|
|
-- URL: https://github.com/pta2002/intellitab.nvim?tab=MIT-1-ov-file
|
|
|
|
-- Copyright 2021 Pedro Alves. Licensed under MIT.
|
|
|
|
|
|
|
|
local v = vim.api
|
|
|
|
|
|
|
|
local treesitter = require("nvim-treesitter")
|
|
|
|
|
|
|
|
local function get_line_indent(line, sw)
|
2024-10-10 00:59:42 -07:00
|
|
|
local indent = 0
|
2024-10-09 17:13:16 -07:00
|
|
|
|
2024-10-10 00:59:42 -07:00
|
|
|
for c in line:gmatch("%s") do
|
|
|
|
if c == "\t" then
|
|
|
|
indent = indent + sw
|
|
|
|
else
|
|
|
|
indent = indent + 1
|
|
|
|
end
|
|
|
|
end
|
2024-10-09 17:13:16 -07:00
|
|
|
|
2024-10-10 00:59:42 -07:00
|
|
|
return indent
|
2024-10-09 17:13:16 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
local function indent()
|
2024-10-10 00:59:42 -07:00
|
|
|
local cursor = v.nvim_win_get_cursor(0)
|
|
|
|
local line = v.nvim_buf_get_lines(0, cursor[1] - 1, cursor[1], false)[1]
|
2024-10-09 17:13:16 -07:00
|
|
|
|
2024-10-10 00:59:42 -07:00
|
|
|
local indentexpr = v.nvim_get_option_value("indentexpr", { buf = 0 })
|
|
|
|
local expand = v.nvim_get_option_value("expandtab", { buf = 0 })
|
|
|
|
local shiftwidth = v.nvim_eval("shiftwidth()")
|
|
|
|
local tab_char = v.nvim_replace_termcodes("<Tab>", true, true, true)
|
|
|
|
local indent_goal
|
2024-10-09 17:13:16 -07:00
|
|
|
|
2024-10-10 00:59:42 -07:00
|
|
|
if treesitter ~= nil and treesitter.get_indent ~= nil then
|
|
|
|
indent_goal = treesitter.get_indent(line)
|
|
|
|
end
|
2024-10-09 17:13:16 -07:00
|
|
|
|
2024-10-10 00:59:42 -07:00
|
|
|
if indent_goal == nil or indent_goal < 0 then
|
|
|
|
if indentexpr ~= "" then
|
|
|
|
indent_goal = v.nvim_eval(indentexpr)
|
|
|
|
elseif v.nvim_get_option_value("cindent", { buf = 0 }) then
|
|
|
|
indent_goal = v.nvim_call_function("cindent", { cursor[1] })
|
|
|
|
else
|
|
|
|
indent_goal = v.nvim_call_function("indent", { cursor[1] })
|
|
|
|
end
|
|
|
|
end
|
2024-10-09 17:13:16 -07:00
|
|
|
|
2024-10-10 00:59:42 -07:00
|
|
|
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]
|
|
|
|
indent_goal = get_line_indent(prev_line, shiftwidth)
|
|
|
|
end
|
2024-10-09 17:13:16 -07:00
|
|
|
|
2024-10-10 00:59:42 -07:00
|
|
|
-- Reset the cursor, since the functions are free to move it
|
|
|
|
v.nvim_win_set_cursor(0, cursor)
|
2024-10-09 17:13:16 -07:00
|
|
|
|
2024-10-10 00:59:42 -07:00
|
|
|
if cursor[2] == 0 and line == "" and indent_goal ~= nil and indent_goal > 0 then
|
|
|
|
local i = 0
|
|
|
|
while i < indent_goal do
|
|
|
|
if expand then
|
|
|
|
v.nvim_feedkeys(" ", "n", true)
|
|
|
|
i = i + 1
|
|
|
|
else
|
|
|
|
v.nvim_feedkeys(tab_char, "n", true)
|
|
|
|
i = i + shiftwidth
|
|
|
|
end
|
|
|
|
end
|
|
|
|
print(i, indent_goal)
|
|
|
|
else
|
|
|
|
v.nvim_feedkeys(tab_char, "n", true)
|
|
|
|
end
|
2024-10-09 17:13:16 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
return { indent = indent }
|