aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/cppeditor/cpprefactoringchanges.cpp
blob: 053daeb245f8595a6ac75631588d1004560232e2 (plain)
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "cpprefactoringchanges.h"

#include "cppeditorconstants.h"
#include "cppworkingcopy.h"

#include <projectexplorer/editorconfiguration.h>

#include <utils/qtcassert.h>

#include <texteditor/icodestylepreferencesfactory.h>
#include <texteditor/tabsettings.h>
#include <texteditor/textdocument.h>
#include <texteditor/texteditorsettings.h>

#include <QTextDocument>

#include <utility>

using namespace CPlusPlus;
using namespace Utils;

namespace CppEditor {
namespace Internal {
class CppRefactoringChangesData
{
public:
    explicit CppRefactoringChangesData(const CPlusPlus::Snapshot &snapshot);

    CPlusPlus::Snapshot m_snapshot;
    WorkingCopy m_workingCopy;
};
} // namespace Internal

using namespace Internal;

CppRefactoringChanges::CppRefactoringChanges(const Snapshot &snapshot)
    : m_data(new CppRefactoringChangesData(snapshot))
{
}

CppRefactoringFilePtr CppRefactoringChanges::file(TextEditor::TextEditorWidget *editor, const Document::Ptr &document)
{
    CppRefactoringFilePtr result(new CppRefactoringFile(editor));
    result->setCppDocument(document);
    return result;
}

TextEditor::RefactoringFilePtr CppRefactoringChanges::file(const FilePath &filePath) const
{
    return cppFile(filePath);
}

CppRefactoringFilePtr CppRefactoringChanges::cppFile(const Utils::FilePath &filePath) const
{
    return CppRefactoringFilePtr(new CppRefactoringFile(filePath, m_data));
}

CppRefactoringFileConstPtr CppRefactoringChanges::fileNoEditor(const FilePath &filePath) const
{
    QTextDocument *document = nullptr;
    if (const auto source = m_data->m_workingCopy.source(filePath))
        document = new QTextDocument(QString::fromUtf8(*source));
    CppRefactoringFilePtr result(new CppRefactoringFile(document, filePath));
    result->m_data = m_data.staticCast<CppRefactoringChangesData>();

    return result;
}

const Snapshot &CppRefactoringChanges::snapshot() const
{
    return m_data->m_snapshot;
}

CppRefactoringFile::CppRefactoringFile(const FilePath &filePath, const QSharedPointer<CppRefactoringChangesData> &data)
    : RefactoringFile(filePath), m_data(data)
{
    const Snapshot &snapshot = data->m_snapshot;
    m_cppDocument = snapshot.document(filePath);
}

CppRefactoringFile::CppRefactoringFile(QTextDocument *document, const FilePath &filePath)
    : RefactoringFile(document, filePath)
{
}

CppRefactoringFile::CppRefactoringFile(TextEditor::TextEditorWidget *editor)
    : RefactoringFile(editor)
{
}

Document::Ptr CppRefactoringFile::cppDocument() const
{
    if (!m_cppDocument || !m_cppDocument->translationUnit() ||
            !m_cppDocument->translationUnit()->ast()) {
        const QByteArray source = document()->toPlainText().toUtf8();
        const Snapshot &snapshot = m_data->m_snapshot;

        m_cppDocument = snapshot.preprocessedDocument(source, filePath());
        m_cppDocument->check();
    }

    return m_cppDocument;
}

void CppRefactoringFile::setCppDocument(Document::Ptr document)
{
    m_cppDocument = document;
}

Scope *CppRefactoringFile::scopeAt(unsigned index) const
{
    int line, column;
    cppDocument()->translationUnit()->getTokenPosition(index, &line, &column);
    return cppDocument()->scopeAt(line, column);
}

bool CppRefactoringFile::isCursorOn(unsigned tokenIndex) const
{
    QTextCursor tc = cursor();
    int cursorBegin = tc.selectionStart();

    int start = startOf(tokenIndex);
    int end = endOf(tokenIndex);

    return cursorBegin >= start && cursorBegin <= end;
}

bool CppRefactoringFile::isCursorOn(const AST *ast) const
{
    if (!ast)
        return false;

    QTextCursor tc = cursor();
    int cursorBegin = tc.selectionStart();

    int start = startOf(ast);
    int end = endOf(ast);

    return cursorBegin >= start && cursorBegin <= end;
}

QList<Token> CppRefactoringFile::tokensForCursor() const
{
    return tokensForCursor(cursor());
}

QList<Token> CppRefactoringFile::tokensForCursor(const QTextCursor &cursor) const
{
    int pos = cursor.selectionStart();
    int endPos = cursor.selectionEnd();
    if (pos > endPos)
        std::swap(pos, endPos);

    // Skip whitespace.
    while (pos < endPos && document()->characterAt(pos).isSpace())
        ++pos;
    while (endPos > pos && document()->characterAt(endPos).isSpace())
        --endPos;

    const std::vector<Token> &allTokens = cppDocument()->translationUnit()->allTokens();
    const int firstIndex = tokenIndexForPosition(allTokens, pos, 0);
    if (firstIndex == -1)
        return {};

    const int lastIndex = pos == endPos
                              ? firstIndex
                              : tokenIndexForPosition(allTokens, endPos, firstIndex);
    if (lastIndex == -1)
        return {};
    QTC_ASSERT(lastIndex >= firstIndex, return {});
    QList<Token> result;
    for (int i = firstIndex; i <= lastIndex; ++i)
        result.push_back(allTokens.at(i));
    return result;
}

QList<Token> CppRefactoringFile::tokensForLine(int line) const
{
    const QTextBlock block = document()->findBlockByNumber(line - 1);
    QTextCursor cursor(block);
    cursor.select(QTextCursor::BlockUnderCursor);
    return tokensForCursor(cursor);
}

ChangeSet::Range CppRefactoringFile::range(unsigned tokenIndex) const
{
    const Token &token = tokenAt(tokenIndex);
    int line, column;
    cppDocument()->translationUnit()->getPosition(token.utf16charsBegin(), &line, &column);
    const int start = document()->findBlockByNumber(line - 1).position() + column - 1;
    return {start, start + token.utf16chars()};
}

ChangeSet::Range CppRefactoringFile::range(const AST *ast) const
{
    return {startOf(ast), endOf(ast)};
}

int CppRefactoringFile::startOf(unsigned index) const
{
    int line, column;
    cppDocument()->translationUnit()->getPosition(tokenAt(index).utf16charsBegin(), &line, &column);
    return document()->findBlockByNumber(line - 1).position() + column - 1;
}

int CppRefactoringFile::startOf(const AST *ast) const
{
    QTC_ASSERT(ast, return 0);
    int firstToken = ast->firstToken();
    const int lastToken = ast->lastToken();
    while (tokenAt(firstToken).generated() && firstToken < lastToken)
        ++firstToken;
    return startOf(firstToken);
}

int CppRefactoringFile::endOf(unsigned index) const
{
    int line, column;
    cppDocument()->translationUnit()->getPosition(tokenAt(index).utf16charsEnd(), &line, &column);
    return document()->findBlockByNumber(line - 1).position() + column - 1;
}

int CppRefactoringFile::endOf(const AST *ast) const
{
    QTC_ASSERT(ast, return 0);
    int lastToken = ast->lastToken() - 1;
    QTC_ASSERT(lastToken >= 0, return -1);
    const int firstToken = ast->firstToken();
    while (tokenAt(lastToken).generated() && lastToken > firstToken)
        --lastToken;
    return endOf(lastToken);
}

void CppRefactoringFile::startAndEndOf(unsigned index, int *start, int *end) const
{
    int line, column;
    Token token(tokenAt(index));
    cppDocument()->translationUnit()->getPosition(token.utf16charsBegin(), &line, &column);
    *start = document()->findBlockByNumber(line - 1).position() + column - 1;
    *end = *start + token.utf16chars();
}

QString CppRefactoringFile::textOf(const AST *ast) const
{
    return textOf(startOf(ast), endOf(ast));
}

const Token &CppRefactoringFile::tokenAt(unsigned index) const
{
    return cppDocument()->translationUnit()->tokenAt(index);
}

void CppRefactoringFile::fileChanged()
{
    QTC_ASSERT(!filePath().isEmpty(), return);
    m_cppDocument.clear();
    CppModelManager::updateSourceFiles({filePath()});
}

Id CppRefactoringFile::indenterId() const
{
    return Constants::CPP_SETTINGS_ID;
}

int CppRefactoringFile::tokenIndexForPosition(const std::vector<CPlusPlus::Token> &tokens,
                                              int pos, int startIndex) const
{
    const TranslationUnit * const tu = cppDocument()->translationUnit();

    // Binary search
    for (int l = startIndex, u = int(tokens.size()) - 1; l <= u; ) {
        const int i = (l + u) / 2;
        const int tokenPos = tu->getTokenPositionInDocument(tokens.at(i), document());
        if (pos < tokenPos) {
            u = i - 1;
            continue;
        }
        const int tokenEndPos = tu->getTokenEndPositionInDocument(tokens.at(i), document());
        if (pos > tokenEndPos) {
            l = i + 1;
            continue;
        }
        return i;
    }
    return -1;
}

CppRefactoringChangesData::CppRefactoringChangesData(const Snapshot &snapshot)
    : m_snapshot(snapshot)
    , m_workingCopy(CppModelManager::workingCopy())
{}

} // namespace CppEditor