Performance: widest line cached (recomputed only when the document or tab size changed, from per-line column counts cleared by insertText/deleteText), Bracketeer:update tests the opener/closer tables before the colour, the last getSectionText result kept until the document version or range changes (the selected text handed to the IME / text proxy every frame), scratch glyph position in renderText, @native on the per-glyph loops (glyphColumns, lineColumns, maxLineColumns, getGlyphWidth, Bracketeer:update, isBracketCandidate, renderText, docPos2VisPos, renderMatchingBracketLines); tests/t_caches.luau checks the caches against fresh computations (3000-line Lua file, headless --roblox, lib med: idle 12.4 -> 1.4 ms, typing every frame 22.7 -> 6.3 ms; draw data unchanged incl. scroll/typing/selection/drag, LÖVE draw data unchanged)
This commit is contained in:
1 parent
a3efb2bf46
commit
0e91c4fee3
3 files changed
+171
-20
No files matched your search
+60
-10
@@ -140,6 +140,7 @@ return function(G)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- [Port] no east asian width tables: every glyph is one column wide
|
-- [Port] no east asian width tables: every glyph is one column wide
|
||||||
|
@native
|
||||||
function CodePoint.getGlyphWidth(codepoint)
|
function CodePoint.getGlyphWidth(codepoint)
|
||||||
return 1
|
return 1
|
||||||
end
|
end
|
||||||
@@ -159,7 +160,7 @@ return function(G)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function Document.new()
|
function Document.new()
|
||||||
local self = setmetatable({ lines = {}, updated = false }, Document)
|
local self = setmetatable({ lines = {}, updated = false, version = 0 }, Document)
|
||||||
self:appendLine()
|
self:appendLine()
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
@@ -221,6 +222,7 @@ return function(G)
|
|||||||
|
|
||||||
-- [Port] updateIndents skipped (used by rendering only)
|
-- [Port] updateIndents skipped (used by rendering only)
|
||||||
self.updated = true
|
self.updated = true
|
||||||
|
self.version = self.version + 1
|
||||||
end
|
end
|
||||||
|
|
||||||
-- insert/delete part of the document's text (strings are UTF-8 encoded)
|
-- insert/delete part of the document's text (strings are UTF-8 encoded)
|
||||||
@@ -262,10 +264,13 @@ return function(G)
|
|||||||
|
|
||||||
-- mark affected lines as changed
|
-- mark affected lines as changed
|
||||||
for j = start.line, e.line do
|
for j = start.line, e.line do
|
||||||
self:at(j).needsColorizing = true
|
local changed = self:at(j)
|
||||||
|
changed.needsColorizing = true
|
||||||
|
changed.columns = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
self.updated = true
|
self.updated = true
|
||||||
|
self.version = self.version + 1
|
||||||
return e
|
return e
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -296,7 +301,9 @@ return function(G)
|
|||||||
|
|
||||||
-- mark line and document as changed
|
-- mark line and document as changed
|
||||||
startLine.needsColorizing = true
|
startLine.needsColorizing = true
|
||||||
|
startLine.columns = nil
|
||||||
self.updated = true
|
self.updated = true
|
||||||
|
self.version = self.version + 1
|
||||||
end
|
end
|
||||||
|
|
||||||
-- get document text as UTF-8
|
-- get document text as UTF-8
|
||||||
@@ -316,7 +323,20 @@ return function(G)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- get part of document text (returned strings are UTF-8 encoded)
|
-- get part of document text (returned strings are UTF-8 encoded)
|
||||||
|
-- [Port] the last result is kept until the document changes (version) or another section is asked for: the editor
|
||||||
|
-- hands the selected text to the platform (IME / Roblox text proxy) every frame
|
||||||
function Document:getSectionText(start, e)
|
function Document:getSectionText(start, e)
|
||||||
|
local memo = self.sectionMemo
|
||||||
|
if memo and memo[1] == self.version and memo[2] == start.line and memo[3] == start.index and memo[4] == e.line and memo[5] == e.index then
|
||||||
|
return memo[6]
|
||||||
|
end
|
||||||
|
local text = self:getSectionTextUncached(start, e)
|
||||||
|
if not memo then memo = {}; self.sectionMemo = memo end
|
||||||
|
memo[1], memo[2], memo[3], memo[4], memo[5], memo[6] = self.version, start.line, start.index, e.line, e.index, text
|
||||||
|
return text
|
||||||
|
end
|
||||||
|
|
||||||
|
function Document:getSectionTextUncached(start, e)
|
||||||
local parts = {}
|
local parts = {}
|
||||||
local lineNo = start.line
|
local lineNo = start.line
|
||||||
local index = start.index
|
local index = start.index
|
||||||
@@ -1623,6 +1643,7 @@ return function(G)
|
|||||||
local openers = { [40] = 41, [123] = 125, [91] = 93 } -- ( { [
|
local openers = { [40] = 41, [123] = 125, [91] = 93 } -- ( { [
|
||||||
local closers = { [41] = 40, [125] = 123, [93] = 91 } -- ) } ]
|
local closers = { [41] = 40, [125] = 123, [93] = 91 } -- ) } ]
|
||||||
|
|
||||||
|
@native
|
||||||
local function isBracketCandidate(glyph)
|
local function isBracketCandidate(glyph)
|
||||||
local color = glyph.color
|
local color = glyph.color
|
||||||
return color == Color.punctuation or color == Color.matchingBracketLevel1 or color == Color.matchingBracketLevel2
|
return color == Color.punctuation or color == Color.matchingBracketLevel1 or color == Color.matchingBracketLevel2
|
||||||
@@ -1642,6 +1663,7 @@ return function(G)
|
|||||||
local bracketColors = { Color.matchingBracketLevel1, Color.matchingBracketLevel2, Color.matchingBracketLevel3 }
|
local bracketColors = { Color.matchingBracketLevel1, Color.matchingBracketLevel2, Color.matchingBracketLevel3 }
|
||||||
|
|
||||||
-- [Port] invisible (indentation based) blocks are not generated yet (no language uses indentationForBlocks)
|
-- [Port] invisible (indentation based) blocks are not generated yet (no language uses indentationForBlocks)
|
||||||
|
@native
|
||||||
function Bracketeer:update(config, document)
|
function Bracketeer:update(config, document)
|
||||||
-- see if the configuration changed
|
-- see if the configuration changed
|
||||||
local configChanged = self.showMatchingBrackets ~= config.showMatchingBrackets or self.language ~= config.language
|
local configChanged = self.showMatchingBrackets ~= config.showMatchingBrackets or self.language ~= config.language
|
||||||
@@ -1669,10 +1691,12 @@ return function(G)
|
|||||||
local level = 0
|
local level = 0
|
||||||
|
|
||||||
-- process all the glyphs
|
-- process all the glyphs
|
||||||
|
-- [Port] the (disjoint) opener/closer lookups come before the colour test: most glyphs are neither
|
||||||
for l, line in ipairs(document.lines) do
|
for l, line in ipairs(document.lines) do
|
||||||
for i, glyph in ipairs(line) do
|
for i, glyph in ipairs(line) do
|
||||||
|
local codepoint = glyph.codepoint
|
||||||
-- handle a "bracket opener" that is not in a comment, string or preprocessor statement
|
-- handle a "bracket opener" that is not in a comment, string or preprocessor statement
|
||||||
if isBracketCandidate(glyph) and openers[glyph.codepoint] then
|
if openers[codepoint] and isBracketCandidate(glyph) then
|
||||||
-- start a new level
|
-- start a new level
|
||||||
list[#list + 1] = { startChar = glyph.codepoint, start = newDocPos(l - 1, i - 1), endChar = 0, ["end"] = newDocPos(0, 0), level = level, visible = true }
|
list[#list + 1] = { startChar = glyph.codepoint, start = newDocPos(l - 1, i - 1), endChar = 0, ["end"] = newDocPos(0, 0), level = level, visible = true }
|
||||||
levels[#levels + 1] = #list
|
levels[#levels + 1] = #list
|
||||||
@@ -1680,7 +1704,7 @@ return function(G)
|
|||||||
level = level + 1
|
level = level + 1
|
||||||
|
|
||||||
-- handle a "bracket closer" that is not in a comment, string or preprocessor statement
|
-- handle a "bracket closer" that is not in a comment, string or preprocessor statement
|
||||||
elseif isBracketCandidate(glyph) and closers[glyph.codepoint] then
|
elseif closers[codepoint] and isBracketCandidate(glyph) then
|
||||||
if #levels > 0 then
|
if #levels > 0 then
|
||||||
local lastBracket = list[table.remove(levels)]
|
local lastBracket = list[table.remove(levels)]
|
||||||
level = level - 1
|
level = level - 1
|
||||||
@@ -2017,12 +2041,14 @@ return function(G)
|
|||||||
self.bracketeer:resetUpdated()
|
self.bracketeer:resetUpdated()
|
||||||
|
|
||||||
-- get "new" total editor size in pixels
|
-- get "new" total editor size in pixels
|
||||||
-- ponytail: column count rescans every line each call, cache per line if huge documents get slow
|
-- [Port] the widest line is looked for only when the document (or tab size) changed (every edit goes through
|
||||||
local columns = 0
|
-- Document:setUtf8Text/insertText/deleteText, which set document.updated), using the column count cached on
|
||||||
|
-- each line (cleared by insertText/deleteText on the lines they change; new lines have none)
|
||||||
local tabSize = self.config.tabSize
|
local tabSize = self.config.tabSize
|
||||||
for _, line in ipairs(self.document.lines) do
|
local columns = self.maxColumns
|
||||||
local c = self.lineColumns(line, tabSize)
|
if documentChanged or columns == nil or self.maxColumnsDocument ~= self.document or self.maxColumnsTabSize ~= tabSize then
|
||||||
if c > columns then columns = c end
|
columns = self.maxLineColumns(self.document.lines, tabSize)
|
||||||
|
self.maxColumns, self.maxColumnsDocument, self.maxColumnsTabSize = columns, self.document, tabSize
|
||||||
end
|
end
|
||||||
local width = self.textLeftOffset + columns * self.glyphSize.x + self.cursorWidth
|
local width = self.textLeftOffset + columns * self.glyphSize.x + self.cursorWidth
|
||||||
local height = self.document:size() * self.glyphSize.y
|
local height = self.document:size() * self.glyphSize.y
|
||||||
@@ -2192,6 +2218,7 @@ return function(G)
|
|||||||
|
|
||||||
TextEditor.Scroll = { alignTop = 0, alignMiddle = 1, alignBottom = 2 }
|
TextEditor.Scroll = { alignTop = 0, alignMiddle = 1, alignBottom = 2 }
|
||||||
|
|
||||||
|
@native
|
||||||
local function glyphColumns(codepoint, column, tabSize)
|
local function glyphColumns(codepoint, column, tabSize)
|
||||||
if codepoint == 9 then
|
if codepoint == 9 then
|
||||||
return tabSize - column % tabSize
|
return tabSize - column % tabSize
|
||||||
@@ -2199,6 +2226,7 @@ return function(G)
|
|||||||
return CodePoint.getGlyphWidth(codepoint)
|
return CodePoint.getGlyphWidth(codepoint)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@native
|
||||||
function TextEditor.lineColumns(line, tabSize)
|
function TextEditor.lineColumns(line, tabSize)
|
||||||
local column = 0
|
local column = 0
|
||||||
for i = 1, #line do
|
for i = 1, #line do
|
||||||
@@ -2207,6 +2235,23 @@ return function(G)
|
|||||||
return column
|
return column
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- [Port] widest line in columns, through the per-line cache (line.columns for line.columnsTabSize)
|
||||||
|
@native
|
||||||
|
function TextEditor.maxLineColumns(lines, tabSize)
|
||||||
|
local lineColumns = TextEditor.lineColumns
|
||||||
|
local columns = 0
|
||||||
|
for l = 1, #lines do
|
||||||
|
local line = lines[l]
|
||||||
|
local c = line.columns
|
||||||
|
if c == nil or line.columnsTabSize ~= tabSize then
|
||||||
|
c = lineColumns(line, tabSize)
|
||||||
|
line.columns, line.columnsTabSize = c, tabSize
|
||||||
|
end
|
||||||
|
if c > columns then columns = c end
|
||||||
|
end
|
||||||
|
return columns
|
||||||
|
end
|
||||||
|
|
||||||
local function isPairOpener(ch) return ch == 123 or ch == 91 or ch == 40 or ch == 39 or ch == 34 end
|
local function isPairOpener(ch) return ch == 123 or ch == 91 or ch == 40 or ch == 39 or ch == 34 end
|
||||||
local function toPairCloser(ch)
|
local function toPairCloser(ch)
|
||||||
if ch == 123 then return 125 elseif ch == 91 then return 93 elseif ch == 40 then return 41 end
|
if ch == 123 then return 125 elseif ch == 91 then return 93 elseif ch == 40 then return 41 end
|
||||||
@@ -2214,6 +2259,7 @@ return function(G)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- coordinate conversions
|
-- coordinate conversions
|
||||||
|
@native
|
||||||
function TextEditor:docPos2VisPos(pos)
|
function TextEditor:docPos2VisPos(pos)
|
||||||
local line = self.document:at(pos.line)
|
local line = self.document:at(pos.line)
|
||||||
local tabSize = self.config.tabSize
|
local tabSize = self.config.tabSize
|
||||||
@@ -2595,6 +2641,7 @@ return function(G)
|
|||||||
-- TextEditor::renderMatchingBracketLines
|
-- TextEditor::renderMatchingBracketLines
|
||||||
--
|
--
|
||||||
|
|
||||||
|
@native
|
||||||
function TextEditor:renderMatchingBracketLines()
|
function TextEditor:renderMatchingBracketLines()
|
||||||
if self.config.showMatchingBrackets and #self.bracketeer.list > 0 then
|
if self.config.showMatchingBrackets and #self.bracketeer.list > 0 then
|
||||||
local active = self.bracketeer:getEnclosingBrackets(self.cursors:getMain():getInteractiveEnd())
|
local active = self.bracketeer:getEnclosingBrackets(self.cursors:getMain():getInteractiveEnd())
|
||||||
@@ -2621,6 +2668,7 @@ return function(G)
|
|||||||
-- TextEditor::renderText
|
-- TextEditor::renderText
|
||||||
--
|
--
|
||||||
|
|
||||||
|
@native
|
||||||
function TextEditor:renderText()
|
function TextEditor:renderText()
|
||||||
local glyphSize = self.glyphSize
|
local glyphSize = self.glyphSize
|
||||||
local tabSize = self.config.tabSize
|
local tabSize = self.config.tabSize
|
||||||
@@ -2633,6 +2681,7 @@ return function(G)
|
|||||||
local rowY = self.cursorScreenPos.y + self.firstVisibleRow * glyphSize.y
|
local rowY = self.cursorScreenPos.y + self.firstVisibleRow * glyphSize.y
|
||||||
local firstRenderableColumn = math.floor(self.firstVisibleColumn / tabSize) * tabSize
|
local firstRenderableColumn = math.floor(self.firstVisibleColumn / tabSize) * tabSize
|
||||||
local lastVisibleColumn = self.lastVisibleColumn
|
local lastVisibleColumn = self.lastVisibleColumn
|
||||||
|
local charPos = ImVec2(0, 0) -- [Port] scratch glyph position (RenderChar only reads it)
|
||||||
|
|
||||||
-- only process all visible rows
|
-- only process all visible rows
|
||||||
for i = self.firstVisibleRow, self.lastVisibleRow do
|
for i = self.firstVisibleRow, self.lastVisibleRow do
|
||||||
@@ -2667,7 +2716,8 @@ return function(G)
|
|||||||
|
|
||||||
-- handle regular glyphs
|
-- handle regular glyphs
|
||||||
elseif column >= firstRenderableColumn then
|
elseif column >= firstRenderableColumn then
|
||||||
font:RenderChar(drawList, fontSize, ImVec2(glyphX, rowY), palette[glyph.color], codepoint)
|
charPos.x, charPos.y = glyphX, rowY
|
||||||
|
font:RenderChar(drawList, fontSize, charPos, palette[glyph.color], codepoint)
|
||||||
end
|
end
|
||||||
|
|
||||||
column = column + width
|
column = column + width
|
||||||
|
|||||||
Generated
+55
-10
@@ -142,6 +142,7 @@ return function(G)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- [Port] no east asian width tables: every glyph is one column wide
|
-- [Port] no east asian width tables: every glyph is one column wide
|
||||||
|
|
||||||
function CodePoint.getGlyphWidth(codepoint)
|
function CodePoint.getGlyphWidth(codepoint)
|
||||||
return 1
|
return 1
|
||||||
end
|
end
|
||||||
@@ -161,7 +162,7 @@ return function(G)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function Document.new()
|
function Document.new()
|
||||||
local self = setmetatable({ lines = {}, updated = false }, Document)
|
local self = setmetatable({ lines = {}, updated = false, version = 0 }, Document)
|
||||||
self:appendLine()
|
self:appendLine()
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
@@ -223,6 +224,7 @@ return function(G)
|
|||||||
|
|
||||||
-- [Port] updateIndents skipped (used by rendering only)
|
-- [Port] updateIndents skipped (used by rendering only)
|
||||||
self.updated = true
|
self.updated = true
|
||||||
|
self.version = self.version + 1
|
||||||
end
|
end
|
||||||
|
|
||||||
-- insert/delete part of the document's text (strings are UTF-8 encoded)
|
-- insert/delete part of the document's text (strings are UTF-8 encoded)
|
||||||
@@ -264,10 +266,13 @@ return function(G)
|
|||||||
|
|
||||||
-- mark affected lines as changed
|
-- mark affected lines as changed
|
||||||
for j = start.line, e.line do
|
for j = start.line, e.line do
|
||||||
self:at(j).needsColorizing = true
|
local changed = self:at(j)
|
||||||
|
changed.needsColorizing = true
|
||||||
|
changed.columns = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
self.updated = true
|
self.updated = true
|
||||||
|
self.version = self.version + 1
|
||||||
return e
|
return e
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -298,7 +303,9 @@ return function(G)
|
|||||||
|
|
||||||
-- mark line and document as changed
|
-- mark line and document as changed
|
||||||
startLine.needsColorizing = true
|
startLine.needsColorizing = true
|
||||||
|
startLine.columns = nil
|
||||||
self.updated = true
|
self.updated = true
|
||||||
|
self.version = self.version + 1
|
||||||
end
|
end
|
||||||
|
|
||||||
-- get document text as UTF-8
|
-- get document text as UTF-8
|
||||||
@@ -318,7 +325,20 @@ return function(G)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- get part of document text (returned strings are UTF-8 encoded)
|
-- get part of document text (returned strings are UTF-8 encoded)
|
||||||
|
-- [Port] the last result is kept until the document changes (version) or another section is asked for: the editor
|
||||||
|
-- hands the selected text to the platform (IME / Roblox text proxy) every frame
|
||||||
function Document:getSectionText(start, e)
|
function Document:getSectionText(start, e)
|
||||||
|
local memo = self.sectionMemo
|
||||||
|
if memo and memo[1] == self.version and memo[2] == start.line and memo[3] == start.index and memo[4] == e.line and memo[5] == e.index then
|
||||||
|
return memo[6]
|
||||||
|
end
|
||||||
|
local text = self:getSectionTextUncached(start, e)
|
||||||
|
if not memo then memo = {}; self.sectionMemo = memo end
|
||||||
|
memo[1], memo[2], memo[3], memo[4], memo[5], memo[6] = self.version, start.line, start.index, e.line, e.index, text
|
||||||
|
return text
|
||||||
|
end
|
||||||
|
|
||||||
|
function Document:getSectionTextUncached(start, e)
|
||||||
local parts = {}
|
local parts = {}
|
||||||
local lineNo = start.line
|
local lineNo = start.line
|
||||||
local index = start.index
|
local index = start.index
|
||||||
@@ -1644,6 +1664,7 @@ return function(G)
|
|||||||
local bracketColors = { Color.matchingBracketLevel1, Color.matchingBracketLevel2, Color.matchingBracketLevel3 }
|
local bracketColors = { Color.matchingBracketLevel1, Color.matchingBracketLevel2, Color.matchingBracketLevel3 }
|
||||||
|
|
||||||
-- [Port] invisible (indentation based) blocks are not generated yet (no language uses indentationForBlocks)
|
-- [Port] invisible (indentation based) blocks are not generated yet (no language uses indentationForBlocks)
|
||||||
|
|
||||||
function Bracketeer:update(config, document)
|
function Bracketeer:update(config, document)
|
||||||
-- see if the configuration changed
|
-- see if the configuration changed
|
||||||
local configChanged = self.showMatchingBrackets ~= config.showMatchingBrackets or self.language ~= config.language
|
local configChanged = self.showMatchingBrackets ~= config.showMatchingBrackets or self.language ~= config.language
|
||||||
@@ -1671,10 +1692,12 @@ return function(G)
|
|||||||
local level = 0
|
local level = 0
|
||||||
|
|
||||||
-- process all the glyphs
|
-- process all the glyphs
|
||||||
|
-- [Port] the (disjoint) opener/closer lookups come before the colour test: most glyphs are neither
|
||||||
for l, line in __iter(ipairs(document.lines)) do
|
for l, line in __iter(ipairs(document.lines)) do
|
||||||
for i, glyph in __iter(ipairs(line)) do
|
for i, glyph in __iter(ipairs(line)) do
|
||||||
|
local codepoint = glyph.codepoint
|
||||||
-- handle a "bracket opener" that is not in a comment, string or preprocessor statement
|
-- handle a "bracket opener" that is not in a comment, string or preprocessor statement
|
||||||
if isBracketCandidate(glyph) and openers[glyph.codepoint] then
|
if openers[codepoint] and isBracketCandidate(glyph) then
|
||||||
-- start a new level
|
-- start a new level
|
||||||
list[#list + 1] = { startChar = glyph.codepoint, start = newDocPos(l - 1, i - 1), endChar = 0, ["end"] = newDocPos(0, 0), level = level, visible = true }
|
list[#list + 1] = { startChar = glyph.codepoint, start = newDocPos(l - 1, i - 1), endChar = 0, ["end"] = newDocPos(0, 0), level = level, visible = true }
|
||||||
levels[#levels + 1] = #list
|
levels[#levels + 1] = #list
|
||||||
@@ -1682,7 +1705,7 @@ return function(G)
|
|||||||
level = level + 1
|
level = level + 1
|
||||||
|
|
||||||
-- handle a "bracket closer" that is not in a comment, string or preprocessor statement
|
-- handle a "bracket closer" that is not in a comment, string or preprocessor statement
|
||||||
elseif isBracketCandidate(glyph) and closers[glyph.codepoint] then
|
elseif closers[codepoint] and isBracketCandidate(glyph) then
|
||||||
if #levels > 0 then
|
if #levels > 0 then
|
||||||
local lastBracket = list[table.remove(levels)]
|
local lastBracket = list[table.remove(levels)]
|
||||||
level = level - 1
|
level = level - 1
|
||||||
@@ -2019,12 +2042,14 @@ return function(G)
|
|||||||
self.bracketeer:resetUpdated()
|
self.bracketeer:resetUpdated()
|
||||||
|
|
||||||
-- get "new" total editor size in pixels
|
-- get "new" total editor size in pixels
|
||||||
-- ponytail: column count rescans every line each call, cache per line if huge documents get slow
|
-- [Port] the widest line is looked for only when the document (or tab size) changed (every edit goes through
|
||||||
local columns = 0
|
-- Document:setUtf8Text/insertText/deleteText, which set document.updated), using the column count cached on
|
||||||
|
-- each line (cleared by insertText/deleteText on the lines they change; new lines have none)
|
||||||
local tabSize = self.config.tabSize
|
local tabSize = self.config.tabSize
|
||||||
for _, line in __iter(ipairs(self.document.lines)) do
|
local columns = self.maxColumns
|
||||||
local c = self.lineColumns(line, tabSize)
|
if documentChanged or columns == nil or self.maxColumnsDocument ~= self.document or self.maxColumnsTabSize ~= tabSize then
|
||||||
if c > columns then columns = c end
|
columns = self.maxLineColumns(self.document.lines, tabSize)
|
||||||
|
self.maxColumns, self.maxColumnsDocument, self.maxColumnsTabSize = columns, self.document, tabSize
|
||||||
end
|
end
|
||||||
local width = self.textLeftOffset + columns * self.glyphSize.x + self.cursorWidth
|
local width = self.textLeftOffset + columns * self.glyphSize.x + self.cursorWidth
|
||||||
local height = self.document:size() * self.glyphSize.y
|
local height = self.document:size() * self.glyphSize.y
|
||||||
@@ -2209,6 +2234,23 @@ return function(G)
|
|||||||
return column
|
return column
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- [Port] widest line in columns, through the per-line cache (line.columns for line.columnsTabSize)
|
||||||
|
|
||||||
|
function TextEditor.maxLineColumns(lines, tabSize)
|
||||||
|
local lineColumns = TextEditor.lineColumns
|
||||||
|
local columns = 0
|
||||||
|
for l = 1, #lines do
|
||||||
|
local line = lines[l]
|
||||||
|
local c = line.columns
|
||||||
|
if c == nil or line.columnsTabSize ~= tabSize then
|
||||||
|
c = lineColumns(line, tabSize)
|
||||||
|
line.columns, line.columnsTabSize = c, tabSize
|
||||||
|
end
|
||||||
|
if c > columns then columns = c end
|
||||||
|
end
|
||||||
|
return columns
|
||||||
|
end
|
||||||
|
|
||||||
local function isPairOpener(ch) return ch == 123 or ch == 91 or ch == 40 or ch == 39 or ch == 34 end
|
local function isPairOpener(ch) return ch == 123 or ch == 91 or ch == 40 or ch == 39 or ch == 34 end
|
||||||
local function toPairCloser(ch)
|
local function toPairCloser(ch)
|
||||||
if ch == 123 then return 125 elseif ch == 91 then return 93 elseif ch == 40 then return 41 end
|
if ch == 123 then return 125 elseif ch == 91 then return 93 elseif ch == 40 then return 41 end
|
||||||
@@ -2216,6 +2258,7 @@ return function(G)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- coordinate conversions
|
-- coordinate conversions
|
||||||
|
|
||||||
function TextEditor:docPos2VisPos(pos)
|
function TextEditor:docPos2VisPos(pos)
|
||||||
local line = self.document:at(pos.line)
|
local line = self.document:at(pos.line)
|
||||||
local tabSize = self.config.tabSize
|
local tabSize = self.config.tabSize
|
||||||
@@ -2635,6 +2678,7 @@ return function(G)
|
|||||||
local rowY = self.cursorScreenPos.y + self.firstVisibleRow * glyphSize.y
|
local rowY = self.cursorScreenPos.y + self.firstVisibleRow * glyphSize.y
|
||||||
local firstRenderableColumn = math.floor(self.firstVisibleColumn / tabSize) * tabSize
|
local firstRenderableColumn = math.floor(self.firstVisibleColumn / tabSize) * tabSize
|
||||||
local lastVisibleColumn = self.lastVisibleColumn
|
local lastVisibleColumn = self.lastVisibleColumn
|
||||||
|
local charPos = ImVec2(0, 0) -- [Port] scratch glyph position (RenderChar only reads it)
|
||||||
|
|
||||||
-- only process all visible rows
|
-- only process all visible rows
|
||||||
for i = self.firstVisibleRow, self.lastVisibleRow do
|
for i = self.firstVisibleRow, self.lastVisibleRow do
|
||||||
@@ -2669,7 +2713,8 @@ return function(G)
|
|||||||
|
|
||||||
-- handle regular glyphs
|
-- handle regular glyphs
|
||||||
elseif column >= firstRenderableColumn then
|
elseif column >= firstRenderableColumn then
|
||||||
font:RenderChar(drawList, fontSize, ImVec2(glyphX, rowY), palette[glyph.color], codepoint)
|
charPos.x, charPos.y = glyphX, rowY
|
||||||
|
font:RenderChar(drawList, fontSize, charPos, palette[glyph.color], codepoint)
|
||||||
end
|
end
|
||||||
|
|
||||||
column = column + width
|
column = column + width
|
||||||
|
|||||||
@@ -0,0 +1,56 @@
|
|||||||
|
-- Cached state stays equal to a fresh computation: widest line (per-line column cache) and the selected text memo,
|
||||||
|
-- through edits (single/multi-line inserts and deletes, tabs, undo/redo), tab size changes and SetText.
|
||||||
|
local RS = game:GetService("ReplicatedStorage")
|
||||||
|
local G = require(RS:WaitForChild("ImGui"))
|
||||||
|
local TextEditor = require(RS:WaitForChild("TextEditor"))(G)
|
||||||
|
local P = TextEditor.DocPos.new
|
||||||
|
G.ImGui.CreateContext()
|
||||||
|
|
||||||
|
local function eq(a, b, msg)
|
||||||
|
assert(a == b, (msg or "") .. ": expected " .. tostring(b) .. ", got " .. tostring(a))
|
||||||
|
end
|
||||||
|
|
||||||
|
local e = TextEditor.new()
|
||||||
|
local function check(msg)
|
||||||
|
e:updateState()
|
||||||
|
local columns = 0
|
||||||
|
for _, line in ipairs(e.document.lines) do columns = math.max(columns, TextEditor.lineColumns(line, e.config.tabSize)) end
|
||||||
|
eq(e.maxColumns, columns, msg .. " columns")
|
||||||
|
local c = e.cursors:getCurrent()
|
||||||
|
eq(e:GetSelectedText(), e.document:getSectionTextUncached(c:getSelectionStart(), c:getSelectionEnd()), msg .. " selected text")
|
||||||
|
end
|
||||||
|
|
||||||
|
e:SetText("short\na much longer line here\n\tx\nmid line")
|
||||||
|
check("set")
|
||||||
|
e:SelectRegion(P(1, 0), P(1, 6))
|
||||||
|
check("select")
|
||||||
|
e:ReplaceTextInAllCursors("")
|
||||||
|
check("delete in line")
|
||||||
|
e:SelectRegion(P(0, 2), P(2, 1))
|
||||||
|
check("select multi")
|
||||||
|
e:ReplaceTextInAllCursors("")
|
||||||
|
check("delete multi")
|
||||||
|
e:SetCursor(P(0, 1))
|
||||||
|
e:ReplaceTextInAllCursors("0123456789012345678901234567890123456789\nnext\t\tline")
|
||||||
|
check("insert multi")
|
||||||
|
e:SelectRegion(P(1, 0), P(1, 4))
|
||||||
|
check("select after insert")
|
||||||
|
e:Undo()
|
||||||
|
check("undo")
|
||||||
|
e:Redo()
|
||||||
|
check("redo")
|
||||||
|
e:SetTabSize(8)
|
||||||
|
check("tab size")
|
||||||
|
e:SetText("a\n\t\tb")
|
||||||
|
check("set again")
|
||||||
|
e:SelectAll()
|
||||||
|
check("select all")
|
||||||
|
e:ReplaceTextInAllCursors("x")
|
||||||
|
check("replace all")
|
||||||
|
-- same section asked for again after an edit inside it
|
||||||
|
e:SetText("hello world")
|
||||||
|
eq(e:GetSectionText(P(0, 0), P(0, 5)), "hello", "section")
|
||||||
|
e:SetCursor(P(0, 1))
|
||||||
|
e:ReplaceTextInAllCursors("J")
|
||||||
|
eq(e:GetSectionText(P(0, 0), P(0, 5)), "hJell", "section after edit")
|
||||||
|
print("OK")
|
||||||
Reference in new issue
Block a user