1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-12-13 19:50:31 +00:00

Modification of edit.lua to use blit in writeHighlighted

Reduces amount of string operations and write calls inside writeHighlighted function by switching it to blit and using the 3rd argument in string.match.
This commit is contained in:
Wojbie 2017-06-16 23:13:53 +02:00
parent c611b20db8
commit efb351e1b0

View File

@ -135,39 +135,65 @@ local tKeywords = {
["while"] = true, ["while"] = true,
} }
local function tryWrite( sLine, regex, colour ) local tHex = {
local match = string.match( sLine, regex ) [ colors.white ] = "0",
if match then [ colors.orange ] = "1",
if type(colour) == "number" then [ colors.magenta ] = "2",
term.setTextColour( colour ) [ colors.lightBlue ] = "3",
else [ colors.yellow ] = "4",
term.setTextColour( colour(match) ) [ colors.lime ] = "5",
end [ colors.pink ] = "6",
term.write( match ) [ colors.gray ] = "7",
term.setTextColour( textColour ) [ colors.lightGray ] = "8",
return string.sub( sLine, string.len(match) + 1 ) [ colors.cyan ] = "9",
end [ colors.purple ] = "a",
return nil [ colors.blue ] = "b",
end [ colors.brown ] = "c",
[ colors.green ] = "d",
[ colors.red ] = "e",
[ colors.black ] = "f",
}
local tPattern = {
[ 1 ] = "^(%-%-%[%[.-%]%])()",
[ 2 ] = "^(%-%-.*)()",
[ 3 ] = "^(\"\")()",
[ 4 ] = "^(\".-[^\\]\")()",
[ 5 ] = "^(\'\')()",
[ 6 ] = "^(\'.-[^\\]\')()",
[ 7 ] = "^(%[%[.-%]%])()",
[ 8 ] = "^([%w_]+)()",
[ 9 ] = "^([^%w_])()",
}
local tPatternColors = {
[ 1 ] = commentColour,
[ 2 ] = commentColour,
[ 3 ] = stringColour,
[ 4 ] = stringColour,
[ 5 ] = stringColour,
[ 6 ] = stringColour,
[ 7 ] = stringColour,
[ 8 ] = function( sMatch ) return tKeywords[ sMatch ] and keywordColour or textColour end,
[ 9 ] = textColour,
}
local function writeHighlighted( sLine ) local function writeHighlighted( sLine )
while string.len(sLine) > 0 do local tColor = {}
sLine = local nPosition = 1
tryWrite( sLine, "^%-%-%[%[.-%]%]", commentColour ) or local sMatch,nNextPosition
tryWrite( sLine, "^%-%-.*", commentColour ) or local nColor
tryWrite( sLine, "^\"\"", stringColour ) or while nPosition <= #sLine do
tryWrite( sLine, "^\".-[^\\]\"", stringColour ) or local i = 0
tryWrite( sLine, "^\'\'", stringColour ) or repeat
tryWrite( sLine, "^\'.-[^\\]\'", stringColour ) or i=i+1
tryWrite( sLine, "^%[%[.-%]%]", stringColour ) or sMatch,nNextPosition = string.match( sLine, tPattern[i] , nPosition)
tryWrite( sLine, "^[%w_]+", function( match ) until sMatch
if tKeywords[ match ] then nColor = tPatternColors[i]
return keywordColour tColor[#tColor+1] = string.rep( tHex[ type(nColor) == "number" and nColor or nColor(sMatch) ] , nNextPosition - nPosition)
end nPosition = nNextPosition
return textColour
end ) or
tryWrite( sLine, "^[^%w_]", textColour )
end end
term.blit( sLine, table.concat( tColor ), string.rep( tHex[bgColour], #sLine ) )
end end
local tCompletions local tCompletions