---@param content string
---@param lang string
---@return table|nil
local function get_syntax_highlights(content, lang)
lang = lang or "markdown"
local lines = vim.split(content, "\n", { plain = true })
vim.api.nvim_set_hl(ns, "Markdown_VT", { bg = "#16161D", fg = "#d2a481" })
local by_line = {}
local current_line = vim.fn.getline(".")
local target_indent = current_line:match("^%s*") or ""
for _, line in ipairs(lines) do
line, _ = line:gsub("^%s+", "")
line = string.rep(" ", #target_indent + 2) .. line
local highlights = {}
local linenr = vim.fn.line(".")
vim.api.nvim_buf_set_lines(0, linenr, linenr, true, { line })
-- vim.cmd("syntax sync fromstart")
-- vim.cmd("redraw")
local j = 1
local line_length = #line
while j < line_length do
local char_synconceal = vim.fn.synconcealed(linenr + 1, j)
local hl_group = vim.fn.synIDattr(vim.fn.synID(linenr + 1, j, 1), "name")
if hl_group == "" then
hl_group = "Markdown_VT"
end
local char = " "
if char_synconceal[1] ~= 0 then
char = char_synconceal[2]
local syntax_region = char_synconceal[3]
while vim.fn.synconcealed(linenr + 1, j)[3] == syntax_region and j < #line do
j = j + 1
end
else
char = line:sub(j, j)
vim.print("char", char)
j = j + 1
end
table.insert(highlights, { char, hl_group })
end
vim.api.nvim_buf_set_lines(0, linenr, linenr + 1, false, {})
table.insert(by_line, highlights)
end
return by_line
end
This one uses custom conceal functions for the text, but the conceal features don't incorporate the highighting, which is generically set for the whole line.
lua
local build_virtual_lines = function(lines)
local virt = {}
for _, l in ipairs(lines) do
l = conceal_links(l)
l = latex.conceal_latex(l)
l = markdown.conceal_markdown(l)
table.insert(virt, { { "", "" }, { l, "Markdown_Virtual" } })
end
return virt
end
Assistant
Analysis of the Problem
The two functions provided serve different purposes in the context of syntax highlighting and virtual text rendering in Neovim. Here's a breakdown of their roles:
get_syntax_highlights Function:
This function processes a block of text (content) and applies syntax highlighting based on the specified language (lang).
It uses Neovim's built-in syntax highlighting engine to determine the highlight group for each character in the text.
The function constructs a table (by_line) where each line contains a list of characters paired with their respective highlight groups.
However, it relies on the synconcealed function to handle concealed characters, which may not fully integrate with the highlighting logic.
build_virtual_lines Function:
This function generates virtual lines by applying custom concealment logic (e.g., for links, LaTeX, and Markdown).
It does not rely on Neovim's syntax highlighting engine but instead applies generic highlighting (Markdown_Virtual) to the entire line.
The concealment logic is handled by external functions (conceal_links, latex.conceal_latex, markdown.conceal_markdown), which are not detailed in the provided code.
Key Issues
Highlighting Integration:
The build_virtual_lines function does not incorporate the detailed per-character highlighting provided by get_syntax_highlights. Instead, it applies a generic highlight group (Markdown_Virtual) to the entire line.
This results in a loss of granularity in highlighting when using virtual text.
Concealment and Highlighting:
The get_syntax_highlights function attempts to handle concealed characters using synconcealed, but this approach is limited to Neovim's internal syntax engine.
The build_virtual_lines function uses custom concealment logic, which is decoupled from the syntax highlighting engine, leading to inconsistencies.
Performance Concerns:
Both functions involve iterating over lines and characters, which could become computationally expensive for large documents.
User
i dont need the conceal functions anymore
Assistant
If the conceal functions are no longer needed, we can simplify the implementation by removing all references to concealment logic. This will streamline the function and focus solely on generating syntax highlights for each character in the input text.
Below is the updated version of the get_syntax_highlights function without any concealment-related logic:
Updated Implementation (Without Concealment)
lua
---@param content string
---@param lang string
---@return table|nil
local function get_syntax_highlights(content, lang)
lang = lang or "markdown"
local lines = vim.split(content, "\n", { plain = true })
vim.api.nvim_set_hl(ns, "Markdown_VT", { bg = "#16161D", fg = "#d2a481" })
local by_line = {}
local current_line = vim.fn.getline(".")
local target_indent = current_line:match("^%s*") or ""
for _, line in ipairs(lines) do
-- Remove leading whitespace and re-indent
line, _ = line:gsub("^%s+", "")
line = string.rep(" ", #target_indent + 2) .. line
local highlights = {}
local linenr = vim.fn.line(".")
vim.api.nvim_buf_set_lines(0, linenr, linenr, true, { line })
local j = 1
local line_length = #line
while j <= line_length do
-- Get the highlight group for the current character
local hl_group = vim.fn.synIDattr(vim.fn.synID(linenr + 1, j, 1), "name")
if hl_group == "" then
hl_group = "Markdown_VT"
end
-- Extract the character at position `j`
local char = line:sub(j, j)
table.insert(highlights, { char, hl_group })
-- Move to the next character
j = j + 1
end
-- Clear the temporary buffer line
vim.api.nvim_buf_set_lines(0, linenr, linenr + 1, false, {})
table.insert(by_line, highlights)
end
return by_line
end
Proposed Solution
To address these issues, we can integrate the strengths of both functions:
Use get_syntax_highlights to determine the highlight groups for each character.
Apply custom concealment logic (from build_virtual_lines) while preserving the per-character highlighting.
Here’s how we can achieve this:
Updated Implementation
lua
local function get_syntax_highlights_with_conceal(content, lang)
lang = lang or "markdown"
local lines = vim.split(content, "\n", { plain = true })
vim.api.nvim_set_hl(ns, "Markdown_VT", { bg = "#16161D", fg = "#d2a481" })
local by_line = {}
local current_line = vim.fn.getline(".")
local target_indent = current_line:match("^%s*") or ""
for _, line in ipairs(lines) do
-- Apply custom concealment logic
line = conceal_links(line)
line = latex.conceal_latex(line)
line = markdown.conceal_markdown(line)
-- Remove leading whitespace and re-indent
line, _ = line:gsub("^%s+", "")
line = string.rep(" ", #target_indent + 2) .. line
local highlights = {}
local linenr = vim.fn.line(".")
vim.api.nvim_buf_set_lines(0, linenr, linenr, true, { line })
local j = 1
local line_length = #line
while j <= line_length do
-- Check if the character is concealed
local char_synconceal = vim.fn.synconcealed(linenr + 1, j)
local hl_group = vim.fn.synIDattr(vim.fn.synID(linenr + 1, j, 1), "name")
if hl_group == "" then
hl_group = "Markdown_VT"
end
local char = " "
if char_synconceal[1] ~= 0 then
char = char_synconceal[2]
local syntax_region = char_synconceal[3]
-- Skip over concealed regions
while vim.fn.synconcealed(linenr + 1, j)[3] == syntax_region and j < line_length do
j = j + 1
end
else
char = line:sub(j, j)
j = j + 1
end
-- Insert the character and its highlight group
table.insert(highlights, { char, hl_group })
end
-- Clear the temporary buffer line
vim.api.nvim_buf_set_lines(0, linenr, linenr + 1, false, {})
table.insert(by_line, highlights)
end
return by_line
end
Explanation of Changes
Custom Concealment Integration:
Before processing the line for syntax highlighting, the function applies custom concealment logic (conceal_links, latex.conceal_latex, markdown.conceal_markdown).
Preservation of Highlighting:
The function retains the per-character highlighting logic from get_syntax_highlights.
Concealed characters are handled using synconcealed, ensuring compatibility with Neovim's syntax engine.
Improved Readability:
The code is structured to clearly separate concealment and highlighting logic, making it easier to maintain and extend.
Performance Optimization:
By combining the concealment and highlighting steps into a single pass, we reduce redundant computations.
Usage Example
lua
local content = [[
# Heading
This is a [link](https://example.com) and some **bold text**.
]]
local lang = "markdown"
local highlights = get_syntax_highlights_with_conceal(content, lang)
for _, line in ipairs(highlights) do
for _, char_info in ipairs(line) do
local char, hl_group = unpack(char_info)
print(string.format("Char: %s, Highlight Group: %s", char, hl_group))
end
end
Conclusion
This updated implementation integrates the detailed syntax highlighting of get_syntax_highlights with the custom concealment logic of build_virtual_lines. It ensures that concealed characters are properly highlighted and maintains the granularity of per-character highlighting.
Explanation of Changes
Removed Concealment Logic:
All references to synconcealed and concealed characters have been removed.
The function now focuses solely on determining the syntax highlight group for each character.
Simplified Character Processing:
Each character is processed directly using line:sub(j, j) without checking for concealed regions.
The loop increments j by 1 for each character, ensuring a straightforward traversal of the line.
Preserved Highlighting Granularity:
The function retains its ability to determine the highlight group for each character using Neovim's syntax engine.
If no highlight group is found, it defaults to Markdown_VT.
Improved Readability:
By removing unnecessary complexity, the code is easier to understand and maintain.
Usage Example
lua
local content = [[
# Heading
This is some **bold text** and `inline code`.
]]
local lang = "markdown"
local highlights = get_syntax_highlights(content, lang)
for _, line in ipairs(highlights) do
for _, char_info in ipairs(line) do
local char, hl_group = unpack(char_info)
print(string.format("Char: %s, Highlight Group: %s", char, hl_group))
end
end
Output Example
For the given content, the output might look like this (depending on your syntax highlighting configuration):
The function now performs only one task: generating per-character syntax highlights.
This makes it easier to test, debug, and extend.
Reduced Complexity:
By removing concealment logic, the function is simpler and more efficient.
Compatibility with Syntax Engine:
The function fully leverages Neovim's syntax highlighting engine, ensuring accurate and consistent results.
Scalability:
The streamlined implementation can handle larger documents more efficiently, as it avoids unnecessary checks for concealed characters.
Conclusion
This updated implementation removes all concealment-related logic, focusing exclusively on generating detailed syntax highlights for each character. It is simpler, more efficient, and easier to maintain while preserving the granularity of per-character highlighting.