diff --git a/src/plugins/texteditor/basetexteditor.cpp b/src/plugins/texteditor/basetexteditor.cpp index 73d20440d42b27aa7ee75b461e8df335ca24b59e..e197a6fd85f2cd1a71c64414e4457031bb57e91d 100644 --- a/src/plugins/texteditor/basetexteditor.cpp +++ b/src/plugins/texteditor/basetexteditor.cpp @@ -611,12 +611,44 @@ bool BaseTextEditor::open(const QString &fileName) { if (d->m_document->open(fileName)) { moveCursor(QTextCursor::Start); + if (d->m_displaySettings.m_autoFoldFirstComment) + d->collapseLicenseHeader(); setReadOnly(d->m_document->hasDecodingError()); return true; } return false; } +/* + Collapses the first comment in a file, if there is only whitespace above + */ +void BaseTextEditorPrivate::collapseLicenseHeader() +{ + QTextDocument *doc = q->document(); + TextEditDocumentLayout *documentLayout = qobject_cast<TextEditDocumentLayout*>(doc->documentLayout()); + QTC_ASSERT(documentLayout, return); + QTextBlock block = doc->firstBlock(); + const TabSettings &ts = m_document->tabSettings(); + while (block.isValid()) { + TextBlockUserData *data = TextBlockUserData::canCollapse(block); + if (data && block.next().isVisible()) { + QChar character; + static_cast<TextBlockUserData*>(data)->collapseAtPos(&character); + if (character != QLatin1Char('+')) + break; // not a comment + TextBlockUserData::doCollapse(block, false); + moveCursorVisible(); + documentLayout->requestUpdate(); + documentLayout->emitDocumentSizeChanged(); + break; + } + QString text = block.text(); + if (ts.firstNonSpace(text) < text.size()) + break; + block = block.next(); + } +} + const Utils::ChangeSet &BaseTextEditor::changeSet() const { return d->m_changeSet; @@ -1618,9 +1650,9 @@ int Parenthesis::collapseAtPos(const Parentheses &parentheses, QChar *character) } -int TextBlockUserData::collapseAtPos() const +int TextBlockUserData::collapseAtPos(QChar *character) const { - return Parenthesis::collapseAtPos(m_parentheses); + return Parenthesis::collapseAtPos(m_parentheses, character); } int TextBlockUserData::braceDepthDelta() const diff --git a/src/plugins/texteditor/basetexteditor.h b/src/plugins/texteditor/basetexteditor.h index 6c135b86f3caf6904c41cc37b20954c1d729732a..6f5453f1f4c7e4b26f1a9b9a359d6ec0dcce47c9 100644 --- a/src/plugins/texteditor/basetexteditor.h +++ b/src/plugins/texteditor/basetexteditor.h @@ -173,7 +173,7 @@ public: static QTextBlock testCollapse(const QTextBlock& block); static void doCollapse(const QTextBlock& block, bool visible); - int collapseAtPos() const; + int collapseAtPos(QChar *character = 0) const; enum MatchType { NoMatch, Match, Mismatch }; static MatchType checkOpenParenthesis(QTextCursor *cursor, QChar c); diff --git a/src/plugins/texteditor/basetexteditor_p.h b/src/plugins/texteditor/basetexteditor_p.h index eb58cfdd2b3794fc5af9d13fd88593f63bf71171..3a7bcdc5171d300baadb62b9e53228cf5d67ca60 100644 --- a/src/plugins/texteditor/basetexteditor_p.h +++ b/src/plugins/texteditor/basetexteditor_p.h @@ -196,6 +196,7 @@ public: int suggestedVisibleCollapsedBlockNumber; void clearVisibleCollapsedBlock(); bool m_mouseOnCollapsedMarker; + void collapseLicenseHeader(); QBasicTimer autoScrollTimer; void updateMarksLineNumber(); diff --git a/src/plugins/texteditor/displaysettings.cpp b/src/plugins/texteditor/displaysettings.cpp index aca646ccf93c0885326cfb979c13fa59a978f4b4..8bbee972a59046ea84439394884f5dd214e80883 100644 --- a/src/plugins/texteditor/displaysettings.cpp +++ b/src/plugins/texteditor/displaysettings.cpp @@ -45,6 +45,7 @@ static const char * const highlightBlocksKey = "HighlightBlocksKey"; static const char * const animateMatchingParenthesesKey= "AnimateMatchingParenthesesKey"; static const char * const mouseNavigationKey = "MouseNavigation"; static const char * const markTextChangesKey = "MarkTextChanges"; +static const char * const autoFoldFirstCommentKey= "AutoFoldFirstComment"; static const char * const groupPostfix = "DisplaySettings"; namespace TextEditor { @@ -60,7 +61,8 @@ DisplaySettings::DisplaySettings() : m_highlightBlocks(false), m_animateMatchingParentheses(true), m_mouseNavigation(true), - m_markTextChanges(true) + m_markTextChanges(true), + m_autoFoldFirstComment(true) { } @@ -81,6 +83,7 @@ void DisplaySettings::toSettings(const QString &category, QSettings *s) const s->setValue(QLatin1String(animateMatchingParenthesesKey), m_animateMatchingParentheses); s->setValue(QLatin1String(mouseNavigationKey), m_mouseNavigation); s->setValue(QLatin1String(markTextChangesKey), m_markTextChanges); + s->setValue(QLatin1String(autoFoldFirstCommentKey), m_autoFoldFirstComment); s->endGroup(); } @@ -104,6 +107,7 @@ void DisplaySettings::fromSettings(const QString &category, const QSettings *s) m_animateMatchingParentheses = s->value(group + QLatin1String(animateMatchingParenthesesKey), m_animateMatchingParentheses).toBool(); m_mouseNavigation = s->value(group + QLatin1String(mouseNavigationKey), m_mouseNavigation).toBool(); m_markTextChanges = s->value(group + QLatin1String(markTextChangesKey), m_markTextChanges).toBool(); + m_autoFoldFirstComment = s->value(group + QLatin1String(autoFoldFirstCommentKey), m_autoFoldFirstComment).toBool(); } bool DisplaySettings::equals(const DisplaySettings &ds) const @@ -119,6 +123,7 @@ bool DisplaySettings::equals(const DisplaySettings &ds) const && m_animateMatchingParentheses == ds.m_animateMatchingParentheses && m_mouseNavigation == ds.m_mouseNavigation && m_markTextChanges == ds.m_markTextChanges + && m_autoFoldFirstComment== ds.m_autoFoldFirstComment ; } diff --git a/src/plugins/texteditor/displaysettings.h b/src/plugins/texteditor/displaysettings.h index 321e1e72c0e1b7b160abf2ab7d1b78f5cac1147e..4b0eddc6a11de79417afda16fcb26a51f38ea602 100644 --- a/src/plugins/texteditor/displaysettings.h +++ b/src/plugins/texteditor/displaysettings.h @@ -56,6 +56,7 @@ struct TEXTEDITOR_EXPORT DisplaySettings bool m_animateMatchingParentheses; bool m_mouseNavigation; bool m_markTextChanges; + bool m_autoFoldFirstComment; bool equals(const DisplaySettings &ds) const; }; diff --git a/src/plugins/texteditor/displaysettingspage.cpp b/src/plugins/texteditor/displaysettingspage.cpp index ecfa4c2d546340906dbbed60836a2f9354f25c6d..4995371f52f4aad9c1a7489cc329965ca5bb06ba 100644 --- a/src/plugins/texteditor/displaysettingspage.cpp +++ b/src/plugins/texteditor/displaysettingspage.cpp @@ -102,7 +102,8 @@ QWidget *DisplaySettingsPage::createPage(QWidget *parent) << ' ' << m_d->m_page.visualizeWhitespace->text() << ' ' << m_d->m_page.animateMatchingParentheses->text() << ' ' << m_d->m_page.enableTextWrapping->text() - << ' ' << m_d->m_page.mouseNavigation->text(); + << ' ' << m_d->m_page.mouseNavigation->text() + << ' ' << m_d->m_page.autoFoldFirstComment->text(); m_d->m_searchKeywords.remove(QLatin1Char('&')); } return w; @@ -139,6 +140,7 @@ void DisplaySettingsPage::settingsFromUI(DisplaySettings &displaySettings) const displaySettings.m_animateMatchingParentheses = m_d->m_page.animateMatchingParentheses->isChecked(); displaySettings.m_mouseNavigation = m_d->m_page.mouseNavigation->isChecked(); displaySettings.m_markTextChanges = m_d->m_page.markTextChanges->isChecked(); + displaySettings.m_autoFoldFirstComment = m_d->m_page.autoFoldFirstComment->isChecked(); } void DisplaySettingsPage::settingsToUI() @@ -155,6 +157,7 @@ void DisplaySettingsPage::settingsToUI() m_d->m_page.animateMatchingParentheses->setChecked(displaySettings.m_animateMatchingParentheses); m_d->m_page.mouseNavigation->setChecked(displaySettings.m_mouseNavigation); m_d->m_page.markTextChanges->setChecked(displaySettings.m_markTextChanges); + m_d->m_page.autoFoldFirstComment->setChecked(displaySettings.m_autoFoldFirstComment); } DisplaySettings DisplaySettingsPage::displaySettings() const diff --git a/src/plugins/texteditor/displaysettingspage.ui b/src/plugins/texteditor/displaysettingspage.ui index 8bb8ab1352265f90bfa38354e503b1c95fb539ed..8a8170029155067067d1376b7a650b977965a1b1 100644 --- a/src/plugins/texteditor/displaysettingspage.ui +++ b/src/plugins/texteditor/displaysettingspage.ui @@ -6,7 +6,7 @@ <rect> <x>0</x> <y>0</y> - <width>448</width> + <width>450</width> <height>330</height> </rect> </property> @@ -61,7 +61,7 @@ <item row="2" column="0"> <widget class="QCheckBox" name="markTextChanges"> <property name="text"> - <string>Mark text changes</string> + <string>Mark &text changes</string> </property> </widget> </item> @@ -78,7 +78,14 @@ <item row="2" column="1"> <widget class="QCheckBox" name="animateMatchingParentheses"> <property name="text"> - <string>Animate matching parentheses</string> + <string>&Animate matching parentheses</string> + </property> + </widget> + </item> + <item row="4" column="1"> + <widget class="QCheckBox" name="autoFoldFirstComment"> + <property name="text"> + <string>Auto-fold first &comment</string> </property> </widget> </item>