Skip to content

Commit

Permalink
Don't skip blank labels & refresh when text changes
Browse files Browse the repository at this point in the history
  • Loading branch information
boatbomber committed Sep 18, 2022
1 parent 8f852bd commit 50c580d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@boatbomber/highlighter",
"version": "0.5.1",
"version": "0.5.2",
"license": "MIT",
"repository": {
"type": "git",
Expand Down
48 changes: 28 additions & 20 deletions src/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,6 @@ local Highlighter = {
defaultLexer = require(script.lexer),
}

function Highlighter.refresh(): ()
-- Rehighlight existing labels using latest colors
for textObject, data in pairs(LastData) do
for _, lineLabel in ipairs(data.Lines) do
lineLabel.TextColor3 = TokenColors["iden"]
end

Highlighter.highlight({
textObject = textObject,
src = data.Text,
forceUpdate = true,
lexer = data.Lexer,
})
end
end

function Highlighter.highlight(props: HighlightProps)
-- Gather props
local textObject = props.textObject
Expand All @@ -78,15 +62,14 @@ function Highlighter.highlight(props: HighlightProps)

-- Avoid updating when unnecessary
local data = LastData[textObject]
if not data then
if data == nil then
data = {
Text = "",
Lexer = lexer,
Lines = {},
}
LastData[textObject] = data
end
if props.forceUpdate ~= true and data.Text == src then
elseif props.forceUpdate ~= true and data.Text == src then
return
end

Expand Down Expand Up @@ -122,7 +105,7 @@ function Highlighter.highlight(props: HighlightProps)
textBounds = textObject.TextBounds
end

local textHeight = textBounds.Y / numLines
local textHeight = textBounds.Y / numLines * textObject.LineHeight

local lineLabels = LastData[textObject].Lines
for i = 1, math.max(numLines, #lineLabels) do
Expand Down Expand Up @@ -222,6 +205,15 @@ function Highlighter.highlight(props: HighlightProps)
})
end)
)
table.insert(
connections,
textObject:GetPropertyChangedSignal("Text"):Connect(function()
Highlighter.highlight({
textObject = textObject,
lexer = lexer,
})
end)
)
table.insert(
connections,
textObject:GetPropertyChangedSignal("AbsoluteSize"):Connect(function()
Expand All @@ -236,6 +228,22 @@ function Highlighter.highlight(props: HighlightProps)
return cleanup
end

function Highlighter.refresh(): ()
-- Rehighlight existing labels using latest colors
for textObject, data in pairs(LastData) do
for _, lineLabel in ipairs(data.Lines) do
lineLabel.TextColor3 = TokenColors["iden"]
end

Highlighter.highlight({
textObject = textObject,
src = data.Text,
forceUpdate = true,
lexer = data.Lexer,
})
end
end

function Highlighter.setTokenColors(colors: HighlighterColors)
for token, color in colors do
TokenColors[token] = color
Expand Down
2 changes: 1 addition & 1 deletion wally.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "boatbomber/highlighter"
description = "RichText highlighting Lua code with a pure Lua lexer"
version = "0.5.1"
version = "0.5.2"
license = "MIT"
authors = ["boatbomber (https://boatbomber.com)"]
registry = "https://github.com/upliftgames/wally-index"
Expand Down

0 comments on commit 50c580d

Please sign in to comment.