[feat] nvim: highlight lua codesnippets
All checks were successful
/ check (push) Successful in 29s
/ build (docs) (push) Successful in 2s
/ build (nixosConfigurations.vm.config.system.build.toplevel) (push) Successful in 24s
/ build (nvim) (push) Successful in 16s

Adding a comment with the language will automatically highlight the
code:

```nix
luaCode = /*lua*/ ''
    function() return "look ma I'm highlighted!" end
'';
```

(May only work using treesitter)
This commit is contained in:
Jalil David Salamé Messina 2024-07-02 09:29:44 +02:00
parent 351e042a7c
commit dc130e4723
Signed by: jalil
GPG key ID: F016B9E770737A0B
4 changed files with 227 additions and 103 deletions

View file

@ -12,28 +12,38 @@ in {
group = "highlightOnYank"; group = "highlightOnYank";
event = "TextYankPost"; event = "TextYankPost";
pattern = "*"; pattern = "*";
callback = mkRaw '' callback =
function() mkRaw
vim.highlight.on_yank { /*
higroup = ( lua
vim.fn['hlexists'] 'HighlightedyankRegion' > 0 and 'HighlightedyankRegion' or 'IncSearch' */
), ''
timeout = 200, function()
} vim.highlight.on_yank {
end higroup = (
''; vim.fn['hlexists'] 'HighlightedyankRegion' > 0 and 'HighlightedyankRegion' or 'IncSearch'
),
timeout = 200,
}
end
'';
} }
{ {
group = "restoreCursorPosition"; group = "restoreCursorPosition";
event = "BufReadPost"; event = "BufReadPost";
pattern = "*"; pattern = "*";
callback = mkRaw '' callback =
function() mkRaw
if vim.fn.line '\'"' > 0 and vim.fn.line '\'"' <= vim.fn.line '$' then /*
vim.cmd [[execute "normal! g'\""]] lua
*/
''
function()
if vim.fn.line '\'"' > 0 and vim.fn.line '\'"' <= vim.fn.line '$' then
vim.cmd [[execute "normal! g'\""]]
end
end end
end '';
'';
} }
{ {
group = "lspConfig"; group = "lspConfig";
@ -42,7 +52,11 @@ in {
callback = let callback = let
opts = "noremap = true, buffer = bufnr"; opts = "noremap = true, buffer = bufnr";
in in
mkRaw '' mkRaw
/*
lua
*/
''
function(opts) function(opts)
local bufnr = opts.buf local bufnr = opts.buf
local client = vim.lsp.get_client_by_id(opts.data.client_id) local client = vim.lsp.get_client_by_id(opts.data.client_id)

View file

@ -51,99 +51,180 @@ in {
{ {
mode = "n"; mode = "n";
key = "<leader>dj"; key = "<leader>dj";
action = mkRaw "vim.diagnostic.goto_next"; action =
mkRaw
# lua
''
vim.diagnostic.goto_next
'';
options.desc = "Diagnostics next [J]"; options.desc = "Diagnostics next [J]";
} }
{ {
mode = "n"; mode = "n";
key = "<leader>dk"; key = "<leader>dk";
action = mkRaw "vim.diagnostic.goto_prev"; action =
mkRaw
# lua
''
vim.diagnostic.goto_prev
'';
options.desc = "Diagnostics previous [K]"; options.desc = "Diagnostics previous [K]";
} }
{ {
mode = "n"; mode = "n";
key = "<leader>xx"; key = "<leader>xx";
action = mkRaw "require('trouble').toggle"; action =
mkRaw
# lua
''
require('trouble').toggle
'';
options.desc = "Toggle trouble"; options.desc = "Toggle trouble";
} }
{ {
mode = "n"; mode = "n";
key = "<leader>xw"; key = "<leader>xw";
action = mkRaw "function() require('trouble').toggle('workspace_diagnostics') end"; action =
mkRaw
# lua
''
function() require('trouble').toggle('workspace_diagnostics') end
'';
options.desc = "Toggle Workspace trouble"; options.desc = "Toggle Workspace trouble";
} }
{ {
mode = "n"; mode = "n";
key = "<leader>xd"; key = "<leader>xd";
action = mkRaw "function() require('trouble').toggle('document_diagnostics') end"; action =
mkRaw
# lua
''
function() require('trouble').toggle('document_diagnostics') end
'';
options.desc = "Toggle Document trouble"; options.desc = "Toggle Document trouble";
} }
{ {
mode = "n"; mode = "n";
key = "<leader>xq"; key = "<leader>xq";
action = mkRaw "function() require('trouble').toggle('quickfix') end"; action =
mkRaw
# lua
''
function() require('trouble').toggle('quickfix') end
'';
options.desc = "Toggle Quickfix trouble"; options.desc = "Toggle Quickfix trouble";
} }
{ {
mode = "n"; mode = "n";
key = "<leader>xl"; key = "<leader>xl";
action = mkRaw "function() require('trouble').toggle('loclist') end"; action =
mkRaw
# lua
''
function() require('trouble').toggle('loclist') end
'';
options.desc = "Toggle Loclist trouble"; options.desc = "Toggle Loclist trouble";
} }
{ {
mode = "n"; mode = "n";
key = "gR"; key = "gR";
action = mkRaw "function() require('trouble').toggle('lsp_references') end"; action =
mkRaw
# lua
''
function() require('trouble').toggle('lsp_references') end
'';
options.desc = "Toggle lsp References trouble"; options.desc = "Toggle lsp References trouble";
} }
# Telescope # Telescope
{ {
mode = "n"; mode = "n";
key = "<leader>ff"; key = "<leader>ff";
action = mkRaw "require('telescope.builtin').find_files"; action =
mkRaw
# lua
''
require('telescope.builtin').find_files
'';
options.desc = "Find Files"; options.desc = "Find Files";
} }
{ {
mode = "n"; mode = "n";
key = "<leader>fg"; key = "<leader>fg";
action = mkRaw "require('telescope.builtin').live_grep"; action =
mkRaw
# lua
''
require('telescope.builtin').live_grep
'';
options.desc = "Find Grep"; options.desc = "Find Grep";
} }
{ {
mode = "n"; mode = "n";
key = "<leader>fh"; key = "<leader>fh";
action = mkRaw "require('telescope.builtin').help_tags"; action =
mkRaw
# lua
''
require('telescope.builtin').help_tags
'';
options.desc = "Find Help"; options.desc = "Find Help";
} }
{ {
mode = "n"; mode = "n";
key = "<leader>fb"; key = "<leader>fb";
action = mkRaw "require('telescope.builtin').buffers"; action =
mkRaw
# lua
''
require('telescope.builtin').buffers
'';
options.desc = "Find Buffer"; options.desc = "Find Buffer";
} }
{ {
mode = "n"; mode = "n";
key = "<leader>fd"; key = "<leader>fd";
action = mkRaw "require('telescope.builtin').diagnostics"; action =
mkRaw
# lua
''
require('telescope.builtin').diagnostics
'';
options.desc = "Find Diagnostics"; options.desc = "Find Diagnostics";
} }
{ {
mode = "n"; mode = "n";
key = "<leader>fq"; key = "<leader>fq";
action = mkRaw "require('telescope.builtin').quickfix"; action =
mkRaw
# lua
''
require('telescope.builtin').quickfix
'';
options.desc = "Find Quickfix"; options.desc = "Find Quickfix";
} }
{ {
mode = "n"; mode = "n";
key = "<leader>w"; key = "<leader>w";
action = mkRaw "require('conform').format"; action =
mkRaw
# lua
''
require('conform').format
'';
options.desc = "Format buffer"; options.desc = "Format buffer";
} }
# Nvim Silicon # Nvim Silicon
{ {
mode = "v"; mode = "v";
key = "<leader>sc"; key = "<leader>sc";
action = mkRaw "require('nvim-silicon').clip"; action =
mkRaw
# lua
''
require('nvim-silicon').clip
'';
options.desc = "Snap Code (to clipboard)"; options.desc = "Snap Code (to clipboard)";
} }
]; ];

View file

@ -15,14 +15,28 @@ in {
enable = true; enable = true;
cmdline = { cmdline = {
"/" = { "/" = {
mapping = mkRaw "cmp.mapping.preset.cmdline()"; mapping =
mkRaw
/*
lua
*/
''
cmp.mapping.preset.cmdline()
'';
sources = [ sources = [
{name = "rg";} {name = "rg";}
{name = "buffer";} {name = "buffer";}
]; ];
}; };
":" = { ":" = {
mapping = mkRaw "cmp.mapping.preset.cmdline()"; mapping =
mkRaw
/*
lua
*/
''
cmp.mapping.preset.cmdline()
'';
sources = [ sources = [
{name = "path";} {name = "path";}
{name = "cmdline";} {name = "cmdline";}
@ -31,7 +45,13 @@ in {
}; };
settings = { settings = {
# Snippets # Snippets
snippet.expand = "function(args) require('luasnip').lsp_expand(args.body) end"; snippet.expand =
/*
lua
*/
''
function(args) require('luasnip').lsp_expand(args.body) end
'';
# Completion Sources # Completion Sources
sources = [ sources = [
{ {
@ -83,42 +103,47 @@ in {
groupIndex = 1; groupIndex = 1;
} }
]; ];
mapping = mkRaw '' mapping =
cmp.mapping.preset.insert({ mkRaw
["<C-n>"] = function(fallback) /*
if cmp.visible() then lua
cmp.select_next_item() */
elseif require("luasnip").expand_or_jumpable() then ''
require("luasnip").expand_or_jump() cmp.mapping.preset.insert({
elseif has_words_before() then ["<C-n>"] = function(fallback)
cmp.complete() if cmp.visible() then
else cmp.select_next_item()
fallback() elseif require("luasnip").expand_or_jumpable() then
end require("luasnip").expand_or_jump()
end, elseif has_words_before() then
["<C-p>"] = function(fallback) cmp.complete()
if cmp.visible() then else
cmp.select_prev_item() fallback()
elseif require("luasnip").jumpable(-1) then end
require("luasnip").jump(-1) end,
else ["<C-p>"] = function(fallback)
fallback() if cmp.visible() then
end cmp.select_prev_item()
end, elseif require("luasnip").jumpable(-1) then
["<C-u>"] = cmp.mapping(function(fallback) require("luasnip").jump(-1)
if require("luasnip").choice_active() then else
require("luasnip").next_choice() fallback()
else end
fallback() end,
end ["<C-u>"] = cmp.mapping(function(fallback)
end), if require("luasnip").choice_active() then
["<C-b>"] = cmp.mapping.scroll_docs(-4), require("luasnip").next_choice()
["<C-f>"] = cmp.mapping.scroll_docs(4), else
["<C-Space>"] = cmp.mapping.complete { }, fallback()
["<C-e>"] = cmp.mapping.close(), end
["<CR>"] = cmp.mapping.confirm { select = true }, end),
}) ["<C-b>"] = cmp.mapping.scroll_docs(-4),
''; ["<C-f>"] = cmp.mapping.scroll_docs(4),
["<C-Space>"] = cmp.mapping.complete { },
["<C-e>"] = cmp.mapping.close(),
["<CR>"] = cmp.mapping.confirm { select = true },
})
'';
}; };
}; };
# FIXME: doesn't include formatters # FIXME: doesn't include formatters

View file

@ -55,37 +55,41 @@
pkgs.unstable.typos pkgs.unstable.typos
pkgs.unstable.yamlfmt pkgs.unstable.yamlfmt
]; ];
extraConfigLuaPre = '' extraConfigLuaPre =
-- Lua Pre Config # lua
if vim.fn.has 'termguicolors' then ''
-- Enable RGB colors -- Lua Pre Config
vim.g.termguicolors = true if vim.fn.has 'termguicolors' then
end -- Enable RGB colors
vim.g.termguicolors = true
end
-- Useful function -- Useful function
local has_words_before = function() local has_words_before = function()
-- unpack = unpack or table.unpack -- unpack = unpack or table.unpack
local line, col = unpack(vim.api.nvim_win_get_cursor(0)) local line, col = unpack(vim.api.nvim_win_get_cursor(0))
return col ~= 0 return col ~= 0
and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match '%s' == nil and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match '%s' == nil
end end
-- END: Lua Pre Config -- END: Lua Pre Config
''; '';
extraConfigLua = '' extraConfigLua =
-- Lua Config # lua
require("nvim-silicon").setup { ''
theme = "gruvbox-dark", -- Lua Config
pad_horiz = 16, require("nvim-silicon").setup {
pad_vert = 16, theme = "gruvbox-dark",
-- Current buffer name pad_horiz = 16,
window_title = function() pad_vert = 16,
return vim.fn.fnamemodify( -- Current buffer name
vim.api.nvim_buf_get_name(vim.api.nvim_get_current_buf()), window_title = function()
":t" return vim.fn.fnamemodify(
) vim.api.nvim_buf_get_name(vim.api.nvim_get_current_buf()),
end, ":t"
} )
-- END: Lua Config end,
''; }
-- END: Lua Config
'';
}; };
} }