diff --git a/README.md b/README.md index f3eba26..d5e5bff 100644 --- a/README.md +++ b/README.md @@ -5,18 +5,61 @@ Syntax-highlighting text editor widget for imgui-luau (Roblox / LÖVE). Luau port of [TextEditor](https://github.com/goossens/ImGuiColorTextEdit) for [imgui-luau](https://git.bug.tools/bug/imgui-luau) ([GitHub](https://github.com/4DBug/imgui-luau)). Mirrors: [git.bug.tools](https://git.bug.tools/bug/imguicolortextedit-luau) / [GitHub](https://github.com/4DBug/imguicolortextedit-luau). -**Status: work in progress.** +**Status: work in progress** (core editor usable, see below). + +## Usage ```lua local G = require(ReplicatedStorage.ImGui) local TextEditor = require(ReplicatedStorage.TextEditor)(G) + +local editor = TextEditor.new() +editor:SetLanguage(TextEditor.Language.Lua()) +editor:SetText("local x = 1\nprint(x)") + +-- every frame, between ImGui.NewFrame() and ImGui.Render(): +ImGui.Begin("Editor") +editor:Render("TextEditor") -- Render(title, size?, childFlags?, windowFlags?) +ImGui.End() ``` +Full demo (menu bar, status line, palettes): `examples/roblox/main.client.luau`. + +**Positions are 0-based**, exactly like upstream: `TextEditor.DocPos.new(line, index)`, line numbers and +cursor numbers all start at 0 (internally the document is a 1-based Lua array). + +## Public API + +- Construction / config: `TextEditor.new`, `Set/GetTabSize`, `SetInsertSpacesOnTabs`, `Set/IsReadOnlyEnabled`, + `SetAutoIndentEnabled`, `SetOverwriteEnabled`/`IsOverwriteEnabled`, `SetShowWhitespacesEnabled`, `SetShowSpacesEnabled`, + `SetShowTabsEnabled`, `SetShowLineNumbersEnabled`, `SetShowCurrentLineHighlightEnabled`, `SetShowMatchingBrackets`, + `SetCompletePairedGlyphs`, `SetLineSpacing`, `SetCaretsVisible`, `SetLineNumberLeftMargin`, `SetTextLeftMargin` +- Palette: `SetPalette`/`GetPalette`, `TextEditor.GetDarkPalette`, `GetLightPalette`, `Set/GetDefaultPalette` +- Language: `SetLanguage`/`GetLanguage`/`HasLanguage`/`GetLanguageName`, `SetLanguageChangeCallback`, + `TextEditor.Language.C()`, `.Cpp()`, `.Lua()` +- Text: `SetText`, `GetText`, `GetSectionText`, `GetCursorText`, `GetLineText`, `GetSelectedText`, + `ReplaceSectionText`, `ReplaceTextInCurrentCursor`, `ReplaceTextInAllCursors`, `ClearText`, `IsEmpty`, `GetLineCount` +- Undo: `Undo`, `Redo`, `CanUndo`, `CanRedo`, `GetUndoIndex`, `SetTransactionCallback`/`ClearTransactionCallback` +- Selection / cursors: `SelectAll`, `SelectLine`, `SelectLines`, `SelectRegion`, `SelectToBrackets`, `SetCursor`, + `AddCursor`, `ClearCursors`, `GetNumberOfCursors`, `Get{Main,Current}CursorPosition`, `Get{Main,Current}CursorSelection`, + `AnyCursorHasSelection`, `AllCursorsHaveSelection`, `FindWordStart`/`FindWordEnd` +- Clipboard: `Cut`, `Copy`, `Paste` +- Rendering / view: `Render`, `SetFocus`, `ScrollToLine`, `GetFirst/LastVisibleRow/Column`, `GetGlyphColor` +- Markers / menus: `AddMarker`, `ClearMarkers`, `HasMarkers`, `SetLineNumberContextMenuCallback`, `SetTextContextMenuCallback` + +## Port status + +Ported: document model, multi-cursor editing, undo/redo transactions, keyboard/mouse input, clipboard, +find/replace, bracket matching, markers, palettes, colorizer, C / C++ / Lua languages. + +`[Port] TODO`: word wrap, line folding, mini-map, autocomplete, squiggles/decorations, other languages +(C#, AngelScript, Python, GLSL, HLSL, JSON, Markdown, SQL), TextDiff, LSP bridge. + ## Development ```sh git clone --recursive https://git.bug.tools/bug/imguicolortextedit-luau.git -bash tools/test.sh # tests/t_*.luau in the core's Roblox mock +bash tools/test.sh # tests/t_*.luau + the example in the core's Roblox mock ``` Upstream source (pinned) is in `tools/upstream/`; port-specific comments are tagged `[Port]`. diff --git a/examples/roblox/main.client.luau b/examples/roblox/main.client.luau index 9b7dfc2..bba962e 100644 --- a/examples/roblox/main.client.luau +++ b/examples/roblox/main.client.luau @@ -1,15 +1,93 @@ +-- Text editor demo, modelled on the classic ImGuiColorTextEdit example. local RS = game:GetService("ReplicatedStorage") local RunService = game:GetService("RunService") local G = require(RS:WaitForChild("ImGui")) local TextEditor = require(RS:WaitForChild("TextEditor"))(G) local ImGui = G.ImGui +local SampleCode = [==[ +-- Sample Lua code +local Players = game:GetService("Players") + +local function Greet(name: string): string + return "Hello, " .. name .. "!" +end + +for i = 1, 10 do + if i % 2 == 0 then + print(Greet("Player" .. i), 3.14 * i, 0xFF) + end +end + +--[[ block + comment ]] +local t = { a = 1, b = "two", [3] = true, nil } +]==] + +local Editor = TextEditor.new() +Editor:SetLanguage(TextEditor.Language.Lua()) +Editor:SetText(SampleCode) +local SavedUndoIndex = Editor:GetUndoIndex() + +local function RenderDemo() + ImGui.SetNextWindowSize(G.ImVec2(800, 600), G.ImGuiCond_FirstUseEver) + ImGui.Begin("Text Editor Demo", nil, G.ImGuiWindowFlags_MenuBar) + + if ImGui.BeginMenuBar() then + if ImGui.BeginMenu("File") then + if ImGui.MenuItem("Save") then + SavedUndoIndex = Editor:GetUndoIndex() -- stub: nowhere to save to + end + if ImGui.MenuItem("Quit", "Alt-F4") then + -- stub + end + ImGui.EndMenu() + end + if ImGui.BeginMenu("Edit") then + local ReadOnly = Editor:IsReadOnlyEnabled() + if ImGui.MenuItem("Read-only mode", nil, ReadOnly) then + Editor:SetReadOnlyEnabled(not ReadOnly) + end + ImGui.Separator() + if ImGui.MenuItem("Undo", "Ctrl-Z", nil, Editor:CanUndo()) then Editor:Undo() end + if ImGui.MenuItem("Redo", "Ctrl-Y", nil, Editor:CanRedo()) then Editor:Redo() end + ImGui.Separator() + local HasSelection = Editor:AnyCursorHasSelection() + if ImGui.MenuItem("Copy", "Ctrl-C", nil, HasSelection) then Editor:Copy() end + if ImGui.MenuItem("Cut", "Ctrl-X", nil, not ReadOnly and HasSelection) then Editor:Cut() end + if ImGui.MenuItem("Delete", "Del", nil, not ReadOnly and HasSelection) then + Editor:ReplaceTextInAllCursors("") + end + if ImGui.MenuItem("Paste", "Ctrl-V", nil, not ReadOnly) then Editor:Paste() end + ImGui.Separator() + if ImGui.MenuItem("Select all", "Ctrl-A") then Editor:SelectAll() end + ImGui.EndMenu() + end + if ImGui.BeginMenu("View") then + if ImGui.MenuItem("Dark palette") then Editor:SetPalette(TextEditor.GetDarkPalette()) end + if ImGui.MenuItem("Light palette") then Editor:SetPalette(TextEditor.GetLightPalette()) end + ImGui.EndMenu() + end + ImGui.EndMenuBar() + end + + local Pos = Editor:GetMainCursorPosition() + ImGui.Text(string.format("%d/%d | %d lines | %s | %s | %s", + Pos.line + 1, Pos.index + 1, Editor:GetLineCount(), + Editor:IsOverwriteEnabled() and "Ovr" or "Ins", + Editor:GetUndoIndex() ~= SavedUndoIndex and "*" or " ", + Editor:GetLanguageName())) + + Editor:Render("TextEditor") + ImGui.End() +end + ImGui.CreateContext() -G.ImGui_ImplRoblox.Init(game:GetService("Players").LocalPlayer:WaitForChild("PlayerGui")) +G.ImGui_ImplRoblox.Init() -- defaults to LocalPlayer.PlayerGui RunService.RenderStepped:Connect(function() G.ImGui_ImplRoblox.NewFrame() ImGui.NewFrame() - -- TextEditor example goes here + RenderDemo() ImGui.Render() G.ImGui_ImplRoblox.RenderDrawData(ImGui.GetDrawData()) end) diff --git a/tools/test.sh b/tools/test.sh index 07a975b..f1caa75 100755 --- a/tools/test.sh +++ b/tools/test.sh @@ -6,4 +6,8 @@ for t in tests/t_*.luau; do out=$(python3 tools/core/tools/test/run.py --module TextEditor=TextEditor.lua --main "$t" --quiet --roblox 2>&1) if echo "$out" | grep -q "^OK"; then echo "PASS $t"; else echo "FAIL $t"; echo "$out" | tail -20; fail=1; fi done +# example LocalScript: 400 frames of random input (mock drives Stepped, not RenderStepped) +ex=$(mktemp --suffix=.luau); sed 's/RenderStepped/Stepped/' examples/roblox/main.client.luau > "$ex" +out=$(python3 tools/core/tools/test/run.py --module TextEditor=TextEditor.lua --main "$ex" --roblox 2>&1); rm -f "$ex" +if echo "$out" | grep -q "^avg frame"; then echo "PASS examples/roblox/main.client.luau"; else echo "FAIL examples/roblox/main.client.luau"; echo "$out" | tail -20; fail=1; fi exit $fail