From c511448b28a5b5c1e60ab87518f65245d169ae5b Mon Sep 17 00:00:00 2001 From: mitchell <70453897+orbitalquark@users.noreply.github.com> Date: Sat, 17 Oct 2020 16:02:51 -0400 Subject: Added `textadept.run.set_arguments()`. This replaces custom code in *modules/textadept/menu.lua*. --- docs/manual.md | 3 +++ modules/textadept/menu.lua | 28 +--------------------------- modules/textadept/run.lua | 45 +++++++++++++++++++++++++++++++++++++++++++++ test/test.lua | 35 +++++++++++++++++++++++++++++++---- 4 files changed, 80 insertions(+), 31 deletions(-) diff --git a/docs/manual.md b/docs/manual.md index 420bf653..e41dfb19 100644 --- a/docs/manual.md +++ b/docs/manual.md @@ -1914,6 +1914,8 @@ find\_incremental() |Replaced|[incremental][]c find\_incremental\_keys |Removed | N/A |Added |[highlight_all_matches][] **textadept.history** |Added |[textadept.history][] +**textadept.run** | | +N/A |Added |[set_arguments][] **textadept.snippets** | | \_insert() |Renamed |[insert()][] \_previous() |Renamed |[previous()][] @@ -1967,6 +1969,7 @@ section below. [incremental]: api.html#ui.find.incremental [highlight_all_matches]: api.html#ui.find.highlight_all_matches [textadept.history]: api.html#textadept.history +[set_arguments]: api.html#textadept.run.set_arguments [insert()]: api.html#textadept.snippets.insert [previous()]: api.html#textadept.snippets.previous [cancel_current()]: api.html#textadept.snippets.cancel_current diff --git a/modules/textadept/menu.lua b/modules/textadept/menu.lua index 1c7aa4b1..6f59b7ff 100644 --- a/modules/textadept/menu.lua +++ b/modules/textadept/menu.lua @@ -172,33 +172,7 @@ local default_menubar = { SEPARATOR, {_L['Run'], textadept.run.run}, {_L['Compile'], textadept.run.compile}, - {_L['Set Arguments...'], function() - if not buffer.filename then return end - local run_commands = textadept.run.run_commands - local compile_commands = textadept.run.compile_commands - local base_commands, utf8_args = {}, {} - for i, commands in ipairs{run_commands, compile_commands} do - -- Compare the base run/compile command with the one for the current - -- file. The difference is any additional arguments set previously. - base_commands[i] = commands[buffer.filename:match('[^.]+$')] or - commands[buffer:get_lexer()] or '' - local current_command = commands[buffer.filename] or '' - local args = current_command:sub(#base_commands[i] + 2) - utf8_args[i] = args:iconv('UTF-8', _CHARSET) - end - local button, utf8_args = ui.dialogs.inputbox{ - title = _L['Set Arguments...']:gsub('_', ''), informative_text = { - _L['Command line arguments'], _L['For Run:'], _L['For Compile:'] - }, text = utf8_args, width = not CURSES and 400 or nil - } - if button ~= 1 then return end - for i, commands in ipairs{run_commands, compile_commands} do - -- Add the additional arguments to the base run/compile command and set - -- the new command to be the one used for the current file. - commands[buffer.filename] = string.format('%s %s', base_commands[i], - utf8_args[i]:iconv(_CHARSET, 'UTF-8')) - end - end}, + {_L['Set Arguments...'], textadept.run.set_arguments}, {_L['Build'], textadept.run.build}, {_L['Stop'], textadept.run.stop}, {_L['Next Error'], function() textadept.run.goto_error(true) end}, diff --git a/modules/textadept/run.lua b/modules/textadept/run.lua index eedc2b68..e2c54db8 100644 --- a/modules/textadept/run.lua +++ b/modules/textadept/run.lua @@ -270,6 +270,51 @@ function M.run(filename) end events.connect(events.RUN_OUTPUT, print_output) +--- +-- Appends the command line argument strings *run* and *compile* to their +-- respective run and compile commands for file *filename* or the current file. +-- If either is `nil`, prompts the user for missing the arguments. Each filename +-- has its own set of compile and run arguments. +-- @param filename Optional path to the file to set run/compile arguments for. +-- @param run Optional string run arguments to set. If `nil`, the user is +-- prompted for them. Pass the empty string for no run arguments. +-- @param compile Optional string compile arguments to set. If `nil`, the user +-- is prompted for them. Pass the empty string for no compile arguments. +-- @see run_commands +-- @see compile_commands +-- @name set_arguments +function M.set_arguments(filename, run, compile) + assert_type(filename, 'string/nil', 1) + assert_type(run, 'string/nil', 2) + assert_type(compile, 'string/nil', 3) + if not filename then filename = buffer.filename end + local base_commands, utf8_args = {}, {} + for i, commands in ipairs{M.run_commands, M.compile_commands} do + -- Compare the base run/compile command with the one for the current + -- file. The difference is any additional arguments set previously. + base_commands[i] = commands[filename:match('[^.]+$')] or + commands[buffer:get_lexer()] or '' + local current_command = commands[filename] or '' + local args = (i == 1 and run or compile) or + current_command:sub(#base_commands[i] + 2) + utf8_args[i] = args:iconv('UTF-8', _CHARSET) + end + if not run or not compile then + local button, utf8_args = ui.dialogs.inputbox{ + title = _L['Set Arguments...']:gsub('_', ''), informative_text = { + _L['Command line arguments'], _L['For Run:'], _L['For Compile:'] + }, text = utf8_args, width = not CURSES and 400 or nil + } + if button ~= 1 then return end + end + for i, commands in ipairs{M.run_commands, M.compile_commands} do + -- Add the additional arguments to the base run/compile command and set + -- the new command to be the one used for the current file. + commands[filename] = string.format('%s %s', base_commands[i], + utf8_args[i]:iconv(_CHARSET, 'UTF-8')) + end +end + --- -- Map of project root paths and "makefiles" to their associated "build" shell -- command line strings or functions that return such strings. diff --git a/test/test.lua b/test/test.lua index 30f22433..1505f2c2 100644 --- a/test/test.lua +++ b/test/test.lua @@ -2892,14 +2892,10 @@ end function test_menu_functions_interactive() buffer.new() - buffer.filename = '/tmp/test.lua' - textadept.menu.menubar[_L['Tools']][_L['Set Arguments...']][2]() textadept.menu.menubar[_L['Help']][_L['About']][2]() buffer:close(true) end --- TODO: test set arguments more thoroughly. - function test_menu_select_command_interactive() local num_buffers = #_BUFFERS textadept.menu.select_command() @@ -2971,6 +2967,37 @@ function test_run_compile_run() assert_raises(function() textadept.run.run({}) end, 'string/nil expected, got table') end +function test_run_set_arguments() + local lua_run_command = textadept.run.run_commands.lua + local lua_compile_command = textadept.run.compile_commands.lua + + buffer.new() + buffer.filename = '/tmp/test.lua' + textadept.run.set_arguments(nil, '-i', '-p') + assert_equal(textadept.run.run_commands[buffer.filename], lua_run_command .. ' -i') + assert_equal(textadept.run.compile_commands[buffer.filename], lua_compile_command .. ' -p') + textadept.run.set_arguments(buffer.filename, '', '') + assert_equal(textadept.run.run_commands[buffer.filename], lua_run_command .. ' ') + assert_equal(textadept.run.compile_commands[buffer.filename], lua_compile_command .. ' ') + buffer:close(true) + + assert_raises(function() textadept.run.set_arguments(1) end, 'string/nil expected, got number') + assert_raises(function() textadept.run.set_arguments('', true) end, 'string/nil expected, got boolean') + assert_raises(function() textadept.run.set_arguments('', '', {}) end, 'string/nil expected, got table') +end + +function test_run_set_arguments_interactive() + local lua_run_command = textadept.run.run_commands.lua + local lua_compile_command = textadept.run.compile_commands.lua + buffer.new() + buffer.filename = '/tmp/test.lua' + textadept.run.set_arguments(nil, '-i', '-p') + textadept.run.set_arguments() + assert_equal(textadept.run.run_commands[buffer.filename], lua_run_command .. ' -i') + assert_equal(textadept.run.compile_commands[buffer.filename], lua_compile_command .. ' -p') + buffer:close(true) +end + function test_run_build() textadept.run.build_commands[_HOME] = function() return 'lua modules/textadept/run/build.lua', _HOME .. '/test/' -- intentional trailing '/' -- cgit v1.2.3