From 6775e623eedf510b65e7541d8696c46d30ee8d23 Mon Sep 17 00:00:00 2001
From: hjk <hjk121@nokiamail.com>
Date: Mon, 1 Sep 2014 16:16:44 +0200
Subject: [PATCH] TextEditor: Move comment definitions back to *Widget

Only used there, and avoids back-links to editors.

Change-Id: I81206057ce89d42aef7febb840cf9e44b869df0e
Reviewed-by: Christian Stenger <christian.stenger@digia.com>
---
 src/libs/utils/uncommentselection.cpp         |  5 +++
 src/libs/utils/uncommentselection.h           |  2 +-
 src/plugins/android/javaeditor.cpp            |  2 +-
 .../cmakeprojectmanager/cmakeeditor.cpp       |  2 +-
 src/plugins/cppeditor/cppeditor.cpp           |  1 -
 src/plugins/cppeditor/cppeditorplugin.cpp     |  1 +
 src/plugins/glsleditor/glsleditor.cpp         |  2 +-
 src/plugins/pythoneditor/pythoneditor.cpp     |  2 +-
 .../qmakeprojectmanager/profileeditor.cpp     |  2 +-
 src/plugins/qmljseditor/qmljseditor.cpp       |  3 +-
 src/plugins/texteditor/basetexteditor.cpp     | 43 ++++++++-----------
 src/plugins/texteditor/basetexteditor.h       |  8 ++--
 12 files changed, 35 insertions(+), 38 deletions(-)

diff --git a/src/libs/utils/uncommentselection.cpp b/src/libs/utils/uncommentselection.cpp
index 8cc9752d8f..18a5dd1b27 100644
--- a/src/libs/utils/uncommentselection.cpp
+++ b/src/libs/utils/uncommentselection.cpp
@@ -50,6 +50,11 @@ void CommentDefinition::setStyle(Style style)
             multiLineStart.clear();
             multiLineEnd.clear();
             break;
+        case NoStyle:
+            singleLine.clear();
+            multiLineStart.clear();
+            multiLineEnd.clear();
+            break;
     }
 }
 
diff --git a/src/libs/utils/uncommentselection.h b/src/libs/utils/uncommentselection.h
index f2bcc90ec3..6a41c8128e 100644
--- a/src/libs/utils/uncommentselection.h
+++ b/src/libs/utils/uncommentselection.h
@@ -45,7 +45,7 @@ class QTCREATOR_UTILS_EXPORT CommentDefinition
 public:
     CommentDefinition();
 
-    enum Style { CppStyle, HashStyle };
+    enum Style { NoStyle, CppStyle, HashStyle };
     void setStyle(Style style);
 
     bool isValid() const;
diff --git a/src/plugins/android/javaeditor.cpp b/src/plugins/android/javaeditor.cpp
index de1c64cc71..e6c875c034 100644
--- a/src/plugins/android/javaeditor.cpp
+++ b/src/plugins/android/javaeditor.cpp
@@ -59,7 +59,6 @@ public:
     {
         addContext(Constants::C_JAVA_EDITOR);
         setDuplicateSupported(true);
-        setCommentStyle(Utils::CommentDefinition::CppStyle);
         setCompletionAssistProvider(ExtensionSystem::PluginManager::getObject<JavaCompletionAssistProvider>());
     }
 };
@@ -112,6 +111,7 @@ JavaEditorFactory::JavaEditorFactory()
     setDocumentCreator([]() { return new JavaDocument; });
     setAutoCompleterCreator([]() { return new JavaAutoCompleter; });
     setGenericSyntaxHighlighter(QLatin1String(Constants::JAVA_MIMETYPE));
+    setCommentStyle(Utils::CommentDefinition::CppStyle);
 
     setEditorActionHandlers(Constants::C_JAVA_EDITOR,
                   TextEditor::TextEditorActionHandler::UnCommentSelection);
diff --git a/src/plugins/cmakeprojectmanager/cmakeeditor.cpp b/src/plugins/cmakeprojectmanager/cmakeeditor.cpp
index 3b8b584a4e..0b4277a9aa 100644
--- a/src/plugins/cmakeprojectmanager/cmakeeditor.cpp
+++ b/src/plugins/cmakeprojectmanager/cmakeeditor.cpp
@@ -67,7 +67,6 @@ CMakeEditor::CMakeEditor()
 {
     addContext(Constants::C_CMAKEEDITOR);
     setDuplicateSupported(true);
-    setCommentStyle(Utils::CommentDefinition::HashStyle);
     setCompletionAssistProvider(ExtensionSystem::PluginManager::getObject<CMakeFileCompletionAssistProvider>());
 }
 
@@ -288,6 +287,7 @@ CMakeEditorFactory::CMakeEditorFactory()
     setEditorWidgetCreator([]() { return new CMakeEditorWidget; });
     setDocumentCreator([]() { return new CMakeDocument; });
     setGenericSyntaxHighlighter(QLatin1String(Constants::CMAKEMIMETYPE));
+    setCommentStyle(Utils::CommentDefinition::HashStyle);
 
     setEditorActionHandlers(Constants::C_CMAKEEDITOR,
             TextEditorActionHandler::UnCommentSelection
diff --git a/src/plugins/cppeditor/cppeditor.cpp b/src/plugins/cppeditor/cppeditor.cpp
index c17dff66f1..58abf75202 100644
--- a/src/plugins/cppeditor/cppeditor.cpp
+++ b/src/plugins/cppeditor/cppeditor.cpp
@@ -96,7 +96,6 @@ CppEditor::CppEditor()
     m_context.add(ProjectExplorer::Constants::LANG_CXX);
     m_context.add(TextEditor::Constants::C_TEXTEDITOR);
     setDuplicateSupported(true);
-    setCommentStyle(Utils::CommentDefinition::CppStyle);
     setCompletionAssistProvider([this] () -> TextEditor::CompletionAssistProvider * {
         if (CppEditorDocument *document = qobject_cast<CppEditorDocument *>(textDocument()))
             return document->completionAssistProvider();
diff --git a/src/plugins/cppeditor/cppeditorplugin.cpp b/src/plugins/cppeditor/cppeditorplugin.cpp
index 79032644b0..8d769cf99f 100644
--- a/src/plugins/cppeditor/cppeditorplugin.cpp
+++ b/src/plugins/cppeditor/cppeditorplugin.cpp
@@ -91,6 +91,7 @@ public:
         setEditorWidgetCreator([]() { return new CppEditorWidget; });
         setEditorCreator([]() { return new CppEditor; });
         setAutoCompleterCreator([]() { return new CppAutoCompleter; });
+        setCommentStyle(Utils::CommentDefinition::CppStyle);
 
         setEditorActionHandlers(Constants::C_CPPEDITOR,
                                 TextEditorActionHandler::Format
diff --git a/src/plugins/glsleditor/glsleditor.cpp b/src/plugins/glsleditor/glsleditor.cpp
index 07aa79473c..c567d4183b 100644
--- a/src/plugins/glsleditor/glsleditor.cpp
+++ b/src/plugins/glsleditor/glsleditor.cpp
@@ -336,7 +336,6 @@ public:
     {
         addContext(Constants::C_GLSLEDITOR_ID);
         setDuplicateSupported(true);
-        setCommentStyle(Utils::CommentDefinition::CppStyle);
         setCompletionAssistProvider(ExtensionSystem::PluginManager::getObject<GlslCompletionAssistProvider>());
     }
 
@@ -368,6 +367,7 @@ GlslEditorFactory::GlslEditorFactory()
     setEditorCreator([]() { return new GlslEditor; });
     setIndenterCreator([]() { return new GlslIndenter; });
     setSyntaxHighlighterCreator([]() { return new GlslHighlighter; });
+    setCommentStyle(Utils::CommentDefinition::CppStyle);
 
     setEditorActionHandlers(Constants::C_GLSLEDITOR_ID,
                             TextEditorActionHandler::Format
diff --git a/src/plugins/pythoneditor/pythoneditor.cpp b/src/plugins/pythoneditor/pythoneditor.cpp
index 96be07bc01..103165e283 100644
--- a/src/plugins/pythoneditor/pythoneditor.cpp
+++ b/src/plugins/pythoneditor/pythoneditor.cpp
@@ -64,7 +64,6 @@ public:
     {
         addContext(Constants::C_PYTHONEDITOR_ID);
         setDuplicateSupported(true);
-        setCommentStyle(Utils::CommentDefinition::HashStyle);
     }
 
     bool open(QString *errorString, const QString &fileName, const QString &realFileName)
@@ -111,6 +110,7 @@ PythonEditorFactory::PythonEditorFactory()
     setEditorCreator([]() { return new PythonEditor; });
     setIndenterCreator([]() { return new PythonIndenter; });
     setSyntaxHighlighterCreator([]() { return new PythonHighlighter; });
+    setCommentStyle(Utils::CommentDefinition::HashStyle);
 }
 
 } // namespace Internal
diff --git a/src/plugins/qmakeprojectmanager/profileeditor.cpp b/src/plugins/qmakeprojectmanager/profileeditor.cpp
index 4b63db0bee..fd7f6a697f 100644
--- a/src/plugins/qmakeprojectmanager/profileeditor.cpp
+++ b/src/plugins/qmakeprojectmanager/profileeditor.cpp
@@ -62,7 +62,6 @@ public:
     {
         addContext(Constants::C_PROFILEEDITOR);
         setDuplicateSupported(true);
-        setCommentStyle(Utils::CommentDefinition::HashStyle);
         setCompletionAssistProvider(ExtensionSystem::PluginManager::getObject<ProFileCompletionAssistProvider>());
     }
 };
@@ -229,6 +228,7 @@ ProFileEditorFactory::ProFileEditorFactory()
     setEditorWidgetCreator([]() { return new ProFileEditorWidget; });
     setEditorCreator([]() { return new ProFileEditor; });
 
+    setCommentStyle(Utils::CommentDefinition::HashStyle);
     setEditorActionHandlers(Constants::C_PROFILEEDITOR,
                   TextEditorActionHandler::UnCommentSelection
                 | TextEditorActionHandler::JumpToFileUnderCursor);
diff --git a/src/plugins/qmljseditor/qmljseditor.cpp b/src/plugins/qmljseditor/qmljseditor.cpp
index 084b9e9644..5fb099c6d5 100644
--- a/src/plugins/qmljseditor/qmljseditor.cpp
+++ b/src/plugins/qmljseditor/qmljseditor.cpp
@@ -880,7 +880,6 @@ QmlJSEditor::QmlJSEditor()
     addContext(Constants::C_QMLJSEDITOR_ID);
     addContext(ProjectExplorer::Constants::LANG_QMLJS);
     setDuplicateSupported(true);
-    setCommentStyle(Utils::CommentDefinition::CppStyle);
     setCompletionAssistProvider(ExtensionSystem::PluginManager::getObject<Internal::QmlJSCompletionAssistProvider>());
 }
 
@@ -917,13 +916,13 @@ QmlJSEditorFactory::QmlJSEditorFactory()
     setEditorWidgetCreator([]() { return new QmlJSEditorWidget; });
     setEditorCreator([]() { return new QmlJSEditor; });
     setAutoCompleterCreator([]() { return new AutoCompleter; });
+    setCommentStyle(Utils::CommentDefinition::CppStyle);
 
     setEditorActionHandlers(Constants::C_QMLJSEDITOR_ID,
           TextEditorActionHandler::Format
         | TextEditorActionHandler::UnCommentSelection
         | TextEditorActionHandler::UnCollapseAll
         | TextEditorActionHandler::FollowSymbolUnderCursor);
-
 }
 
 } // namespace Internal
diff --git a/src/plugins/texteditor/basetexteditor.cpp b/src/plugins/texteditor/basetexteditor.cpp
index c1e2dbc34c..ffe470e8ee 100644
--- a/src/plugins/texteditor/basetexteditor.cpp
+++ b/src/plugins/texteditor/basetexteditor.cpp
@@ -230,7 +230,6 @@ class BaseTextEditorPrivate
 public:
     BaseTextEditorPrivate() {}
 
-    CommentDefinition m_commentDefinition;
     std::function<CompletionAssistProvider *()> m_completionAssistProvider;
 
     QPointer<BaseTextEditorFactory> m_origin;
@@ -442,6 +441,7 @@ public:
     bool m_editorIsFallBack;
 
     QScopedPointer<AutoCompleter> m_autoCompleter;
+    CommentDefinition m_commentDefinition;
 };
 
 BaseTextEditorWidgetPrivate::BaseTextEditorWidgetPrivate(BaseTextEditorWidget *parent)
@@ -1540,18 +1540,17 @@ void BaseTextEditorWidgetPrivate::moveLineUpDown(bool up)
     m_refactorOverlay->setMarkers(nonAffectedMarkers + affectedMarkers);
 
     bool shouldReindent = true;
-    const CommentDefinition &cd = q->editor()->commentDefinition();
-    if (cd.isValid()) {
+    if (m_commentDefinition.isValid()) {
         QString trimmedText(text.trimmed());
 
-        if (cd.hasSingleLineStyle()) {
-            if (trimmedText.startsWith(cd.singleLine))
+        if (m_commentDefinition.hasSingleLineStyle()) {
+            if (trimmedText.startsWith(m_commentDefinition.singleLine))
                 shouldReindent = false;
         }
-        if (shouldReindent && cd.hasMultiLineStyle()) {
+        if (shouldReindent && m_commentDefinition.hasMultiLineStyle()) {
             // Don't have any single line comments; try multi line.
-            if (trimmedText.startsWith(cd.multiLineStart)
-                && trimmedText.endsWith(cd.multiLineEnd)) {
+            if (trimmedText.startsWith(m_commentDefinition.multiLineStart)
+                && trimmedText.endsWith(m_commentDefinition.multiLineEnd)) {
                 shouldReindent = false;
             }
         }
@@ -6022,7 +6021,7 @@ void BaseTextEditorWidget::rewrapParagraph()
 
 void BaseTextEditorWidget::unCommentSelection()
 {
-    Utils::unCommentSelection(this, editor()->commentDefinition());
+    Utils::unCommentSelection(this, d->m_commentDefinition);
 }
 
 void BaseTextEditorWidget::showEvent(QShowEvent* e)
@@ -6708,16 +6707,6 @@ void BaseTextEditor::select(int toPos)
     editorWidget()->setTextCursor(tc);
 }
 
-CommentDefinition &BaseTextEditor::commentDefinition() const
-{
-    return d->m_commentDefinition;
-}
-
-void BaseTextEditor::setCommentStyle(CommentDefinition::Style style)
-{
-    d->m_commentDefinition.setStyle(style);
-}
-
 CompletionAssistProvider *BaseTextEditor::completionAssistProvider()
 {
     return d->m_completionAssistProvider();
@@ -7132,11 +7121,10 @@ void BaseTextEditorWidget::configureMimeType(const MimeType &mimeType)
             const QSharedPointer<HighlightDefinition> &definition =
                 Manager::instance()->definition(definitionId);
             if (!definition.isNull() && definition->isValid()) {
-                CommentDefinition &cd = editor()->commentDefinition();
-                cd.isAfterWhiteSpaces = definition->isCommentAfterWhiteSpaces();
-                cd.singleLine = definition->singleLineComment();
-                cd.multiLineStart = definition->multiLineCommentStart();
-                cd.multiLineEnd = definition->multiLineCommentEnd();
+                d->m_commentDefinition.isAfterWhiteSpaces = definition->isCommentAfterWhiteSpaces();
+                d->m_commentDefinition.singleLine = definition->singleLineComment();
+                d->m_commentDefinition.multiLineStart = definition->multiLineCommentStart();
+                d->m_commentDefinition.multiLineEnd = definition->multiLineCommentEnd();
 
                 setCodeFoldingSupported(true);
             }
@@ -7227,6 +7215,7 @@ BaseTextEditorFactory::BaseTextEditorFactory(QObject *parent)
 {
     m_editorCreator = []() { return new BaseTextEditor; };
     m_widgetCreator = []() { return new BaseTextEditorWidget; };
+    m_commentStyle = CommentDefinition::NoStyle;
 }
 
 void BaseTextEditorFactory::setDocumentCreator(const DocumentCreator &creator)
@@ -7278,6 +7267,11 @@ void BaseTextEditorFactory::setEditorActionHandlers(uint optionalActions)
     new TextEditorActionHandler(this, id(), optionalActions);
 }
 
+void BaseTextEditorFactory::setCommentStyle(CommentDefinition::Style style)
+{
+    m_commentStyle = style;
+}
+
 BaseTextEditor *BaseTextEditorFactory::duplicateTextEditor(BaseTextEditor *other)
 {
     BaseTextEditor *editor = createEditorHelper(other->editorWidget()->textDocumentPtr());
@@ -7312,6 +7306,7 @@ BaseTextEditor *BaseTextEditorFactory::createEditorHelper(const BaseTextDocument
     widget->setTextDocument(document);
 
     widget->d->m_codeAssistant.configure(editor);
+    widget->d->m_commentDefinition.setStyle(m_commentStyle);
 
     if (m_autoCompleterCreator)
         widget->setAutoCompleter(m_autoCompleterCreator());
diff --git a/src/plugins/texteditor/basetexteditor.h b/src/plugins/texteditor/basetexteditor.h
index bc09f2566c..9f49dff411 100644
--- a/src/plugins/texteditor/basetexteditor.h
+++ b/src/plugins/texteditor/basetexteditor.h
@@ -199,11 +199,6 @@ public:
     /*! Selects text between current cursor position and \a toPos. */
     virtual void select(int toPos);
 
-    /*! Full access to comment definition, */
-    Utils::CommentDefinition &commentDefinition() const;
-    /*! Convenience style setter. */
-    void setCommentStyle(Utils::CommentDefinition::Style style);
-
     CompletionAssistProvider *completionAssistProvider();
     void setCompletionAssistProvider(CompletionAssistProvider *provider); // Not owned.
 
@@ -654,6 +649,8 @@ public:
     void setEditorActionHandlers(Core::Id contextId, uint optionalActions);
     void setEditorActionHandlers(uint optionalActions);
 
+    void setCommentStyle(Utils::CommentDefinition::Style style);
+
     Core::IEditor *createEditor();
 
 private:
@@ -669,6 +666,7 @@ private:
     AutoCompleterCreator m_autoCompleterCreator;
     IndenterCreator m_indenterCreator;
     SyntaxHighLighterCreator m_syntaxHighlighterCreator;
+    Utils::CommentDefinition::Style m_commentStyle;
 };
 
 } // namespace TextEditor
-- 
GitLab