File: basicrefactoring.h

package info (click to toggle)
kdevelop 4%3A5.6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 57,892 kB
  • sloc: cpp: 278,773; javascript: 3,558; python: 3,385; sh: 1,317; ansic: 689; xml: 273; php: 95; makefile: 40; lisp: 13; sed: 12
file content (162 lines) | stat: -rw-r--r-- 6,234 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/* This file is part of KDevelop
 *
 * Copyright 2014 Miquel Sabaté <mikisabate@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Library 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 BASICREFACTORING_H_
#define BASICREFACTORING_H_

#include <QObject>
#include <QSharedPointer>
#include <language/languageexport.h>
#include <language/codegen/documentchangeset.h>
#include <language/duchain/navigation/useswidget.h>

class CppLanguageSupport;

namespace KDevelop {
class ContextMenuExtension;
class IndexedDeclaration;
class Context;
class Declaration;
class DUContext;

/**
 * A widget that show the uses that it has collected for
 * the given declaration.
 */
class KDEVPLATFORMLANGUAGE_EXPORT BasicRefactoringCollector
    : public UsesWidget::UsesWidgetCollector
{
    Q_OBJECT

public:
    explicit BasicRefactoringCollector(const IndexedDeclaration& decl);
    QVector<IndexedTopDUContext> allUsingContexts() const;

protected:
    /// Process the uses for the given TopDUContext.
    void processUses(KDevelop::ReferencedTopDUContext topContext) override;

private:
    QVector<IndexedTopDUContext> m_allUsingContexts;
};

/// The base class for Refactoring classes from Language plugins.
class KDEVPLATFORMLANGUAGE_EXPORT BasicRefactoring
    : public QObject
{
    Q_OBJECT

public:
    explicit BasicRefactoring(QObject* parent = nullptr);

    /// Update the context menu with the "Rename" action.
    virtual void fillContextMenu(KDevelop::ContextMenuExtension& extension, KDevelop::Context* context,
                                 QWidget* parent);

    struct NameAndCollector
    {
        QString newName;
        QSharedPointer<BasicRefactoringCollector> collector;
    };

    /**
     * @return Suggestion for new filename based on the current file's name @p current and new identifer @p newName
     */
    virtual QString newFileName(const QUrl& current, const QString& newName);

    /**
     * Add the change(s) related to renaming @p file to @p newName to @p changes and return the result.
     *
     * @param current The URL for the file you want to rename.
     * @param newName The new name of the file *without* the file extension.
     * @param changes The change set to add the rename changes to.
     */
    virtual KDevelop::DocumentChangeSet::ChangeResult addRenameFileChanges(const QUrl& current,
                                                                           const QString& newName,
                                                                           KDevelop::DocumentChangeSet* changes);

    virtual bool shouldRenameUses(Declaration* declaration) const;

    /**
     * @return true if the declaration's file should be renamed if the declaration
     *         was renamed.
     */
    virtual bool shouldRenameFile(KDevelop::Declaration* declaration);

public Q_SLOTS:
    void executeRenameAction();

protected:
    /**
     * Apply the changes to the uses that can be found inside the given
     * context and its children.
     * NOTE: the DUChain must be locked.
     */
    virtual DocumentChangeSet::ChangeResult applyChanges(const QString& oldName, const QString& newName,
                                                         DocumentChangeSet& changes, DUContext* context,
                                                         int usedDeclarationIndex);

    /**
     * Apply the changes to the given declarations.
     * NOTE: the DUChain must be locked.
     */
    virtual DocumentChangeSet::ChangeResult applyChangesToDeclarations(const QString& oldName, const QString& newName,
                                                                       DocumentChangeSet& changes,
                                                                       const QList<IndexedDeclaration>& declarations);

    /**
     * Get the declaration under the current position of the cursor.
     *
     * @param allowUse Set to false if the declarations to be returned
     * cannot come from uses.
     */
    virtual IndexedDeclaration declarationUnderCursor(bool allowUse = true);

    /**
     * Start the renaming of a declaration.
     * This function retrieves the new name for declaration @p decl and if approved renames all instances of it.
     */
    virtual void startInteractiveRename(const KDevelop::IndexedDeclaration& decl);

    /**
     * Asks user to input a new name for @p declaration
     * @return new name or empty string if user changed his mind or new name contains inappropriate symbols (e.g. spaces, points, braces e.t.c) and the collector used for collecting information about @p declaration.
     * NOTE: unlock the DUChain before calling this.
     */
    virtual BasicRefactoring::NameAndCollector newNameForDeclaration(const KDevelop::DeclarationPointer& declaration);

    /**
     * Renames all declarations collected by @p collector from @p oldName to @p newName
     * @param apply - if changes should be applied immediately
     * @return all changes if @p apply is false and empty set otherwise.
     */
    DocumentChangeSet renameCollectedDeclarations(KDevelop::BasicRefactoringCollector* collector,
                                                  const QString& replacementName, const QString& originalName,
                                                  bool apply = true);

    /**
     * @returns true if we can show the interactive rename widget for the
     * given declaration. The default implementation just returns true.
     */
    virtual bool acceptForContextMenu(const Declaration* decl);
};
} // End of namespace KDevelop

#endif /* BASICREFACTORING_H_ */