Erendir
Newbie | Редактировать | Профиль | Сообщение | Цитировать | Сообщить модератору --[[-------------------------------------------------- HighlightFoldingKeywords.lua Authors: TymurGubayev Version: 1.0 ------------------------------------------------------ Description: marks folding keywords with find.mark.MNr tested only with lua (other languages require customization) may work OOB correct with C ------------------------------------------------------ Installation: * add the script to SciTEStartup.lua * add find.mark.6=#CC00FF,@50 to your properties (or find.mark.6=#FFFFFF,plain or something else on your choice) * modify the tables of keywords, if needed (it should work good with Lua and C out of the box) * make a look at constants section --]]-------------------------------------------------- -- shortcuts local editor = editor ---------------------------------------------------------------------------------- -- constants local NEWLINELENGTH = 2 -- is it possible to get this from editor propertys?.. local MNr = 6 -- Mark style Nr used in the script ---------------------------------------------------------------------------------- local trunk = 2^4 -- only 4 bits are used to define level of line local function GetLevel ( index ) return (editor.FoldLevel[ index ])%trunk end -- GetLevel local keywords = {} keywords.foldstart = { 'if', 'local function', 'function', 'repeat', 'while', 'for', 'do', '{', '('} keywords.foldend = { 'end', 'until', '}', ')' } --@todo: brackets dont work really correct keywords.foldmid = { ['if'] = {'then','elseif','else'}, ['while'] = {'do'}, ['for'] = {'do'}, } local function MarkKeywordInLine ( lineNr, keywords ) local line = editor:GetLine(lineNr) if not line then return end -- in case of EOF local keyword, _start, _end for _, k in ipairs( keywords ) do _start, _end = line:find( k, 1, true) if _start then keyword = k break end end --for if not keyword then --error("No keyword found?!! It's impossible!!!") return end local pos = editor:PositionFromLine(lineNr) EditorMarkText( pos+_start-1, #keyword, MNr ) return keyword, pos end -- MarkKeywordInLine local function MarkKeywordsInBlock ( midkeywords, blockstart, blockend ) if not midkeywords then return end local level = GetLevel( editor:LineFromPosition(blockstart) ) for i = 1, #midkeywords do local keyword = midkeywords[ i ] local searchstart = blockstart local _start = editor:findtext( keyword, SCFIND_WHOLEWORD, searchstart, blockend ) while _start do if not IsComment(_start) then local linelvl = GetLevel( editor:LineFromPosition(_start) ) if linelvl == level then EditorMarkText( _start, #keyword, MNr ) elseif linelvl == level+1 then -- process 'elseif'-case local lineNr = editor:LineFromPosition(_start) local linestart = editor:PositionFromLine(lineNr) local _, linelength = editor:GetLine(lineNr) -- is the line with keyword start of a new block? local hasstartblockkey = false for k, _ in pairs( keywords.foldmid ) do if editor:findtext( k, SCFIND_WHOLEWORD, linestart, linestart+linelength ) then hasstartblockkey = true break end end --for if not hasstartblockkey then EditorMarkText( _start, #keyword, MNr ) end --if end end -- if searchstart = _start + #keyword _start = editor:findtext( keyword, SCFIND_WHOLEWORD, searchstart, blockend ) end end -- for end -- MarkKeywordsInBlock local function Update( lineNr ) EditorClearMarks(MNr) lineNr = lineNr or editor:GetLine() -- local lineNr = 25 local foldstart = editor.FoldParent[ lineNr ] + 1 if foldstart == 0 then return end local level = GetLevel( lineNr ) local foldend = lineNr while GetLevel( foldend ) >= level do foldend = foldend + 1 end local keyword, _start = MarkKeywordInLine( foldstart-1, keywords.foldstart ) local _, _end = MarkKeywordInLine( foldend-1, keywords.foldend ) if _end then MarkKeywordsInBlock( keywords.foldmid[keyword], _start, _end ) end end -- Add user event handler OnUpdateUI local old_OnUpdateUI = OnUpdateUI function OnUpdateUI () local result if old_OnUpdateUI then result = old_OnUpdateUI() end if props['FileName'] ~= '' then Update(editor:LineFromPosition(editor.CurrentPos)) end return result end |