From 7e56e3f9249dde6e18a52f01779fa702044bced9 Mon Sep 17 00:00:00 2001 From: Huynh Duc Dung Date: Thu, 2 May 2024 21:20:07 +0800 Subject: [PATCH] feat(hurl.nvim): add variable editing functionality in HurlManageVariable buffer --- README.md | 7 ++++++- lua/hurl/main.lua | 17 ++++++++++++++++- lua/hurl/popup.lua | 11 +++++++++-- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 0fad2e7..da15900 100644 --- a/README.md +++ b/README.md @@ -184,7 +184,12 @@ To use this command, simply type `:HurlManageVariable` in the command line: :HurlManageVariable ``` -Please note that as of now, this command only supports viewing the variables. The ability to add new variables, modify existing ones, or delete variables directly from this buffer is not available yet. However, this feature is on the roadmap and will be added in future updates. +The default keymap for this buffer is: + +- `q`: Close the buffer +- `e`: Edit the variable + +[![Manage variables](https://i.gyazo.com/0492719eb7a14f42cebff6996bde8672.gif)](https://gyazo.com/0492719eb7a14f42cebff6996bde8672) For now, if you want to modify the global variables, you can do so by using the `HurlSetVariable` command or by editing your `vars.env` file directly. diff --git a/lua/hurl/main.lua b/lua/hurl/main.lua index 171b264..1135f90 100644 --- a/lua/hurl/main.lua +++ b/lua/hurl/main.lua @@ -479,7 +479,22 @@ function M.setup() end local popup = require('hurl.popup') - popup.show_text('Hurl.nvim - Global variables', lines) + local text_popup = popup.show_text( + 'Hurl.nvim - Global variables', + lines, + 'Press `q` to close. Press `e` to edit the variable.' + ) + + -- Add e key binding to edit the variable + text_popup:map('n', 'e', function() + local line = vim.api.nvim_get_current_line() + local var_name = line:match('^(.-) =') + if var_name then + local new_value = vim.fn.input('Enter new value for ' .. var_name .. ': ') + _HURL_GLOBAL_CONFIG.global_vars[var_name] = new_value + vim.api.nvim_set_current_line(var_name .. ' = ' .. new_value) + end + end) end, { nargs = '*', range = true, diff --git a/lua/hurl/popup.lua b/lua/hurl/popup.lua index ba710e9..3c7c7d1 100644 --- a/lua/hurl/popup.lua +++ b/lua/hurl/popup.lua @@ -118,7 +118,12 @@ M.clear = function() vim.api.nvim_buf_set_lines(popups.bottom.bufnr, 0, -1, false, { 'Processing...' }) end -M.show_text = function(title, lines) +--- Show text in a popup +---@param title string +---@param lines table +---@param bottom? string +---@return any +M.show_text = function(title, lines, bottom) -- Create a new popup local text_popup = Popup({ enter = true, @@ -128,7 +133,7 @@ M.show_text = function(title, lines) text = { top = title, top_align = 'center', - bottom = 'Press `q` to close', + bottom = bottom or 'Press `q` to close', bottom_align = 'left', }, }, @@ -156,6 +161,8 @@ M.show_text = function(title, lines) end) text_popup:mount() + + return text_popup end return M