From d7473a87807f30cdb5ee7268a560b5c33f22da67 Mon Sep 17 00:00:00 2001 From: boatbomber Date: Sun, 10 Nov 2024 17:30:21 -0800 Subject: [PATCH] Add buildRichTextLines --- README.md | 14 +++++++++++++- aftman.toml | 6 +++--- package.json | 26 +++++++++++++------------- src/init.lua | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ src/types.lua | 6 ++++++ wally.toml | 2 +- 6 files changed, 85 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 6c73ecf..0a452b2 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Wally: ```toml [dependencies] -Highlighter = "boatbomber/highlighter@0.8.3" +Highlighter = "boatbomber/highlighter@0.9.0" ``` Roblox Model: @@ -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(): () ``` @@ -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, diff --git a/aftman.toml b/aftman.toml index 2896017..b0a8cc8 100644 --- a/aftman.toml +++ b/aftman.toml @@ -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/run-in-roblox@0.3.0" wally = "upliftgames/wally@0.3.2" -selene = "Kampfkarren/selene@0.25.0" -stylua = "JohnnyMorganz/stylua@0.18.1" +selene = "Kampfkarren/selene@0.27.1" +stylua = "JohnnyMorganz/stylua@0.20.0" diff --git a/package.json b/package.json index 88c0c9a..24b6402 100644 --- a/package.json +++ b/package.json @@ -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" + } } diff --git a/src/init.lua b/src/init.lua index d858aaf..12a1168 100644 --- a/src/init.lua +++ b/src/init.lua @@ -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 diff --git a/src/types.lua b/src/types.lua index 8d6fef0..0e84637 100644 --- a/src/types.lua +++ b/src/types.lua @@ -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, diff --git a/wally.toml b/wally.toml index 9803f8f..95c2e5a 100644 --- a/wally.toml +++ b/wally.toml @@ -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"