File: refactoring.cpp

package info (click to toggle)
kdevelop-php 24.12.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,616 kB
  • sloc: cpp: 20,858; php: 15,243; xml: 136; sh: 58; makefile: 10
file content (118 lines) | stat: -rw-r--r-- 3,706 bytes parent folder | download | duplicates (2)
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
/*
    SPDX-FileCopyrightText: 2014 Miquel Sabaté <mikisabate@gmail.com>

    SPDX-License-Identifier: LGPL-2.0-or-later
*/


#include <QObject>
#include <codegen/refactoring.h>
#include <language/duchain/declaration.h>
#include <language/duchain/use.h>

namespace Php
{

using KDevelop::DocumentChange;
using KDevelop::DocumentChangeSet;
using KDevelop::DUContext;
using KDevelop::TopDUContext;
using KDevelop::IndexedDeclaration;
using KDevelop::Declaration;
using KDevelop::Use;

Refactoring::Refactoring(QObject *parent)
    : BasicRefactoring(parent)
{
    /* There's nothing to do here. */
}

DocumentChangeSet::ChangeResult Refactoring::applyChanges(
    const QString& oldName,
    const QString& newName,
    DocumentChangeSet& changes,
    DUContext* context,
    int usedDeclarationIndex
)
{
    if (usedDeclarationIndex == std::numeric_limits<int>::max())
        return DocumentChangeSet::ChangeResult::successfulResult();

    for (int a = 0; a < context->usesCount(); ++a) {
        Use use = context->uses()[a];
        if (use.m_declarationIndex != usedDeclarationIndex)
        {
            continue;
        }
        if (use.m_range.isEmpty()) {
            continue;
        }

        if ( shouldRemoveDollar(use.m_range, oldName) )
        {
            use.m_range.start.column++;
        }
        DocumentChangeSet::ChangeResult result =
            changes.addChange(DocumentChange(context->url(), context->transformFromLocalRevision(use.m_range), oldName,
                                             newName));
        if (!result)
            return result;
    }

    const auto childContexts = context->childContexts();
    for (DUContext* child : childContexts) {
       DocumentChangeSet::ChangeResult result = applyChanges(oldName, newName, changes, child, usedDeclarationIndex);
        if (!result)
            return result;
    }

    return DocumentChangeSet::ChangeResult::successfulResult();
}

DocumentChangeSet::ChangeResult Refactoring::applyChangesToDeclarations(const QString& oldName,
                                                        const QString& newName,
                                                        DocumentChangeSet& changes,
                                                        const QList<IndexedDeclaration>& declarations)
{
    KTextEditor::Range range_to_modify;

    for (const IndexedDeclaration decl : declarations) {
        Declaration *declaration = decl.data();
        if (!declaration)
            continue;
        TopDUContext *top = declaration->topContext();
        range_to_modify = declaration->rangeInCurrentRevision();
        if ( shouldRemoveDollar(range_to_modify, oldName) )
        {
            range_to_modify = KTextEditor::Range(
                range_to_modify.start().line(),
                range_to_modify.start().column()+1,
                range_to_modify.end().line(),
                range_to_modify.end().column());
        }
        DocumentChangeSet::ChangeResult result = changes.addChange(DocumentChange(top->url(), range_to_modify, oldName, newName));
        if (!result)
            return result;
    }
    return DocumentChangeSet::ChangeResult::successfulResult();
}

bool Refactoring::shouldRemoveDollar(const KDevelop::RangeInRevision &range, QString name)
{
    if ( range.start.line != range.end.line )
    {
        return false;
    }
    return ( range.end.column - range.start.column == name.length() + 1 );
}

bool Refactoring::shouldRemoveDollar(const KTextEditor::Range &range, QString name)
{
    if ( range.start().line() != range.end().line() )
    {
        return false;
    }
    return ( range.columnWidth() == name.length() + 1 );
}

} // End of namespace Php