1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25 
26 #pragma once
27 
28 #include "qmljstools_global.h"
29 
30 #include <qmljs/qmljsdocument.h>
31 #include <qmljs/qmljscontext.h>
32 #include <qmljs/qmljsstaticanalysismessage.h>
33 #include <qmljs/parser/qmljsengine_p.h>
34 
35 #include <QTextCursor>
36 
37 namespace QmlJS {
38 class ScopeChain;
39 namespace AST { class Node; }
40 }
41 
42 namespace QmlJSTools {
43 
44 class QMLJSTOOLS_EXPORT Range
45 {
46 public:
47     // attributes
48     QmlJS::AST::Node *ast = nullptr;
49     QTextCursor begin;
50     QTextCursor end;
51 };
52 
53 class QMLJSTOOLS_EXPORT SemanticInfo
54 {
55 public:
56     SemanticInfo() = default;
57     explicit SemanticInfo(QmlJS::ScopeChain *rootScopeChain);
58 
59     bool isValid() const;
60     int revision() const;
61 
62     // Returns the AST path
63     QList<QmlJS::AST::Node *> astPath(int cursorPosition) const;
64 
65     // Returns the AST node at the offset (the last member of the astPath)
66     QmlJS::AST::Node *astNodeAt(int cursorPosition) const;
67 
68     // Returns the list of declaration-type nodes that enclose the given position.
69     // It is more robust than astPath because it tracks ranges with text cursors
70     // and will thus be correct even if the document was changed and not yet
71     // reparsed. It does not return the full path of AST nodes.
72     QList<QmlJS::AST::Node *> rangePath(int cursorPosition) const;
73 
74     // Returns the declaring member
75     QmlJS::AST::Node *rangeAt(int cursorPosition) const;
76     QmlJS::AST::Node *declaringMemberNoProperties(int cursorPosition) const;
77 
78     // Returns a scopeChain for the given path
79     QmlJS::ScopeChain scopeChain(const QList<QmlJS::AST::Node *> &path = QList<QmlJS::AST::Node *>()) const;
80 
81     void setRootScopeChain(QSharedPointer<const QmlJS::ScopeChain> rootScopeChain);
82 
83 public: // attributes
84     QmlJS::Document::Ptr document;
85     QmlJS::Snapshot snapshot;
86     QmlJS::ContextPtr context;
87     QList<Range> ranges;
88     QHash<QString, QList<QmlJS::SourceLocation> > idLocations;
89 
90     // these are in addition to the parser messages in the document
91     QList<QmlJS::DiagnosticMessage> semanticMessages;
92     QList<QmlJS::StaticAnalysis::Message> staticAnalysisMessages;
93 
94 private:
95     QSharedPointer<const QmlJS::ScopeChain> m_rootScopeChain;
96 };
97 
98 } // namespace QmlJSTools
99 
100 Q_DECLARE_METATYPE(QmlJSTools::SemanticInfo)
101