From 0972e97cfba7cc49ace1cb0d213b7e6b2e7cc8aa Mon Sep 17 00:00:00 2001 From: Youwen Wu Date: Thu, 31 Oct 2024 11:04:57 -0700 Subject: [PATCH] feat: add intellitab and chameleon.nvim --- init.lua | 6 +++ lua/plugins/blink-cmp.lua | 35 +++++++++------ lua/scripts/chameleon.lua | 91 ++++++++++++++++++++++++++++++++++++++ lua/scripts/intellitab.lua | 70 +++++++++++++++++++++++++++++ 4 files changed, 189 insertions(+), 13 deletions(-) create mode 100644 lua/scripts/chameleon.lua create mode 100644 lua/scripts/intellitab.lua diff --git a/init.lua b/init.lua index 6f6b9ee..89201ae 100644 --- a/init.lua +++ b/init.lua @@ -27,6 +27,10 @@ vim.opt.listchars = { tab = "» ", trail = "·", nbsp = "␣" } vim.opt.scrolloff = 10 +if os.getenv("TERM") == "xterm-kitty" then + require("scripts.chameleon").setup() +end + vim.cmd.colorscheme("rose-pine") require("nvim-treesitter.configs").setup({ @@ -69,3 +73,5 @@ vim.api.nvim_create_autocmd("TextYankPost", { vim.highlight.on_yank({ higroup = "Visual", timeout = 300 }) end, }) + +-- vim.keymap.set("i", "", require("scripts.intellitab").indent) diff --git a/lua/plugins/blink-cmp.lua b/lua/plugins/blink-cmp.lua index eecba0d..5819646 100644 --- a/lua/plugins/blink-cmp.lua +++ b/lua/plugins/blink-cmp.lua @@ -6,19 +6,30 @@ return { -- for keymap, all values may be string | string[] -- use an empty table to disable a keymap keymap = { - show = "", - hide = "", - accept = "", - select_prev = { "", "" }, - select_next = { "", "" }, + [""] = { "show", "show_documentation", "hide_documentation" }, + [""] = { "hide" }, - show_documentation = {}, - hide_documentation = {}, - scroll_documentation_up = "", - scroll_documentation_down = "", + [""] = { + function(cmp) + if cmp.is_in_snippet() then + return cmp.accept() + else + return cmp.select_and_accept() + end + end, + "snippet_forward", + require("scripts.intellitab").indent, + "fallback", + }, + [""] = { "snippet_backward", "fallback" }, - snippet_forward = "", - snippet_backward = "", + [""] = { "select_prev", "fallback" }, + [""] = { "select_next", "fallback" }, + [""] = { "select_prev", "fallback" }, + [""] = { "select_next", "fallback" }, + + [""] = { "scroll_documentation_up", "fallback" }, + [""] = { "scroll_documentation_down", "fallback" }, }, fuzzy = { @@ -27,8 +38,6 @@ return { -- proximity bonus boosts the score of items with a value in the buffer use_proximity = true, max_items = 200, - -- controls which sorts to use and in which order, these three are currently the only allowed options - sorts = { "label", "kind", "score" }, prebuiltBinaries = { -- Whether or not to automatically download a prebuilt binary from github. If this is set to `false` diff --git a/lua/scripts/chameleon.lua b/lua/scripts/chameleon.lua new file mode 100644 index 0000000..ab7a8e0 --- /dev/null +++ b/lua/scripts/chameleon.lua @@ -0,0 +1,91 @@ +-- MIT License +-- Copyright (c) 2023 Shaun +-- + +local M = {} +local fn = vim.fn +local api = vim.api +M.original_color = nil + +local get_kitty_background = function() + if M.original_color == nil then + fn.jobstart({ "kitty", "@", "get-colors" }, { + on_stdout = function(_, d, _) + for _, result in ipairs(d) do + if string.match(result, "^background") then + local color = vim.split(result, "%s+")[2] + M.original_color = color + break + end + end + end, + on_stderr = function(_, d, _) + if #d > 1 then + api.nvim_err_writeln("Chameleon.nvim: Error getting background. Make sure kitty remote control is turned on.") + end + end, + }) + end +end + +local change_background = function(color, sync) + local arg = 'background="' .. color .. '"' + local command = "kitty @ set-colors " .. arg + if not sync then + fn.jobstart(command, { + on_stderr = function(_, d, _) + if #d > 1 then + api.nvim_err_writeln( + "Chameleon.nvim: Error changing background. Make sure kitty remote control is turned on." + ) + end + end, + }) + else + fn.system(command) + end +end + +local setup_autocmds = function() + local autocmd = api.nvim_create_autocmd + local autogroup = api.nvim_create_augroup + local bg_change = autogroup("BackgroundChange", { clear = true }) + + autocmd({ "ColorScheme", "VimResume" }, { + pattern = "*", + callback = function() + local color = string.format("#%06X", vim.api.nvim_get_hl(0, { name = "Normal" }).bg) + change_background(color) + end, + group = bg_change, + }) + + autocmd("User", { + pattern = "NvChadThemeReload", + callback = function() + local color = string.format("#%06X", vim.api.nvim_get_hl(0, { name = "Normal" }).bg) + change_background(color) + end, + group = bg_change, + }) + + autocmd({ "VimLeavePre", "VimSuspend" }, { + callback = function() + if M.original_color ~= nil then + change_background(M.original_color, true) + -- Looks like it was silently fixed in NVIM 0.10. At least, I can't reproduce it anymore, + -- so for now disable it and see if anyone reports it again. + -- https://github.com/neovim/neovim/issues/21856 + -- vim.cmd[[sleep 10m]] + end + end, + group = autogroup("BackgroundRestore", { clear = true }), + }) +end + +M.setup = function() + get_kitty_background() + setup_autocmds() +end + +return M diff --git a/lua/scripts/intellitab.lua b/lua/scripts/intellitab.lua new file mode 100644 index 0000000..3ece9b1 --- /dev/null +++ b/lua/scripts/intellitab.lua @@ -0,0 +1,70 @@ +-- Copyright 2021 Pedro Alves + +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 + else + 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 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 + 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 + 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) + 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 + 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 +end + +return { indent = indent }