forked from qt-creator/qt-creator
Qml: Make import classification in Bind less verbose.
This commit is contained in:
@@ -49,8 +49,7 @@ using namespace QmlJS::Interpreter;
|
||||
information that goes beyond that, you need to create a
|
||||
\l{QmlJS::Interpreter::Context} using \l{QmlJS::Link}.
|
||||
|
||||
The document's imports are classified and available through fileImports(),
|
||||
directoryImports() and libraryImports().
|
||||
The document's imports are classified and available through imports().
|
||||
|
||||
It allows AST to code model lookup through findQmlObject() and findFunctionScope().
|
||||
*/
|
||||
@@ -69,19 +68,9 @@ Bind::~Bind()
|
||||
{
|
||||
}
|
||||
|
||||
QList<Bind::ImportInfo> Bind::fileImports() const
|
||||
QList<Bind::ImportInfo> Bind::imports() const
|
||||
{
|
||||
return _fileImports;
|
||||
}
|
||||
|
||||
QList<Bind::ImportInfo> Bind::directoryImports() const
|
||||
{
|
||||
return _directoryImports;
|
||||
}
|
||||
|
||||
QList<Bind::ImportInfo> Bind::libraryImports() const
|
||||
{
|
||||
return _libraryImports;
|
||||
return _imports;
|
||||
}
|
||||
|
||||
Interpreter::ObjectValue *Bind::idEnvironment() const
|
||||
@@ -209,18 +198,19 @@ bool Bind::visit(UiImport *ast)
|
||||
}
|
||||
|
||||
if (ast->importUri) {
|
||||
info.type = ImportInfo::LibraryImport;
|
||||
info.name = toString(ast->importUri, QLatin1Char('/'));
|
||||
_libraryImports += info;
|
||||
} else if (ast->fileName) {
|
||||
const QFileInfo importFileInfo(_doc->path() + QLatin1Char('/') + ast->fileName->asString());
|
||||
info.name = importFileInfo.absoluteFilePath();
|
||||
if (importFileInfo.isFile())
|
||||
_fileImports += info;
|
||||
info.type = ImportInfo::FileImport;
|
||||
else if (importFileInfo.isDir())
|
||||
_directoryImports += info;
|
||||
//else
|
||||
// error: file or directory does not exist
|
||||
info.type = ImportInfo::DirectoryImport;
|
||||
else
|
||||
info.type = ImportInfo::InvalidFileImport;
|
||||
}
|
||||
_imports += info;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -52,14 +52,22 @@ public:
|
||||
virtual ~Bind();
|
||||
|
||||
struct ImportInfo {
|
||||
enum Type {
|
||||
LibraryImport,
|
||||
FileImport,
|
||||
DirectoryImport,
|
||||
InvalidFileImport // refers a file/directoy that wasn't found
|
||||
};
|
||||
|
||||
Type type;
|
||||
// LibraryImport: uri with '/' separator
|
||||
// Other: absoluteFilePath
|
||||
QString name;
|
||||
ComponentVersion version;
|
||||
AST::UiImport *ast;
|
||||
};
|
||||
|
||||
QList<ImportInfo> fileImports() const;
|
||||
QList<ImportInfo> directoryImports() const;
|
||||
QList<ImportInfo> libraryImports() const;
|
||||
QList<ImportInfo> imports() const;
|
||||
|
||||
Interpreter::ObjectValue *idEnvironment() const;
|
||||
Interpreter::ObjectValue *rootObjectValue() const;
|
||||
@@ -111,9 +119,7 @@ private:
|
||||
QHash<AST::FunctionDeclaration *, Interpreter::ObjectValue *> _functionScopes;
|
||||
QStringList _includedScripts;
|
||||
|
||||
QList<ImportInfo> _fileImports;
|
||||
QList<ImportInfo> _directoryImports;
|
||||
QList<ImportInfo> _libraryImports;
|
||||
QList<ImportInfo> _imports;
|
||||
};
|
||||
|
||||
} // end of namespace Qml
|
||||
|
||||
@@ -579,15 +579,7 @@ AST::Node *SemanticInfo::nodeUnderCursor(int pos) const
|
||||
|
||||
const unsigned cursorPosition = pos;
|
||||
|
||||
foreach (const Bind::ImportInfo &import, document->bind()->fileImports()) {
|
||||
if (importContainsCursor(import.ast, cursorPosition))
|
||||
return import.ast;
|
||||
}
|
||||
foreach (const Bind::ImportInfo &import, document->bind()->directoryImports()) {
|
||||
if (importContainsCursor(import.ast, cursorPosition))
|
||||
return import.ast;
|
||||
}
|
||||
foreach (const Bind::ImportInfo &import, document->bind()->libraryImports()) {
|
||||
foreach (const Bind::ImportInfo &import, document->bind()->imports()) {
|
||||
if (importContainsCursor(import.ast, cursorPosition))
|
||||
return import.ast;
|
||||
}
|
||||
@@ -1389,8 +1381,8 @@ TextEditor::BaseTextEditor::Link QmlJSTextEditor::findLinkAt(const QTextCursor &
|
||||
|
||||
if (AST::UiImport *importAst = cast<AST::UiImport *>(node)) {
|
||||
// if it's a file import, link to the file
|
||||
foreach (const Bind::ImportInfo &import, semanticInfo.document->bind()->fileImports()) {
|
||||
if (import.ast == importAst) {
|
||||
foreach (const Bind::ImportInfo &import, semanticInfo.document->bind()->imports()) {
|
||||
if (import.ast == importAst && import.type == Bind::ImportInfo::FileImport) {
|
||||
BaseTextEditor::Link link(import.name);
|
||||
link.begin = importAst->firstSourceLocation().begin();
|
||||
link.end = importAst->lastSourceLocation().end();
|
||||
|
||||
@@ -276,15 +276,16 @@ static void findNewFileImports(const Document::Ptr &doc, const Snapshot &snapsho
|
||||
QStringList *importedFiles, QSet<QString> *scannedPaths)
|
||||
{
|
||||
// scan files and directories that are explicitly imported
|
||||
foreach (const Bind::ImportInfo &fileImport, doc->bind()->fileImports()) {
|
||||
if (! snapshot.document(fileImport.name))
|
||||
*importedFiles += fileImport.name;
|
||||
foreach (const Bind::ImportInfo &import, doc->bind()->imports()) {
|
||||
if (import.type == Bind::ImportInfo::FileImport) {
|
||||
if (! snapshot.document(import.name))
|
||||
*importedFiles += import.name;
|
||||
} else if (import.type == Bind::ImportInfo::DirectoryImport) {
|
||||
if (snapshot.documentsInDirectory(import.name).isEmpty()) {
|
||||
if (! scannedPaths->contains(import.name)) {
|
||||
*importedFiles += qmlFilesInDirectory(import.name);
|
||||
scannedPaths->insert(import.name);
|
||||
}
|
||||
foreach (const Bind::ImportInfo &directoryImport, doc->bind()->directoryImports()) {
|
||||
if (snapshot.documentsInDirectory(directoryImport.name).isEmpty()) {
|
||||
if (! scannedPaths->contains(directoryImport.name)) {
|
||||
*importedFiles += qmlFilesInDirectory(directoryImport.name);
|
||||
scannedPaths->insert(directoryImport.name);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -296,10 +297,12 @@ static void findNewLibraryImports(const Document::Ptr &doc, const Snapshot &snap
|
||||
{
|
||||
// scan library imports
|
||||
const QStringList importPaths = modelManager->importPaths();
|
||||
foreach (const Bind::ImportInfo &libraryImport, doc->bind()->libraryImports()) {
|
||||
foreach (const Bind::ImportInfo &import, doc->bind()->imports()) {
|
||||
if (import.type != Bind::ImportInfo::LibraryImport)
|
||||
continue;
|
||||
foreach (const QString &importPath, importPaths) {
|
||||
QDir dir(importPath);
|
||||
dir.cd(libraryImport.name);
|
||||
dir.cd(import.name);
|
||||
const QString targetPath = dir.absolutePath();
|
||||
|
||||
// if we know there is a library, done
|
||||
|
||||
Reference in New Issue
Block a user