contextbuilder.cpp 3.63 KB
Newer Older
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
1 2
/*************************************************************************************
 *  Copyright (C) 2012 by Aleix Pol <aleixpol@kde.org>                               *
3
 *  Copyright (C) 2012 by Milian Wolff <mail@milianw.de>                             *
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *                                                                                   *
 *  This program is free software; you can redistribute it and/or                    *
 *  modify it under the terms of the GNU General Public License                      *
 *  as published by the Free Software Foundation; either version 2                   *
 *  of the License, or (at your option) any later version.                           *
 *                                                                                   *
 *  This program is distributed in the hope that it will be useful,                  *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of                   *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                    *
 *  GNU General Public License for more details.                                     *
 *                                                                                   *
 *  You should have received a copy of the GNU General Public License                *
 *  along with this program; if not, write to the Free Software                      *
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA   *
 *************************************************************************************/

#include "contextbuilder.h"
21
#include "expressionvisitor.h"
22
#include "parsesession.h"
23
#include "qmljsducontext.h"
24

25 26 27 28
using namespace KDevelop;

ContextBuilder::ContextBuilder()
: ContextBuilderBase()
29
, m_session(0)
30
, m_mapAst(false)
31
, m_editor(new Editor(&m_session))
32 33 34 35 36 37 38 39 40
{
}

RangeInRevision ContextBuilder::editorFindRange(QmlJS::AST::Node* fromNode, QmlJS::AST::Node* toNode)
{
    return m_session->editorFindRange(fromNode, toNode);
}

QualifiedIdentifier ContextBuilder::identifierForNode(QmlJS::AST::IdentifierPropertyName* node)
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
41
{
42
    return QualifiedIdentifier(node->id.toString());
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
43 44
}

45
ContextBuilder::ExpressionType ContextBuilder::findType(QmlJS::AST::Node* node)
46
{
47 48
    ExpressionType ret;

49
    if (!node) {
50 51 52
        ret.type = AbstractType::Ptr(new IntegralType(IntegralType::TypeMixed));

        return ret;
53 54
    }

55
    ExpressionVisitor visitor(currentContext());
56 57 58 59 60

    // Build every needed declaration in node, and then try to guess its type
    node->accept(this);
    node->accept(&visitor);

61 62 63 64
    ret.type = visitor.lastType();
    ret.declaration = visitor.lastDeclaration();

    return ret;
65 66
}

67
void ContextBuilder::setContextOnNode(QmlJS::AST::Node* node, DUContext* context)
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
68
{
69
    m_session->setContextOnNode(node, context);
70
}
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
71

72 73
DUContext* ContextBuilder::contextFromNode(QmlJS::AST::Node* node)
{
74
    return m_session->contextFromNode(node);
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
75 76 77 78
}

void ContextBuilder::startVisiting(QmlJS::AST::Node* node)
{
79 80
    QmlJS::AST::Node::accept(node, this);
}
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
81

82 83 84 85
TopDUContext* ContextBuilder::newTopContext(const RangeInRevision& range, ParsingEnvironmentFile* file)
{
    if (!file) {
        file = new ParsingEnvironmentFile(m_session->url());
Milian Wolff's avatar
Milian Wolff committed
86
        /// identify environment files from this language plugin
87 88
        file->setLanguage(m_session->languageString());
    }
89 90 91 92 93 94 95

    return new QmlJS::QmlJSTopDUContext(m_session->url(), range, file);
}

DUContext* ContextBuilder::newContext(const RangeInRevision& range)
{
    return new QmlJS::QmlJSNormalDUContext(range, currentContext());
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
96
}
97 98 99 100 101

void ContextBuilder::setParseSession(ParseSession* session)
{
    m_session = session;
}
102

103 104 105 106
Editor* ContextBuilder::editor() const
{
    return m_editor.data();
}