| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2019 The Qt Company Ltd. | 
| 4 | ** Contact: https://www.qt.io/licensing/ | 
| 5 | ** | 
| 6 | ** This file is part of the tools applications of the Qt Toolkit. | 
| 7 | ** | 
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ | 
| 9 | ** Commercial License Usage | 
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | 
| 11 | ** accordance with the commercial license agreement provided with the | 
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | 
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | 
| 15 | ** information use the contact form at https://www.qt.io/contact-us. | 
| 16 | ** | 
| 17 | ** GNU General Public License Usage | 
| 18 | ** Alternatively, this file may be used under the terms of the GNU | 
| 19 | ** General Public License version 3 as published by the Free Software | 
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT | 
| 21 | ** included in the packaging of this file. Please review the following | 
| 22 | ** information to ensure the GNU General Public License requirements will | 
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. | 
| 24 | ** | 
| 25 | ** $QT_END_LICENSE$ | 
| 26 | ** | 
| 27 | ****************************************************************************/ | 
| 28 |  | 
| 29 | #ifndef COMMENTASTVISITOR_H | 
| 30 | #define  | 
| 31 |  | 
| 32 | #include <QtQml/private/qqmljsastvisitor_p.h> | 
| 33 | #include <QtQml/private/qqmljsast_p.h> | 
| 34 | #include <QtQml/private/qqmljsengine_p.h> | 
| 35 |  | 
| 36 | #include <QHash> | 
| 37 | #include <QString> | 
| 38 | #include <QVector> | 
| 39 |  | 
| 40 | using namespace QQmlJS::AST; | 
| 41 | using namespace QQmlJS; | 
| 42 |  | 
| 43 | struct  | 
| 44 | { | 
| 45 |     enum  : int | 
| 46 |     { | 
| 47 |          = 1, | 
| 48 |          = Front << 1, | 
| 49 |          = Front_Inline << 1, | 
| 50 |          = Back << 1, | 
| 51 |          = Front | Back_Inline, | 
| 52 |          = Front | Back | Front_Inline | Back_Inline | 
| 53 |     }  = Front; | 
| 54 |  | 
| 55 |     () = default; | 
| 56 |     (const QQmlJS::Engine *engine, Location location, QList<SourceLocation> srcLocations) | 
| 57 |         : m_location(location), m_srcLocations(srcLocations) { | 
| 58 |         for (const auto& srcLoc : srcLocations) { | 
| 59 |             m_text += engine->code().mid(position: static_cast<int>(srcLoc.begin()), | 
| 60 |                                          n: static_cast<int>(srcLoc.end() - srcLoc.begin())) + "\n" ; | 
| 61 |         } | 
| 62 |  | 
| 63 |         m_text.chop(n: 1); | 
| 64 |     } | 
| 65 |  | 
| 66 |     QList<SourceLocation> ; | 
| 67 |  | 
| 68 |     bool () const { return !m_srcLocations.isEmpty(); } | 
| 69 |     bool () const { return m_text.contains(s: "\n" ); } | 
| 70 |     bool () const { return m_srcLocations.size() > 1; } | 
| 71 |  | 
| 72 |     bool (const SourceLocation& location) const { | 
| 73 |         for (const SourceLocation& srcLoc : m_srcLocations) { | 
| 74 |             if (srcLoc.begin() == location.begin() && srcLoc.end() == location.end()) | 
| 75 |                 return true; | 
| 76 |         } | 
| 77 |  | 
| 78 |         return false; | 
| 79 |     } | 
| 80 |  | 
| 81 |     quint32 () const | 
| 82 |     { | 
| 83 |         if (isSyntheticMultiline() || !isValid()) | 
| 84 |             return 0; | 
| 85 |  | 
| 86 |         return m_srcLocations[0].startLine + m_text.count(c: QLatin1Char('\n')); | 
| 87 |     } | 
| 88 |  | 
| 89 |     QString ; | 
| 90 | }; | 
| 91 |  | 
| 92 | class  : protected Visitor | 
| 93 | { | 
| 94 | public: | 
| 95 |     (QQmlJS::Engine *engine, Node *rootNode); | 
| 96 |  | 
| 97 |     void () override {} | 
| 98 |  | 
| 99 |     const QHash<Node *, Comment> () const { return m_attachedComments; } | 
| 100 |     const QHash<quint32, Comment> () const { return m_listItemComments; } | 
| 101 |     const QHash<Node *, QVector<Comment>> () const { return m_orphanComments; } | 
| 102 |  | 
| 103 |     bool (UiScriptBinding *node) override; | 
| 104 |     bool (UiObjectBinding *node) override; | 
| 105 |  | 
| 106 |     bool (UiArrayBinding *node) override; | 
| 107 |     void (UiArrayBinding *node) override; | 
| 108 |  | 
| 109 |     bool (UiObjectDefinition *node) override; | 
| 110 |     void (UiObjectDefinition *) override; | 
| 111 |  | 
| 112 |     bool (UiEnumDeclaration *node) override; | 
| 113 |     void (UiEnumDeclaration *node) override; | 
| 114 |  | 
| 115 |     bool (UiEnumMemberList *node) override; | 
| 116 |  | 
| 117 |     bool (StatementList *node) override; | 
| 118 |     void (StatementList *node) override; | 
| 119 |  | 
| 120 |     bool (UiImport *node) override; | 
| 121 |     bool (UiPragma *node) override; | 
| 122 |     bool (UiPublicMember *node) override; | 
| 123 |     bool (FunctionDeclaration *node) override; | 
| 124 | private: | 
| 125 |     bool (const SourceLocation& location) const; | 
| 126 |  | 
| 127 |     QList<SourceLocation> (quint32 line, bool includePrevious = false) const; | 
| 128 |  | 
| 129 |     Comment (SourceLocation first, SourceLocation last, | 
| 130 |                         int locations = Comment::DefaultLocations) const; | 
| 131 |  | 
| 132 |     Comment (Node *node, int locations = Comment::DefaultLocations) const; | 
| 133 |     QVector<Comment> (Node *node) const; | 
| 134 |     void (Node *node, int locations = Comment::DefaultLocations); | 
| 135 |  | 
| 136 |     QQmlJS::Engine *; | 
| 137 |     QHash<Node *, Comment> ; | 
| 138 |     QHash<quint32, Comment> ; | 
| 139 |     QHash<Node *, QVector<Comment>> ; | 
| 140 | }; | 
| 141 |  | 
| 142 | #endif // COMMENTASTVISITOR_H | 
| 143 |  |