Commit 16862432 authored by Rhys Weatherley's avatar Rhys Weatherley
Browse files

Make GLSL highlighter aware of language variant

parent 85aa32df
......@@ -159,7 +159,7 @@ GLSLTextEditor::GLSLTextEditor(QWidget *parent) :
connect(this, SIGNAL(textChanged()), this, SLOT(updateDocument()));
baseTextDocument()->setSyntaxHighlighter(new Highlighter(document()));
baseTextDocument()->setSyntaxHighlighter(new Highlighter(this, document()));
// if (m_modelManager) {
// m_semanticHighlighter->setModelManager(m_modelManager);
......@@ -204,8 +204,8 @@ QString GLSLEditorEditable::id() const
bool GLSLEditorEditable::open(const QString &fileName)
{
bool b = TextEditor::BaseTextEditorEditable::open(fileName);
editor()->setMimeType(Core::ICore::instance()->mimeDatabase()->findByFile(QFileInfo(fileName)).type());
bool b = TextEditor::BaseTextEditorEditable::open(fileName);
return b;
}
......@@ -316,16 +316,7 @@ void GLSLTextEditor::updateDocumentNow()
{
m_updateDocumentTimer->stop();
int variant = 0;
if (isDesktopShader())
variant |= Lexer::Variant_GLSL_120;
else
variant |= Lexer::Variant_GLSL_Qt;
if (isVertexShader())
variant |= Lexer::Variant_VertexShader;
if (isFragmentShader())
variant |= Lexer::Variant_FragmentShader;
int variant = languageVariant();
const QString contents = toPlainText(); // get the code from the editor
const QByteArray preprocessedCode = contents.toLatin1(); // ### use the QtCreator C++ preprocessor.
......@@ -384,28 +375,42 @@ void GLSLTextEditor::updateDocumentNow()
}
}
bool GLSLTextEditor::isDesktopShader() const
{
return mimeType() == QLatin1String("text/x-glsl-vert") ||
mimeType() == QLatin1String("text/x-glsl-frag") ||
mimeType() == QLatin1String("text/x-glsl") || // Could be either.
mimeType() == QLatin1String("application/x-glsl");
}
bool GLSLTextEditor::isVertexShader() const
{
return mimeType() == QLatin1String("text/x-glsl-vert") ||
mimeType() == QLatin1String("text/x-glsl-es-vert") ||
mimeType() == QLatin1String("text/x-glsl") || // Could be either.
mimeType() == QLatin1String("application/x-glsl");
}
bool GLSLTextEditor::isFragmentShader() const
int GLSLTextEditor::languageVariant() const
{
return mimeType() == QLatin1String("text/x-glsl-frag") ||
mimeType() == QLatin1String("text/x-glsl-es-frag") ||
mimeType() == QLatin1String("text/x-glsl") || // Could be either.
mimeType() == QLatin1String("application/x-glsl");
int variant = 0;
QString type = mimeType();
bool isVertex = false;
bool isFragment = false;
bool isDesktop = false;
if (type.isEmpty()) {
// ### Before file has been opened, so don't know the mime type.
isVertex = true;
isFragment = true;
} else if (type == QLatin1String("text/x-glsl") ||
type == QLatin1String("application/x-glsl")) {
isVertex = true;
isFragment = true;
isDesktop = true;
} else if (type == QLatin1String("text/x-glsl-vert")) {
isVertex = true;
isDesktop = true;
} else if (type == QLatin1String("text/x-glsl-frag")) {
isFragment = true;
isDesktop = true;
} else if (type == QLatin1String("text/x-glsl-es-vert")) {
isVertex = true;
} else if (type == QLatin1String("text/x-glsl-es-frag")) {
isFragment = true;
}
if (isDesktop)
variant |= Lexer::Variant_GLSL_120;
else
variant |= Lexer::Variant_GLSL_Qt;
if (isVertex)
variant |= Lexer::Variant_VertexShader;
if (isFragment)
variant |= Lexer::Variant_FragmentShader;
return variant;
}
Document::Ptr GLSLTextEditor::glslDocument() const
......
......@@ -97,9 +97,7 @@ public:
QSet<QString> identifiers() const;
bool isDesktopShader() const;
bool isVertexShader() const;
bool isFragmentShader() const;
int languageVariant() const;
Document::Ptr glslDocument() const;
......
......@@ -27,6 +27,7 @@
**
**************************************************************************/
#include "glslhighlighter.h"
#include "glsleditor.h"
#include <glsl/glsllexer.h>
#include <glsl/glslparser.h>
#include <texteditor/basetextdocumentlayout.h>
......@@ -37,8 +38,8 @@ using namespace GLSLEditor;
using namespace GLSLEditor::Internal;
using namespace TextEditor;
Highlighter::Highlighter(QTextDocument *parent)
: TextEditor::SyntaxHighlighter(parent)
Highlighter::Highlighter(GLSLTextEditor *editor, QTextDocument *parent)
: TextEditor::SyntaxHighlighter(parent), m_editor(editor)
{
}
......@@ -68,9 +69,7 @@ void Highlighter::highlightBlock(const QString &text)
lex.setState(state);
lex.setScanKeywords(false);
lex.setScanComments(true);
const int variant = GLSL::Lexer::Variant_GLSL_Qt | // ### FIXME: hardcoded
GLSL::Lexer::Variant_VertexShader |
GLSL::Lexer::Variant_FragmentShader;
const int variant = m_editor->languageVariant();
lex.setVariant(variant);
int initialState = state;
......@@ -341,9 +340,7 @@ void Highlighter::highlightBlock(const QString &text)
lex.setState(qMax(0, previousState));
lex.setScanKeywords(false);
lex.setScanComments(true);
const int variant = GLSL::Lexer::Variant_GLSL_Qt | // ### FIXME: hardcoded
GLSL::Lexer::Variant_VertexShader |
GLSL::Lexer::Variant_FragmentShader;
const int variant = m_editor->languageVariant();
lex.setVariant(variant);
int foldingIndent = initialBraceDepth;
......
......@@ -32,6 +32,8 @@
#include <texteditor/syntaxhighlighter.h>
namespace GLSLEditor {
class GLSLTextEditor;
namespace Internal {
class Highlighter : public TextEditor::SyntaxHighlighter
......@@ -55,7 +57,7 @@ public:
NumGLSLFormats
};
explicit Highlighter(QTextDocument *parent);
explicit Highlighter(GLSLTextEditor *editor, QTextDocument *parent);
virtual ~Highlighter();
void setFormats(const QVector<QTextCharFormat> &formats);
......@@ -67,6 +69,7 @@ protected:
private:
QTextCharFormat m_formats[NumGLSLFormats];
GLSLTextEditor *m_editor;
};
} // namespace Internal
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment