OSDN Git Service

QmlJS checks: Limit warning about unintentional empty blocks.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / texteditor / basefilefind.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 **
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 **
23 ** Other Usage
24 **
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at info@qt.nokia.com.
30 **
31 **************************************************************************/
32
33 #include "basefilefind.h"
34
35 #include <aggregation/aggregate.h>
36 #include <coreplugin/icore.h>
37 #include <coreplugin/progressmanager/progressmanager.h>
38 #include <coreplugin/progressmanager/futureprogress.h>
39 #include <coreplugin/editormanager/editormanager.h>
40 #include <coreplugin/editormanager/ieditor.h>
41 #include <coreplugin/filemanager.h>
42 #include <find/textfindconstants.h>
43 #include <texteditor/itexteditor.h>
44 #include <texteditor/basetexteditor.h>
45 #include <utils/stylehelper.h>
46 #include <utils/fileutils.h>
47 #include <utils/qtcassert.h>
48
49 #include <QtCore/QDebug>
50 #include <QtCore/QDirIterator>
51 #include <QtCore/QSettings>
52 #include <QtGui/QFileDialog>
53 #include <QtGui/QCheckBox>
54 #include <QtGui/QComboBox>
55 #include <QtGui/QHBoxLayout>
56 #include <QtGui/QLabel>
57 #include <QtGui/QMainWindow>
58 #include <QtGui/QPushButton>
59 #include <QtGui/QTextBlock>
60
61 using namespace Utils;
62 using namespace Find;
63 using namespace TextEditor;
64
65 BaseFileFind::BaseFileFind()
66   : m_currentSearch(0),
67     m_currentSearchCount(0),
68     m_watcher(0),
69     m_isSearching(false),
70     m_resultLabel(0),
71     m_filterCombo(0)
72 {
73 }
74
75 BaseFileFind::~BaseFileFind()
76 {
77 }
78
79 bool BaseFileFind::isEnabled() const
80 {
81     return !m_isSearching;
82 }
83
84 void BaseFileFind::cancel()
85 {
86     QTC_ASSERT(m_watcher, return);
87     m_watcher->cancel();
88 }
89
90 QStringList BaseFileFind::fileNameFilters() const
91 {
92     QStringList filters;
93     if (m_filterCombo && !m_filterCombo->currentText().isEmpty()) {
94         const QStringList parts = m_filterCombo->currentText().split(QLatin1Char(','));
95         foreach (const QString &part, parts) {
96             const QString filter = part.trimmed();
97             if (!filter.isEmpty()) {
98                 filters << filter;
99             }
100         }
101     }
102     return filters;
103 }
104
105 void BaseFileFind::runNewSearch(const QString &txt, Find::FindFlags findFlags,
106                                     SearchResultWindow::SearchMode searchMode)
107 {
108     m_isSearching = true;
109     m_currentFindSupport = 0;
110     emit changed();
111     if (m_filterCombo)
112         updateComboEntries(m_filterCombo, true);
113     delete m_watcher;
114     m_watcher = new QFutureWatcher<FileSearchResultList>();
115     m_watcher->setPendingResultsLimit(1);
116     connect(m_watcher, SIGNAL(resultReadyAt(int)), this, SLOT(displayResult(int)));
117     connect(m_watcher, SIGNAL(finished()), this, SLOT(searchFinished()));
118     m_currentSearchCount = 0;
119     m_currentSearch = Find::SearchResultWindow::instance()->startNewSearch(label(),
120                            toolTip().arg(Find::IFindFilter::descriptionForFindFlags(findFlags)),
121                            txt, searchMode, QString::fromLatin1("TextEditor"));
122     m_currentSearch->setTextToReplace(txt);
123     QVariantList searchParameters;
124     searchParameters << qVariantFromValue(txt) << qVariantFromValue(findFlags);
125     m_currentSearch->setUserData(searchParameters);
126     connect(m_currentSearch, SIGNAL(activated(Find::SearchResultItem)), this, SLOT(openEditor(Find::SearchResultItem)));
127     if (searchMode == SearchResultWindow::SearchAndReplace) {
128         connect(m_currentSearch, SIGNAL(replaceButtonClicked(QString,QList<Find::SearchResultItem>)),
129                 this, SLOT(doReplace(QString,QList<Find::SearchResultItem>)));
130     }
131     connect(m_currentSearch, SIGNAL(visibilityChanged(bool)), this, SLOT(hideHighlightAll(bool)));
132     Find::SearchResultWindow::instance()->popup(true);
133     if (findFlags & Find::FindRegularExpression) {
134         m_watcher->setFuture(Utils::findInFilesRegExp(txt, files(),
135             textDocumentFlagsForFindFlags(findFlags), ITextEditor::openedTextEditorsContents()));
136     } else {
137         m_watcher->setFuture(Utils::findInFiles(txt, files(),
138             textDocumentFlagsForFindFlags(findFlags), ITextEditor::openedTextEditorsContents()));
139     }
140     connect(m_currentSearch, SIGNAL(cancelled()), this, SLOT(cancel()));
141     Core::FutureProgress *progress =
142         Core::ICore::instance()->progressManager()->addTask(m_watcher->future(),
143                                                                         tr("Search"),
144                                                                         Constants::TASK_SEARCH);
145     progress->setWidget(createProgressWidget());
146     connect(progress, SIGNAL(clicked()), Find::SearchResultWindow::instance(), SLOT(popup()));
147 }
148
149 void BaseFileFind::findAll(const QString &txt, Find::FindFlags findFlags)
150 {
151     runNewSearch(txt, findFlags, SearchResultWindow::SearchOnly);
152 }
153
154 void BaseFileFind::replaceAll(const QString &txt, Find::FindFlags findFlags)
155 {
156     runNewSearch(txt, findFlags, SearchResultWindow::SearchAndReplace);
157 }
158
159 void BaseFileFind::doReplace(const QString &text,
160                                const QList<Find::SearchResultItem> &items)
161 {
162     QStringList files = replaceAll(text, items);
163     Core::FileManager *fileManager = Core::ICore::instance()->fileManager();
164     if (!files.isEmpty()) {
165         fileManager->notifyFilesChangedInternally(files);
166         Find::SearchResultWindow::instance()->hide();
167     }
168 }
169
170 void BaseFileFind::displayResult(int index) {
171     if (!m_currentSearch) {
172         m_watcher->cancel();
173         return;
174     }
175     Utils::FileSearchResultList results = m_watcher->resultAt(index);
176     QList<Find::SearchResultItem> items;
177     foreach (const Utils::FileSearchResult &result, results) {
178         Find::SearchResultItem item;
179         item.path = QStringList() << QDir::toNativeSeparators(result.fileName);
180         item.lineNumber = result.lineNumber;
181         item.text = result.matchingLine;
182         item.textMarkLength = result.matchLength;
183         item.textMarkPos = result.matchStart;
184         item.useTextEditorFont = true;
185         item.userData = result.regexpCapturedTexts;
186         items << item;
187     }
188     m_currentSearch->addResults(items, Find::SearchResult::AddOrdered);
189     m_currentSearchCount += items.count();
190     if (m_resultLabel)
191         m_resultLabel->setText(tr("%1 found").arg(m_currentSearchCount));
192 }
193
194 void BaseFileFind::searchFinished()
195 {
196     if (m_currentSearch)
197         m_currentSearch->finishSearch();
198     m_currentSearch = 0;
199     m_isSearching = false;
200     m_resultLabel = 0;
201     m_watcher->deleteLater();
202     m_watcher = 0;
203     emit changed();
204 }
205
206 QWidget *BaseFileFind::createProgressWidget()
207 {
208     m_resultLabel = new QLabel;
209     m_resultLabel->setAlignment(Qt::AlignCenter);
210     // ### TODO this setup should be done by style
211     QFont f = m_resultLabel->font();
212     f.setBold(true);
213     f.setPointSizeF(StyleHelper::sidebarFontSize());
214     m_resultLabel->setFont(f);
215     m_resultLabel->setPalette(StyleHelper::sidebarFontPalette(m_resultLabel->palette()));
216     m_resultLabel->setText(tr("%1 found").arg(m_currentSearchCount));
217     return m_resultLabel;
218 }
219
220 QWidget *BaseFileFind::createPatternWidget()
221 {
222     QString filterToolTip = tr("List of comma separated wildcard filters");
223     m_filterCombo = new QComboBox;
224     m_filterCombo->setEditable(true);
225     m_filterCombo->setModel(&m_filterStrings);
226     m_filterCombo->setMaxCount(10);
227     m_filterCombo->setMinimumContentsLength(10);
228     m_filterCombo->setSizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLengthWithIcon);
229     m_filterCombo->setInsertPolicy(QComboBox::InsertAtBottom);
230     m_filterCombo->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
231     m_filterCombo->setToolTip(filterToolTip);
232     syncComboWithSettings(m_filterCombo, m_filterSetting);
233     return m_filterCombo;
234 }
235
236 void BaseFileFind::writeCommonSettings(QSettings *settings)
237 {
238     settings->setValue("filters", m_filterStrings.stringList());
239     if (m_filterCombo)
240         settings->setValue("currentFilter", m_filterCombo->currentText());
241 }
242
243 void BaseFileFind::readCommonSettings(QSettings *settings, const QString &defaultFilter)
244 {
245     QStringList filters = settings->value("filters").toStringList();
246     m_filterSetting = settings->value("currentFilter").toString();
247     if (filters.isEmpty())
248         filters << defaultFilter;
249     if (m_filterSetting.isEmpty())
250         m_filterSetting = filters.first();
251     m_filterStrings.setStringList(filters);
252     if (m_filterCombo)
253         syncComboWithSettings(m_filterCombo, m_filterSetting);
254 }
255
256 void BaseFileFind::syncComboWithSettings(QComboBox *combo, const QString &setting)
257 {
258     if (!combo)
259         return;
260     int index = combo->findText(setting);
261     if (index < 0)
262         combo->setEditText(setting);
263     else
264         combo->setCurrentIndex(index);
265 }
266
267 void BaseFileFind::updateComboEntries(QComboBox *combo, bool onTop)
268 {
269     int index = combo->findText(combo->currentText());
270     if (index < 0) {
271         if (onTop) {
272             combo->insertItem(0, combo->currentText());
273         } else {
274             combo->addItem(combo->currentText());
275         }
276         combo->setCurrentIndex(combo->findText(combo->currentText()));
277     }
278 }
279
280 void BaseFileFind::openEditor(const Find::SearchResultItem &item)
281 {
282     SearchResult *result = qobject_cast<SearchResult *>(sender());
283     Core::IEditor *openedEditor = 0;
284     if (item.path.size() > 0) {
285         openedEditor = TextEditor::BaseTextEditorWidget::openEditorAt(QDir::fromNativeSeparators(item.path.first()),
286                                                                       item.lineNumber,
287                                                                       item.textMarkPos,
288                                                                       QString(),
289                                                                       Core::EditorManager::ModeSwitch);
290     } else {
291         openedEditor = Core::EditorManager::instance()->openEditor(item.text, QString(),
292                                                                    Core::EditorManager::ModeSwitch);
293     }
294     if (m_currentFindSupport)
295         m_currentFindSupport->clearResults();
296     m_currentFindSupport = 0;
297     if (!openedEditor)
298         return;
299     // highlight results
300     if (IFindSupport *findSupport = Aggregation::query<IFindSupport>(openedEditor->widget())) {
301         if (result) {
302             QVariantList userData = result->userData().value<QVariantList>();
303             QTC_ASSERT(userData.size() != 0, return);
304             m_currentFindSupport = findSupport;
305             m_currentFindSupport->highlightAll(userData.at(0).toString(), userData.at(1).value<FindFlags>());
306         }
307     }
308 }
309
310 void BaseFileFind::hideHighlightAll(bool visible)
311 {
312     if (!visible && m_currentFindSupport)
313         m_currentFindSupport->clearResults();
314 }
315
316 // #pragma mark Static methods
317
318 static void applyChanges(QTextDocument *doc, const QString &text, const QList<Find::SearchResultItem> &items)
319 {
320     QList<QPair<QTextCursor, QString> > changes;
321
322     foreach (const Find::SearchResultItem &item, items) {
323         const int blockNumber = item.lineNumber - 1;
324         QTextCursor tc(doc->findBlockByNumber(blockNumber));
325
326         const int cursorPosition = tc.position() + item.textMarkPos;
327
328         int cursorIndex = 0;
329         for (; cursorIndex < changes.size(); ++cursorIndex) {
330             const QTextCursor &otherTc = changes.at(cursorIndex).first;
331
332             if (otherTc.position() == cursorPosition)
333                 break;
334         }
335
336         if (cursorIndex != changes.size())
337             continue; // skip this change.
338
339         tc.setPosition(cursorPosition);
340         tc.setPosition(tc.position() + item.textMarkLength,
341                        QTextCursor::KeepAnchor);
342         QString substitutionText;
343         if (item.userData.canConvert<QStringList>() && !item.userData.toStringList().isEmpty())
344             substitutionText = Utils::expandRegExpReplacement(text, item.userData.toStringList());
345         else
346             substitutionText = text;
347         changes.append(QPair<QTextCursor, QString>(tc, substitutionText));
348     }
349
350     for (int i = 0; i < changes.size(); ++i) {
351         QPair<QTextCursor, QString> &cursor = changes[i];
352         cursor.first.insertText(cursor.second);
353     }
354 }
355
356 QStringList BaseFileFind::replaceAll(const QString &text,
357                                const QList<Find::SearchResultItem> &items)
358 {
359     if (items.isEmpty())
360         return QStringList();
361
362     QHash<QString, QList<Find::SearchResultItem> > changes;
363
364     foreach (const Find::SearchResultItem &item, items)
365         changes[QDir::fromNativeSeparators(item.path.first())].append(item);
366
367     Core::EditorManager *editorManager = Core::EditorManager::instance();
368
369     QHashIterator<QString, QList<Find::SearchResultItem> > it(changes);
370     while (it.hasNext()) {
371         it.next();
372
373         const QString fileName = it.key();
374         const QList<Find::SearchResultItem> changeItems = it.value();
375
376         const QList<Core::IEditor *> editors = editorManager->editorsForFileName(fileName);
377         TextEditor::BaseTextEditorWidget *textEditor = 0;
378         foreach (Core::IEditor *editor, editors) {
379             textEditor = qobject_cast<TextEditor::BaseTextEditorWidget *>(editor->widget());
380             if (textEditor != 0)
381                 break;
382         }
383
384         if (textEditor != 0) {
385             QTextCursor tc = textEditor->textCursor();
386             tc.beginEditBlock();
387             applyChanges(textEditor->document(), text, changeItems);
388             tc.endEditBlock();
389         } else {
390             Utils::FileReader reader;
391             if (reader.fetch(fileName, Core::ICore::instance()->mainWindow())) {
392                 // Keep track of line ending since QTextDocument is '\n' based.
393                 bool convertLineEnding = false;
394                 const QByteArray &data = reader.data();
395                 const int lf = data.indexOf('\n');
396                 if (lf > 0 && data.at(lf - 1) == '\r')
397                     convertLineEnding = true;
398
399                 QTextDocument doc;
400                 // ### set the encoding
401                 doc.setPlainText(QString::fromLocal8Bit(data));
402                 applyChanges(&doc, text, changeItems);
403                 QString plainText = doc.toPlainText();
404
405                 if (convertLineEnding)
406                     plainText.replace(QLatin1Char('\n'), QLatin1String("\r\n"));
407
408                 Utils::FileSaver saver(fileName);
409                 if (!saver.hasError()) {
410                     QTextStream stream(saver.file());
411                     // ### set the encoding
412                     stream << plainText;
413                     saver.setResult(&stream);
414                 }
415                 saver.finalize(Core::ICore::instance()->mainWindow());
416             }
417         }
418     }
419
420     return changes.keys();
421 }