Skip to content

Commit

Permalink
feat: highlight quarto code chunks without plugin (#103)
Browse files Browse the repository at this point in the history
feat: make animals more visible

feat: exclude quarto docs output dir from telescope grep

fix: don't show mode in cmd line, already in statusline

fix: colorizer config

fix: lsp examples

fix: hide more quarto output dirs from search
  • Loading branch information
jmbuhr authored Apr 11, 2024
1 parent e4bd56c commit 9fc14a9
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 27 deletions.
59 changes: 58 additions & 1 deletion ftplugin/quarto.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
local api = vim.api
local ts = vim.treesitter

vim.b.slime_cell_delimiter = '```'
vim.b['quarto_is_r_mode'] = nil
vim.b['reticulate_running'] = false
Expand All @@ -13,7 +16,61 @@ vim.wo.showbreak = '|'
vim.api.nvim_buf_set_var(0, 'did_ftplugin', true)

-- markdown vs. quarto hacks
local ns = vim.api.nvim_create_namespace 'quartoHighlight'
local ns = vim.api.nvim_create_namespace 'QuartoHighlight'
vim.api.nvim_set_hl(ns, '@markup.strikethrough', { strikethrough = false })
vim.api.nvim_set_hl(ns, '@markup.doublestrikethrough', { strikethrough = true })
vim.api.nvim_win_set_hl_ns(0, ns)

-- highlight code cells similar to
-- 'lukas-reineke/headlines.nvim'
-- (disabled in lua/plugins/ui.lua)
local buf = api.nvim_get_current_buf()

local parsername = 'markdown'
local parser = ts.get_parser(buf, parsername)
local tsquery = '(fenced_code_block)@codecell'

-- vim.api.nvim_set_hl(0, '@markup.codecell', { bg = '#000055' })
vim.api.nvim_set_hl(0, '@markup.codecell', {
link = 'CursorLine',
})

local function clear_all()
local all = api.nvim_buf_get_extmarks(buf, ns, 0, -1, {})
for _, mark in ipairs(all) do
vim.api.nvim_buf_del_extmark(buf, ns, mark[1])
end
end

local function highlight_range(from, to)
for i = from, to do
vim.api.nvim_buf_set_extmark(buf, ns, i, 0, {
hl_eol = true,
line_hl_group = '@markup.codecell',
})
end
end

local function highlight_cells()
clear_all()

local query = ts.query.parse(parsername, tsquery)
local tree = parser:parse()
local root = tree[1]:root()
for _, match, _ in query:iter_matches(root, buf, 0, -1, { all = true }) do
for _, nodes in pairs(match) do
for _, node in ipairs(nodes) do
local start_line, _, end_line, _ = node:range()
pcall(highlight_range, start_line, end_line - 1)
end
end
end
end

highlight_cells()

vim.api.nvim_create_autocmd({ 'ModeChanged', 'BufWrite' }, {
group = vim.api.nvim_create_augroup('QuartoCellHighlight', { clear = true }),
buffer = buf,
callback = highlight_cells,
})
5 changes: 4 additions & 1 deletion lua/config/global.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ vim.opt.updatetime = 250 -- for autocommands and hovers
-- don't ask about existing swap files
vim.opt.shortmess:append 'A'

-- mode is already in statusline
vim.opt.showmode = false

-- use less indentation
local tabsize = 2
vim.opt.expandtab = true
Expand Down Expand Up @@ -67,7 +70,7 @@ let g:currentmode={

math.randomseed(os.time())
local i = math.random(#animals)
vim.opt.statusline = '%{%g:currentmode[mode()]%} %{%reg_recording()%} %* %t | %y | %* %= c:%c l:%l/%L %p%% ' .. animals[i] .. ' %*'
vim.opt.statusline = '%{%g:currentmode[mode()]%} %{%reg_recording()%} %* %t | %y | %* %= c:%c l:%l/%L %p%% %#NonText# ' .. animals[i] .. ' %*'

-- hide cmdline when not used
vim.opt.cmdheight = 1
Expand Down
68 changes: 68 additions & 0 deletions lua/config/wip/focus-image.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
local api = require 'image'

M = {}

local function get_image_at_cursor(buf)
local images = api.get_images { buffer = buf }
local row = vim.api.nvim_win_get_cursor(0)[1]
for _, img in ipairs(images) do
if img.geometry ~= nil and img.geometry.y == row then
return img
end
end
return nil
end

M.main = function()
local cur_buf = vim.api.nvim_get_current_buf()
local image = get_image_at_cursor(cur_buf)
if image == nil then
return
end

local width = vim.api.nvim_get_option_value('columns', {})
local height = vim.api.nvim_get_option_value('lines', {})

local preview_buf = vim.api.nvim_create_buf(false, true)
local win = vim.api.nvim_open_win(preview_buf, true, {
relative = 'editor',
anchor = 'NW',
width = width,
height = height,
row = 0,
col = 0,
zindex = 300,
style = 'minimal',
})

-- copy original options to reset afterwards
local og_options = {}
for k, v in pairs(image.global_state.options) do
og_options[k] = v
end

local og_extmark = image.extmark

image.global_state.options.max_height = nil
image.global_state.options.max_width = nil
image.global_state.options.max_width_window_percentage = nil
image.global_state.options.max_height_window_percentage = nil

for _, img in ipairs(api.get_images()) do
img:clear()
end

vim.print(image.global_state)

api.hijack_buffer(image.path, win, preview_buf)

vim.keymap.set('n', 'q', function()
api.clear()
image.global_state.options = og_options
image.extmark = og_extmark
image:render()
vim.api.nvim_win_close(win, true)
end, { buffer = preview_buf })
end

vim.keymap.set('n', '<leader>io', M.main, { desc = '[i]mage [o]pen' })
44 changes: 23 additions & 21 deletions lua/plugins/colorthemes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,27 +46,29 @@ return {
enabled = true,
opts = {
filetypes = { '*' },
RGB = true, -- #RGB hex codes
RRGGBB = true, -- #RRGGBB hex codes
names = true, -- "Name" codes like Blue or blue
RRGGBBAA = true, -- #RRGGBBAA hex codes
AARRGGBB = false, -- 0xAARRGGBB hex codes
rgb_fn = false, -- CSS rgb() and rgba() functions
hsl_fn = false, -- CSS hsl() and hsla() functions
css = false, -- Enable all CSS features: rgb_fn, hsl_fn, names, RGB, RRGGBB
css_fn = false, -- Enable all CSS *functions*: rgb_fn, hsl_fn
-- Available modes for `mode`: foreground, background, virtualtext
mode = 'background', -- Set the display mode.
-- Available methods are false / true / "normal" / "lsp" / "both"
-- True is same as normal
tailwind = false, -- Enable tailwind colors
-- parsers can contain values used in |user_default_options|
sass = { enable = false, parsers = { 'css' } }, -- Enable sass colors
virtualtext = '',
-- update color values even if buffer is not focused
-- example use: cmp_menu, cmp_docs
always_update = false,
-- all the sub-options of filetypes apply to buftypes
user_default_options = {
RGB = true, -- #RGB hex codes
RRGGBB = true, -- #RRGGBB hex codes
names = true, -- "Name" codes like Blue or blue
RRGGBBAA = true, -- #RRGGBBAA hex codes
AARRGGBB = false, -- 0xAARRGGBB hex codes
rgb_fn = false, -- CSS rgb() and rgba() functions
hsl_fn = false, -- CSS hsl() and hsla() functions
css = false, -- Enable all CSS features: rgb_fn, hsl_fn, names, RGB, RRGGBB
css_fn = false, -- Enable all CSS *functions*: rgb_fn, hsl_fn
-- Available modes for `mode`: foreground, background, virtualtext
mode = 'background', -- Set the display mode.
-- Available methods are false / true / "normal" / "lsp" / "both"
-- True is same as normal
tailwind = false, -- Enable tailwind colors
-- parsers can contain values used in |user_default_options|
sass = { enable = false, parsers = { 'css' } }, -- Enable sass colors
virtualtext = '',
-- update color values even if buffer is not focused
-- example use: cmp_menu, cmp_docs
always_update = false,
-- all the sub-options of filetypes apply to buftypes
},
buftypes = {},
},
},
Expand Down
11 changes: 8 additions & 3 deletions lua/plugins/lsp.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
return {
{

-- for lsp features in code cells / embedded code
'jmbuhr/otter.nvim',
dev = true,
dev = false,
dependencies = {
{
'neovim/nvim-lspconfig',
Expand All @@ -18,6 +19,7 @@ return {
},
buffers = {
set_filetype = true,
write_to_disk = false,
},
handle_leading_whitespace = true,
},
Expand Down Expand Up @@ -225,13 +227,16 @@ return {
-- See `:h lspconfig-all` for the configuration.
-- Like e.g. Haskell:
-- lspconfig.hls.setup {
-- on_attach = on_attach,
-- capabilities = capabilities,
-- flags = lsp_flags
-- }

-- lspconfig.clangd.setup {
-- capabilities = capabilities,
-- flags = lsp_flags,
-- }

-- lspconfig.rust_analyzer.setup{
-- on_attach = on_attach,
-- capabilities = capabilities,
-- settings = {
-- ['rust-analyzer'] = {
Expand Down
11 changes: 10 additions & 1 deletion lua/plugins/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,21 @@ return {
end
end)
end

local telescope_config = require 'telescope.config'
-- Clone the default Telescope configuration
local vimgrep_arguments = { unpack(telescope_config.values.vimgrep_arguments) }
-- I don't want to search in the `docs` directory (rendered quarto output).
table.insert(vimgrep_arguments, '--glob')
table.insert(vimgrep_arguments, '!docs/*')

telescope.setup {
defaults = {
buffer_previewer_maker = new_maker,
vimgrep_arguments = vimgrep_arguments,
file_ignore_patterns = {
'node_modules',
'%_files/*.html',
'%_files/',
'%_cache',
'.git/',
'site_libs',
Expand Down

0 comments on commit 9fc14a9

Please sign in to comment.