Skip to content

Commit

Permalink
Add buildRichTextLines
Browse files Browse the repository at this point in the history
  • Loading branch information
boatbomber committed Nov 11, 2024
1 parent 9d5330f commit d7473a8
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 18 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Wally:

```toml
[dependencies]
Highlighter = "boatbomber/highlighter@0.8.3"
Highlighter = "boatbomber/highlighter@0.9.0"
```

Roblox Model:
Expand All @@ -28,6 +28,12 @@ Highlights the given textObject with the given props and returns a cleanup funct
Highlighting will automatically update when needed, so the cleanup function will disconnect
those connections and remove all labels.

```Lua
function Highlighter.buildRichTextLines(props: types.BuildRichTextLinesProps): { string }
```

Builds rich text lines from the given props. Useful for building rich text highlight strings for other UI objects.

```Lua
function Highlighter.refresh(): ()
```
Expand Down Expand Up @@ -90,6 +96,12 @@ type HighlightProps = {
customLang: { [string]: string }?,
}

type BuildRichTextLinesProps = {
src: string,
lexer: Lexer?,
customLang: { [string]: string }?,
}

type Lexer = {
scan: (src: string) -> () -> (string, string),
navigator: () -> any,
Expand Down
6 changes: 3 additions & 3 deletions aftman.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

# To add a new tool, add an entry to this table.
[tools]
rojo = "rojo-rbx/rojo@7.3.0"
rojo = "rojo-rbx/rojo@7.4.4"
run-in-roblox = "rojo-rbx/[email protected]"
wally = "upliftgames/[email protected]"
selene = "Kampfkarren/selene@0.25.0"
stylua = "JohnnyMorganz/stylua@0.18.1"
selene = "Kampfkarren/selene@0.27.1"
stylua = "JohnnyMorganz/stylua@0.20.0"
26 changes: 13 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"name": "@boatbomber/highlighter",
"version": "0.8.3",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/boatbomber/Highlighter.git"
},
"contributors": [
"boatbomber"
],
"bugs": {
"url": "https://github.com/boatbomber/Highlighter/issues"
}
"name": "@boatbomber/highlighter",
"version": "0.9.0",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/boatbomber/Highlighter.git"
},
"contributors": [
"boatbomber"
],
"bugs": {
"url": "https://github.com/boatbomber/Highlighter/issues"
}
}
49 changes: 49 additions & 0 deletions src/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,55 @@ function Highlighter._populateLabels(props: types.HighlightProps)
end
end

--[[
Builds rich text lines from the given source code.
Useful for cases where you want to render the labels yourself for something.
]]
function Highlighter.buildRichTextLines(props: types.BuildRichTextLinesProps): { string }
-- Gather props
local src = utility.convertTabsToSpaces(utility.removeControlChars(props.src))
local lexer = props.lexer or Highlighter.defaultLexer
local customLang = props.customLang
local idenColor = theme.getColor("iden")

local richTextLines = table.create(select(2, string.gsub(src, "\n", "\n")) + 1)
local richTextBuffer, bufferIndex = table.create(5), 0
local lineNumber = 1

for token: types.TokenName, content: string in lexer.scan(src) do
local Color = if customLang and customLang[content]
then theme.getColor("custom")
else theme.getColor(token) or idenColor

local tokenLines = string.split(utility.sanitizeRichText(content), "\n")

for l, tokenLine in tokenLines do
-- If multiline token, then set line & move to next
if l > 1 then
-- Set line
richTextLines[lineNumber] = table.concat(richTextBuffer)
-- Move to next line
lineNumber += 1
bufferIndex = 0
table.clear(richTextBuffer)
end

bufferIndex += 1
-- Only add RichText tags when the characters are non-whitespace
if string.find(tokenLine, "[%S%C]") then
richTextBuffer[bufferIndex] = theme.getColoredRichText(Color, tokenLine)
else
richTextBuffer[bufferIndex] = tokenLine
end
end
end

-- Set final line
richTextLines[lineNumber] = table.concat(richTextBuffer)

return richTextLines
end

--[[
Highlights the given textObject with the given props and returns a cleanup function.
Highlighting will automatically update when needed, so the cleanup function will disconnect
Expand Down
6 changes: 6 additions & 0 deletions src/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ export type HighlightProps = {
customLang: { [string]: string }?,
}

export type BuildRichTextLinesProps = {
src: string,
lexer: Lexer?,
customLang: { [string]: string }?,
}

export type Lexer = {
scan: (src: string) -> () -> (string, string),
navigator: () -> any,
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.8.3"
version = "0.9.0"
license = "MIT"
authors = ["boatbomber (https://boatbomber.com)"]
registry = "https://github.com/upliftgames/wally-index"
Expand Down

0 comments on commit d7473a8

Please sign in to comment.