diff --git a/comment-toggle.lua b/comment-toggle.lua new file mode 100644 index 00000000..8c00c90d --- /dev/null +++ b/comment-toggle.lua @@ -0,0 +1,58 @@ +-- Function to detect if a line contains JSX/TSX content +local function is_jsx_line(line) + -- Check for JSX patterns: , or JSX expressions + return line:match('<[/%a]') ~= nil + or line:match('/>%s*$') ~= nil + or line:match('^%s*{') ~= nil -- Lines starting with { (JSX expressions) +end + +-- Function to comment out line(s) +local function comment_line() + local count = vim.v.count + 1 + local start_line = vim.fn.line '.' -- Get current line number + local filetype = vim.bo.filetype + + -- Check only the first line to determine comment style for all lines + local first_line = vim.fn.getline(start_line) + + -- Detect if it's a JSX comment or regular comment on first line + local is_jsx_commented = first_line:match '^%s*{/%*' ~= nil + local is_regular_commented = first_line:match '^%s*//' ~= nil + + -- Determine comment style to use based on first line + local use_jsx_comment = false + if not is_jsx_commented and not is_regular_commented then + -- When commenting: check if first line is JSX content + use_jsx_comment = (filetype == 'typescriptreact' or filetype == 'javascriptreact') and is_jsx_line(first_line) + end + + for i = 0, count - 1 do + local line_num = start_line + i + local line = vim.fn.getline(line_num) + local new_line + + if is_jsx_commented then + -- Uncomment JSX: remove {/* and */} + new_line = line:gsub('^(%s*){/%*%s*(.-)%s*%*/}', '%1%2') + elseif is_regular_commented then + -- Uncomment: remove leading whitespace, //, and optional space after // + new_line = line:gsub('^%s*//%s?', '') + else + -- Comment: use style determined from first line + if use_jsx_comment then + -- JSX comment: {/* ... */} + new_line = line:gsub('^(%s*)(.*)', '%1{/* %2 */}') + else + -- Regular comment: // + new_line = '// ' .. line + end + end + + vim.fn.setline(line_num, new_line) + end + + -- Move cursor down by count lines + vim.cmd('normal! ' .. count .. 'j') +end + +vim.keymap.set('n', 'c', comment_line, { desc = 'Toggle line comment(s)' }) diff --git a/init.lua b/init.lua index 23f26ead..f04892f5 100644 --- a/init.lua +++ b/init.lua @@ -51,36 +51,8 @@ vim.opt.confirm = true vim.keymap.set('v', 'l', "yoconsole.log('pa', pa);", { desc = 'log selected value' }) vim.keymap.set('n', 'e', ':e.', { desc = 'Open file three' }) --- Function to comment out line(s) -local function comment_line() - local count = vim.v.count + 1 - local start_line = vim.fn.line '.' -- Get current line number - - -- Check only the first line to determine action - local first_line = vim.fn.getline(start_line) - local is_commented = first_line:match '^%s*//' - - for i = 0, count - 1 do - local line_num = start_line + i - local line = vim.fn.getline(line_num) - local new_line - - if is_commented then - -- Uncomment: remove leading whitespace, //, and optional space after // - new_line = line:gsub('^%s*//%s?', '') - else - -- Comment: add // at the very beginning of the line - new_line = '// ' .. line - end - - vim.fn.setline(line_num, new_line) - end - - -- Move cursor down by count lines - vim.cmd('normal! ' .. count .. 'j') -end - -vim.keymap.set('n', 'c', comment_line, { desc = 'Toggle line comment(s)' }) +-- Load comment toggle functionality +dofile(vim.fn.expand '~/repo/config/comment-toggle.lua') -- NOTE: My personal key bining END @@ -90,6 +62,14 @@ vim.keymap.set('n', '', 'nohlsearch') -- Diagnostic keymaps vim.keymap.set('n', 'q', vim.diagnostic.setloclist, { desc = 'Open diagnostic [Q]uickfix list' }) +vim.keymap.set('n', 'd', vim.diagnostic.open_float, { desc = 'Show [D]iagnostic in floating window' }) + +-- Auto-show diagnostic in floating window on cursor hold +vim.api.nvim_create_autocmd('CursorHold', { + callback = function() + vim.diagnostic.open_float(nil, { focus = false, scope = 'cursor' }) + end, +}) -- Exit terminal mode in the builtin terminal with a shortcut that is a bit easier -- for people to discover. Otherwise, you normally need to press , which @@ -315,12 +295,27 @@ require('lazy').setup({ -- You can put your default mappings / updates / etc. in here -- All the info you're looking for is in `:help telescope.setup()` -- - -- defaults = { - -- mappings = { - -- i = { [''] = 'to_fuzzy_refine' }, - -- }, - -- }, - -- pickers = {} + defaults = { + -- mappings = { + -- i = { [''] = 'to_fuzzy_refine' }, + -- }, + file_ignore_patterns = { '.git/' }, + }, + pickers = { + find_files = { + hidden = true, + }, + live_grep = { + additional_args = function() + return { '--hidden' } + end, + }, + grep_string = { + additional_args = function() + return { '--hidden' } + end, + }, + }, extensions = { ['ui-select'] = { require('telescope.themes').get_dropdown(), @@ -501,13 +496,12 @@ require('lazy').setup({ source = 'if_many', spacing = 2, format = function(diagnostic) - local diagnostic_message = { - [vim.diagnostic.severity.ERROR] = diagnostic.message, - [vim.diagnostic.severity.WARN] = diagnostic.message, - [vim.diagnostic.severity.INFO] = diagnostic.message, - [vim.diagnostic.severity.HINT] = diagnostic.message, - } - return diagnostic_message[diagnostic.severity] + local max_width = 80 + local message = diagnostic.message + if #message > max_width then + message = message:sub(1, max_width) .. '...' + end + return message end, }, } diff --git a/tmux-s b/tmux-s index 4a0bfa57..6e166236 100755 --- a/tmux-s +++ b/tmux-s @@ -1,6 +1,6 @@ #!/bin/bash -TS_SEARCH_PATHS=("$HOME/repo:1" "$HOME/repo/astro:2") +TS_SEARCH_PATHS=("$HOME/repo:1" "$HOME/repo/astro:2" "$HOME/repo/tripletex-frontend/scripts/migrate-repo/:2") TS_MAX_DEPTH=2 find_dirs() {