Skip to content

Commit

Permalink
Fix support for hide leading stars option.
Browse files Browse the repository at this point in the history
  • Loading branch information
kristijanhusak committed Oct 14, 2021
1 parent 2ee5fdf commit fed342a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ Highlights are partially supported.
#### Known highlighting issues and limitations
* Performance issues. This is generally an issue in Neovim that should be resolved before 0.6 release (https://github.com/neovim/neovim/issues/14762, https://github.com/neovim/neovim/issues/14762)
* Anything that requires concealing ([org_hide_emphasis_markers](/DOCS.md#org_hide_emphasis_markers), links concealing) is not (yet) supported in TS highlighter
* Option [org_hide_leading_stars](/DOCS.md#org_hide_leading_stars) does not work due to limitations with grammar and Neovim.
* LaTex is still highlighted through syntax file

#### Improvements over Vim's syntax highlighting
Expand Down
44 changes: 44 additions & 0 deletions lua/orgmode/colors/hide_leading_stars.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
local config = require('orgmode.config')
local namespace = vim.api.nvim_create_namespace('org_hide_leading_stars')
local valid_bufnrs = {}

local function update_line_highlight(bufnr, line_index, line)
local stars = line:match('^%*+')
if stars then
vim.api.nvim_buf_set_extmark(bufnr, namespace, line_index, 0, {
end_line = line_index,
end_col = stars:len() - 1,
hl_group = 'OrgHideLeadingStars',
ephemeral = true,
})
end
end

local function update_range_highlight(bufnr, first_line, last_line)
local changed_lines = vim.api.nvim_buf_get_lines(bufnr, first_line, last_line, false)
for i, line in ipairs(changed_lines) do
update_line_highlight(bufnr, first_line + i - 1, line)
end
end

local function setup_hide_leading_stars()
if not config.org_hide_leading_stars then
return
end
vim.api.nvim_set_decoration_provider(namespace, {
on_win = function(_, _, bufnr, topline, botline)
if valid_bufnrs[bufnr] then
return update_range_highlight(bufnr, topline, botline)
end
local ft = vim.api.nvim_buf_get_option(bufnr, 'filetype')
if ft == 'org' then
valid_bufnrs[bufnr] = true
return update_range_highlight(bufnr, topline, botline)
end
end,
})
end

return {
setup = setup_hide_leading_stars,
}
8 changes: 4 additions & 4 deletions lua/orgmode/colors/highlights.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ function M.define_org_headline_colors(faces)
table.insert(contains, face)
end
if config.org_hide_leading_stars then
vim.cmd([[
syn match OrgHideLeadingStars /^\*\{2,\}/me=e-1 contained
hi def link OrgHideLeadingStars org_hide_leading_stars
]])
if not ts_highlights_enabled then
vim.cmd([[syn match OrgHideLeadingStars /^\*\{2,\}/me=e-1 contained]])
end
vim.cmd([[hi def link OrgHideLeadingStars org_hide_leading_stars]])
table.insert(contains, 'OrgHideLeadingStars')
end
contains = table.concat(contains, ',')
Expand Down
1 change: 1 addition & 0 deletions lua/orgmode/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ function Org:init()
return
end
require('orgmode.colors.todo_highlighter').add_todo_keyword_highlights()
require('orgmode.colors.hide_leading_stars').setup()
self.files = require('orgmode.parser.files').new()
self.agenda = require('orgmode.agenda'):new()
self.capture = require('orgmode.capture'):new()
Expand Down

0 comments on commit fed342a

Please sign in to comment.