File indexing completed on 2024-05-19 04:41:58

0001 /*
0002     SPDX-FileCopyrightText: 2012 Aleix Pol <aleixpol@kde.org>
0003     SPDX-FileCopyrightText: 2012 Milian Wolff <mail@milianw.de>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "contextbuilder.h"
0009 #include "expressionvisitor.h"
0010 #include "parsesession.h"
0011 #include "qmljsducontext.h"
0012 
0013 using namespace KDevelop;
0014 
0015 ContextBuilder::ContextBuilder()
0016 : ContextBuilderBase()
0017 , m_session(nullptr)
0018 {
0019 }
0020 
0021 RangeInRevision ContextBuilder::editorFindRange(QmlJS::AST::Node* fromNode, QmlJS::AST::Node* toNode)
0022 {
0023     return m_session->editorFindRange(fromNode, toNode);
0024 }
0025 
0026 QualifiedIdentifier ContextBuilder::identifierForNode(QmlJS::AST::IdentifierPropertyName* node)
0027 {
0028     return QualifiedIdentifier(node->id.toString());
0029 }
0030 
0031 ContextBuilder::ExpressionType ContextBuilder::findType(QmlJS::AST::Node* node)
0032 {
0033     ExpressionType ret;
0034 
0035     if (!node) {
0036         ret.type = AbstractType::Ptr(new IntegralType(IntegralType::TypeMixed));
0037         ret.isPrototype = false;
0038 
0039         return ret;
0040     }
0041 
0042     ExpressionVisitor visitor(currentContext());
0043 
0044     // Build every needed declaration in node, and then try to guess its type
0045     node->accept(this);
0046     node->accept(&visitor);
0047 
0048     ret.type = visitor.lastType();
0049     ret.declaration = visitor.lastDeclaration();
0050     ret.isPrototype = visitor.isPrototype();
0051 
0052     return ret;
0053 }
0054 
0055 void ContextBuilder::setContextOnNode(QmlJS::AST::Node* node, DUContext* context)
0056 {
0057     m_session->setContextOnNode(node, context);
0058 }
0059 
0060 DUContext* ContextBuilder::contextFromNode(QmlJS::AST::Node* node)
0061 {
0062     return m_session->contextFromNode(node);
0063 }
0064 
0065 void ContextBuilder::startVisiting(QmlJS::AST::Node* node)
0066 {
0067     QmlJS::AST::Node::accept(node, this);
0068 }
0069 
0070 TopDUContext* ContextBuilder::newTopContext(const RangeInRevision& range, ParsingEnvironmentFile* file)
0071 {
0072     if (!file) {
0073         file = new ParsingEnvironmentFile(m_session->url());
0074         /// identify environment files from this language plugin
0075         file->setLanguage(m_session->languageString());
0076     }
0077 
0078     return new QmlJS::QmlJSTopDUContext(m_session->url(), range, file);
0079 }
0080 
0081 DUContext* ContextBuilder::newContext(const RangeInRevision& range)
0082 {
0083     return new QmlJS::QmlJSNormalDUContext(range, currentContext());
0084 }
0085 
0086 void ContextBuilder::setParseSession(ParseSession* session)
0087 {
0088     m_session = session;
0089 }