Merge pull request #4 from supertosse/st-branch-1
move comment lines into separate file
This commit is contained in:
commit
2c9585d95d
|
|
@ -0,0 +1,58 @@
|
||||||
|
-- Function to detect if a line contains JSX/TSX content
|
||||||
|
local function is_jsx_line(line)
|
||||||
|
-- Check for JSX patterns: <tag, </tag, self-closing <tag />, 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', '<leader>c', comment_line, { desc = 'Toggle line comment(s)' })
|
||||||
80
init.lua
80
init.lua
|
|
@ -51,36 +51,8 @@ vim.opt.confirm = true
|
||||||
vim.keymap.set('v', '<leader>l', "yoconsole.log('<Esc>pa', <Esc>pa);<Esc>", { desc = 'log selected value' })
|
vim.keymap.set('v', '<leader>l', "yoconsole.log('<Esc>pa', <Esc>pa);<Esc>", { desc = 'log selected value' })
|
||||||
vim.keymap.set('n', '<leader>e', ':e.<CR>', { desc = 'Open file three' })
|
vim.keymap.set('n', '<leader>e', ':e.<CR>', { desc = 'Open file three' })
|
||||||
|
|
||||||
-- Function to comment out line(s)
|
-- Load comment toggle functionality
|
||||||
local function comment_line()
|
dofile(vim.fn.expand '~/repo/config/comment-toggle.lua')
|
||||||
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', '<leader>c', comment_line, { desc = 'Toggle line comment(s)' })
|
|
||||||
|
|
||||||
-- NOTE: My personal key bining END
|
-- NOTE: My personal key bining END
|
||||||
|
|
||||||
|
|
@ -90,6 +62,14 @@ vim.keymap.set('n', '<Esc>', '<cmd>nohlsearch<CR>')
|
||||||
|
|
||||||
-- Diagnostic keymaps
|
-- Diagnostic keymaps
|
||||||
vim.keymap.set('n', '<leader>q', vim.diagnostic.setloclist, { desc = 'Open diagnostic [Q]uickfix list' })
|
vim.keymap.set('n', '<leader>q', vim.diagnostic.setloclist, { desc = 'Open diagnostic [Q]uickfix list' })
|
||||||
|
vim.keymap.set('n', '<leader>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
|
-- 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 <C-\><C-n>, which
|
-- for people to discover. Otherwise, you normally need to press <C-\><C-n>, which
|
||||||
|
|
@ -315,12 +295,27 @@ require('lazy').setup({
|
||||||
-- You can put your default mappings / updates / etc. in here
|
-- You can put your default mappings / updates / etc. in here
|
||||||
-- All the info you're looking for is in `:help telescope.setup()`
|
-- All the info you're looking for is in `:help telescope.setup()`
|
||||||
--
|
--
|
||||||
-- defaults = {
|
defaults = {
|
||||||
-- mappings = {
|
-- mappings = {
|
||||||
-- i = { ['<c-enter>'] = 'to_fuzzy_refine' },
|
-- i = { ['<c-enter>'] = 'to_fuzzy_refine' },
|
||||||
-- },
|
-- },
|
||||||
-- },
|
file_ignore_patterns = { '.git/' },
|
||||||
-- pickers = {}
|
},
|
||||||
|
pickers = {
|
||||||
|
find_files = {
|
||||||
|
hidden = true,
|
||||||
|
},
|
||||||
|
live_grep = {
|
||||||
|
additional_args = function()
|
||||||
|
return { '--hidden' }
|
||||||
|
end,
|
||||||
|
},
|
||||||
|
grep_string = {
|
||||||
|
additional_args = function()
|
||||||
|
return { '--hidden' }
|
||||||
|
end,
|
||||||
|
},
|
||||||
|
},
|
||||||
extensions = {
|
extensions = {
|
||||||
['ui-select'] = {
|
['ui-select'] = {
|
||||||
require('telescope.themes').get_dropdown(),
|
require('telescope.themes').get_dropdown(),
|
||||||
|
|
@ -501,13 +496,12 @@ require('lazy').setup({
|
||||||
source = 'if_many',
|
source = 'if_many',
|
||||||
spacing = 2,
|
spacing = 2,
|
||||||
format = function(diagnostic)
|
format = function(diagnostic)
|
||||||
local diagnostic_message = {
|
local max_width = 80
|
||||||
[vim.diagnostic.severity.ERROR] = diagnostic.message,
|
local message = diagnostic.message
|
||||||
[vim.diagnostic.severity.WARN] = diagnostic.message,
|
if #message > max_width then
|
||||||
[vim.diagnostic.severity.INFO] = diagnostic.message,
|
message = message:sub(1, max_width) .. '...'
|
||||||
[vim.diagnostic.severity.HINT] = diagnostic.message,
|
end
|
||||||
}
|
return message
|
||||||
return diagnostic_message[diagnostic.severity]
|
|
||||||
end,
|
end,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue