From 98df147ad8b333919003f67cfa0f2ff26cc7078f Mon Sep 17 00:00:00 2001 From: Bryan Date: Wed, 9 Oct 2024 22:23:36 -0600 Subject: [PATCH] cloned from https://github.com/ysmb-wtsg/in-and-out.nvim, fixed vim.validate() being called incorrectly in config function --- .gitignore | 0 LICENSE | 21 +++++++++ README.md | 99 +++++++++++++++++++++++++++++++++++++++++ doc/in_and_out.txt | 13 ++++++ doc/tags | 2 + lua/in-and-out/init.lua | 65 +++++++++++++++++++++++++++ stylua.toml | 6 +++ 7 files changed, 206 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 doc/in_and_out.txt create mode 100644 doc/tags create mode 100644 lua/in-and-out/init.lua create mode 100644 stylua.toml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..a12aa6c --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 ysmb-wtsg + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..e7cac24 --- /dev/null +++ b/README.md @@ -0,0 +1,99 @@ +# in-and-out.nvim + +

+ +

+ +`in-and-out` is a Neovim plugin designed to quickly navigate in and out of surrounding characters like quotes (`"`, `'`), parentheses (`(`, `)`), curly braces (`{`, `}`), square brackets (`[`, `]`), and backticks (`). + +![demo](https://github.com/ysmb-wtsg/in-and-out.nvim/assets/85178719/9b641329-bd71-4931-8ad8-c1051641ccd2) + +## Installation + +Use your favorite package manager. For example, using `lazy`: + +```lua +{ + "ysmb-wtsg/in-and-out.nvim", + keys = { + { + "", + function() + require("in-and-out").in_and_out() + end, + mode = "i" + }, + }, +} +``` + +## Configuration + +By default, this plugin will jump into and out of the following surrounding characters: + +```lua +{ '"', "'", "(", ")", "{", "}", "[", "]", "`" } +``` + +If you are happy with this list of targets, you don't need to do any configuration, and you don't need to call `setup`. + +On the other hand, if you want to add to the list of targets or replace it altogether, use the plugin's `setup` method. To add targets, pass `setup` an options table containing a sublist named `additional_targets`. To replace the original targets list entirely, pass `setup` an options table with a sublist called `targets`. + +Note: you cannot use both the `targets` and the `additional_targets` sublists at the same time. If you try, the plugin will apply `targets` and ignore `additional_targets`. + +See the examples below. + +Using `lazy` to add smart quotes as a target: + +```lua +{ + "ysmb-wtsg/in-and-out.nvim", + keys = { + { + "", + function() + require("in-and-out").in_and_out() + end, + mode = "i" + }, + }, + opts = { additional_targets = { "“", "”" } }, +} +``` + +Manual `require` to reset the targets altogether: + +```lua +require("in-and-out.nvim").setup({ + targets = { "(", ")", "[", "]" } +}) +``` + +## Mapping + +By default, the plugin does not set any mapping. You can set one through your plugin manager (if it supports setting mappings) or manually. + +Using `lazy`: + +```lua +{ + "ysmb-wtsg/in-and-out.nvim", + keys = { + { + "", + function() + require("in-and-out").in_and_out() + end, + mode = "i" + }, + }, + opts = { additional_targets = { "“", "”" } }, +} +``` + +Manually: + +```lua +vim.keymap.set("i", "", function() require("in-and-out").in_and_out() +end) +``` diff --git a/doc/in_and_out.txt b/doc/in_and_out.txt new file mode 100644 index 0000000..8f0150c --- /dev/null +++ b/doc/in_and_out.txt @@ -0,0 +1,13 @@ +*in-and-out.txt* in-and-out plugin for Neovim + +INTRODUCTION *in-and-out* + +in-and-out is a Neovim plugin designed to quickly navigate in and out of surrounding characters like quotes (`"`, `'`), parentheses (`(`, `)`), curly braces (`{`, `}`), square brackets (`[`, `]`), and backticks ("`"). + +USAGE + +In insert mode, press to move the cursor out of the paired characters. + +AUTHOR + +Author: ysmb-wtsg diff --git a/doc/tags b/doc/tags new file mode 100644 index 0000000..080cb21 --- /dev/null +++ b/doc/tags @@ -0,0 +1,2 @@ +in-and-out in_and_out.txt /*in-and-out* +in-and-out.txt in_and_out.txt /*in-and-out.txt* diff --git a/lua/in-and-out/init.lua b/lua/in-and-out/init.lua new file mode 100644 index 0000000..0292585 --- /dev/null +++ b/lua/in-and-out/init.lua @@ -0,0 +1,65 @@ +local M = {} + +local function escape_lua_pattern(s) + local matches = { + ["^"] = "%^", + ["$"] = "%$", + ["("] = "%(", + [")"] = "%)", + ["%"] = "%%", + ["."] = "%.", + ["["] = "%[", + ["]"] = "%]", + ["*"] = "%*", + ["+"] = "%+", + ["-"] = "%-", + ["?"] = "%?", + } + return s:gsub(".", matches) +end + +local targets = { '"', "'", "(", ")", "{", "}", "[", "]", "`" } + +function M.setup(config) + vim.validate({ config = { config, "table", true } }) + vim.validate({ additional_targets = { config.additional_targets, "table", true } }) + vim.validate({ targets = { config.targets, "table", true } }) + -- + if config and config.targets then + targets = config.targets + return + end + + if config and config.additional_targets then + for _, target in ipairs(config.additional_targets) do + targets[#targets + 1] = target + end + end +end + +function M.in_and_out() + local current_row, current_col = unpack(vim.api.nvim_win_get_cursor(0)) + local lines_in_buf = vim.api.nvim_buf_get_lines(0, current_row - 1, -1, false) + + local target_row = 0 + local target_col = nil + for i, line in ipairs(lines_in_buf) do + for _, char in ipairs(targets) do + local found_col = string.find(line, escape_lua_pattern(char), current_col + 1) + if found_col and (not target_col or found_col < target_col) then + -- If char is a multibyte character, we need to take into account the extra bytes. + target_col = found_col + vim.fn.strlen(char) - 1 + end + end + if target_col then + target_row = current_row + i - 1 + break + end + current_col = 0 + end + if target_col then + vim.api.nvim_win_set_cursor(0, { target_row, target_col }) + end +end + +return M diff --git a/stylua.toml b/stylua.toml new file mode 100644 index 0000000..8adf588 --- /dev/null +++ b/stylua.toml @@ -0,0 +1,6 @@ +column_width = 120 +line_endings = "Unix" +indent_type = "Tabs" +quote_style = "AutoPreferDouble" +call_parentheses = "Always" +collapse_simple_statement = "Never"