From 7cce5e42040fbae4ad3177ab6bbd5568f6065ee6 Mon Sep 17 00:00:00 2001 From: Youwen Wu Date: Sat, 1 Feb 2025 14:18:20 -0800 Subject: [PATCH] feat: fennelize intellitab --- fnl/scripts/intellitab.fnl | 52 +++++++++++++++++++++++++++++++ lua/scripts/intellitab.lua | 63 ++++++++++++++++---------------------- 2 files changed, 78 insertions(+), 37 deletions(-) create mode 100644 fnl/scripts/intellitab.fnl diff --git a/fnl/scripts/intellitab.fnl b/fnl/scripts/intellitab.fnl new file mode 100644 index 0000000..419f0df --- /dev/null +++ b/fnl/scripts/intellitab.fnl @@ -0,0 +1,52 @@ +;; Copyright 2021 Pedro Alves + +;; Makes it so hitting at the start of a line puts you where smartindent +;; would have +;; +;; Converted by antifennel + +(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 : 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} diff --git a/lua/scripts/intellitab.lua b/lua/scripts/intellitab.lua index 3ece9b1..82bf9ca 100644 --- a/lua/scripts/intellitab.lua +++ b/lua/scripts/intellitab.lua @@ -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("", 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) + else end - - if indent_goal == nil or indent_goal < 0 then - if indentexpr ~= "" then + 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] }) + 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] }) + indent_goal = v.nvim_call_function("indent", {cursor[1]}) end + 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] + 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}