File indexing completed on 2024-05-12 04:39:09

0001 /*
0002     SPDX-FileCopyrightText: 2009 David Nolden <david.nolden.kdevelop@art-master.de>
0003     SPDX-FileCopyrightText: 2015 Sergey Kalinichev <kalinichev.so.0@gmail.com>
0004 
0005     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0006 */
0007 
0008 #ifndef CLANG_SOURCEMANIPULATION_H
0009 #define CLANG_SOURCEMANIPULATION_H
0010 
0011 #include <language/codegen/coderepresentation.h>
0012 #include <language/codegen/documentchangeset.h>
0013 #include <language/duchain/declaration.h>
0014 
0015 class SourceCodeInsertion : public QSharedData
0016 {
0017 public:
0018     explicit SourceCodeInsertion(KDevelop::TopDUContext* topContext);
0019     ~SourceCodeInsertion();
0020 
0021     /// Set optional sub-scope into which the code should be inserted, under 'context'
0022     void setSubScope(const KDevelop::QualifiedIdentifier& scope);
0023 
0024     /// @param body function-body, including parens
0025     bool insertFunctionDeclaration(KDevelop::Declaration* decl, const KDevelop::Identifier& id, const QString& body = QString());
0026 
0027     KDevelop::DocumentChangeSet changes();
0028 
0029 private:
0030 
0031     /// Returns the exact position where the item should be inserted so it is in the given line.
0032     /// The inserted item has to start with a newline, and does not need to end with a newline.
0033     KTextEditor::Range insertionRange(int line);
0034 
0035     /// Returns a line for inserting the given declaration
0036     int findInsertionPoint() const;
0037 
0038     // Should apply m_scope to the given declaration string
0039     QString applySubScope(const QString& decl) const;
0040 
0041     QString indentation() const;
0042     QString applyIndentation(const QString& decl) const;
0043 
0044     KTextEditor::Cursor end() const;
0045 
0046 private:
0047     KDevelop::DocumentChangeSet m_changeSet;
0048     KDevelop::DUContextPointer m_context;
0049     KDevelop::QualifiedIdentifier m_scope;
0050     KDevelop::TopDUContextPointer m_topContext;
0051     // Represents the whole code of the manipulated top-context for reading.
0052     // Must be checked for zero before using. It is zero if the file could not be read.
0053     const KDevelop::CodeRepresentation::Ptr m_codeRepresentation;
0054 };
0055 
0056 #endif // CLANG_SOURCEMANIPULATION_H