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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
|
/****************************************************************************
**
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "refactoringchanges.h"
#include "basetexteditor.h"
#include <coreplugin/icore.h>
#include <coreplugin/editormanager/editormanager.h>
#include <extensionsystem/pluginmanager.h>
#include <utils/qtcassert.h>
#include <QFile>
#include <QSet>
#include <QTextBlock>
#include <QTextCursor>
#include <QTextDocument>
#include <QDebug>
using namespace TextEditor;
RefactoringChanges::RefactoringChanges()
: m_data(new RefactoringChangesData)
{}
RefactoringChanges::RefactoringChanges(RefactoringChangesData *data)
: m_data(data)
{}
RefactoringChanges::~RefactoringChanges()
{}
BaseTextEditorWidget *RefactoringChanges::editorForFile(const QString &fileName)
{
Core::EditorManager *editorManager = Core::EditorManager::instance();
const QList<Core::IEditor *> editors = editorManager->editorsForFileName(fileName);
foreach (Core::IEditor *editor, editors) {
BaseTextEditorWidget *textEditor = qobject_cast<BaseTextEditorWidget *>(editor->widget());
if (textEditor != 0)
return textEditor;
}
return 0;
}
QList<QPair<QTextCursor, QTextCursor > > RefactoringChanges::rangesToSelections(QTextDocument *document,
const QList<Range> &ranges)
{
QList<QPair<QTextCursor, QTextCursor> > selections;
foreach (const Range &range, ranges) {
QTextCursor start(document);
start.setPosition(range.start);
start.setKeepPositionOnInsert(true);
QTextCursor end(document);
end.setPosition(qMin(range.end, document->characterCount() - 1));
selections.append(qMakePair(start, end));
}
return selections;
}
bool RefactoringChanges::createFile(const QString &fileName, const QString &contents, bool reindent, bool openEditor) const
{
if (QFile::exists(fileName))
return false;
// Create a text document for the new file:
QTextDocument *document = new QTextDocument;
QTextCursor cursor(document);
cursor.beginEditBlock();
cursor.insertText(contents);
// Reindent the contents:
if (reindent) {
cursor.select(QTextCursor::Document);
m_data->indentSelection(cursor, fileName, 0);
}
cursor.endEditBlock();
// Write the file to disk:
Utils::TextFileFormat format;
format.codec = Core::EditorManager::instance()->defaultTextCodec();
QString error;
bool saveOk = format.writeFile(fileName, document->toPlainText(), &error);
delete document;
if (!saveOk)
return false;
m_data->fileChanged(fileName);
if (openEditor)
this->openEditor(fileName, /*bool activate =*/ false, -1, -1);
return true;
}
bool RefactoringChanges::removeFile(const QString &fileName) const
{
if (!QFile::exists(fileName))
return false;
// ### implement!
qWarning() << "RefactoringChanges::removeFile is not implemented";
return true;
}
BaseTextEditorWidget *RefactoringChanges::openEditor(const QString &fileName, bool activate, int line, int column)
{
Core::EditorManager::OpenEditorFlags flags = Core::EditorManager::IgnoreNavigationHistory;
if (!activate)
flags |= Core::EditorManager::NoActivate;
if (line != -1) {
// openEditorAt uses a 1-based line and a 0-based column!
column -= 1;
}
Core::IEditor *editor = BaseTextEditorWidget::openEditorAt(
fileName, line, column, Core::Id(), flags);
if (editor)
return qobject_cast<BaseTextEditorWidget *>(editor->widget());
else
return 0;
}
RefactoringFilePtr RefactoringChanges::file(BaseTextEditorWidget *editor)
{
return RefactoringFilePtr(new RefactoringFile(editor));
}
RefactoringFilePtr RefactoringChanges::file(const QString &fileName) const
{
return RefactoringFilePtr(new RefactoringFile(fileName, m_data));
}
RefactoringFile::RefactoringFile(QTextDocument *document, const QString &fileName)
: m_fileName(fileName)
, m_document(document)
, m_editor(0)
, m_openEditor(false)
, m_activateEditor(false)
, m_editorCursorPosition(-1)
, m_appliedOnce(false)
{ }
RefactoringFile::RefactoringFile(BaseTextEditorWidget *editor)
: m_fileName(editor->editorDocument()->fileName())
, m_document(0)
, m_editor(editor)
, m_openEditor(false)
, m_activateEditor(false)
, m_editorCursorPosition(-1)
, m_appliedOnce(false)
{ }
RefactoringFile::RefactoringFile(const QString &fileName, const QSharedPointer<RefactoringChangesData> &data)
: m_fileName(fileName)
, m_data(data)
, m_document(0)
, m_editor(0)
, m_openEditor(false)
, m_activateEditor(false)
, m_editorCursorPosition(-1)
, m_appliedOnce(false)
{
m_editor = RefactoringChanges::editorForFile(fileName);
}
RefactoringFile::~RefactoringFile()
{
delete m_document;
}
bool RefactoringFile::isValid() const
{
if (m_fileName.isEmpty())
return false;
return document();
}
const QTextDocument *RefactoringFile::document() const
{
return mutableDocument();
}
QTextDocument *RefactoringFile::mutableDocument() const
{
if (m_editor)
return m_editor->document();
else if (!m_document) {
QString fileContents;
if (!m_fileName.isEmpty()) {
QString error;
QTextCodec *defaultCodec = Core::EditorManager::instance()->defaultTextCodec();
Utils::TextFileFormat::ReadResult result = Utils::TextFileFormat::readFile(
m_fileName, defaultCodec,
&fileContents, &m_textFileFormat,
&error);
if (result != Utils::TextFileFormat::ReadSuccess) {
qWarning() << "Could not read " << m_fileName << ". Error: " << error;
m_textFileFormat.codec = 0;
}
}
// always make a QTextDocument to avoid excessive null checks
m_document = new QTextDocument(fileContents);
}
return m_document;
}
const QTextCursor RefactoringFile::cursor() const
{
if (m_editor)
return m_editor->textCursor();
else if (!m_fileName.isEmpty()) {
if (QTextDocument *doc = mutableDocument())
return QTextCursor(doc);
}
return QTextCursor();
}
QString RefactoringFile::fileName() const
{
return m_fileName;
}
int RefactoringFile::position(unsigned line, unsigned column) const
{
Q_ASSERT(line != 0);
Q_ASSERT(column != 0);
if (const QTextDocument *doc = document())
return doc->findBlockByNumber(line - 1).position() + column - 1;
return -1;
}
void RefactoringFile::lineAndColumn(int offset, unsigned *line, unsigned *column) const
{
Q_ASSERT(line);
Q_ASSERT(column);
Q_ASSERT(offset >= 0);
QTextCursor c(cursor());
c.setPosition(offset);
*line = c.blockNumber() + 1;
*column = c.positionInBlock() + 1;
}
QChar RefactoringFile::charAt(int pos) const
{
if (const QTextDocument *doc = document())
return doc->characterAt(pos);
return QChar();
}
QString RefactoringFile::textOf(int start, int end) const
{
QTextCursor c = cursor();
c.setPosition(start);
c.setPosition(end, QTextCursor::KeepAnchor);
return c.selectedText();
}
QString RefactoringFile::textOf(const Range &range) const
{
return textOf(range.start, range.end);
}
void RefactoringFile::setChangeSet(const Utils::ChangeSet &changeSet)
{
if (m_fileName.isEmpty())
return;
m_changes = changeSet;
}
void RefactoringFile::appendIndentRange(const Range &range)
{
if (m_fileName.isEmpty())
return;
m_indentRanges.append(range);
}
void RefactoringFile::appendReindentRange(const Range &range)
{
if (m_fileName.isEmpty())
return;
m_reindentRanges.append(range);
}
void RefactoringFile::setOpenEditor(bool activate, int pos)
{
m_openEditor = true;
m_activateEditor = activate;
m_editorCursorPosition = pos;
}
void RefactoringFile::apply()
{
// open / activate / goto position
if (m_openEditor && !m_fileName.isEmpty()) {
unsigned line = unsigned(-1), column = unsigned(-1);
if (m_editorCursorPosition != -1)
lineAndColumn(m_editorCursorPosition, &line, &column);
m_editor = RefactoringChanges::openEditor(m_fileName, m_activateEditor, line, column);
m_openEditor = false;
m_activateEditor = false;
m_editorCursorPosition = -1;
}
// apply changes, if any
if (m_data && !(m_indentRanges.isEmpty() && m_changes.isEmpty())) {
QTextDocument *doc = mutableDocument();
if (doc) {
QTextCursor c = cursor();
if (m_appliedOnce)
c.joinPreviousEditBlock();
else
c.beginEditBlock();
// build indent selections now, applying the changeset will change locations
const QList<QPair<QTextCursor, QTextCursor> > &indentSelections =
RefactoringChanges::rangesToSelections(doc, m_indentRanges);
m_indentRanges.clear();
const QList<QPair<QTextCursor, QTextCursor> > &reindentSelections =
RefactoringChanges::rangesToSelections(doc, m_reindentRanges);
m_reindentRanges.clear();
// apply changes and reindent
m_changes.apply(&c);
m_changes.clear();
indentOrReindent(&RefactoringChangesData::indentSelection, indentSelections);
indentOrReindent(&RefactoringChangesData::reindentSelection, reindentSelections);
c.endEditBlock();
// if this document doesn't have an editor, write the result to a file
if (!m_editor && m_textFileFormat.codec) {
QTC_ASSERT(!m_fileName.isEmpty(), return);
QString error;
if (!m_textFileFormat.writeFile(m_fileName, doc->toPlainText(), &error))
qWarning() << "Could not apply changes to" << m_fileName << ". Error: " << error;
}
fileChanged();
}
}
m_appliedOnce = true;
}
void RefactoringFile::indentOrReindent(void (RefactoringChangesData::*mf)(const QTextCursor &,
const QString &,
const BaseTextEditorWidget *) const,
const QList<QPair<QTextCursor, QTextCursor> > &ranges)
{
QPair<QTextCursor, QTextCursor> p;
foreach (p, ranges) {
QTextCursor selection(p.first.document());
selection.setPosition(p.first.position());
selection.setPosition(p.second.position(), QTextCursor::KeepAnchor);
((*m_data).*(mf))(selection, m_fileName, m_editor);
}
}
void RefactoringFile::fileChanged()
{
if (!m_fileName.isEmpty())
m_data->fileChanged(m_fileName);
}
RefactoringChangesData::~RefactoringChangesData()
{}
void RefactoringChangesData::indentSelection(const QTextCursor &, const QString &, const BaseTextEditorWidget *) const
{
qWarning() << Q_FUNC_INFO << "not implemented";
}
void RefactoringChangesData::reindentSelection(const QTextCursor &, const QString &, const BaseTextEditorWidget *) const
{
qWarning() << Q_FUNC_INFO << "not implemented";
}
void RefactoringChangesData::fileChanged(const QString &)
{
}
|