Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A buggy scope highlighter for ZeroBrane Studio
local golden_ratio = (1+math.sqrt(5))/2
local offset = 0.05
local function HSVtoRGB(h, s, v)
if s == 0 then
return v,v,v
end
h = h/60
i = math.floor( h )
f = h - i
p = v * ( 1 - s )
q = v * ( 1 - s * f )
t = v * ( 1 - s * ( 1 - f ) )
if i == 0 then r, g, b = v, t, p
elseif i == 1 then r, g, b = q, v, p
elseif i == 2 then r, g, b = p, v, t
elseif i == 3 then r, g, b = p, q, v
elseif i == 4 then r, g, b = t, p, v
elseif i == 5 then r, g, b = v, p, q
end
return math.floor(r * 255), math.floor(g * 255), math.floor(b * 255)
end
local function goldenColor(i)
return HSVtoRGB(((offset + (golden_ratio * i) )% 1)*360,1,1)
end
local function defaultdict(default_value_factory)
local mt = {
__index = function(t, key)
if not rawget(t, key) then
rawset(t, key, default_value_factory(key))
end
return rawget(t, key)
end
}
return setmetatable({}, mt)
end
local indicators = defaultdict(function(n)
return ide:AddIndicator("scopecolor.ind" .. n)
end)
local editor_indicators = defaultdict(function(editor)
return defaultdict(function(n)
local i = indicators[n]
local color = wx.wxColour(goldenColor(n))
editor:IndicatorSetStyle(i, wxstc.wxSTC_INDIC_ROUNDBOX)
editor:IndicatorSetUnder(i, true)
editor:IndicatorSetForeground(i, color)
editor:IndicatorSetAlpha(i, 34)
return i
end)
end)
local function ApplyContextStyles(editor, style_tree)
print(style_tree, style_tree.color)
if style_tree.color ~= 0 then
for i=1,#style_tree.segments/2 do
local start, finish = style_tree.segments[i*2-1], style_tree.segments[i*2]
local indicator = editor_indicators[editor][style_tree.color]
editor:SetIndicatorCurrent(indicator)
editor:IndicatorFillRange(start, finish - start)
end
end
for i, child in ipairs(style_tree.children) do
ApplyContextStyles(editor, child)
end
end
local function ApplyColors(editor)
local tokens = editor:GetTokenList()
for _, indicator in pairs(editor_indicators[editor]) do
editor:SetIndicatorCurrent(indicator)
editor:IndicatorClearRange(0, #editor:GetText())
end
local tree = {parent = nil, children = {}, color = 0, next_color = 1, segments={0}}
local current = tree
for i, t in ipairs(tokens) do
if t[1] == "Scope" then
local position = t.fpos + #t.name
current = {
parent = current,
color = current.color + 1,
next_color = current.color + 2,
segments = {},
children = {}
}
table.insert(current.parent.children, current)
current.parent.next_color = current.color + 1
table.insert(current.segments, position)
table.insert(current.parent.segments, position)
elseif t[1] == "EndScope" then
local position = 0
--if t.name == "then" then
--position = tokens[i-1].fpos + #tokens[i-1].name - 1
--else
position = t.fpos - 1
--end
table.insert(current.segments, position)
table.insert(current.parent.segments, position)
current = current.parent
end
end
table.insert(tree.segments, #editor:GetText())
ApplyContextStyles(editor, tree)
end
return {
name = "Scopecolor",
description = "Highlights scopes for improved visual clarity",
author = "Llamageddon",
version = 0.7,
onRegister = function(self) end,
onUnRegister = function(self) end,
onEditorCharAdded = function(self, editor, event)
if ide.config.scopecolor and ide.config.scopecolor.enabled == false then return end
pcall(ApplyColors, editor) -- Catch errors, they're caused by user still editing
end,
onEditorLoad = function(self, editor)
-- For some reason causes a crash on startup
-- ApplyColors(editor)
end,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment