From b3b90a5dc0056d0f813de1fdcbe0efd8aff40e6c Mon Sep 17 00:00:00 2001 From: Alan Kilborn Date: Sun, 19 May 2024 07:40:22 -0400 Subject: [PATCH] Add auto-indent feature for Python Fix #15122, close #15159 --- PowerEditor/src/Notepad_plus.cpp | 33 ++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/PowerEditor/src/Notepad_plus.cpp b/PowerEditor/src/Notepad_plus.cpp index ae6752ba9..08c8605b7 100644 --- a/PowerEditor/src/Notepad_plus.cpp +++ b/PowerEditor/src/Notepad_plus.cpp @@ -3683,6 +3683,39 @@ void Notepad_plus::maintainIndentation(TCHAR ch) */ } } + else if (type == L_PYTHON) + { + if (((eolMode == SC_EOL_CRLF || eolMode == SC_EOL_LF) && ch == '\n') || + (eolMode == SC_EOL_CR && ch == '\r')) + { + // Search the non-empty previous line + while (prevLine >= 0 && _pEditView->getLineLength(prevLine) == 0) + prevLine--; + + // Get previous line's Indent + if (prevLine >= 0) + { + indentAmountPrevLine = _pEditView->getLineIndent(prevLine); + } + + _pEditView->execute(SCI_SETSEARCHFLAGS, SCFIND_REGEXP | SCFIND_POSIX); + + auto startPos = _pEditView->execute(SCI_POSITIONFROMLINE, prevLine); + auto endPos = _pEditView->execute(SCI_GETLINEENDPOSITION, prevLine); + _pEditView->execute(SCI_SETTARGETRANGE, startPos, endPos); + + const char colonExpr[] = ":[ \t]*(#|$)"; // colon optionally followed by only whitespace and/or start-of-comment + + if (_pEditView->execute(SCI_SEARCHINTARGET, strlen(colonExpr), reinterpret_cast(colonExpr)) >= 0) + { + _pEditView->setLineIndent(curLine, indentAmountPrevLine + tabWidth); + } + else if (indentAmountPrevLine > 0) + { + _pEditView->setLineIndent(curLine, indentAmountPrevLine); + } + } + } else // Basic indentation mode { if (((eolMode == SC_EOL_CRLF || eolMode == SC_EOL_LF) && ch == '\n') ||