--[[ Indenter.lua Authors: frs version 0.0.1 Форматирование кода отступами основанное на уровнях фолдинга с поправками на конкретные реализации лексеров Code indenting code based on folding level with correctins for some definite lexers ]] local sel_text = editor:GetSelText() if sel_text == '' then line_start = 0 line_end = editor.LineCount-1 else line_start = props["SelectionStartLine"] - 1 line_end = props["SelectionEndLine"] - 2 end -- значения по умолчанию для непрописанных лексеров use_outdents=false outdent_last_line=true -- списки слов для убавления отступа, по мере испытаний будут правится -- возможно стоит их внести в properties соответствующих языков -- к началу слов добавлено подчеркивания для снятия конфликтов -- я плохо знаю lua, возможно кто-нибудь напишет более простую конструкцию хранения списка и проверки наличия в нем слова outdents={_end=1,_else=1,_next=1,_wend=1,_elseif=1} -- список слов по умолчанию if lex== "pascal" then outdents={_end=1,_except=1} end -- список для паскаля -- добавьте списки для других языков если требуется lex=editor.LexerLanguage if lex=="lua" or lex== "pascal" then use_outdents=true end if lex=="au3" then outdent_last_line=false end if lex~="python" then -- не вмешиваемся в отступы питона editor:BeginUndoAction() tb=4 -- ручное задание размера табуляций и индентов editor.TabWidth=tb --tb=editor.TabWidth -- автоматическое задание табуляций и индентов editor.TabIndents=tb editor.Indent=tb ind=0 ind=editor.FoldLevel[line_start] ind=math.fmod (ind, 1024) for line_num = line_start, line_end do local line = editor:GetLine(line_num) if line ~= nil and line ~="" then ind_next=editor.FoldLevel[line_num+1] ind_next=math.fmod (ind_next, 1024) if use_outdents then -- убавления отступа для слов из списка if outdents["_"..string.gsub(line,"^%s*(%w*)(.*)","%1")]==1 then ind=ind-1 end end if outdent_last_line then -- убавления отступа в последней строке блока фолдинга if ind_next<ind then ind=ind-1 end end editor.LineIndentation[line_num]=tb*ind else editor.LineIndentation[line_num]=0 end ind=ind_next end editor:EndUndoAction() end |