Commit 14ffb1e4 authored by Roberto Raggi's avatar Roberto Raggi
Browse files

Some refactoring needed to create ranges for JavaScript functions.

parent 03fa188b
......@@ -314,6 +314,9 @@ bool Bind::visit(FunctionDeclaration *ast)
return false;
ASTFunctionValue *function = new ASTFunctionValue(ast, &_interp);
// ### set the function's scope.
_currentObjectValue->setProperty(ast->name->asString(), function);
return false; // ### eventually want to visit function bodies
}
......@@ -72,7 +72,6 @@ protected:
protected:
Interpreter::ObjectValue *switchObjectValue(Interpreter::ObjectValue *newObjectValue);
const Interpreter::ObjectValue *lookupType(AST::UiQualifiedId *qualifiedTypeNameId);
Interpreter::ObjectValue *bindObject(AST::UiQualifiedId *qualifiedTypeNameId, AST::UiObjectInitializer *initializer);
private:
......
......@@ -51,7 +51,7 @@ static ObjectValue *pushScope(ObjectValue *next, ObjectValue *onto)
return next;
}
ObjectValue *Link::scopeChainAt(Document::Ptr doc, UiObjectMember *currentObject)
ObjectValue *Link::scopeChainAt(Document::Ptr doc, Node *currentObject)
{
BindPtr bind = doc->bind();
......@@ -188,6 +188,8 @@ void Link::populateImportedTypes(Interpreter::ObjectValue *typeEnv, Document::Pt
void Link::importFile(Interpreter::ObjectValue *typeEnv, Document::Ptr doc,
AST::UiImport *import, const QString &startPath)
{
Q_UNUSED(doc)
if (!import->fileName)
return;
......
......@@ -24,7 +24,7 @@ public:
~Link();
// Get the scope chain for the currentObject inside doc.
Interpreter::ObjectValue *scopeChainAt(Document::Ptr doc, AST::UiObjectMember *currentObject);
Interpreter::ObjectValue *scopeChainAt(Document::Ptr doc, AST::Node *currentObject);
private:
static QList<Document::Ptr> reachableDocuments(Document::Ptr startDoc, const Snapshot &snapshot);
......
......@@ -618,7 +618,7 @@ int QmlCodeCompletion::startCompletion(TextEditor::ITextEditable *editor)
const SemanticInfo semanticInfo = edit->semanticInfo();
const QmlJS::Snapshot snapshot = semanticInfo.snapshot;
const Document::Ptr qmlDocument = semanticInfo.document;
const Document::Ptr document = semanticInfo.document;
const QFileInfo currentFileInfo(fileName);
......@@ -632,11 +632,11 @@ int QmlCodeCompletion::startCompletion(TextEditor::ITextEditable *editor)
// Set up the current scope chain.
Interpreter::ObjectValue *scope = interp.globalObject();
Link link(qmlDocument, snapshot, &interp);
Link link(document, snapshot, &interp);
if (isQmlFile && qmlDocument) {
AST::UiObjectMember *declaringMember = semanticInfo.declaringMember(editor->position());
scope = link.scopeChainAt(qmlDocument, declaringMember);
if (document) {
AST::Node *declaringMember = semanticInfo.declaringMember(editor->position());
scope = link.scopeChainAt(document, declaringMember);
}
// Search for the operator that triggered the completion.
......
......@@ -169,7 +169,7 @@ void QmlHoverHandler::updateHelpIdAndTooltip(TextEditor::ITextEditor *editor, in
if (m_helpId.isEmpty()) {
AST::Node *node = semanticInfo.nodeUnderCursor(pos);
if (node && !(AST::cast<AST::StringLiteral *>(node) != 0 || AST::cast<AST::NumericLiteral *>(node) != 0)) {
AST::UiObjectMember *declaringMember = semanticInfo.declaringMember(pos);
AST::Node *declaringMember = semanticInfo.declaringMember(pos);
Interpreter::Engine interp;
Link link(qmlDocument, snapshot, &interp);
......
......@@ -409,6 +409,20 @@ protected:
return true;
}
#if 0 // ### create ranges for function declarations.
virtual bool visit(AST::FunctionExpression *ast)
{
_ranges.append(createRange(ast));
return true;
}
virtual bool visit(AST::FunctionDeclaration *ast)
{
_ranges.append(createRange(ast));
return true;
}
#endif
Range createRange(AST::UiObjectMember *member, AST::UiObjectInitializer *ast)
{
Range range;
......@@ -422,6 +436,21 @@ protected:
range.end.setPosition(ast->rbraceToken.end());
return range;
}
Range createRange(AST::FunctionExpression *ast)
{
Range range;
range.ast = ast;
range.begin = QTextCursor(_textDocument);
range.begin.setPosition(ast->lbraceToken.begin());
range.end = QTextCursor(_textDocument);
range.end.setPosition(ast->rbraceToken.end());
return range;
}
};
......@@ -463,9 +492,9 @@ protected:
} // end of anonymous namespace
AST::UiObjectMember *SemanticInfo::declaringMember(int cursorPosition) const
AST::Node *SemanticInfo::declaringMember(int cursorPosition) const
{
AST::UiObjectMember *declaringMember = 0;
AST::Node *declaringMember = 0;
for (int i = ranges.size() - 1; i != -1; --i) {
const Range &range = ranges.at(i);
......
......@@ -93,7 +93,7 @@ public:
Range(): ast(0) {}
public: // attributes
QmlJS::AST::UiObjectMember *ast;
QmlJS::AST::Node *ast;
QTextCursor begin;
QTextCursor end;
};
......@@ -106,7 +106,7 @@ public:
int revision() const;
// Returns the declaring member
QmlJS::AST::UiObjectMember *declaringMember(int cursorPosition) const;
QmlJS::AST::Node *declaringMember(int cursorPosition) const;
// Returns the AST node under cursor
QmlJS::AST::Node *nodeUnderCursor(int cursorPosition) const;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment