parsesession.h 6.17 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*************************************************************************************
*  Copyright (C) 2012 by Milian Wolff <mail@milianw.de>                             *
*                                                                                   *
*  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   *
*************************************************************************************/

#ifndef PARSESESSION_H
#define PARSESESSION_H

#include <qmljs/qmljsdocument.h>
23

24
#include <language/duchain/indexedstring.h>
25
#include <language/duchain/problem.h>
26
#include <language/duchain/topducontext.h>
27 28

#include "duchainexport.h"
29 30 31 32

namespace KDevelop
{
class SimpleRange;
33
class RangeInRevision;
34 35
}

36 37
typedef QPair<KDevelop::DUContextPointer, KDevelop::RangeInRevision> SimpleUse;

38 39 40 41
/**
 * This class wraps the qmljs parser and offers some helper functions
 * that make it simpler to use the parser in KDevelop.
 */
42
class KDEVQMLJSDUCHAIN_EXPORT ParseSession
43 44 45 46 47 48 49
{
public:
    /**
     * @return a unique identifier for QML/JS documents.
     */
    static KDevelop::IndexedString languageString();

50 51 52 53 54
    /**
     * @return the QML/JS language corresponding to a file suffix
     */
    static QmlJS::Language::Enum guessLanguageFromSuffix(const QString& path);

55 56 57 58 59 60
    /**
     * Parse the given @p contents.
     *
     * @param url The url for the document you want to parse.
     * @param contents The contents of the document you want to parse.
     */
61
    ParseSession(const KDevelop::IndexedString& url, const QString& contents, int priority);
62 63 64 65 66 67

    /**
     * @return the URL of this session
     */
    KDevelop::IndexedString url() const;

68
    /**
69
     * @return The module name of this file ("/foo/QtQuick_1.0.qml" yields "QtQuick")
70
     */
71 72 73 74 75 76
    QString moduleName() const;

    /**
     * @return The version of this file, or QString if none ("QtQuick_1.0.qml" yields "1.0")
     */
    QString moduleVersion() const;
77

78 79 80 81 82
    /**
     * @return true if the document was properly parsed, false otherwise.
     */
    bool isParsedCorrectly() const;

83 84 85 86 87 88 89 90
    /**
     * @return the root AST node or null if it failed to parse.
     */
    QmlJS::AST::Node* ast() const;

    /**
     * @return the problems encountered during parsing.
     */
Milian Wolff's avatar
cleanup  
Milian Wolff committed
91
    QList<KDevelop::ProblemPointer> problems() const;
92

93 94 95
    /**
     * @return the string representation of @p location.
     */
Milian Wolff's avatar
Milian Wolff committed
96
    QString symbolAt(const QmlJS::AST::SourceLocation& location) const;
97

98 99 100
    /**
     * @return the language of the parsed document.
     */
101
    QmlJS::Language::Enum language() const;
102

103 104 105 106 107
    /**
     * @return the comment related to the given source location or an empty string
     */
    QString commentForLocation(const QmlJS::AST::SourceLocation& location) const;

108 109 110 111 112 113 114 115 116 117 118
    /**
     * Convert @p location to a KDevelop::RangeInRevision and return that.
     */
    KDevelop::RangeInRevision locationToRange(const QmlJS::AST::SourceLocation& location) const;

    /**
     * Convert @p locationFrom and @p locationTo to a KDevelop::RangeInRevision and return that.
     */
    KDevelop::RangeInRevision locationsToRange(const QmlJS::AST::SourceLocation& locationFrom,
                                               const QmlJS::AST::SourceLocation& locationTo) const;

119 120 121 122 123 124
    /**
     * Range that starts at the end of the first token, and ends at the beginning of the second token
     */
    KDevelop::RangeInRevision locationsToInnerRange(const QmlJS::AST::SourceLocation& locationFrom,
                                                    const QmlJS::AST::SourceLocation& locationTo) const;

125 126 127 128 129
    /**
     * @return a range that spans @p fromNode and @p toNode.
     */
    KDevelop::RangeInRevision editorFindRange(QmlJS::AST::Node* fromNode, QmlJS::AST::Node* toNode) const;

130 131 132 133 134 135 136 137 138
    /**
     * Implemented to make the AbstractUseBuilder happy.
     */
    void mapAstUse(QmlJS::AST::Node* node, const SimpleUse& use)
    {
        Q_UNUSED(node);
        Q_UNUSED(use);
    }

139 140 141
    void setContextOnNode(QmlJS::AST::Node* node, KDevelop::DUContext* context);
    KDevelop::DUContext* contextFromNode(QmlJS::AST::Node* node) const;

142 143 144 145 146
    /**
     * Return the context containing the definitions of a QML module
     *
     * @return NULL if the specified module does not exist
     */
147
    KDevelop::ReferencedTopDUContext contextOfModule(const QString &module);
148

149 150 151 152 153 154 155 156 157 158
    /**
     * Return the context of a given QML file, NULL if this file is not yet known
     * to the DUChain.
     *
     * When a file that exists is passed to this method and the file hasn't yet
     * been parsed, it is queued for parsing, and the current file will also be
     * re-parsed after it.
     */
    KDevelop::ReferencedTopDUContext contextOfFile(const QString &fileName);

159 160 161 162 163
    /**
     * Dump AST tree to stdout.
     */
    void dumpNode(QmlJS::AST::Node* node) const;

164 165
private:
    KDevelop::IndexedString m_url;
166 167
    QString m_baseNameWithoutVersion;
    QString m_version;
168
    QmlJS::Document::MutablePtr m_doc;
169
    int m_ownPriority;
170

171 172
    typedef QHash<QmlJS::AST::Node*, KDevelop::DUContext*> NodeToContextHash;
    NodeToContextHash m_astToContext;
173 174 175

    typedef QHash<QString, KDevelop::ReferencedTopDUContext> ModuleToContextHash;
    ModuleToContextHash m_moduleToContext;
176 177 178
};

#endif // PARSESESSION_H