forked from qt-creator/qt-creator
Add files missing from 02b7eacf4b
This commit is contained in:
67
src/libs/qmljs/qmljsscopeastpath.cpp
Normal file
67
src/libs/qmljs/qmljsscopeastpath.cpp
Normal file
@@ -0,0 +1,67 @@
|
||||
#include "qmljsscopeastpath.h"
|
||||
|
||||
#include "parser/qmljsast_p.h"
|
||||
|
||||
using namespace QmlJS;
|
||||
using namespace AST;
|
||||
|
||||
ScopeAstPath::ScopeAstPath(Document::Ptr doc)
|
||||
: _doc(doc)
|
||||
{
|
||||
}
|
||||
|
||||
QList<Node *> ScopeAstPath::operator()(quint32 offset)
|
||||
{
|
||||
_result.clear();
|
||||
_offset = offset;
|
||||
if (_doc)
|
||||
Node::accept(_doc->ast(), this);
|
||||
return _result;
|
||||
}
|
||||
|
||||
void ScopeAstPath::accept(Node *node)
|
||||
{ Node::acceptChild(node, this); }
|
||||
|
||||
bool ScopeAstPath::preVisit(Node *node)
|
||||
{
|
||||
if (Statement *stmt = node->statementCast()) {
|
||||
return containsOffset(stmt->firstSourceLocation(), stmt->lastSourceLocation());
|
||||
} else if (ExpressionNode *exp = node->expressionCast()) {
|
||||
return containsOffset(exp->firstSourceLocation(), exp->lastSourceLocation());
|
||||
} else if (UiObjectMember *ui = node->uiObjectMemberCast()) {
|
||||
return containsOffset(ui->firstSourceLocation(), ui->lastSourceLocation());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ScopeAstPath::visit(UiObjectDefinition *node)
|
||||
{
|
||||
_result.append(node);
|
||||
Node::accept(node->initializer, this);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ScopeAstPath::visit(UiObjectBinding *node)
|
||||
{
|
||||
_result.append(node);
|
||||
Node::accept(node->initializer, this);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ScopeAstPath::visit(FunctionDeclaration *node)
|
||||
{
|
||||
return visit(static_cast<FunctionExpression *>(node));
|
||||
}
|
||||
|
||||
bool ScopeAstPath::visit(FunctionExpression *node)
|
||||
{
|
||||
Node::accept(node->formals, this);
|
||||
_result.append(node);
|
||||
Node::accept(node->body, this);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ScopeAstPath::containsOffset(SourceLocation start, SourceLocation end)
|
||||
{
|
||||
return _offset >= start.begin() && _offset <= end.end();
|
||||
}
|
||||
38
src/libs/qmljs/qmljsscopeastpath.h
Normal file
38
src/libs/qmljs/qmljsscopeastpath.h
Normal file
@@ -0,0 +1,38 @@
|
||||
#ifndef QMLJSSCOPEASTPATH_H
|
||||
#define QMLJSSCOPEASTPATH_H
|
||||
|
||||
#include "qmljs_global.h"
|
||||
#include "parser/qmljsastvisitor_p.h"
|
||||
#include "qmljsdocument.h"
|
||||
|
||||
namespace QmlJS {
|
||||
|
||||
class QMLJS_EXPORT ScopeAstPath: protected AST::Visitor
|
||||
{
|
||||
public:
|
||||
ScopeAstPath(Document::Ptr doc);
|
||||
|
||||
QList<AST::Node *> operator()(quint32 offset);
|
||||
|
||||
protected:
|
||||
void accept(AST::Node *node);
|
||||
|
||||
using Visitor::visit;
|
||||
|
||||
virtual bool preVisit(AST::Node *node);
|
||||
virtual bool visit(AST::UiObjectDefinition *node);
|
||||
virtual bool visit(AST::UiObjectBinding *node);
|
||||
virtual bool visit(AST::FunctionDeclaration *node);
|
||||
virtual bool visit(AST::FunctionExpression *node);
|
||||
|
||||
private:
|
||||
bool containsOffset(AST::SourceLocation start, AST::SourceLocation end);
|
||||
|
||||
QList<AST::Node *> _result;
|
||||
Document::Ptr _doc;
|
||||
quint32 _offset;
|
||||
};
|
||||
|
||||
} // namespace QmlJS
|
||||
|
||||
#endif // QMLJSSCOPEASTPATH_H
|
||||
Reference in New Issue
Block a user