forked from qt-creator/qt-creator
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>
This commit is contained in:
@@ -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
|
||||
};
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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,57 +332,56 @@ IAssistInterface *GlslEditorWidget::createAssistInterface(
|
||||
}
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// GlslEditor
|
||||
//
|
||||
//////////////////////////////////////////////////////////////////
|
||||
|
||||
GlslEditor::GlslEditor()
|
||||
class GlslEditor : public TextEditor::BaseTextEditor
|
||||
{
|
||||
addContext(C_GLSLEDITOR_ID);
|
||||
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,
|
||||
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
|
||||
} // namespace GlslEditor
|
||||
|
||||
@@ -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;
|
||||
int languageVariant(const QString &mimeType);
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -34,7 +34,5 @@ QtcPlugin {
|
||||
"glslhoverhandler.h",
|
||||
"glslindenter.cpp",
|
||||
"glslindenter.h",
|
||||
"reuse.cpp",
|
||||
"reuse.h",
|
||||
]
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user