From bba66a1351ff1f1bd078c8a1b00b8d12b3ed0017 Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Thu, 28 Nov 2024 12:10:51 -0700 Subject: [PATCH] feat: initial version --- .build.yml | 11 +++++ LICENSE | 21 +++++++++ README.md | 69 ++++++++++++++++++++++++++++ lua/lazyjj/health.lua | 30 ++++++++++++ lua/lazyjj/init.lua | 103 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 234 insertions(+) create mode 100644 .build.yml create mode 100644 LICENSE create mode 100644 README.md create mode 100644 lua/lazyjj/health.lua create mode 100644 lua/lazyjj/init.lua diff --git a/.build.yml b/.build.yml new file mode 100644 index 0000000..f8408ca --- /dev/null +++ b/.build.yml @@ -0,0 +1,11 @@ +image: alpine/edge +secrets: + - 88cce185-d2ec-4573-9546-e33f36f79bbf +sources: + - git+ssh://git@git.sr.ht/~swaits/lazyjj.nvim +tasks: + - mirror-to-github: | + cd ~/lazyjj.nvim + ssh-keyscan -t rsa github.com >> ~/.ssh/known_hosts + git remote add github git@github.com:swaits/lazyjj.nvim.git + git push --mirror github diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..35d39a7 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Stephen Waits + +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..b179705 --- /dev/null +++ b/README.md @@ -0,0 +1,69 @@ +# lazyjj.nvim + +A Neovim plugin that provides a floating window interface for +[lazyjj](https://github.com/Cretezy/lazyjj). + +## Requirements + +- Neovim >= 0.5.0 +- [plenary.nvim](https://github.com/nvim-lua/plenary.nvim) +- [lazyjj](https://github.com/Cretezy/lazyjj) installed and available in your PATH + +## Installation + +Using [packer.nvim](https://github.com/wbthomason/packer.nvim): + +```lua +use { + 'swaits/lazyjj.nvim', + requires = 'nvim-lua/plenary.nvim', + config = function() + require('lazyjj').setup() + end +} +``` + +Using [lazy.nvim](https://github.com/folke/lazy.nvim): + +```lua +{ + 'swaits/lazyjj.nvim', + dependencies = 'nvim-lua/plenary.nvim', + opts = {} +} +``` + +## Configuration + +The plugin can be configured by passing options to the setup function: + +```lua +require('lazyjj').setup({ + mapping = 'jj' -- Change the default keymap +}) +``` + +Or when using [lazy.nvim](https://github.com/folke/lazy.nvim): + +```lua +{ + 'swaits/lazyjj.nvim', + dependencies = 'nvim-lua/plenary.nvim', + opts = { + mapping = 'jj' + } +} +``` + +## Usage + +- Use the configured keymap (default: `jj`) to open `lazyjj` in a floating + window +- Or use the `:LazyJJ` command +- Press `q` or lose focus to close the window +- The window will automatically return focus to your previous window when + closing `lazyjj` + +## License + +This plugin is licensed under the [MIT license](LICENSE). diff --git a/lua/lazyjj/health.lua b/lua/lazyjj/health.lua new file mode 100644 index 0000000..54e105d --- /dev/null +++ b/lua/lazyjj/health.lua @@ -0,0 +1,30 @@ +local M = {} + +local health = vim.health or require("health") + +function M.check() + -- Start health check + health.start("lazyjj.nvim") + + -- Check for required plugins + if pcall(require, "plenary") then + health.ok("plenary.nvim installed") + else + health.error("plenary.nvim not installed", { + "Install plenary.nvim", + "https://github.com/nvim-lua/plenary.nvim", + }) + end + + -- Check for lazyjj executable + if vim.fn.executable("lazyjj") == 1 then + health.ok("lazyjj executable found in PATH") + else + health.error("lazyjj executable not found", { + "Install lazyjj", + "Ensure lazyjj is in your PATH", + }) + end +end + +return M diff --git a/lua/lazyjj/init.lua b/lua/lazyjj/init.lua new file mode 100644 index 0000000..259a33f --- /dev/null +++ b/lua/lazyjj/init.lua @@ -0,0 +1,103 @@ +-- File: lua/lazyjj/init.lua + +local M = {} + +local function open_floating_window() + -- Get plenary's float window module which handles window creation + local plenary = require("plenary.window.float") + + -- Create centered floating window at 90% of screen size with rounded borders + local win = plenary.percentage_range_window(0.9, 0.8, { + border = { + "╭", + "─", + "╮", + "│", + "│", + "╰", + "─", + "╯", + }, + }) + + -- Set buffer filetype for syntax highlighting + vim.bo[win.bufnr].filetype = "lazyjj" + + -- Ensure window is completely opaque + vim.wo[win.win_id].winblend = 0 + + -- Hide buffer when window closes rather than delete it + vim.cmd("setlocal bufhidden=hide") + + -- Automatically hide window when focus is lost + vim.api.nvim_create_autocmd("WinLeave", { + buffer = win.bufnr, + callback = function() + vim.cmd("hide") + end, + once = true, + }) + + -- Return window and buffer handles for further manipulation if needed + return win.win_id, win.bufnr +end + +function M.setup(opts) + -- Store user config for later use + M.config = vim.tbl_deep_extend("force", { + -- Default configuration options can go here + mapping = "jj", -- Default keymapping + }, opts or {}) + + -- Create user command + vim.api.nvim_create_user_command("LazyJJ", M.open, {}) + + -- Set up keymapping if provided + if M.config.mapping then + vim.keymap.set("n", M.config.mapping, M.open, { + noremap = true, + silent = true, + desc = "LazyJJ", -- This will work with WhichKey and similar plugins + }) + end +end + +function M.open() + -- Store current window ID for late -- Check if lazyjj is available + local cmd = "lazyjj" + if vim.fn.executable(cmd) ~= 1 then + vim.notify( + "lazyjj executable not found. Please install lazyjj and ensure it's in your PATH.", + vim.log.levels.ERROR + ) + return + end + + local prev_win = vim.api.nvim_get_current_win() + + -- Create floating window + open_floating_window() + + -- Execute lazyjj in the floating window + vim.fn.termopen(cmd, { + -- Return to previous window on exit + on_exit = function() + if vim.api.nvim_win_is_valid(prev_win) then + vim.api.nvim_set_current_win(prev_win) + end + end, + -- Handle potential errors + on_stderr = function(_, data) + for _, line in ipairs(data) do + if line:match("^Error:.*") then + vim.notify("lazyjj error: " .. line, vim.log.levels.ERROR) + end + end + end, + }) + + -- Enter insert mode to allow immediate interaction + vim.cmd("startinsert") +end + +return M