diff options
author | hjk <hjk121@nokiamail.com> | 2014-08-22 17:16:02 +0200 |
---|---|---|
committer | hjk <hjk121@nokiamail.com> | 2014-08-25 11:58:44 +0200 |
commit | 3430514eff316c40e80c6392bce75de1c3c00451 (patch) | |
tree | 23739d425a6a7d4a20afe62f5c230180ddc0bad2 | |
parent | 44f0f4dc95a9bae640b33ca935770ff3adc4684b (diff) |
GlslEditor: Some editor creation related cleanup
Use a BaseEditorFactory derived class, move some code around.
Change-Id: Id560a215102016cdbe21809f97be8fc190ed5cf5
Reviewed-by: Christian Stenger <christian.stenger@digia.com>
-rw-r--r-- | src/plugins/glsleditor/glslcompletionassist.cpp | 32 | ||||
-rw-r--r-- | src/plugins/glsleditor/glslcompletionassist.h | 39 | ||||
-rw-r--r-- | src/plugins/glsleditor/glsleditor.cpp | 125 | ||||
-rw-r--r-- | src/plugins/glsleditor/glsleditor.h | 90 | ||||
-rw-r--r-- | src/plugins/glsleditor/glsleditor.pro | 6 | ||||
-rw-r--r-- | src/plugins/glsleditor/glsleditor.qbs | 2 | ||||
-rw-r--r-- | src/plugins/glsleditor/glslhighlighter.cpp | 2 | ||||
-rw-r--r-- | src/plugins/glsleditor/glslhoverhandler.cpp | 11 | ||||
-rw-r--r-- | src/plugins/glsleditor/reuse.cpp | 80 | ||||
-rw-r--r-- | src/plugins/glsleditor/reuse.h | 43 |
10 files changed, 134 insertions, 296 deletions
diff --git a/src/plugins/glsleditor/glslcompletionassist.cpp b/src/plugins/glsleditor/glslcompletionassist.cpp index 28a7934962..2a6a6ed50b 100644 --- a/src/plugins/glsleditor/glslcompletionassist.cpp +++ b/src/plugins/glsleditor/glslcompletionassist.cpp @@ -30,7 +30,6 @@ #include "glslcompletionassist.h" #include "glsleditorconstants.h" #include "glsleditorplugin.h" -#include "reuse.h" #include <glsl/glslengine.h> #include <glsl/glsllexer.h> @@ -64,6 +63,37 @@ using namespace TextEditor; namespace GlslEditor { namespace Internal { +Document::Document() + : _engine(0) + , _ast(0) + , _globalScope(0) +{ +} + +Document::~Document() +{ + delete _globalScope; + delete _engine; +} + +GLSL::Scope *Document::scopeAt(int position) const +{ + foreach (const Range &c, _cursors) { + if (position >= c.cursor.selectionStart() && position <= c.cursor.selectionEnd()) + return c.scope; + } + return _globalScope; +} + +void Document::addRange(const QTextCursor &cursor, GLSL::Scope *scope) +{ + Range c; + c.cursor = cursor; + c.scope = scope; + _cursors.append(c); +} + + enum CompletionOrder { SpecialMemberOrder = -5 }; diff --git a/src/plugins/glsleditor/glslcompletionassist.h b/src/plugins/glsleditor/glslcompletionassist.h index 4ab56aa946..39e23bf275 100644 --- a/src/plugins/glsleditor/glslcompletionassist.h +++ b/src/plugins/glsleditor/glslcompletionassist.h @@ -39,16 +39,51 @@ #include <utils/qtcoverride.h> -#include <QScopedPointer> #include <QIcon> +#include <QScopedPointer> +#include <QSharedPointer> -namespace GLSL { class Function; } +namespace GLSL { +class Engine; +class Function; +class TranslationUnitAST; +class Scope; +} // namespace GLSL namespace TextEditor { class BasicProposalItem; } namespace GlslEditor { namespace Internal { +class Document +{ +public: + typedef QSharedPointer<Document> Ptr; + + Document(); + ~Document(); + + GLSL::Engine *engine() const { return _engine; } + GLSL::TranslationUnitAST *ast() const { return _ast; } + GLSL::Scope *globalScope() const { return _globalScope; } + + GLSL::Scope *scopeAt(int position) const; + void addRange(const QTextCursor &cursor, GLSL::Scope *scope); + +private: + struct Range { + QTextCursor cursor; + GLSL::Scope *scope; + }; + + GLSL::Engine *_engine; + GLSL::TranslationUnitAST *_ast; + GLSL::Scope *_globalScope; + QList<Range> _cursors; + + friend class GlslEditorWidget; +}; + class GlslCompletionAssistInterface; class GlslCompletionAssistProvider : public TextEditor::CompletionAssistProvider diff --git a/src/plugins/glsleditor/glsleditor.cpp b/src/plugins/glsleditor/glsleditor.cpp index 67dbbb0b0c..58a3d461b0 100644 --- a/src/plugins/glsleditor/glsleditor.cpp +++ b/src/plugins/glsleditor/glsleditor.cpp @@ -75,7 +75,6 @@ using namespace TextEditor; using namespace GLSL; -using namespace GlslEditor::Constants; namespace GlslEditor { namespace Internal { @@ -109,35 +108,33 @@ protected: } }; -Document::Document() - : _engine(0) - , _ast(0) - , _globalScope(0) -{ -} +// +// GlslEditorWidget +// -Document::~Document() +class GlslEditorWidget : public BaseTextEditorWidget { - delete _globalScope; - delete _engine; -} +public: + GlslEditorWidget(); -GLSL::Scope *Document::scopeAt(int position) const -{ - foreach (const Range &c, _cursors) { - if (position >= c.cursor.selectionStart() && position <= c.cursor.selectionEnd()) - return c.scope; - } - return _globalScope; -} + int editorRevision() const; + bool isOutdated() const; -void Document::addRange(const QTextCursor &cursor, GLSL::Scope *scope) -{ - Range c; - c.cursor = cursor; - c.scope = scope; - _cursors.append(c); -} + QSet<QString> identifiers() const; + + IAssistInterface *createAssistInterface(AssistKind assistKind, AssistReason reason) const; + +private: + BaseTextEditor *createEditor(); + + void updateDocumentNow(); + void setSelectedElements(); + QString wordUnderCursor() const; + + QTimer m_updateDocumentTimer; + QComboBox *m_outlineCombo; + Document::Ptr m_glslDocument; +}; GlslEditorWidget::GlslEditorWidget() { @@ -200,13 +197,6 @@ bool GlslEditorWidget::isOutdated() const return false; } -bool GlslEditor::open(QString *errorString, const QString &fileName, const QString &realFileName) -{ - textDocument()->setMimeType(Core::MimeDatabase::findByFile(QFileInfo(fileName)).type()); - bool b = BaseTextEditor::open(errorString, fileName, realFileName); - return b; -} - QString GlslEditorWidget::wordUnderCursor() const { QTextCursor tc = textCursor(); @@ -291,7 +281,7 @@ void GlslEditorWidget::updateDocumentNow() } } -int GlslEditorWidget::languageVariant(const QString &type) +int languageVariant(const QString &type) { int variant = 0; bool isVertex = false; @@ -342,56 +332,55 @@ IAssistInterface *GlslEditorWidget::createAssistInterface( } -////////////////////////////////////////////////////////////////// // // GlslEditor // -////////////////////////////////////////////////////////////////// -GlslEditor::GlslEditor() +class GlslEditor : public TextEditor::BaseTextEditor { - addContext(C_GLSLEDITOR_ID); - setDuplicateSupported(true); - setCommentStyle(Utils::CommentDefinition::CppStyle); - setCompletionAssistProvider(ExtensionSystem::PluginManager::getObject<GlslCompletionAssistProvider>()); +public: + GlslEditor() + { + addContext(Constants::C_GLSLEDITOR_ID); + setDuplicateSupported(true); + setCommentStyle(Utils::CommentDefinition::CppStyle); + setCompletionAssistProvider(ExtensionSystem::PluginManager::getObject<GlslCompletionAssistProvider>()); + } - setEditorCreator([]() { return new GlslEditor; }); - setWidgetCreator([]() { return new GlslEditorWidget; }); - - setDocumentCreator([]() -> BaseTextDocument * { - auto doc = new BaseTextDocument(C_GLSLEDITOR_ID); - doc->setIndenter(new GlslIndenter); - new Highlighter(doc); - return doc; - }); -} + bool open(QString *errorString, const QString &fileName, const QString &realFileName) + { + textDocument()->setMimeType(Core::MimeDatabase::findByFile(QFileInfo(fileName)).type()); + bool b = BaseTextEditor::open(errorString, fileName, realFileName); + return b; + } +}; -////////////////////////////////////////////////////////////////// // // GlslEditorFactory // -////////////////////////////////////////////////////////////////// GlslEditorFactory::GlslEditorFactory() { - setId(C_GLSLEDITOR_ID); - setDisplayName(qApp->translate("OpenWith::Editors", C_GLSLEDITOR_DISPLAY_NAME)); - addMimeType(GLSL_MIMETYPE); - addMimeType(GLSL_MIMETYPE_VERT); - addMimeType(GLSL_MIMETYPE_FRAG); - addMimeType(GLSL_MIMETYPE_VERT_ES); - addMimeType(GLSL_MIMETYPE_FRAG_ES); - new TextEditorActionHandler(this, C_GLSLEDITOR_ID, - TextEditorActionHandler::Format - | TextEditorActionHandler::UnCommentSelection - | TextEditorActionHandler::UnCollapseAll); + setId(Constants::C_GLSLEDITOR_ID); + setDisplayName(qApp->translate("OpenWith::Editors", Constants::C_GLSLEDITOR_DISPLAY_NAME)); + addMimeType(Constants::GLSL_MIMETYPE); + addMimeType(Constants::GLSL_MIMETYPE_VERT); + addMimeType(Constants::GLSL_MIMETYPE_FRAG); + addMimeType(Constants::GLSL_MIMETYPE_VERT_ES); + addMimeType(Constants::GLSL_MIMETYPE_FRAG_ES); + + setDocumentCreator([]() { return new BaseTextDocument(Constants::C_GLSLEDITOR_ID); }); + setEditorWidgetCreator([]() { return new GlslEditorWidget; }); + setEditorCreator([]() { return new GlslEditor; }); + setIndenterCreator([]() { return new GlslIndenter; }); + setSyntaxHighlighterCreator([]() { return new Highlighter; }); -} + setEditorActionHandlers(Constants::C_GLSLEDITOR_ID, + TextEditorActionHandler::Format + | TextEditorActionHandler::UnCommentSelection + | TextEditorActionHandler::UnCollapseAll); -Core::IEditor *GlslEditorFactory::createEditor() -{ - return new GlslEditor; } } // namespace Internal diff --git a/src/plugins/glsleditor/glsleditor.h b/src/plugins/glsleditor/glsleditor.h index 59605bd430..d596860983 100644 --- a/src/plugins/glsleditor/glsleditor.h +++ b/src/plugins/glsleditor/glsleditor.h @@ -31,104 +31,18 @@ #define GLSLEDITOR_H #include <texteditor/basetexteditor.h> -#include <coreplugin/editormanager/ieditorfactory.h> - -#include <QSharedPointer> -#include <QSet> -#include <QTimer> - -QT_BEGIN_NAMESPACE -class QComboBox; -QT_END_NAMESPACE - -namespace GLSL { -class Engine; -class TranslationUnitAST; -class Scope; -} // namespace GLSL namespace GlslEditor { namespace Internal { -class GlslEditor; -class GlslEditorWidget; - -class Document -{ -public: - typedef QSharedPointer<Document> Ptr; - - Document(); - ~Document(); - - GLSL::Engine *engine() const { return _engine; } - GLSL::TranslationUnitAST *ast() const { return _ast; } - GLSL::Scope *globalScope() const { return _globalScope; } - - GLSL::Scope *scopeAt(int position) const; - void addRange(const QTextCursor &cursor, GLSL::Scope *scope); - -private: - struct Range { - QTextCursor cursor; - GLSL::Scope *scope; - }; - - GLSL::Engine *_engine; - GLSL::TranslationUnitAST *_ast; - GLSL::Scope *_globalScope; - QList<Range> _cursors; - - friend class GlslEditorWidget; -}; - -class GlslEditorWidget : public TextEditor::BaseTextEditorWidget -{ - Q_OBJECT - -public: - GlslEditorWidget(); - - int editorRevision() const; - bool isOutdated() const; - - QSet<QString> identifiers() const; - - static int languageVariant(const QString &mimeType); - - TextEditor::IAssistInterface *createAssistInterface(TextEditor::AssistKind assistKind, - TextEditor::AssistReason reason) const; - -private: - TextEditor::BaseTextEditor *createEditor(); - - void updateDocumentNow(); - void setSelectedElements(); - QString wordUnderCursor() const; - - QTimer m_updateDocumentTimer; - QComboBox *m_outlineCombo; - Document::Ptr m_glslDocument; -}; +int languageVariant(const QString &mimeType); -class GlslEditor : public TextEditor::BaseTextEditor -{ - Q_OBJECT - -public: - GlslEditor(); - - bool open(QString *errorString, const QString &fileName, const QString &realFileName); -}; - -class GlslEditorFactory : public Core::IEditorFactory +class GlslEditorFactory : public TextEditor::BaseTextEditorFactory { Q_OBJECT public: GlslEditorFactory(); - - Core::IEditor *createEditor(); }; } // namespace Internal diff --git a/src/plugins/glsleditor/glsleditor.pro b/src/plugins/glsleditor/glsleditor.pro index 579e483a09..f2d6fc788c 100644 --- a/src/plugins/glsleditor/glsleditor.pro +++ b/src/plugins/glsleditor/glsleditor.pro @@ -12,8 +12,7 @@ glslhighlighter.h \ glslautocompleter.h \ glslindenter.h \ glslhoverhandler.h \ - glslcompletionassist.h \ - reuse.h +glslcompletionassist.h SOURCES += \ glsleditor.cpp \ @@ -23,7 +22,6 @@ glslhighlighter.cpp \ glslautocompleter.cpp \ glslindenter.cpp \ glslhoverhandler.cpp \ - glslcompletionassist.cpp \ - reuse.cpp +glslcompletionassist.cpp RESOURCES += glsleditor.qrc diff --git a/src/plugins/glsleditor/glsleditor.qbs b/src/plugins/glsleditor/glsleditor.qbs index c1b1b4da19..efff01a1e8 100644 --- a/src/plugins/glsleditor/glsleditor.qbs +++ b/src/plugins/glsleditor/glsleditor.qbs @@ -34,7 +34,5 @@ QtcPlugin { "glslhoverhandler.h", "glslindenter.cpp", "glslindenter.h", - "reuse.cpp", - "reuse.h", ] } diff --git a/src/plugins/glsleditor/glslhighlighter.cpp b/src/plugins/glsleditor/glslhighlighter.cpp index 4b98fa9493..f8e01e92b3 100644 --- a/src/plugins/glsleditor/glslhighlighter.cpp +++ b/src/plugins/glsleditor/glslhighlighter.cpp @@ -92,7 +92,7 @@ void Highlighter::highlightBlock(const QString &text) lex.setState(state); lex.setScanKeywords(false); lex.setScanComments(true); - const int variant = GlslEditorWidget::languageVariant(parent() + const int variant = languageVariant(parent() ? static_cast<BaseTextDocument*>(parent())->mimeType() : QString()); lex.setVariant(variant); diff --git a/src/plugins/glsleditor/glslhoverhandler.cpp b/src/plugins/glsleditor/glslhoverhandler.cpp index 4fa4a114af..ef2232fa3c 100644 --- a/src/plugins/glsleditor/glslhoverhandler.cpp +++ b/src/plugins/glsleditor/glslhoverhandler.cpp @@ -29,6 +29,7 @@ #include "glslhoverhandler.h" #include "glsleditor.h" +#include "glsleditorconstants.h" #include <coreplugin/editormanager/ieditor.h> #include <coreplugin/editormanager/editormanager.h> @@ -36,8 +37,6 @@ #include <extensionsystem/pluginmanager.h> #include <texteditor/basetexteditor.h> -#include <QTextCursor> - using namespace Core; namespace GlslEditor { @@ -51,15 +50,13 @@ GlslHoverHandler::~GlslHoverHandler() bool GlslHoverHandler::acceptEditor(IEditor *editor) { - return qobject_cast<GlslEditor *>(editor) != 0; + return editor->context().contains(Constants::C_GLSLEDITOR_ID); } void GlslHoverHandler::identifyMatch(TextEditor::BaseTextEditor *editor, int pos) { - if (GlslEditorWidget *glslEditor = qobject_cast<GlslEditorWidget *>(editor->widget())) { - if (! glslEditor->extraSelectionTooltip(pos).isEmpty()) - setToolTip(glslEditor->extraSelectionTooltip(pos)); - } + if (!editor->editorWidget()->extraSelectionTooltip(pos).isEmpty()) + setToolTip(editor->editorWidget()->extraSelectionTooltip(pos)); } void GlslHoverHandler::decorateToolTip() diff --git a/src/plugins/glsleditor/reuse.cpp b/src/plugins/glsleditor/reuse.cpp deleted file mode 100644 index 5d6266bc21..0000000000 --- a/src/plugins/glsleditor/reuse.cpp +++ /dev/null @@ -1,80 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). -** Contact: http://www.qt-project.org/legal -** -** This file is part of Qt Creator. -** -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and Digia. For licensing terms and -** conditions see http://qt.digia.com/licensing. For further information -** use the contact form at http://qt.digia.com/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -****************************************************************************/ - -#include "reuse.h" - -#include <QLatin1String> - -#include <glsl/glsllexer.h> - -using namespace GLSL; - -namespace GlslEditor { -namespace Internal { - -int languageVariant(const QString &mimeType) -{ - int variant = 0; - bool isVertex = false; - bool isFragment = false; - bool isDesktop = false; - if (mimeType.isEmpty()) { - // ### Before file has been opened, so don't know the mime type. - isVertex = true; - isFragment = true; - } else if (mimeType == QLatin1String("text/x-glsl") || - mimeType == QLatin1String("application/x-glsl")) { - isVertex = true; - isFragment = true; - isDesktop = true; - } else if (mimeType == QLatin1String("text/x-glsl-vert")) { - isVertex = true; - isDesktop = true; - } else if (mimeType == QLatin1String("text/x-glsl-frag")) { - isFragment = true; - isDesktop = true; - } else if (mimeType == QLatin1String("text/x-glsl-es-vert")) { - isVertex = true; - } else if (mimeType == QLatin1String("text/x-glsl-es-frag")) { - isFragment = true; - } - if (isDesktop) - variant |= Lexer::Variant_GLSL_120; - else - variant |= Lexer::Variant_GLSL_ES_100; - if (isVertex) - variant |= Lexer::Variant_VertexShader; - if (isFragment) - variant |= Lexer::Variant_FragmentShader; - return variant; -} - -} // namespace Internal -} // namespace GlslEditor - diff --git a/src/plugins/glsleditor/reuse.h b/src/plugins/glsleditor/reuse.h deleted file mode 100644 index e0f88973cd..0000000000 --- a/src/plugins/glsleditor/reuse.h +++ /dev/null @@ -1,43 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). -** Contact: http://www.qt-project.org/legal -** -** This file is part of Qt Creator. -** -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and Digia. For licensing terms and -** conditions see http://qt.digia.com/licensing. For further information -** use the contact form at http://qt.digia.com/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -****************************************************************************/ - -#ifndef REUSE_H -#define REUSE_H - -#include <QtGlobal> - -namespace GlslEditor { -namespace Internal { - -int languageVariant(const QString &mimeType); - -} // namespace Internal -} // namespace GlslEditor - -#endif // REUSE_H |