From 4710e6b4b2df67a9d483c33e1a2b06a4139f7baa Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Tue, 12 Sep 2017 14:02:00 +0200 Subject: [PATCH] TextEditor: Fold Python/Perl/sh license headers Skip shebang and fold the first comment. Change-Id: I06d55931555f1c77f08bf30427ae7072f38b02da Reviewed-by: hjk --- src/plugins/texteditor/texteditor.cpp | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/plugins/texteditor/texteditor.cpp b/src/plugins/texteditor/texteditor.cpp index c39bf0227ee..34594795670 100644 --- a/src/plugins/texteditor/texteditor.cpp +++ b/src/plugins/texteditor/texteditor.cpp @@ -1162,19 +1162,37 @@ void TextEditorWidgetPrivate::updateCannotDecodeInfo() } } +// Skip over shebang to license header (Python, Perl, sh) +// '#!/bin/sh' +// '' +// '###############' + +static QTextBlock skipShebang(const QTextBlock &block) +{ + if (!block.isValid() || !block.text().startsWith("#!")) + return block; + const QTextBlock nextBlock1 = block.next(); + if (!nextBlock1.isValid() || !nextBlock1.text().isEmpty()) + return block; + const QTextBlock nextBlock2 = nextBlock1.next(); + return nextBlock2.isValid() && nextBlock2.text().startsWith('#') ? nextBlock2 : block; +} + /* - Collapses the first comment in a file, if there is only whitespace above + Collapses the first comment in a file, if there is only whitespace/shebang line + above */ void TextEditorWidgetPrivate::foldLicenseHeader() { QTextDocument *doc = q->document(); TextDocumentLayout *documentLayout = qobject_cast(doc->documentLayout()); QTC_ASSERT(documentLayout, return); - QTextBlock block = doc->firstBlock(); + QTextBlock block = skipShebang(doc->firstBlock()); while (block.isValid() && block.isVisible()) { QString text = block.text(); if (TextDocumentLayout::canFold(block) && block.next().isVisible()) { - if (text.trimmed().startsWith(QLatin1String("/*"))) { + const QString trimmedText = text.trimmed(); + if (trimmedText.startsWith("/*") || trimmedText.startsWith('#')) { TextDocumentLayout::doFoldOrUnfold(block, false); moveCursorVisible(); documentLayout->requestUpdate();