forked from qt-creator/qt-creator
Some work on the syntax checker.
This commit is contained in:
@@ -32,6 +32,13 @@
|
||||
#include "qtscripthighlighter.h"
|
||||
#include "qtscripteditorplugin.h"
|
||||
|
||||
#include "parser/javascriptengine_p.h"
|
||||
#include "parser/javascriptparser_p.h"
|
||||
#include "parser/javascriptlexer_p.h"
|
||||
#include "parser/javascriptnodepool_p.h"
|
||||
#include "parser/javascriptastvisitor_p.h"
|
||||
#include "parser/javascriptast_p.h"
|
||||
|
||||
#include <indenter.h>
|
||||
|
||||
#include <coreplugin/icore.h>
|
||||
@@ -42,6 +49,11 @@
|
||||
#include <texteditor/texteditorconstants.h>
|
||||
|
||||
#include <QtGui/QMenu>
|
||||
#include <QtCore/QTimer>
|
||||
|
||||
enum {
|
||||
UPDATE_DOCUMENT_DEFAULT_INTERVAL = 100
|
||||
};
|
||||
|
||||
namespace QtScriptEditor {
|
||||
namespace Internal {
|
||||
@@ -64,6 +76,14 @@ ScriptEditor::ScriptEditor(const Context &context,
|
||||
setCodeFoldingVisible(true);
|
||||
setMimeType(QtScriptEditor::Constants::C_QTSCRIPTEDITOR_MIMETYPE);
|
||||
|
||||
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()));
|
||||
|
||||
baseTextDocument()->setSyntaxHighlighter(new QtScriptHighlighter);
|
||||
}
|
||||
|
||||
@@ -94,6 +114,55 @@ ScriptEditor::Context ScriptEditorEditable::context() const
|
||||
return m_context;
|
||||
}
|
||||
|
||||
void ScriptEditor::updateDocument()
|
||||
{
|
||||
m_updateDocumentTimer->start(UPDATE_DOCUMENT_DEFAULT_INTERVAL);
|
||||
}
|
||||
|
||||
void ScriptEditor::updateDocumentNow()
|
||||
{
|
||||
// ### move in the parser thread.
|
||||
|
||||
m_updateDocumentTimer->stop();
|
||||
|
||||
const QString fileName = file()->fileName();
|
||||
const QString code = toPlainText();
|
||||
|
||||
JavaScriptParser parser;
|
||||
JavaScriptEnginePrivate driver;
|
||||
|
||||
JavaScript::NodePool nodePool(fileName, &driver);
|
||||
driver.setNodePool(&nodePool);
|
||||
|
||||
JavaScript::Lexer lexer(&driver);
|
||||
lexer.setCode(code, /*line = */ 1);
|
||||
driver.setLexer(&lexer);
|
||||
|
||||
QList<QTextEdit::ExtraSelection> selections;
|
||||
|
||||
if (parser.parse(&driver)) {
|
||||
// do something here
|
||||
} else {
|
||||
QTextEdit::ExtraSelection sel;
|
||||
sel.format.setUnderlineColor(Qt::red);
|
||||
sel.format.setUnderlineStyle(QTextCharFormat::WaveUnderline);
|
||||
|
||||
const int line = parser.errorLineNumber();
|
||||
|
||||
QTextCursor c(document()->findBlockByNumber(line - 1));
|
||||
sel.cursor = c;
|
||||
|
||||
if (parser.errorColumnNumber() > 1)
|
||||
sel.cursor.setPosition(c.position() + parser.errorColumnNumber() - 1);
|
||||
|
||||
sel.cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
|
||||
|
||||
selections.append(sel);
|
||||
}
|
||||
|
||||
setExtraSelections(CodeWarningsSelection, selections);
|
||||
}
|
||||
|
||||
void ScriptEditor::setFontSettings(const TextEditor::FontSettings &fs)
|
||||
{
|
||||
TextEditor::BaseTextEditor::setFontSettings(fs);
|
||||
|
||||
@@ -31,6 +31,7 @@
|
||||
#define QTSCRIPTDITORW_H
|
||||
|
||||
#include <texteditor/basetexteditor.h>
|
||||
#include <QTimer>
|
||||
|
||||
namespace Core {
|
||||
class ICore;
|
||||
@@ -76,6 +77,10 @@ public:
|
||||
public slots:
|
||||
virtual void setFontSettings(const TextEditor::FontSettings &);
|
||||
|
||||
private slots:
|
||||
void updateDocument();
|
||||
void updateDocumentNow();
|
||||
|
||||
protected:
|
||||
void contextMenuEvent(QContextMenuEvent *e);
|
||||
TextEditor::BaseTextEditorEditable *createEditableInterface() { return new ScriptEditorEditable(this, m_context); }
|
||||
@@ -86,6 +91,8 @@ private:
|
||||
|
||||
const Context m_context;
|
||||
TextEditor::TextEditorActionHandler *m_ah;
|
||||
|
||||
QTimer *m_updateDocumentTimer;
|
||||
};
|
||||
|
||||
} // namespace Internal
|
||||
|
||||
Reference in New Issue
Block a user