From 383fd5ceb905b8d0464dbb77278f3f6cff31ccc7 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Thu, 19 Sep 2024 13:45:02 -0700 Subject: [PATCH] [vim] Fix broken color scheme I learned once again that true color schemes don't work in macOS's Terminal.app. I also learned that the default color scheme in neovim is very plain and doesn't provide a lot of variation for treesitter syntax elements. So, choose a different built-in color scheme as the default. I also learned about the ColorScheme autocmd event, which is fired after a color scheme is applied. Convert the (janky) reloadColorscheme function to a set of autocommands that trigger based on the color scheme name. Along the way, add some explicit configuration for treesitter: enable highlighting, indentation, and make sure some of my common languages have plugins installed. Also link the "objcpp" filetype to both "objc" and "cpp" treesitter plugins. Lastly, make some updates to the git gutter color configuration. Create some autocommands that are applied after the plugin is loaded. For the gutter, use color 233 from the 256 color palette, which is a very dark gray. --- config/nvim/after/plugin/gitgutter.lua | 27 ++++++-- config/nvim/init.lua | 5 +- config/nvim/lua/colors.lua | 89 ++++++++++++-------------- config/nvim/lua/treesitter.lua | 16 +++++ 4 files changed, 81 insertions(+), 56 deletions(-) create mode 100644 config/nvim/lua/treesitter.lua diff --git a/config/nvim/after/plugin/gitgutter.lua b/config/nvim/after/plugin/gitgutter.lua index e777ffc..fe20947 100644 --- a/config/nvim/after/plugin/gitgutter.lua +++ b/config/nvim/after/plugin/gitgutter.lua @@ -3,8 +3,25 @@ -- Ensure there's always a gutter column so there's no stutter when changes cause it to appear. vim.wo.signcolumn = "yes" -vim.cmd [[ - highlight GitGutterAdd ctermfg=Green ctermbg=0 - highlight GitGutterChange ctermfg=DarkBlue ctermbg=0 - highlight GitGutterDelete ctermfg=Red ctermbg=0 -]] +local gitgutter_colorscheme_group = vim.api.nvim_create_augroup("GitGutterColorSchemeOverrides", { clear = true }) + +local function update_gitgutter_colors() + vim.cmd [[ + hi! SignColumn ctermbg=233 + hi! GitGutterAdd ctermbg=233 + hi! GitGutterRemove ctermbg=233 + hi! GitGutterChange ctermbg=233 + hi! GitGutterChangeDelete ctermbg=233 + ]] +end + +vim.api.nvim_create_autocmd("User", { + pattern = "GitGutter", + callback = update_gitgutter_colors, + group = gitgutter_colorscheme_group, +}) + +vim.api.nvim_create_autocmd("ColorScheme", { + callback = update_gitgutter_colors, + group = gitgutter_colorscheme_group, +}) diff --git a/config/nvim/init.lua b/config/nvim/init.lua index 42cb7f4..d57d195 100644 --- a/config/nvim/init.lua +++ b/config/nvim/init.lua @@ -61,13 +61,12 @@ vim.cmd [[ ]] require 'autocommands' +require 'colors' require 'configuration' require 'diagnostics' +require 'treesitter' require 'lsp' -local colors = require 'colors' -colors.init() - local keys = require 'keys' keys.init_key_opts() keys.init_window_key_mappings() diff --git a/config/nvim/lua/colors.lua b/config/nvim/lua/colors.lua index e763c1b..ffdde76 100644 --- a/config/nvim/lua/colors.lua +++ b/config/nvim/lua/colors.lua @@ -1,58 +1,51 @@ -- Eryn Wells -local function reloadColorscheme(colorschemeName) - if colorschemeName == nil then +-- Allow using GUI style colors (#RRGGBB hex codes) in color terminals if we +-- know it can do it. This is required for most modern color themes. Apple's +-- Terminal.app doesn't have True Color support though, so make sure it's +-- off for that. +vim.g.termguicolors = not vim.env.TERM_PROGRAM == "Apple_Terminal" + +local colorscheme_group = vim.api.nvim_create_augroup("ColorSchemeOverrides", { clear = true }) + +vim.api.nvim_create_autocmd("ColorScheme", { + callback = function() vim.cmd [[ - highlight clear + hi! ColorColumn cterm=NONE ctermbg=233 + hi! CursorColumn cterm=NONE ctermbg=233 + hi! CursorLine cterm=NONE ctermbg=233 + hi! CursorLineNr cterm=bold ctermfg=White ctermbg=233 + hi! LineNr ctermfg=DarkGray ctermbg=233 ]] - else - vim.cmd { - cmd = "colorscheme", - args = {colorschemeName}, - } - end + end, + group = colorscheme_group, +}) - -- Make some bespoke adjustments for my cursor and line length highlights - vim.cmd [[ - highlight ColorColumn cterm=NONE ctermbg=Black - highlight CursorColumn cterm=NONE ctermbg=Black - highlight CursorLine cterm=NONE ctermbg=Black - highlight CursorLineNr cterm=bold ctermfg=White ctermbg=Black - highlight LineNr ctermfg=DarkGray - ]] +vim.api.nvim_create_autocmd("ColorScheme", { + pattern = "default", + callback = function() + vim.cmd [[ hi! NormalFloat ctermfg=8 ]] + end, + group = colorscheme_group, +}) - -- Allow using GUI style colors (#RRGGBB hex codes) in color terminals if we - -- know it can do it. This is required for most modern color themes. Apple's - -- Terminal.app doesn't have True Color support though, so make sure it's - -- off for that. - vim.g.termguicolors = not vim.env.TERM_PROGRAM == "Apple_Terminal" +vim.api.nvim_create_autocmd("ColorScheme", { + pattern = "dracula", + callback = function() + vim.cmd [[ highlight CursorLineNr guibg=#44475a ]] + end, + group = colorscheme_group, +}) - local themeName = vim.g.colors_name - if themeName == nil then +vim.api.nvim_create_autocmd("ColorScheme", { + pattern = "witchhazel", + callback = function() vim.cmd [[ - highlight! NormalFloat ctermbg=8 + hi! default link LineNr CursorLineNr + hi! default link CursorLineNr CursorLine ]] - elseif themeName == "witchhazel" then - vim.cmd [[ - highlight! default link LineNr CursorLineNr - highlight! default link CursorLineNr CursorLine - ]] - elseif themeName == "dracula" then - vim.cmd [[ - highlight CursorLineNr guibg=#44475a - ]] - end -end + end, + group = colorscheme_group, +}) -local function init() - if vim.env.TERM_PROGRAM == "Apple_Terminal" then - reloadColorscheme(nil) - else - reloadColorscheme("dracula") - end -end - -return { - init = init, - reloadColorscheme = reloadColorscheme -} +vim.cmd [[ color zaibatsu ]] diff --git a/config/nvim/lua/treesitter.lua b/config/nvim/lua/treesitter.lua new file mode 100644 index 0000000..3c198f1 --- /dev/null +++ b/config/nvim/lua/treesitter.lua @@ -0,0 +1,16 @@ +-- Treesitter configuration +-- Eryn Wells + +local treesitter = require 'nvim-treesitter.configs' + +-- For some reason the Lua linter complains about missing fields here even +-- though they're not requried. So, ignore the error. +---@diagnostic disable:missing-fields +treesitter.setup { + highlight = { enable = true }, + indent = { enable = true }, + ensure_installed = { "c", "cpp", "javascript", "lua", "objc", "python", "rust", "vim" }, + auto_install = true, +} + +vim.treesitter.language.register("objcpp", { "objc", "cpp" })