Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested link and orphan characters #431

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -1196,14 +1196,6 @@ For adding/changing TODO keyword colors see [org-todo-keyword-faces](#org_todo_k
* `OrgTSCheckboxChecked`: A checkbox checked with either `[x]` or `[X]`
* `OrgTSCheckboxHalfChecked`: A checkbox checked with `[-]`
* `OrgTSCheckboxUnchecked`: A empty checkbox
* `OrgTSHeadlineLevel1`: Headline at level 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets leave all these highlight changes for a separate PR.

* `OrgTSHeadlineLevel2`: Headline at level 2
* `OrgTSHeadlineLevel3`: Headline at level 3
* `OrgTSHeadlineLevel4`: Headline at level 4
* `OrgTSHeadlineLevel5`: Headline at level 5
* `OrgTSHeadlineLevel6`: Headline at level 6
* `OrgTSHeadlineLevel7`: Headline at level 7
* `OrgTSHeadlineLevel8`: Headline at level 8

* The following use vanilla _Vim_ syntax matching, and will work without _Treesitter_ highlighting enabled.

Expand Down
8 changes: 0 additions & 8 deletions doc/orgmode.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1589,14 +1589,6 @@ HIGHLIGHT GROUPS *orgmode-highlight_group
* `OrgTSCheckboxChecked`: A checkbox checked with either `[x]` or `[X]`
* `OrgTSCheckboxHalfChecked`: A checkbox checked with `[-]`
* `OrgTSCheckboxUnchecked`: A empty checkbox
* `OrgTSHeadlineLevel1`: Headline at level 1
* `OrgTSHeadlineLevel2`: Headline at level 2
* `OrgTSHeadlineLevel3`: Headline at level 3
* `OrgTSHeadlineLevel4`: Headline at level 4
* `OrgTSHeadlineLevel5`: Headline at level 5
* `OrgTSHeadlineLevel6`: Headline at level 6
* `OrgTSHeadlineLevel7`: Headline at level 7
* `OrgTSHeadlineLevel8`: Headline at level 8
* The following use vanilla Vim syntax matching, and will work without Treesitter highlighting enabled.
* `OrgEditSrcHighlight`: The highlight for the source content in an Org buffer while it is being edited in an edit special buffer
* `OrgAgendaDeadline`: A item deadline in the agenda view
Expand Down
8 changes: 4 additions & 4 deletions ftplugin/org.vim
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ function! OrgmodeFormatExpr()
endfunction

setlocal nomodeline
setlocal fillchars+=fold:\
setlocal foldmethod=expr
setlocal foldexpr=nvim_treesitter#foldexpr()
setlocal foldtext=OrgmodeFoldText()
" setlocal fillchars+=fold:\
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be uncommented

" setlocal foldmethod=expr
" setlocal foldexpr=nvim_treesitter#foldexpr()
" setlocal foldtext=OrgmodeFoldText()
setlocal formatexpr=OrgmodeFormatExpr()
setlocal omnifunc=OrgmodeOmni
setlocal commentstring=#\ %s
Expand Down
8 changes: 0 additions & 8 deletions lua/orgmode/colors/highlights.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@ function M.link_ts_highlights()
local links = {
OrgTSTimestampActive = 'PreProc',
OrgTSTimestampInactive = 'Comment',
OrgTSHeadlineLevel1 = 'OrgHeadlineLevel1',
OrgTSHeadlineLevel2 = 'OrgHeadlineLevel2',
OrgTSHeadlineLevel3 = 'OrgHeadlineLevel3',
OrgTSHeadlineLevel4 = 'OrgHeadlineLevel4',
OrgTSHeadlineLevel5 = 'OrgHeadlineLevel5',
OrgTSHeadlineLevel6 = 'OrgHeadlineLevel6',
OrgTSHeadlineLevel7 = 'OrgHeadlineLevel7',
OrgTSHeadlineLevel8 = 'OrgHeadlineLevel8',
OrgTSBullet = 'Identifier',
OrgTSCheckbox = 'PreProc',
OrgTSCheckboxHalfChecked = 'OrgTSCheckbox',
Expand Down
36 changes: 29 additions & 7 deletions lua/orgmode/colors/markup_highlighter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,15 @@ local function load_deps()
vim.treesitter.query.add_predicate('org-is-valid-latex-range?', is_valid_latex_range)
end

-- a function that splits a string on '.'
local function split(string)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a vim.split helper function that you can use instead.

local t = {}
for str in string.gmatch(string, '([^.]+)') do
table.insert(t, str)
end
return t
end

---@param bufnr number
---@param line_index number
---@return table
Expand All @@ -221,18 +230,20 @@ local get_matches = ts_utils.memoize_by_buf_tick(function(bufnr, line_index, roo
local taken_locations = {}

for _, match, _ in query:iter_matches(root, bufnr, line_index, line_index + 1) do
for _, node in pairs(match) do
for id, node in pairs(match) do
local char = node:type()
-- saves unnecessary parsing, since \\ is not used below
if char ~= '\\' then
local range = ts_utils.node_to_lsp_range(node)
local linenr = tostring(range.start.line)
taken_locations[linenr] = taken_locations[linenr] or {}
local capture = split(query.captures[id])[2]
if not taken_locations[linenr][range.start.character] then
table.insert(ranges, {
type = char,
range = range,
node = node,
capture = capture,
})
taken_locations[linenr][range.start.character] = true
end
Expand All @@ -248,7 +259,8 @@ local get_matches = ts_utils.memoize_by_buf_tick(function(bufnr, line_index, roo
end)

local seek = {}
local seek_link = {}
local seek_link = nil
local orphans = {}
local result = {}
local link_result = {}
local latex_result = {}
Expand Down Expand Up @@ -315,23 +327,33 @@ local get_matches = ts_utils.memoize_by_buf_tick(function(bufnr, line_index, roo
to = item.range,
})
else
seek[item.type] = item
nested[#nested + 1] = item.type
can_nest = markers[item.type].nestable
if not (orphans[item.type] and item.capture == 'end') then
seek[item.type] = item
nested[#nested + 1] = item.type
can_nest = markers[item.type].nestable
orphans[item.type] = nil
end
orphans[item.type] = nil
end
else
orphans[item.type] = item.capture
end
end

if item.type == '[' then
if item.type == '[' and can_nest then
seek_link = item
nested[#nested + 1] = item.type
can_nest = false
end

if item.type == ']' and seek_link then
table.insert(link_result, {
from = seek_link.range,
to = item.range,
})
nested[#nested] = nil
seek_link = nil
can_nest = true
end
end

Expand Down Expand Up @@ -401,7 +423,7 @@ local function apply(namespace, bufnr, line_index)
vim.api.nvim_buf_set_extmark(bufnr, namespace, link_range.from.start.line, link_range.from.start.character + 2, {
ephemeral = true,
end_col = link_range.from.start.character - 1 + link_end,
spell = false,
spell = true,
})

vim.api.nvim_buf_set_extmark(bufnr, namespace, link_range.from.start.line, link_range.to['end'].character - 2, {
Expand Down
16 changes: 8 additions & 8 deletions queries/org/highlights.scm
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
(timestamp "<") @OrgTSTimestampActive
(timestamp "[") @OrgTSTimestampInactive
(headline (stars) @stars (#eq? @stars "*")) @OrgTSHeadlineLevel1
(headline (stars) @stars (#eq? @stars "**")) @OrgTSHeadlineLevel2
(headline (stars) @stars (#eq? @stars "***")) @OrgTSHeadlineLevel3
(headline (stars) @stars (#eq? @stars "****")) @OrgTSHeadlineLevel4
(headline (stars) @stars (#eq? @stars "*****")) @OrgTSHeadlineLevel5
(headline (stars) @stars (#eq? @stars "******")) @OrgTSHeadlineLevel6
(headline (stars) @stars (#eq? @stars "*******")) @OrgTSHeadlineLevel7
(headline (stars) @stars (#eq? @stars "********")) @OrgTSHeadlineLevel8
(headline (stars) @text.title.1.marker (#eq? @text.title.1.marker "*")) @text.title.1
(headline (stars) @text.title.2.marker (#eq? @text.title.2.marker "**")) @text.title.2
(headline (stars) @text.title.3.marker (#eq? @text.title.3.marker "***")) @text.title.3
(headline (stars) @text.title.4.marker (#eq? @text.title.4.marker "****")) @text.title.4
(headline (stars) @text.title.5.marker (#eq? @text.title.5.marker "*****")) @text.title.5
(headline (stars) @text.title.6.marker (#eq? @text.title.6.marker "******")) @text.title.6
(headline (stars) @text.title.7.marker (#eq? @text.title.7.marker "*******")) @text.title.7
(headline (stars) @text.title.8.marker (#eq? @text.title.8.marker "********")) @text.title.8
(headline (item) @spell)
(item . (expr) @OrgTODO @nospell (#org-is-todo-keyword? @OrgTODO "TODO"))
(item . (expr) @OrgDONE @nospell (#org-is-todo-keyword? @OrgDONE "DONE"))
Expand Down
2 changes: 1 addition & 1 deletion queries/org/injections.scm
Original file line number Diff line number Diff line change
@@ -1 +1 @@
(block parameter: (expr) @language (contents) @content)
(block parameter: (expr) @injection.language (contents) @injection.content (#set! injection.include-children))