Skip to content

Commit f4dde88

Browse files
feat(agenda): Highlight links and dates using extmarks
1 parent de701e8 commit f4dde88

File tree

6 files changed

+112
-640
lines changed

6 files changed

+112
-640
lines changed

lua/orgmode/agenda/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ function Agenda:get_headline_at_cursor()
623623
end
624624

625625
function Agenda:open_at_point()
626-
local link = OrgHyperlink.from_current_line_position()
626+
local link = OrgHyperlink.from_extmarks_at_cursor()
627627

628628
if link then
629629
return self.links:follow(link.url:to_string())

lua/orgmode/colors/highlighter/markup/dates.lua

Lines changed: 0 additions & 174 deletions
This file was deleted.

lua/orgmode/colors/highlighter/markup/init.lua

Lines changed: 93 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -174,18 +174,104 @@ function OrgMarkup:get_prepared_headline_highlights(headline)
174174
local result = {}
175175

176176
for type, highlight in pairs(highlights) do
177-
vim.list_extend(
178-
result,
179-
self.parsers[type]:prepare_highlights(highlight, function(start_col, end_col)
180-
local text = headline.file:get_node_text(headline:node())
181-
return text:sub(start_col, end_col)
182-
end)
183-
)
177+
vim.list_extend(result, self.parsers[type]:prepare_highlights(highlight))
184178
end
185179

180+
vim.list_extend(result, self:_prepare_ts_highlights(headline))
181+
186182
return result
187183
end
188184

185+
---@private
186+
---@param headline OrgHeadline
187+
---@return OrgMarkupPreparedHighlight[]
188+
function OrgMarkup:_prepare_ts_highlights(headline)
189+
local headline_item_node = headline:node():field('item')[1]
190+
if not headline_item_node then
191+
return {}
192+
end
193+
local result = {}
194+
for node in headline_item_node:iter_children() do
195+
if node:type() == 'link' or node:type() == 'link_desc' then
196+
self:_prepare_link_higlight(headline, node, result)
197+
end
198+
if node:type() == 'timestamp' then
199+
self:_prepare_date_highlight(node, result)
200+
end
201+
end
202+
return result
203+
end
204+
205+
---@param headline OrgHeadline
206+
---@param node TSNode
207+
---@param result OrgMarkupPreparedHighlight[]
208+
function OrgMarkup:_prepare_link_higlight(headline, node, result)
209+
local url = node:field('url')[1]
210+
local desc = node:field('desc')[1]
211+
local url_target = nil
212+
213+
if desc then
214+
local sld, scd, _, ecd = desc:range()
215+
table.insert(result, {
216+
start_line = sld,
217+
start_col = scd,
218+
end_col = ecd,
219+
hl_group = '@org.hyperlink.desc',
220+
})
221+
table.insert(result, {
222+
start_line = sld,
223+
start_col = scd - 2,
224+
end_col = scd,
225+
conceal = '',
226+
})
227+
end
228+
229+
if url then
230+
local slu, scu, _, ecu = url:range()
231+
table.insert(result, {
232+
start_line = slu,
233+
start_col = scu,
234+
end_col = ecu,
235+
hl_group = '@org.hyperlink.url',
236+
spell = false,
237+
conceal = desc and '' or nil,
238+
})
239+
url_target = headline.file:get_node_text(url)
240+
end
241+
local sl, sc, _, ec = node:range()
242+
table.insert(result, {
243+
start_line = sl,
244+
start_col = sc,
245+
end_col = ec,
246+
hl_group = '@org.hyperlink',
247+
url = url_target,
248+
})
249+
table.insert(result, {
250+
start_line = sl,
251+
start_col = sc,
252+
end_col = sc + 2,
253+
conceal = '',
254+
})
255+
table.insert(result, {
256+
start_line = sl,
257+
start_col = ec - 2,
258+
end_col = ec,
259+
conceal = '',
260+
})
261+
end
262+
263+
---@param node TSNode
264+
---@param result OrgMarkupPreparedHighlight[]
265+
function OrgMarkup:_prepare_date_highlight(node, result)
266+
local sl, sc, _, ec = node:range()
267+
table.insert(result, {
268+
start_line = sl,
269+
start_col = sc,
270+
end_col = ec,
271+
hl_group = node:child(0):type() == '<' and '@org.timestamp.active' or '@org.timestamp.inactive',
272+
})
273+
end
274+
189275
function OrgMarkup:on_detach(bufnr)
190276
self.cache[bufnr] = nil
191277
end

0 commit comments

Comments
 (0)