From 7214203534f96a285d437025bf46243726bcb875 Mon Sep 17 00:00:00 2001 From: dvdgamer Date: Sat, 10 May 2025 11:14:11 +0200 Subject: [PATCH] feat: add custom commands for commenting and uncommenting lines in C files --- after/ftplugin/c.lua | 1 + ftplugin/c.lua | 39 ++++++++++++++++++++++++++++++++++++++- init.lua | 38 ++------------------------------------ 3 files changed, 41 insertions(+), 37 deletions(-) diff --git a/after/ftplugin/c.lua b/after/ftplugin/c.lua index 9fb44987..49d6e7d1 100644 --- a/after/ftplugin/c.lua +++ b/after/ftplugin/c.lua @@ -2,4 +2,5 @@ local set = vim.opt_local set.shiftwidth = 4 set.tabstop = 4 +vim.g.sleuth_automatic = 0 set.commentstring = '/* %s */' diff --git a/ftplugin/c.lua b/ftplugin/c.lua index 9fdf210e..417483a0 100644 --- a/ftplugin/c.lua +++ b/ftplugin/c.lua @@ -1,4 +1,9 @@ --- local set = vim.opt_local +-- Import vim module +local set = vim.opt_local + +set.shiftwidth = 4 +set.tabstop = 4 +set.commentstring = '/* %s */' -- Define a custom command ':IntMain' that inserts int main() {} template vim.api.nvim_create_user_command('IntMain', function() @@ -24,3 +29,35 @@ vim.api.nvim_create_user_command('Libft', function() '#include "libft.h"', }) end, {}) + +vim.api.nvim_create_user_command('CommentRestOfFile', function() + local current_line = vim.api.nvim_win_get_cursor(0)[1] + local last_line = vim.api.nvim_buf_line_count(0) + + -- Get all lines from current to end of file + local lines = vim.api.nvim_buf_get_lines(0, current_line - 1, last_line, false) + + -- Prefix each line with // + for i = 1, #lines do + lines[i] = '//' .. lines[i] + end + + -- Set the modified lines back in the buffer + vim.api.nvim_buf_set_lines(0, current_line - 1, last_line, false, lines) +end, {}) + +vim.api.nvim_create_user_command('UncommentRestOfFile', function () + local current_line = vim.api.nvim_win_get_cursor(0)[1] + local last_line = vim.api.nvim_buf_line_count(0) + + -- Get all lines from current to end of file + local lines = vim.api.nvim_buf_get_lines(0, current_line - 1, last_line, false) + + -- Remove leading // if present + for i = 1, #lines do + lines[i] = lines[i]:gsub("^%s*//", "") + end + + -- Set the modified lines back in the buffer + vim.api.nvim_buf_set_lines(0, current_line - 1, last_line, false, lines) +end, {}) diff --git a/init.lua b/init.lua index 5b89c746..984c90b7 100644 --- a/init.lua +++ b/init.lua @@ -1,38 +1,4 @@ ---[[ - -What is Kickstart? - - Kickstart.nvim is *not* a distribution. - - Kickstart.nvim is a starting point for your own configuration. - The goal is that you can read every line of code, top-to-bottom, understand - what your configuration is doing, and modify it to suit your needs. - - Once you've done that, you can start exploring, configuring and tinkering to - make Neovim your own! That might mean leaving Kickstart just the way it is for a while - or immediately breaking it into modular pieces. It's up to you! - - If you don't know anything about Lua, I recommend taking some time to read through - a guide. One possible example which will only take 10-15 minutes: - - https://learnxinyminutes.com/docs/lua/ - - After understanding a bit more about Lua, you can use `:help lua-guide` as a - reference for how Neovim integrates Lua. - - :help lua-guide - - (or HTML version): https://neovim.io/doc/user/lua-guide.html - -Kickstart Guide: - -If you experience any errors while trying to install kickstart, run `:checkhealth` for more info. - -I hope you enjoy your Neovim journey, -- TJ - -P.S. You can delete this when you're done too. It's your config now! :) ---]] - -- Set as the leader key --- See `:help mapleader` -- NOTE: Must happen before plugins are loaded (otherwise wrong leader will be used) vim.g.mapleader = ' ' vim.g.maplocalleader = ' ' @@ -793,7 +759,7 @@ require('lazy').setup({ filter = 'pro', -- classic | octagon | pro | machine | ristretto | spectrum -- Enable this will disable filter option day_night = { - enable = false, -- turn off by default + enable = true, -- turn off by default day_filter = 'pro', -- classic | octagon | pro | machine | ristretto | spectrum night_filter = 'spectrum', -- classic | octagon | pro | machine | ristretto | spectrum }, @@ -806,7 +772,7 @@ require('lazy').setup({ 'renamer', 'notify', 'nvim-tree', - 'neo-tree', + -- 'neo-tree', 'bufferline', -- better used if background of `neo-tree` or `nvim-tree` is cleared }, -- "float_win", "toggleterm", "telescope", "which-key", "renamer", "neo-tree", "nvim-tree", "bufferline" plugins = {