contextbuilder.cpp 3.87 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 21
 *                                                                                   *
 *  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"

22 23 24 25
using namespace KDevelop;

ContextBuilder::ContextBuilder()
: ContextBuilderBase()
26
, m_session()
27 28
, m_editor(0)
, m_mapAst(false)
29 30 31 32 33 34 35 36
{
}

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

37 38 39 40 41 42 43 44 45 46
void ContextBuilder::setEditor(EditorIntegrator *editor)
{
    m_editor = editor;
}

EditorIntegrator* ContextBuilder::editor() const
{
    return m_editor;
}

47
QualifiedIdentifier ContextBuilder::identifierForNode(QmlJS::AST::IdentifierPropertyName* node)
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
48
{
49
    return QualifiedIdentifier(node->id.toString());
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
50 51
}

52
void ContextBuilder::setContextOnNode(QmlJS::AST::Node* node, DUContext* context)
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
53
{
54 55
    m_astToContext.insert(node, context);
}
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
56

57 58 59
DUContext* ContextBuilder::contextFromNode(QmlJS::AST::Node* node)
{
    return m_astToContext.value(node, 0);
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
60 61 62 63
}

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

67 68 69 70
TopDUContext* ContextBuilder::newTopContext(const RangeInRevision& range, ParsingEnvironmentFile* file)
{
    if (!file) {
        file = new ParsingEnvironmentFile(m_session->url());
Milian Wolff's avatar
Milian Wolff committed
71
        /// identify environment files from this language plugin
72 73 74
        file->setLanguage(m_session->languageString());
    }
    return ContextBuilderBase::newTopContext(range, file);
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
75
}
76 77 78 79 80

void ContextBuilder::setParseSession(ParseSession* session)
{
    m_session = session;
}
81 82 83 84 85

bool ContextBuilder::visit(QmlJS::AST::FunctionDeclaration* node)
{
    const QualifiedIdentifier functionName(node->name.toString());

86 87
    const RangeInRevision pRange(m_session->locationToRange(node->lparenToken).end,
                                 m_session->locationToRange(node->rparenToken).start);
88 89 90 91
    DUContext* parameters = openContextInternal(pRange, DUContext::Function, functionName);
    visit(node->formals);
    closeContext();

92 93
    const RangeInRevision bRange(m_session->locationToRange(node->lbraceToken).end,
                                 m_session->locationToRange(node->rbraceToken).start);
94 95 96 97 98 99 100 101
    DUContext* body = openContextInternal(bRange, DUContext::Other, functionName);
    if (compilingContexts()) {
        DUChainWriteLocker lock;
        body->addImportedParentContext(parameters);
    }
    visit(node->body);
    closeContext();

102 103
    // return false, we visited the children manually
    return false;
104
}