feat: fennelize intellitab
This commit is contained in:
parent
7b8d20b65b
commit
7cce5e4204
2 changed files with 78 additions and 37 deletions
52
fnl/scripts/intellitab.fnl
Normal file
52
fnl/scripts/intellitab.fnl
Normal file
|
@ -0,0 +1,52 @@
|
|||
;; Copyright 2021 Pedro Alves
|
||||
|
||||
;; Makes it so hitting <Tab> at the start of a line puts you where smartindent
|
||||
;; would have
|
||||
;;
|
||||
;; Converted by antifennel <https://fennel-lang.org/see>
|
||||
|
||||
(local v vim.api)
|
||||
(local treesitter (require :nvim-treesitter))
|
||||
(fn get-line-indent [line sw]
|
||||
(var indent 0)
|
||||
(each [c (line:gmatch "%s")]
|
||||
(if (= c "\t") (set indent (+ indent sw)) (set indent (+ indent 1))))
|
||||
indent)
|
||||
|
||||
(fn indent []
|
||||
(let [cursor (v.nvim_win_get_cursor 0)
|
||||
line (. (v.nvim_buf_get_lines 0 (- (. cursor 1) 1) (. cursor 1) false)
|
||||
1)
|
||||
indentexpr (v.nvim_get_option_value :indentexpr {:buf 0})
|
||||
expand (v.nvim_get_option_value :expandtab {:buf 0})
|
||||
shiftwidth (v.nvim_eval "shiftwidth()")
|
||||
tab-char (v.nvim_replace_termcodes :<Tab> true true true)]
|
||||
(var indent-goal nil)
|
||||
(when (and (not= treesitter nil) (not= treesitter.get_indent nil))
|
||||
(set indent-goal (treesitter.get_indent line)))
|
||||
(when (or (= indent-goal nil) (< indent-goal 0))
|
||||
(if (not= indentexpr "") (set indent-goal (v.nvim_eval indentexpr))
|
||||
(v.nvim_get_option_value :cindent {:buf 0})
|
||||
(set indent-goal (v.nvim_call_function :cindent [(. cursor 1)]))
|
||||
(set indent-goal (v.nvim_call_function :indent [(. cursor 1)]))))
|
||||
(when (and (= indent-goal (- 1)) (not= (. cursor 1) 1))
|
||||
(local prev-line (. (v.nvim_buf_get_lines 0 (- (. cursor 1) 2)
|
||||
(- (. cursor 1) 1) false)
|
||||
1))
|
||||
(set indent-goal (get-line-indent prev-line shiftwidth)))
|
||||
(v.nvim_win_set_cursor 0 cursor)
|
||||
(if (and (and (and (= (. cursor 2) 0) (= line "")) (not= indent-goal nil))
|
||||
(> indent-goal 0))
|
||||
(do
|
||||
(var i 0)
|
||||
(while (< i indent-goal)
|
||||
(if expand (do
|
||||
(v.nvim_feedkeys " " :n true)
|
||||
(set i (+ i 1)))
|
||||
(do
|
||||
(v.nvim_feedkeys tab-char :n true)
|
||||
(set i (+ i shiftwidth)))))
|
||||
(print i indent-goal))
|
||||
(v.nvim_feedkeys tab-char :n true))))
|
||||
|
||||
{: indent}
|
67
lua/scripts/intellitab.lua
generated
67
lua/scripts/intellitab.lua
generated
|
@ -1,70 +1,59 @@
|
|||
-- Copyright 2021 Pedro Alves
|
||||
|
||||
-- [nfnl] Compiled from ./fnl/scripts/intellitab.fnl by https://github.com/Olical/nfnl, do not edit.
|
||||
local v = vim.api
|
||||
|
||||
local treesitter = require("nvim-treesitter")
|
||||
|
||||
local function get_line_indent(line, sw)
|
||||
local indent = 0
|
||||
|
||||
for c in line:gmatch("%s") do
|
||||
if c == "\t" then
|
||||
indent = indent + sw
|
||||
if (c == "\t") then
|
||||
indent = (indent + sw)
|
||||
else
|
||||
indent = indent + 1
|
||||
indent = (indent + 1)
|
||||
end
|
||||
end
|
||||
|
||||
return indent
|
||||
end
|
||||
|
||||
local function indent()
|
||||
local cursor = v.nvim_win_get_cursor(0)
|
||||
local line = v.nvim_buf_get_lines(0, cursor[1] - 1, cursor[1], false)[1]
|
||||
|
||||
local indentexpr = v.nvim_get_option_value("indentexpr", { buf = 0 })
|
||||
local expand = v.nvim_get_option_value("expandtab", { buf = 0 })
|
||||
local line = v.nvim_buf_get_lines(0, (cursor[1] - 1), cursor[1], false)[1]
|
||||
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
|
||||
|
||||
if treesitter ~= nil and treesitter.get_indent ~= nil then
|
||||
local indent_goal = nil
|
||||
if ((treesitter ~= nil) and (treesitter.get_indent ~= nil)) then
|
||||
indent_goal = treesitter.get_indent(line)
|
||||
end
|
||||
|
||||
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
|
||||
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
|
||||
|
||||
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]
|
||||
else
|
||||
end
|
||||
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)
|
||||
else
|
||||
end
|
||||
|
||||
-- Reset the cursor, since the functions are free to move it
|
||||
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
|
||||
while i < indent_goal do
|
||||
while (i < indent_goal) do
|
||||
if expand then
|
||||
v.nvim_feedkeys(" ", "n", true)
|
||||
i = i + 1
|
||||
i = (i + 1)
|
||||
else
|
||||
v.nvim_feedkeys(tab_char, "n", true)
|
||||
i = i + shiftwidth
|
||||
i = (i + shiftwidth)
|
||||
end
|
||||
end
|
||||
print(i, indent_goal)
|
||||
return print(i, indent_goal)
|
||||
else
|
||||
v.nvim_feedkeys(tab_char, "n", true)
|
||||
return v.nvim_feedkeys(tab_char, "n", true)
|
||||
end
|
||||
end
|
||||
|
||||
return { indent = indent }
|
||||
return {indent = indent}
|
||||
|
|
Loading…
Reference in a new issue