glsleditor.cpp 10.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** 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.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/

#include "glsleditor.h"
#include "glsleditoreditable.h"
#include "glsleditorconstants.h"
#include "glsleditorplugin.h"
34
#include "glslhighlighter.h"
35 36
#include "glslautocompleter.h"
#include "glslindenter.h"
37

Roberto Raggi's avatar
Roberto Raggi committed
38 39 40
#include <glsl/glsllexer.h>
#include <glsl/glslparser.h>
#include <glsl/glslengine.h>
41
#include <glsl/glslsemantic.h>
Roberto Raggi's avatar
Roberto Raggi committed
42
#include <glsl/glslsymbols.h>
Roberto Raggi's avatar
Roberto Raggi committed
43

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/uniqueidmanager.h>
#include <coreplugin/actionmanager/command.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/icore.h>
#include <coreplugin/mimedatabase.h>
#include <extensionsystem/pluginmanager.h>
#include <texteditor/basetextdocument.h>
#include <texteditor/fontsettings.h>
#include <texteditor/tabsettings.h>
#include <texteditor/texteditorconstants.h>
#include <texteditor/texteditorsettings.h>
#include <texteditor/syntaxhighlighter.h>
#include <texteditor/refactoroverlay.h>
#include <texteditor/tooltip/tooltip.h>
#include <qmldesigner/qmldesignerconstants.h>
#include <utils/changeset.h>
#include <utils/uncommentselection.h>

#include <QtCore/QFileInfo>
#include <QtCore/QSignalMapper>
#include <QtCore/QTimer>
Roberto Raggi's avatar
Roberto Raggi committed
67
#include <QtCore/QDebug>
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

#include <QtGui/QMenu>
#include <QtGui/QComboBox>
#include <QtGui/QHeaderView>
#include <QtGui/QInputDialog>
#include <QtGui/QMainWindow>
#include <QtGui/QToolBar>
#include <QtGui/QTreeView>

using namespace GLSL;
using namespace GLSLEditor;
using namespace GLSLEditor::Internal;

enum {
    UPDATE_DOCUMENT_DEFAULT_INTERVAL = 150
};

GLSLTextEditor::GLSLTextEditor(QWidget *parent) :
    TextEditor::BaseTextEditor(parent),
    m_outlineCombo(0)
{
    setParenthesesMatchingEnabled(true);
    setMarksVisible(true);
    setCodeFoldingSupported(true);
92 93
    setIndenter(new GLSLIndenter());
    setAutoCompleter(new GLSLCompleter());
94 95 96 97 98 99 100 101

    m_updateDocumentTimer = new QTimer(this);
    m_updateDocumentTimer->setInterval(UPDATE_DOCUMENT_DEFAULT_INTERVAL);
    m_updateDocumentTimer->setSingleShot(true);
    connect(m_updateDocumentTimer, SIGNAL(timeout()), this, SLOT(updateDocumentNow()));

    connect(this, SIGNAL(textChanged()), this, SLOT(updateDocument()));

102
    baseTextDocument()->setSyntaxHighlighter(new Highlighter(document()));
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131

//    if (m_modelManager) {
//        m_semanticHighlighter->setModelManager(m_modelManager);
//        connect(m_modelManager, SIGNAL(documentUpdated(GLSL::Document::Ptr)),
//                this, SLOT(onDocumentUpdated(GLSL::Document::Ptr)));
//        connect(m_modelManager, SIGNAL(libraryInfoUpdated(QString,GLSL::LibraryInfo)),
//                this, SLOT(forceSemanticRehighlight()));
//        connect(this->document(), SIGNAL(modificationChanged(bool)), this, SLOT(modificationChanged(bool)));
//    }
}

GLSLTextEditor::~GLSLTextEditor()
{
}

int GLSLTextEditor::editorRevision() const
{
    //return document()->revision();
    return 0;
}

bool GLSLTextEditor::isOutdated() const
{
//    if (m_semanticInfo.revision() != editorRevision())
//        return true;

    return false;
}

132 133 134 135 136
QSet<QString> GLSLTextEditor::identifiers() const
{
    return m_identifiers;
}

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
Core::IEditor *GLSLEditorEditable::duplicate(QWidget *parent)
{
    GLSLTextEditor *newEditor = new GLSLTextEditor(parent);
    newEditor->duplicateFrom(editor());
    GLSLEditorPlugin::instance()->initializeEditor(newEditor);
    return newEditor->editableInterface();
}

QString GLSLEditorEditable::id() const
{
    return QLatin1String(GLSLEditor::Constants::C_GLSLEDITOR_ID);
}

bool GLSLEditorEditable::open(const QString &fileName)
{
    bool b = TextEditor::BaseTextEditorEditable::open(fileName);
    editor()->setMimeType(Core::ICore::instance()->mimeDatabase()->findByFile(QFileInfo(fileName)).type());
    return b;
}

Core::Context GLSLEditorEditable::context() const
{
    return m_context;
}

void GLSLTextEditor::setFontSettings(const TextEditor::FontSettings &fs)
{
    TextEditor::BaseTextEditor::setFontSettings(fs);
    Highlighter *highlighter = qobject_cast<Highlighter*>(baseTextDocument()->syntaxHighlighter());
    if (!highlighter)
        return;

    /*
        NumberFormat,
        StringFormat,
        TypeFormat,
        KeywordFormat,
        LabelFormat,
        CommentFormat,
        VisualWhitespace,
     */
    static QVector<QString> categories;
    if (categories.isEmpty()) {
        categories << QLatin1String(TextEditor::Constants::C_NUMBER)
                   << QLatin1String(TextEditor::Constants::C_STRING)
                   << QLatin1String(TextEditor::Constants::C_TYPE)
                   << QLatin1String(TextEditor::Constants::C_KEYWORD)
184 185 186
                   << QLatin1String(TextEditor::Constants::C_OPERATOR)
                   << QLatin1String(TextEditor::Constants::C_PREPROCESSOR)
                   << QLatin1String(TextEditor::Constants::C_LABEL)
187
                   << QLatin1String(TextEditor::Constants::C_COMMENT)
188 189 190 191
                   << QLatin1String(TextEditor::Constants::C_DOXYGEN_COMMENT)
                   << QLatin1String(TextEditor::Constants::C_DOXYGEN_TAG)
                   << QLatin1String(TextEditor::Constants::C_VISUAL_WHITESPACE)
                   << QLatin1String(TextEditor::Constants::C_REMOVED_LINE);
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
    }

    highlighter->setFormats(fs.toTextCharFormats(categories));
    highlighter->rehighlight();
}

QString GLSLTextEditor::wordUnderCursor() const
{
    QTextCursor tc = textCursor();
    const QChar ch = characterAt(tc.position() - 1);
    // make sure that we're not at the start of the next word.
    if (ch.isLetterOrNumber() || ch == QLatin1Char('_'))
        tc.movePosition(QTextCursor::Left);
    tc.movePosition(QTextCursor::StartOfWord);
    tc.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
    const QString word = tc.selectedText();
    return word;
}

TextEditor::BaseTextEditorEditable *GLSLTextEditor::createEditableInterface()
{
    GLSLEditorEditable *editable = new GLSLEditorEditable(this);
    createToolBar(editable);
    return editable;
}

void GLSLTextEditor::createToolBar(GLSLEditorEditable *editable)
{
    m_outlineCombo = new QComboBox;
    m_outlineCombo->setMinimumContentsLength(22);
Roberto Raggi's avatar
Roberto Raggi committed
222 223

    // ### m_outlineCombo->setModel(m_outlineModel);
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256

    QTreeView *treeView = new QTreeView;
    treeView->header()->hide();
    treeView->setItemsExpandable(false);
    treeView->setRootIsDecorated(false);
    m_outlineCombo->setView(treeView);
    treeView->expandAll();

    //m_outlineCombo->setSizeAdjustPolicy(QComboBox::AdjustToContents);

    // Make the combo box prefer to expand
    QSizePolicy policy = m_outlineCombo->sizePolicy();
    policy.setHorizontalPolicy(QSizePolicy::Expanding);
    m_outlineCombo->setSizePolicy(policy);

    QToolBar *toolBar = static_cast<QToolBar*>(editable->toolBar());

    QList<QAction*> actions = toolBar->actions();
    toolBar->insertWidget(actions.first(), m_outlineCombo);
}

bool GLSLTextEditor::event(QEvent *e)
{
    return BaseTextEditor::event(e);
}

void GLSLTextEditor::unCommentSelection()
{
    Utils::unCommentSelection(this);
}

void GLSLTextEditor::updateDocument()
{
Roberto Raggi's avatar
Roberto Raggi committed
257
    m_updateDocumentTimer->start();
258 259 260 261
}

void GLSLTextEditor::updateDocumentNow()
{
Roberto Raggi's avatar
Roberto Raggi committed
262 263
    m_updateDocumentTimer->stop();

264 265 266
    const int variant = Lexer::Variant_GLSL_Qt | // ### hardcoded
                        Lexer::Variant_VertexShader |
                        Lexer::Variant_FragmentShader;
Roberto Raggi's avatar
Roberto Raggi committed
267 268 269 270 271
    const QString contents = toPlainText(); // get the code from the editor
    const QByteArray preprocessedCode = contents.toLatin1(); // ### use the QtCreator C++ preprocessor.

    Engine engine;
    Parser parser(&engine, preprocessedCode.constData(), preprocessedCode.size(), variant);
272
    TranslationUnitAST *ast = parser.parse();
Roberto Raggi's avatar
Roberto Raggi committed
273

Roberto Raggi's avatar
Roberto Raggi committed
274 275 276 277 278 279 280 281
    GLSLEditorPlugin *plugin = GLSLEditorPlugin::instance();

    Semantic sem;
    Scope *globalScope = engine.newNamespace();
    sem.translationUnit(plugin->shaderInit()->ast, globalScope, plugin->shaderInit()->engine);
    sem.translationUnit(plugin->vertexShaderInit()->ast, globalScope, plugin->vertexShaderInit()->engine);
    sem.translationUnit(plugin->fragmentShaderInit()->ast, globalScope, plugin->fragmentShaderInit()->engine);
    sem.translationUnit(ast, globalScope, &engine);
282

Roberto Raggi's avatar
Roberto Raggi committed
283 284 285 286 287 288 289 290 291
    QTextCharFormat errorFormat;
    errorFormat.setUnderlineStyle(QTextCharFormat::WaveUnderline);
    errorFormat.setUnderlineColor(Qt::red);

    QTextCharFormat warningFormat;
    warningFormat.setUnderlineStyle(QTextCharFormat::WaveUnderline);
    warningFormat.setUnderlineColor(Qt::darkYellow);

    QList<QTextEdit::ExtraSelection> sels;
292
    QSet<int> errors;
Roberto Raggi's avatar
Roberto Raggi committed
293 294 295 296

    foreach (const DiagnosticMessage &m, engine.diagnosticMessages()) {
        if (! m.line())
            continue;
297 298 299 300
        else if (errors.contains(m.line()))
            continue;

        errors.insert(m.line());
Roberto Raggi's avatar
Roberto Raggi committed
301 302 303 304 305 306 307 308 309 310 311 312 313

        QTextCursor cursor(document()->findBlockByNumber(m.line() - 1));
        cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);

        QTextEdit::ExtraSelection sel;
        sel.cursor = cursor;
        sel.format = m.isError() ? errorFormat : warningFormat;
        sel.format.setToolTip(m.message());
        sels.append(sel);
    }

    setExtraSelections(CodeWarningsSelection, sels);

314 315
    // refresh the identifiers.
    m_identifiers = engine.identifiers();
316
}