From 814895b1cab856a923a374ee1714291142fb0356 Mon Sep 17 00:00:00 2001 From: Dane Sabo Date: Mon, 24 Mar 2025 15:43:32 -0400 Subject: [PATCH] Markdown easy line --- lua/custom/chadrc.lua | 2 +- .../language_specific_commands/markdown.lua | 6 ++ .../language_specific_commands/rust.lua | 101 +++++++++++------- lua/custom/mappings.lua | 1 + 4 files changed, 69 insertions(+), 41 deletions(-) create mode 100644 lua/custom/language_specific_commands/markdown.lua diff --git a/lua/custom/chadrc.lua b/lua/custom/chadrc.lua index 8903a5f..b9922f5 100644 --- a/lua/custom/chadrc.lua +++ b/lua/custom/chadrc.lua @@ -1,7 +1,7 @@ ---@type ChadrcConfig local M = {} -M.ui = { theme = 'oceanic-light' } +M.ui = { theme = 'one_light' } M.plugins = 'custom.plugins' M.mappings = require "custom.mappings" return M diff --git a/lua/custom/language_specific_commands/markdown.lua b/lua/custom/language_specific_commands/markdown.lua new file mode 100644 index 0000000..01bff18 --- /dev/null +++ b/lua/custom/language_specific_commands/markdown.lua @@ -0,0 +1,6 @@ +vim.keymap.set( + "n", + "li", + "The quick brown fox jumps over the lazy dog. The dog stays blissfully asleep. :)", + { desc = "Print a standard 80 character string for Markdown formatting." } +) diff --git a/lua/custom/language_specific_commands/rust.lua b/lua/custom/language_specific_commands/rust.lua index 252b29a..516d58e 100644 --- a/lua/custom/language_specific_commands/rust.lua +++ b/lua/custom/language_specific_commands/rust.lua @@ -1,45 +1,66 @@ --- We modify our previously defined Cargo user command: -vim.api.nvim_create_user_command("Cargo", function(opts) - -- Split the entire command-line input (opts.args) by spaces. - -- For example, if someone types :Cargo new test_project --bin - -- the split might be {"new", "test_project", "--bin"}. - local args = vim.split(opts.args, "%s+") +local function open_float_terminal_with_command(cmd) + -- 1. Create a scratch buffer (not listed, no file) + local buf = vim.api.nvim_create_buf(false, true) + vim.api.nvim_buf_set_option(buf, "bufhidden", "wipe") - -- The first item is the subcommand: "run", "build", or "new". - local subcommand = args[1] + -- 2. Get the dimensions of the current window (the active split) + local cur_width = vim.api.nvim_win_get_width(0) + local cur_height = vim.api.nvim_win_get_height(0) - if subcommand == "run" then - vim.cmd "!cargo run" - elseif subcommand == "build" then - vim.cmd "!cargo build" - elseif subcommand == "new" then - -- Everything after 'new' is the project name plus optional flags. - -- Let's remove 'new' from the table, then join the rest with spaces. - table.remove(args, 1) -- remove 'new' - local new_args = table.concat(args, " ") - if #new_args == 0 then - print "Usage: :Cargo new [options]" - return + -- 3. Calculate floating window dimensions relative to the current window. + -- Here we're using 80% of the current window's width/height. + local width = math.floor(cur_width * 0.8) + local height = math.floor(cur_height * 0.8) + local row = math.floor((cur_height - height) / 2) + local col = math.floor((cur_width - width) / 2) + + -- 4. Open the floating window relative to the current window + local win = vim.api.nvim_open_win(buf, true, { + relative = "win", -- Relative to the current window instead of the editor + width = width, + height = height, + row = row, + col = col, + style = "minimal", + border = "rounded", + }) + + -- 5. Launch your default shell in the new buffer. + vim.fn.termopen(vim.o.shell) + + -- 6. Use chansend to send the desired command followed by Enter. + vim.fn.chansend(vim.b.terminal_job_id, cmd .. "\n") +end + +-- Create the :Cargo command that accepts subcommands. +vim.api.nvim_create_user_command( + "Cargo", + function(opts) + local args = vim.split(opts.args, "%s+") + local subcommand = args[1] + + if subcommand == "run" then + open_float_terminal_with_command "cargo run" + elseif subcommand == "build" then + open_float_terminal_with_command "cargo build" + elseif subcommand == "new" then + table.remove(args, 1) -- Remove the "new" literal. + local new_args = table.concat(args, " ") + if new_args == "" then + print "Usage: :Cargo new [options]" + return + end + open_float_terminal_with_command("cargo new " .. new_args) + else + print "Valid subcommands: run, build, new" end + end, + { nargs = "*" } -- Allow multiple arguments (e.g. for 'new'). +) - -- Run "cargo new whatever-is-left" - vim.cmd("!cargo new " .. new_args) - else - print( - "Please provide 'run', 'build', or 'new' as an argument to :Cargo. " - .. "For 'new', also specify [options]." - ) - end -end, { - -- We make nargs = '*' so you can type multiple arguments - -- e.g. ':Cargo new test_project --bin' - nargs = "*", -}) +-- Key mappings to trigger our Cargo commands in a floating terminal. +vim.keymap.set("n", "cr", ":Cargo run", { desc = "Cargo: run in floating terminal" }) +vim.keymap.set("n", "cb", ":Cargo build", { desc = "Cargo: build in floating terminal" }) +vim.keymap.set("n", "cn", ":Cargo new ", { desc = "Cargo: new in floating terminal" }) --- Keymaps remain the same, or you could add more if you want: -vim.keymap.set("n", "cr", ":Cargo run", { desc = "Cargo: run" }) -vim.keymap.set("n", "cb", ":Cargo build", { desc = "Cargo: build" }) --- Optionally, bind something for "new": -vim.keymap.set("n", "cn", ":Cargo new ", { desc = "Cargo: new" }) --- Notice we didn't put at the end of ':Cargo new ' because you typically --- want to type your project name before pressing Enter. +print "Rust Keybinds Loaded" diff --git a/lua/custom/mappings.lua b/lua/custom/mappings.lua index e799c04..3dfd0d6 100644 --- a/lua/custom/mappings.lua +++ b/lua/custom/mappings.lua @@ -47,5 +47,6 @@ vim.api.nvim_set_keymap("n", "e", "lua vim.diagnostic.open_float()< require "custom.language_specific_commands.matlab" require "custom.language_specific_commands.rust" +require "custom.language_specific_commands.markdown" return M