From f6849380673f01b36c99cc275684649829a5f8bd Mon Sep 17 00:00:00 2001 From: Ivar Fatland Date: Sun, 13 Aug 2023 13:52:05 +0200 Subject: [PATCH] added run command --- init.lua | 102 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 65 insertions(+), 37 deletions(-) diff --git a/init.lua b/init.lua index e2d82f4..a3693f2 100644 --- a/init.lua +++ b/init.lua @@ -264,47 +264,75 @@ local function packer_startup(use) end -do - local function file_exists(name) - local f = io.open(name,"r") +---@param name string +---@return boolean +local function file_exists(name) + local f = io.open(name,"r") - if f~=nil then - f:close() - return true - else - return false - end + if f~=nil then + f:close() + return true + else + return false end - - -- Opens a default .snippets file for the filetype you are currently editing in a horizontal split pane. - -- If the .snippets file does not exist, it will be created. - -- This requires the snippets folder to exist in the config folder. - -- If the folder does not exist, the command will print out a helpful error message showing what the path - -- should look like. - vim.api.nvim_create_user_command( - 'S', - function () - ---@type string - local snippets_path = vim.fn.stdpath('config') .. '/snippets/' .. - vim.api.nvim_buf_get_option(0, "filetype") .. '.snippets' - - if not file_exists(snippets_path) then - local file = io.open( snippets_path, 'w' ) - assert( - file ~= nil, - ("io.open('%s', 'w') returned nil.\n"):format(snippets_path) .. - "Make sure the snippets folder in the above path exists." - ) - file:close() - print('created file: ', snippets_path) - end - - vim.api.nvim_command(('SnippyEdit %s'):format(snippets_path)) - end, - { nargs = 0 } - ) end +-- Opens a default .snippets file for the filetype you are currently editing in a horizontal split pane. +-- If the .snippets file does not exist, it will be created. +-- This requires the snippets folder to exist in the config folder. +-- If the folder does not exist, the command will print out a helpful error message showing what the path +-- should look like. +vim.api.nvim_create_user_command( + 'S', + function () + ---@type string + local snippets_path = vim.fn.stdpath('config') .. '/snippets/' .. + vim.api.nvim_buf_get_option(0, "filetype") .. '.snippets' + + if not file_exists(snippets_path) then + local file = io.open( snippets_path, 'w' ) + assert( + file ~= nil, + ("io.open('%s', 'w') returned nil.\n"):format(snippets_path) .. + "Make sure the snippets folder in the above path exists." + ) + file:close() + print('created file: ', snippets_path) + end + + vim.api.nvim_command(('SnippyEdit %s'):format(snippets_path)) + end, + { nargs = 0 } +) + +---@type function +local run_file +do + local run_script_name = "run.sh" + + run_file = function() + ---@type "Linux" | "Darwin" | "Windows_NT" + local os_name = vim.loop.os_uname().sysname + if os_name ~= "Linux" then + error('run_file not implemented for non-linux platforms') + end + + local run_script_path = vim.fn.getcwd() .. "/" .. run_script_name + if not file_exists(run_script_path) then + error( "The run script: '" .. run_script_path .. "' does not exist") + end + + vim.cmd('!sh ' .. run_script_path) + end +end + +vim.keymap.set( + 'n', + keymap.test_execute_file, + run_file, + { silent = true } +) + ---Splits the line under the cursor into multiple lines. ---Works on lines with properly opening and closing brackets: (),[],{} ---Starts at the cursor position and moves to the right until it finds an opening bracket.