Last active
May 30, 2016 23:13
-
-
Save exerro/4c34165c50648589d41a3b75786479ee to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[[ | |
newTextBody(string text) | |
- creates a new text object with the given starting text | |
- note: use :format() once created | |
:format(int i = 1, int j = #lines) | |
- updates the formatting on the range of lines given | |
:insert(int n, string line) | |
- inserts a new line and updates formatting | |
:remove(int n) | |
- removes a ling and updates formatting | |
:set(int n, string line) | |
- updates a line to a new value and updates the formatting | |
:get(int n) | |
- returns the raw line | |
:getFormatted(int n) | |
- returns the formatted line | |
:formatLine(table state, string line) | |
- (callback) returns a formatted string for that line | |
]] | |
local function copyt( t ) | |
local t2 = {} | |
for k, v in pairs( t ) do | |
t2[k] = type(v) == "table" and copyt(v) or v | |
end | |
return t2 | |
end | |
local function comparet( a, b ) | |
for k, v in pairs( a ) do | |
if b[k] ~= v then return true end | |
end | |
for k, v in pairs( b ) do | |
if a[k] ~= v then return true end | |
end | |
end | |
local function newTextBody(text) | |
local lines = {} | |
local t = {} | |
for line in text:gmatch "[^\n]*" do | |
lines[#lines + 1] = line | |
end | |
t.state = {} | |
t.lines = lines | |
t.fmtlines = fmtlines | |
t.states = { [0] = t.state } | |
function t:format( i, upper ) | |
upper = upper or i or #self.lines | |
i = i or 1 | |
while i <= upper do | |
local state = copyt( self.states[i - 1] ) -- this is for keeping track of things like (isInClass) to highlight keywords like 'super', 'private', etc | |
self.fmtlines[i] = self:formatLine( state, self.lines[i] ) | |
if i == upper and comparet( state, self.states[i] ) then -- if the line caused the next to have a changed state | |
upper = upper + 1 | |
end | |
self.states[i] = state | |
i = i + 1 | |
end | |
end | |
function t:insert( n, line ) | |
table.insert( self.lines, n, line ) | |
table.insert( self.fmtlines, n, "" ) | |
table.insert( self.states, n, {} ) | |
return self:format( n ) | |
end | |
function t:remove( n ) | |
table.remove( self.lines, n ) | |
table.remove( self.fmtlines, n ) | |
table.remove( self.states, n ) | |
return self:format( n ) | |
end | |
function t:set( n, line ) | |
self.lines[n] = line | |
return self:format( n ) | |
end | |
function t:get( n ) | |
return self.lines[n] | |
end | |
function t:getFormatted( n ) | |
return self.fmtlines[n] | |
end | |
function t:formatLine( state, line ) | |
return line -- with fancy text formatting | |
end | |
return t | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment