Improve font/color change detection in `language_md` (#1614)

* Delay setting font for custom `language_md` token types

* Improve font/color change detection in `language_md`
This commit is contained in:
Guldoman 2023-11-29 22:12:22 +01:00 committed by takase1121
parent bc935906d1
commit 8e2928aeb8
No known key found for this signature in database
GPG Key ID: 60EEFFC68EB3031B
1 changed files with 54 additions and 22 deletions

View File

@ -3,25 +3,6 @@ local syntax = require "core.syntax"
local style = require "core.style" local style = require "core.style"
local core = require "core" local core = require "core"
local initial_color = style.syntax["keyword2"]
-- Add 3 type of font styles for use on markdown files
for _, attr in pairs({"bold", "italic", "bold_italic"}) do
local attributes = {}
if attr ~= "bold_italic" then
attributes[attr] = true
else
attributes["bold"] = true
attributes["italic"] = true
end
style.syntax_fonts["markdown_"..attr] = style.code_font:copy(
style.code_font:get_size(),
attributes
)
-- also add a color for it
style.syntax["markdown_"..attr] = style.syntax["keyword2"]
end
local in_squares_match = "^%[%]" local in_squares_match = "^%[%]"
local in_parenthesis_match = "^%(%)" local in_parenthesis_match = "^%(%)"
@ -224,12 +205,63 @@ syntax.add {
-- Adjust the color on theme changes -- Adjust the color on theme changes
core.add_thread(function() core.add_thread(function()
while true do local custom_fonts = { bold = {font = nil, color = nil}, italic = {}, bold_italic = {} }
if initial_color ~= style.syntax["keyword2"] then local initial_color
for _, attr in pairs({"bold", "italic", "bold_italic"}) do local last_code_font
local function set_font(attr)
local attributes = {}
if attr ~= "bold_italic" then
attributes[attr] = true
else
attributes["bold"] = true
attributes["italic"] = true
end
local font = style.code_font:copy(
style.code_font:get_size(),
attributes
)
custom_fonts[attr].font = font
style.syntax_fonts["markdown_"..attr] = font
end
local function set_color(attr)
custom_fonts[attr].color = style.syntax["keyword2"]
style.syntax["markdown_"..attr] = style.syntax["keyword2"] style.syntax["markdown_"..attr] = style.syntax["keyword2"]
end end
-- Add 3 type of font styles for use on markdown files
for attr, _ in pairs(custom_fonts) do
-- Only set it if the font wasn't manually customized
if not style.syntax_fonts["markdown_"..attr] then
set_font(attr)
end
-- Only set it if the color wasn't manually customized
if not style.syntax["markdown_"..attr] then
set_color(attr)
end
end
while true do
if last_code_font ~= style.code_font then
last_code_font = style.code_font
for attr, _ in pairs(custom_fonts) do
-- Only set it if the font wasn't manually customized
if style.syntax_fonts["markdown_"..attr] == custom_fonts[attr].font then
set_font(attr)
end
end
end
if initial_color ~= style.syntax["keyword2"] then
initial_color = style.syntax["keyword2"] initial_color = style.syntax["keyword2"]
for attr, _ in pairs(custom_fonts) do
-- Only set it if the color wasn't manually customized
if style.syntax["markdown_"..attr] == custom_fonts[attr].color then
set_color(attr)
end
end
end end
coroutine.yield(1) coroutine.yield(1)
end end