Javascript 使用Qt.include(…)导入js文件时缺少QtCreator intellisense
Qt-Creator是一个很好的编辑器,但有时它非常令人沮丧。事实上,智能感知并不总是正确工作 示例项目如下所示:Javascript 使用Qt.include(…)导入js文件时缺少QtCreator intellisense,javascript,qt,qt-creator,qml,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 f
//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();
}
}
//Test1.js文件
函数test1(){
log('hifromtest1');
}
//Test2.js文件
Qt.include('Test1.js');
函数test2(){
log('hifromtest2');
test1();
}
//Test.qml文件
导入QtQuick 1.1
将“Test2.js”作为Test2导入
QtObject{
Component.onCompleted:{
Test2.test1();//快速脏补丁
需要修补2个文件
source\qt creator\src\libs\qmljs\qmljsdocument.h和qmljsdocument.cpp。
但是,如果文件中有Qt.include,则函数“Follow symbol under cursor”可能无法正常工作
@@ -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;
}
@-104,9+104,13@@public:
私人:
bool parse_helper(int-kind);
+void parseqtclude(QString目录、QString文件名、QString和结果);
+
+QList_parsedFileNames;
私人:
QmlJS::Engine*\u Engine;
+QmlJS::Engine*\u code完成引擎;
AST::Node*\u AST;
绑定*\u绑定;
QList_诊断信息;
@@-168,6+168,7@@QList文档::公司语言(语言::枚举语言)
Document::Document(常量QString和文件名,语言::枚举语言)
:_引擎(0)
+,\u编解码器完成引擎(0)
,_ast(0)
,_绑定(0)
,_文件名(QDir::cleanPath(文件名))
@@-197,6+198,9@@Document::~Document()
如果(_引擎)
删除引擎;
+
+如果(\u codeCompleteEngine)
+删除_code完成引擎;
}
Document::MutablePtr Document::create(常量QString&fileName,语言::枚举语言)
@@-337,9+341,54@@public:
}//匿名命名空间
+void Document::parseqtclude(QString dir、QString fileName、QString和result)
+{
+
+QFile文件(dir+QDir::separator()+文件名);
+
+if(_parsedFileNames.contains(file.fileName())| |!file.open(QIODevice::ReadOnly | QIODevice::Text)){
+返回;
+ }
+
+_parsedFileNames.append(file.fileName());
+
+QFileInfo文件信息(文件);
+文件中的QTextStream(&F);
+QString源=QString();
+
+而(!in.atEnd()){
+源+=英寸读取(2048);
+ }
+
+file.close();
+删除(QLatin1String(“.pragma库”);
+
+int-endPos=0;
+int pos=source.indexOf(QLatin1String(“Qt.include”),endPos);
+
+而(pos>=0&&pos+11=0&&pos+11路径(),导入名称,结果);
+
+fullSource.replace(fullStaterment,result);
+
+pos=fullSource.indexOf(QLatin1String(“Qt.include”);
+if(pos==1 | | pos+11==source.length())
+中断;
+逗号=完整源代码(位置+11);
+ }
+
setCode(源代码,/*line=*/1,/*qmlMode=*/isqmliklanguage(_语言));
CollectDirections CollectDirections(路径());
@@-369,9+443,37@@bool Document::parse_helper(int startToken)
}
_ast=parser.rootNode();
+AST::Node*savedAst=\u AST;
_diagnosticMessages=解析器.diagnosticMessages();
+如果(结束位置>0){
+_codeCompleteEngine=新引擎();
+Lexer lexerCodeComplete(_codeCompleteEngine);
+解析器parserCodeComplete(_codeCompleteEngine);
+
+布尔解析;
+
+setCode(fullSource,/*line=*/1,/*qmlMode=*/isqmliklanguage(_语言));
+开关(startToken){
+案例QmlJSGrammar::T_FEED_UI_程序:
+_parsed=parsercodeplete.parse();
+中断;
+案例QmlJSGrammar::T_FEED_JS_程序:
+_parsed=parsercodeplete.parseProgram();
+中断;
+case QmlJSGrammar::T_FEED_JS_表达式:
+_parsed=parsercodeplete.parseExpression();
+中断;
+默认值:
+Q_断言(0);
+ }
+
+如果(_解析)
+_ast=parsercodeplete.rootNode();
+ }
+
_bind=新绑定(this,&_diagnosticMessages,collectDirections.isLibrary,collectDirections.imports);
+_ast=savedAst;
返回正确解析的_;
}