From a7cdb3ce27de1f54beb12fa450f0a45e2a448d2f Mon Sep 17 00:00:00 2001 From: Dung Huynh Duc Date: Sun, 27 Oct 2024 23:00:39 +0800 Subject: [PATCH] refactor(hurl): rename functions and variables for clarity and remove redundant code --- lua/hurl/lib/hurl_runner.lua | 18 +++++------ lua/hurl/main.lua | 58 ++++++++++++++++-------------------- 2 files changed, 34 insertions(+), 42 deletions(-) diff --git a/lua/hurl/lib/hurl_runner.lua b/lua/hurl/lib/hurl_runner.lua index 4ac7dd0..d8e7d5b 100644 --- a/lua/hurl/lib/hurl_runner.lua +++ b/lua/hurl/lib/hurl_runner.lua @@ -7,11 +7,10 @@ local M = {} M.is_running = false M.start_time = nil M.response = {} -M.head_state = '' --- Log the Hurl command ---@param cmd table -local function log_hurl_command(cmd) +local function save_last_hurl_command(cmd) local command_str = table.concat(cmd, ' ') _HURL_GLOBAL_CONFIG.last_hurl_command = command_str utils.log_info('hurl: running command: ' .. command_str) @@ -91,7 +90,7 @@ function M.run_hurl_verbose(filePath, fromEntry, toEntry, isVeryVerbose, additio -- Log the Hurl command local hurl_command = 'hurl ' .. table.concat(args, ' ') - log_hurl_command({ 'hurl', unpack(args) }) + save_last_hurl_command({ 'hurl', unpack(args) }) -- Always use split mode for verbose commands local display = require('hurl.split') @@ -237,7 +236,6 @@ function M.execute_hurl_cmd(opts, callback) M.is_running = true M.start_time = vim.loop.hrtime() -- Capture the start time spinner.show() - M.head_state = '' utils.log_info('hurl: running request') utils.notify('hurl: running request', vim.log.levels.INFO) @@ -245,7 +243,7 @@ function M.execute_hurl_cmd(opts, callback) local is_file_mode = utils.has_file_in_opts(opts) -- Add verbose mode by default if not in JSON mode - if not is_json_mode then + if not is_json_mode and not vim.tbl_contains(opts, '--verbose') then table.insert(opts, '--verbose') end @@ -286,7 +284,7 @@ function M.execute_hurl_cmd(opts, callback) end M.response = {} - log_hurl_command(cmd) + save_last_hurl_command(cmd) -- Clear the display and show processing message with Hurl command local display = require('hurl.' .. _HURL_GLOBAL_CONFIG.mode) @@ -317,7 +315,7 @@ function M.execute_hurl_cmd(opts, callback) utils.notify('Hurl command failed. Check the split view for details.', vim.log.levels.ERROR) -- Show error in split view - local display = require('hurl.split') + local split = require('hurl.split') local error_data = { body = '# Hurl Error\n\n```sh\n' .. stderr_data .. '\n```', headers = {}, @@ -327,7 +325,7 @@ function M.execute_hurl_cmd(opts, callback) response_time = 0, curl_command = 'N/A', } - display.show(error_data, 'markdown') + split.show(error_data, 'markdown') return end @@ -349,7 +347,7 @@ function M.execute_hurl_cmd(opts, callback) local result = hurl_parser.parse_hurl_output(stderr_data, stdout_data) -- Display the result using popup or split based on the configuration - local display = require('hurl.' .. _HURL_GLOBAL_CONFIG.mode) + local container = require('hurl.' .. _HURL_GLOBAL_CONFIG.mode) -- Prepare the data for display local last_entry = result.entries[#result.entries] @@ -394,7 +392,7 @@ function M.execute_hurl_cmd(opts, callback) display_data.display_type = display_type - display.show(display_data, display_type) + container.show(display_data, display_type) history.update_history(display_data) diff --git a/lua/hurl/main.lua b/lua/hurl/main.lua index 648aee4..a9f2fd2 100644 --- a/lua/hurl/main.lua +++ b/lua/hurl/main.lua @@ -15,11 +15,15 @@ local function run_current_file(opts) hurl_runner.execute_hurl_cmd(opts) end --- Create a temporary file with the lines to run ----@param lines string[] +-- Run selection ---@param opts table The options ----@param callback? function The callback function -local function run_lines(lines, opts, callback) +local function run_selection(opts) + opts = opts or {} + local lines = utils.get_visual_selection() + if not lines then + return + end + -- Create a temporary file with the lines to run local fname = utils.create_tmp_file(lines) if not fname then @@ -30,7 +34,7 @@ local function run_lines(lines, opts, callback) -- Add the temporary file to the arguments table.insert(opts, fname) - hurl_runner.execute_hurl_cmd(opts, callback) + hurl_runner.execute_hurl_cmd(opts) -- Clean up the temporary file after a delay local timeout = 1000 @@ -45,34 +49,27 @@ local function run_lines(lines, opts, callback) end, timeout) end --- Run selection ----@param opts table The options -local function run_selection(opts) - opts = opts or {} - local lines = utils.get_visual_selection() - if not lines then - return - end - - run_lines(lines, opts) -end - -- Run at current line ---@param start_line number ----@param end_line number +---@param end_line number|nil ---@param opts table ---@param callback? function local function run_at_lines(start_line, end_line, opts, callback) opts = opts or {} - -- Get the lines from the buffer - local lines = vim.api.nvim_buf_get_lines(0, start_line - 1, end_line, false) + local file_path = vim.fn.expand('%:p') - if not lines or vim.tbl_isempty(lines) then - utils.notify('hurl: no lines to run', vim.log.levels.WARN) - return + -- Insert the file path first + table.insert(opts, file_path) + + -- Then add the --from-entry and --to-entry options + table.insert(opts, '--from-entry') + table.insert(opts, tostring(start_line)) + if end_line then + table.insert(opts, '--to-entry') + table.insert(opts, tostring(end_line)) end - run_lines(lines, opts, callback) + hurl_runner.execute_hurl_cmd(opts, callback) end -- Helper function to run verbose commands in split mode @@ -115,7 +112,7 @@ function M.setup() utils.log_info( 'hurl: running request at line ' .. result.start_line .. ' to ' .. result.end_line ) - run_at_lines(result.start_line, result.end_line, opts.fargs) + run_at_lines(result.current, result.current, opts.fargs) else utils.log_info('hurl: not HTTP method found in the current line' .. result.start_line) utils.notify('hurl: no HTTP method found in the current line', vim.log.levels.INFO) @@ -129,7 +126,7 @@ function M.setup() or http.find_http_verb_positions_in_buffer() utils.log_info('hurl: running request to entry #' .. vim.inspect(result)) if result.current > 0 then - run_at_lines(1, result.end_line, opts.fargs) + run_at_lines(1, result.current, opts.fargs) else utils.log_info('hurl: not HTTP method found in the current line' .. result.end_line) utils.notify('hurl: no HTTP method found in the current line', vim.log.levels.INFO) @@ -336,13 +333,10 @@ function M.setup() local is_support_hurl = utils.is_nightly() or utils.is_hurl_parser_available local result = is_support_hurl and http.find_hurl_entry_positions_in_buffer() or http.find_http_verb_positions_in_buffer() - if result.current > 0 and result.start_line and result.end_line then - utils.log_info( - 'hurl: running request at line ' .. result.start_line .. ' to ' .. result.end_line - ) + if result.current > 0 then + utils.log_info('hurl: running request from entry ' .. result.current .. ' to end') opts.fargs = opts.fargs or {} - local end_line = vim.api.nvim_buf_line_count(0) - run_at_lines(result.start_line, end_line, opts.fargs) + run_at_lines(result.current, nil, opts.fargs) else utils.log_info('hurl: no HTTP method found in the current line') utils.notify('hurl: no HTTP method found in the current line', vim.log.levels.INFO)