Skip to content

Commit

Permalink
Add option to disable UI for long files (#613)
Browse files Browse the repository at this point in the history
  • Loading branch information
epwalsh authored May 31, 2024
1 parent 0e9bc3a commit 96630cb
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Added

- Added config option `ui.max_file_length` to disable the UI for files with more than this many lines. Defaults to 5000.

## [v3.7.13](https://github.com/epwalsh/obsidian.nvim/releases/tag/v3.7.13) - 2024-05-31

### Fixed
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,7 @@ This is a complete list of all of the options that can be passed to `require("ob
ui = {
enable = true, -- set to false to disable all additional syntax features
update_debounce = 200, -- update delay after a text change (in milliseconds)
max_file_length = 5000, -- disable UI features for files with more than this many lines
-- Define how various check-boxes are displayed
checkboxes = {
-- NOTE: the 'char' value has to be a single character, and the highlight groups are defined below.
Expand Down
2 changes: 2 additions & 0 deletions lua/obsidian/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ end
---
---@field enable boolean
---@field update_debounce integer
---@field max_file_length integer|?
---@field checkboxes table<string, obsidian.config.CheckboxSpec>
---@field bullets obsidian.config.UICharSpec|?
---@field external_link_icon obsidian.config.UICharSpec
Expand Down Expand Up @@ -426,6 +427,7 @@ config.UIOpts.default = function()
return {
enable = true,
update_debounce = 200,
max_file_length = 5000,
checkboxes = {
[" "] = { order = 1, char = "󰄱", hl_group = "ObsidianTodo" },
["~"] = { order = 2, char = "󰰱", hl_group = "ObsidianTilde" },
Expand Down
34 changes: 26 additions & 8 deletions lua/obsidian/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -553,13 +553,37 @@ local function update_extmarks(bufnr, ns_id, ui_opts)
log.debug("Added %d new marks, cleared %d old marks in %dms", n_marks_added, n_marks_cleared, runtime)
end

---@param ui_opts obsidian.config.UIOpts
---@param bufnr integer|?
---@return boolean
local function should_update(ui_opts, bufnr)
if ui_opts.enable == false then
return false
end

bufnr = bufnr or 0

if not vim.endswith(vim.api.nvim_buf_get_name(bufnr), ".md") then
return false
end

if ui_opts.max_file_length ~= nil and vim.fn.line "$" > ui_opts.max_file_length then
return false
end

return true
end

---@param ui_opts obsidian.config.UIOpts
---@param throttle boolean
---@return function
local function get_extmarks_autocmd_callback(ui_opts, throttle)
local ns_id = vim.api.nvim_create_namespace(NAMESPACE)

local callback = function(ev)
if not should_update(ui_opts, ev.bufnr) then
return
end
update_extmarks(ev.buf, ns_id, ui_opts)
end

Expand All @@ -574,16 +598,10 @@ end
---@param ui_opts obsidian.config.UIOpts
---@param bufnr integer|?
M.update = function(ui_opts, bufnr)
if ui_opts.enable == false then
bufnr = bufnr or 0
if not should_update(ui_opts, bufnr) then
return
end

bufnr = bufnr and bufnr or 0

if not vim.endswith(vim.api.nvim_buf_get_name(bufnr), ".md") then
return
end

local ns_id = vim.api.nvim_create_namespace(NAMESPACE)
update_extmarks(bufnr, ns_id, ui_opts)
end
Expand Down

0 comments on commit 96630cb

Please sign in to comment.