使用Qt.include(...)导入js文件时缺少QtCreator intellisense

时间:2014-06-04 09:07:50

标签: javascript qt qt-creator qml

Qt Creator是一位优秀的编辑,但有时却非常令人沮丧。事实上,智能感知并不总能正常运作。

示例项目看起来像这样:

//Test1.js file
function test1() {
   console.log('hi from test1');
}

//Test2.js file
Qt.include('Test1.js');
function test2() {
   console.log('hi from test2');
   test1();  
}

//Test.qml file
import QtQuick 1.1
import "Test2.js" as Test2

QtObject {
    Component.onCompleted: {
         Test2.test1(); //<--- intellisense missing here
         Test2.test2();
    }
}

麻烦:

intellisense missing

编辑器intellisense错过导入的test1中包含的Test2.js函数。 Qt.include很简单 - 只需说qml编译器 - 嘿,复制并粘贴这个js文件内容。

问题

  1. 有没有办法解决这个QtCreator行为?
  2. QtCreator插件模型是否允许将此行为添加到现有的智能感知代码中?或者这应该通过修补QtCreator代码库来修复?

1 个答案:

答案 0 :(得分:2)

快速而肮脏的补丁。

需要修补2个文件 source \ qt-creator \ src \ libs \ qmljs \ qmljsdocument.h和qmljsdocument.cpp。 但功能&#34;跟随光标下的符号&#34;如果文件有Qt.include

,则可能无法正常工作
@@ -104,9 +104,13 @@ public:

 private:
     bool parse_helper(int kind);
+    void parseQtInclude(QString dir, QString fileName, QString& result);
+
+    QList<QString> _parsedFileNames;

 private:
     QmlJS::Engine *_engine;
+    QmlJS::Engine *_codeCompleteEngine;
     AST::Node *_ast;
     Bind *_bind;
     QList<QmlJS::DiagnosticMessage> _diagnosticMessages;


@@ -168,6 +168,7 @@ QList<Language::Enum> Document::companionLanguages(Language::Enum language)

 Document::Document(const QString &fileName, Language::Enum language)
     : _engine(0)
+    , _codeCompleteEngine(0)
     , _ast(0)
     , _bind(0)
     , _fileName(QDir::cleanPath(fileName))
@@ -197,6 +198,9 @@ Document::~Document()

     if (_engine)
         delete _engine;
+
+    if (_codeCompleteEngine)
+        delete _codeCompleteEngine;
 }

 Document::MutablePtr Document::create(const QString &fileName, Language::Enum language)
@@ -337,9 +341,54 @@ public:

 } // anonymous namespace

+void Document::parseQtInclude(QString dir, QString fileName, QString& result)
+{
+
+    QFile file(dir + QDir::separator() + fileName);
+
+    if (_parsedFileNames.contains(file.fileName()) || !file.open(QIODevice::ReadOnly | QIODevice::Text)) {
+        return;
+    }
+
+    _parsedFileNames.append(file.fileName());
+
+    QFileInfo fileInfo(file);
+    QTextStream in(&file);
+    QString source = QString();
+
+    while(!in.atEnd()) {
+        source += in.read(2048);
+    }
+
+    file.close();
+    source.remove(QLatin1String(".pragma library"));
+
+    int endPos = 0;
+    int pos = source.indexOf(QLatin1String("Qt.include("), endPos);
+
+    while (pos >= 0 && pos + 11 < source.length()) {
+        QChar comma = source.at(pos + 11);
+        endPos = source.indexOf(comma + QLatin1String(")"), pos);
+        if (endPos == -1)
+            return;
+
+        QString fullStaterment = QString(source.begin() + pos, endPos - pos + 2);
+        QString importName = QString(source.begin() + pos + 12, endPos - pos - 12);
+
+        parseQtInclude(fileInfo.absolutePath(), importName, result);
+
+        source.replace(fullStaterment, QString());
+
+        pos = source.indexOf(QLatin1String("Qt.include("));
+    }
+
+    result += source;
+}
+
 bool Document::parse_helper(int startToken)
 {
     Q_ASSERT(! _engine);
+    Q_ASSERT(! _codeCompleteEngine);
     Q_ASSERT(! _ast);
     Q_ASSERT(! _bind);

@@ -349,6 +398,31 @@ bool Document::parse_helper(int startToken)
     Parser parser(_engine);

     QString source = _source;
+    QString fullSource = _source;
+
+    int endPos = 0;
+    int pos = fullSource.indexOf(QLatin1String("Qt.include("), endPos);
+
+    while (pos >= 0 && pos + 11 < fullSource.length()) {
+        QChar comma = fullSource.at(pos + 11);
+        endPos = fullSource.indexOf(comma + QLatin1String(")"), pos);
+        if (endPos == -1)
+            break;
+
+        QString fullStaterment = QString(fullSource.begin() + pos, endPos - pos + 2);
+        QString importName = QString(fullSource.begin() + pos + 12, endPos - pos - 12);
+        QString result = QString();
+
+        parseQtInclude(this->path(), importName, result);
+
+        fullSource.replace(fullStaterment, result);
+
+        pos = fullSource.indexOf(QLatin1String("Qt.include("));
+        if (pos == -1 || pos + 11 == source.length())
+            break;
+        comma = fullSource.at(pos + 11);
+    }
+
     lexer.setCode(source, /*line = */ 1, /*qmlMode = */isQmlLikeLanguage(_language));

     CollectDirectives collectDirectives(path());
@@ -369,9 +443,37 @@ bool Document::parse_helper(int startToken)
     }

     _ast = parser.rootNode();
+    AST::Node *savedAst = _ast;
     _diagnosticMessages = parser.diagnosticMessages();

+    if (endPos > 0) {
+        _codeCompleteEngine = new Engine();
+        Lexer lexerCodeComplete(_codeCompleteEngine);
+        Parser parserCodeComplete(_codeCompleteEngine);
+
+        bool _parsed;
+
+        lexerCodeComplete.setCode(fullSource, /*line = */ 1, /*qmlMode = */isQmlLikeLanguage(_language));
+        switch (startToken) {
+        case QmlJSGrammar::T_FEED_UI_PROGRAM:
+            _parsed = parserCodeComplete.parse();
+            break;
+        case QmlJSGrammar::T_FEED_JS_PROGRAM:
+            _parsed = parserCodeComplete.parseProgram();
+            break;
+        case QmlJSGrammar::T_FEED_JS_EXPRESSION:
+            _parsed = parserCodeComplete.parseExpression();
+            break;
+        default:
+            Q_ASSERT(0);
+        }
+
+        if (_parsed)
+            _ast = parserCodeComplete.rootNode();
+    }
+
     _bind = new Bind(this, &_diagnosticMessages, collectDirectives.isLibrary, collectDirectives.imports);
+    _ast = savedAst;

     return _parsedCorrectly;
 }