Skip to content

Commit

Permalink
refactor(nvim): highlighting stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
schardev committed Mar 23, 2024
1 parent 70b0172 commit f2aa470
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 41 deletions.
65 changes: 40 additions & 25 deletions config/nvim/lua/core/highlights.lua
Original file line number Diff line number Diff line change
@@ -1,37 +1,52 @@
local autocmd = vim.api.nvim_create_autocmd
local fn = vim.fn

-- Whitespace highlighting
-- Taken from https://github.com/akinsho/dotfiles

-- Do not highlight whitespaces in below filetypes
local ignore_filetypes = {
"help",
"c",
"gitcommit",
"kconfig",
"make",
"TelescopePrompt",
}

autocmd({ "ColorScheme", "FileType" }, {
pattern = "*",
callback = function()
if
vim.tbl_contains(ignore_filetypes, vim.bo.filetype)
or not vim.bo.modifiable
or vim.bo.buftype == "nofile"
then
vim.fn.clearmatches()
return
end
vim.g.tab_highlight = true
vim.cmd([[ highlight ExtraWhitespace ctermbg=red guibg=red ]])
vim.cmd([[ highlight Tabs ctermbg=yellow guibg=yellow ]])
vim.cmd([[ match ExtraWhitespace /\s\+$/ ]])
vim.fn.matchadd("Tabs", "\t")
end,
})
local function is_floating_win()
return fn.win_gettype() == "popup"
end

local function is_invalid_buf()
return vim.bo.filetype == "" or vim.bo.buftype ~= "" or not vim.bo.modifiable
end

local function is_ignored()
return vim.tbl_contains(ignore_filetypes, vim.bo.filetype)
end

local function highlight_trailing()
if is_invalid_buf() or is_floating_win() or is_ignored() then
return
end

local space_pattern = [[\s\+$]]
if vim.w.space_match_number then
fn.matchdelete(vim.w.space_match_number)
fn.matchadd("ExtraWhitespace", space_pattern, 10, vim.w.space_match_number)
else
vim.w.space_match_number = fn.matchadd("ExtraWhitespace", space_pattern)
end

local tabs_pattern = [[\t]]
if vim.w.tabs_match_number then
fn.matchdelete(vim.w.tabs_match_number)
fn.matchadd("Tabs", tabs_pattern, 11, vim.w.tabs_match_number)
else
vim.w.tabs_match_number = fn.matchadd("Tabs", tabs_pattern)
end
end

autocmd("BufWinLeave", {
autocmd({ "BufEnter", "FileType", "InsertLeave" }, {
pattern = "*",
callback = function()
vim.fn.clearmatches()
end,
callback = highlight_trailing,
desc = "Highlight trailing whitespace",
})
34 changes: 18 additions & 16 deletions config/nvim/lua/core/mappings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,18 @@ nnoremap("<Leader>sh", ":set hlsearch!<CR>", {
desc = "Toggle search highlighting",
})
nnoremap("<Leader>th", function()
if vim.g.tab_highlight == true then
if vim.w.whitespace_highlight == true then
vim.cmd("highlight clear Tabs")
vim.cmd("highlight clear ExtraWhitespace")
vim.notify("Disabled whitespace highlighting!")
else
vim.cmd("highlight Tabs guibg=yellow")
vim.cmd([[ highlight Tabs ctermbg=yellow guibg=yellow ]])
vim.cmd([[ highlight ExtraWhitespace ctermbg=red guibg=red ]])
vim.notify("Enabled whitespace highlighting!")
end
vim.g.tab_highlight = not vim.g.tab_highlight
vim.w.whitespace_highlight = not vim.w.whitespace_highlight
end, {
desc = "Toggle tab highlighting",
desc = "Toggle whitespace highlighting",
})

--- Few mappings I stole from @akinsho :)
Expand Down Expand Up @@ -163,22 +165,22 @@ vim.g.mc = [[y/\V<C-r>=escape(@", '/')<CR><CR>]]
nnoremap("cn", "*``cgn")
nnoremap("cN", "*``cgN")

xnoremap("cn", [[g:mc . "``cgn"]], { expr = true })
xnoremap("cN", [[g:mc . "``cgN"]], { expr = true })
-- xnoremap("cn", [[g:mc . "``cgn"]], { expr = true })
-- xnoremap("cN", [[g:mc . "``cgN"]], { expr = true })

nnoremap("cq", [[:\<C-u>call v:lua.Setup_CR()<CR>*``qz]])
nnoremap("cQ", [[:\<C-u>call v:lua.Setup_CR()<CR>#``qz]])

xnoremap(
"cq",
[[":\<C-u>call v:lua.Setup_CR()<CR>gv" . g:mc . "``qz"]],
{ expr = true }
)
xnoremap(
"cQ",
[[":\<C-u>call v:lua.Setup_CR()<CR>gv" . substitute(g:mc, '/', '?', 'g') . "``qz"]],
{ expr = true }
)
-- xnoremap(
-- "cq",
-- [[":\<C-u>call v:lua.Setup_CR()<CR>gv" . g:mc . "``qz"]],
-- { expr = true }
-- )
-- xnoremap(
-- "cQ",
-- [[":\<C-u>call v:lua.Setup_CR()<CR>gv" . substitute(g:mc, '/', '?', 'g') . "``qz"]],
-- { expr = true }
-- )

-- Replicate netrw functionality (gx/gf)
nnoremap("gx", utils.open_link)
Expand Down

0 comments on commit f2aa470

Please sign in to comment.