2010-06-03 12:12:30 +02:00
|
|
|
/**************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
|
|
|
|
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
|
|
|
|
**
|
|
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
|
|
|
**
|
|
|
|
|
** Commercial Usage
|
|
|
|
|
**
|
|
|
|
|
** Licensees holding valid Qt Commercial licenses may use this file in
|
|
|
|
|
** accordance with the Qt Commercial License Agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and Nokia.
|
|
|
|
|
**
|
|
|
|
|
** 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.
|
|
|
|
|
**
|
|
|
|
|
** If you are unsure which license is appropriate for your use, please
|
|
|
|
|
** contact the sales department at http://qt.nokia.com/contact.
|
|
|
|
|
**
|
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "quickfix.h"
|
|
|
|
|
#include "basetexteditor.h"
|
|
|
|
|
|
2010-06-14 14:52:43 +02:00
|
|
|
#include <coreplugin/ifile.h>
|
2010-06-22 14:14:22 +02:00
|
|
|
#include <extensionsystem/pluginmanager.h>
|
2010-06-03 12:12:30 +02:00
|
|
|
#include <QtGui/QApplication>
|
|
|
|
|
#include <QtGui/QTextBlock>
|
|
|
|
|
|
|
|
|
|
#include <QtCore/QDebug>
|
|
|
|
|
|
2010-07-26 13:06:33 +02:00
|
|
|
using namespace TextEditor;
|
2010-06-03 12:12:30 +02:00
|
|
|
|
2010-07-26 13:06:33 +02:00
|
|
|
QuickFixState::QuickFixState(TextEditor::BaseTextEditor *editor)
|
2010-06-03 12:12:30 +02:00
|
|
|
: _editor(editor)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-26 13:06:33 +02:00
|
|
|
QuickFixState::~QuickFixState()
|
2010-06-03 12:12:30 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-26 13:06:33 +02:00
|
|
|
TextEditor::BaseTextEditor *QuickFixState::editor() const
|
2010-06-03 12:12:30 +02:00
|
|
|
{
|
|
|
|
|
return _editor;
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-14 14:52:43 +02:00
|
|
|
|
2010-07-26 13:06:33 +02:00
|
|
|
QuickFixOperation::QuickFixOperation(int priority)
|
|
|
|
|
{
|
|
|
|
|
setPriority(priority);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QuickFixOperation::~QuickFixOperation()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int QuickFixOperation::priority() const
|
|
|
|
|
{
|
|
|
|
|
return _priority;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QuickFixOperation::setPriority(int priority)
|
|
|
|
|
{
|
|
|
|
|
_priority = priority;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString QuickFixOperation::description() const
|
|
|
|
|
{
|
|
|
|
|
return _description;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QuickFixOperation::setDescription(const QString &description)
|
|
|
|
|
{
|
|
|
|
|
_description = description;
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-12 13:46:18 +02:00
|
|
|
|
|
|
|
|
|
2010-07-26 13:06:33 +02:00
|
|
|
QuickFixFactory::QuickFixFactory(QObject *parent)
|
|
|
|
|
: QObject(parent)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QuickFixFactory::~QuickFixFactory()
|
|
|
|
|
{
|
2010-06-03 14:45:55 +02:00
|
|
|
}
|
2010-06-03 15:15:11 +02:00
|
|
|
|
|
|
|
|
QuickFixCollector::QuickFixCollector()
|
|
|
|
|
: _editable(0)
|
2010-07-26 13:06:33 +02:00
|
|
|
{
|
|
|
|
|
}
|
2010-06-03 15:15:11 +02:00
|
|
|
|
|
|
|
|
QuickFixCollector::~QuickFixCollector()
|
2010-07-26 13:06:33 +02:00
|
|
|
{
|
|
|
|
|
}
|
2010-06-03 15:15:11 +02:00
|
|
|
|
|
|
|
|
TextEditor::ITextEditable *QuickFixCollector::editor() const
|
2010-07-26 13:06:33 +02:00
|
|
|
{
|
|
|
|
|
return _editable;
|
|
|
|
|
}
|
2010-06-03 15:15:11 +02:00
|
|
|
|
|
|
|
|
int QuickFixCollector::startPosition() const
|
2010-07-26 13:06:33 +02:00
|
|
|
{
|
|
|
|
|
return _editable->position();
|
|
|
|
|
}
|
2010-06-03 15:15:11 +02:00
|
|
|
|
|
|
|
|
bool QuickFixCollector::triggersCompletion(TextEditor::ITextEditable *)
|
2010-07-26 13:06:33 +02:00
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2010-06-03 15:15:11 +02:00
|
|
|
|
|
|
|
|
int QuickFixCollector::startCompletion(TextEditor::ITextEditable *editable)
|
|
|
|
|
{
|
|
|
|
|
Q_ASSERT(editable != 0);
|
|
|
|
|
|
|
|
|
|
_editable = editable;
|
2010-07-26 13:06:33 +02:00
|
|
|
TextEditor::BaseTextEditor *editor = qobject_cast<TextEditor::BaseTextEditor *>(editable->widget());
|
|
|
|
|
Q_ASSERT(editor != 0);
|
2010-06-03 15:15:11 +02:00
|
|
|
|
2010-07-26 13:06:33 +02:00
|
|
|
if (TextEditor::QuickFixState *state = initializeCompletion(editor)) {
|
|
|
|
|
QMap<int, QList<QuickFixOperation::Ptr> > matchedOps;
|
2010-06-03 15:15:11 +02:00
|
|
|
|
2010-07-26 13:06:33 +02:00
|
|
|
foreach (QuickFixFactory *factory, quickFixFactories()) {
|
|
|
|
|
QList<QuickFixOperation::Ptr> ops = factory->matchingOperations(state);
|
2010-06-03 15:15:11 +02:00
|
|
|
|
2010-07-26 13:06:33 +02:00
|
|
|
foreach (QuickFixOperation::Ptr op, ops) {
|
|
|
|
|
const int priority = op->priority();
|
|
|
|
|
if (priority != -1)
|
|
|
|
|
matchedOps[priority].append(op);
|
|
|
|
|
}
|
2010-06-03 15:15:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QMapIterator<int, QList<TextEditor::QuickFixOperation::Ptr> > it(matchedOps);
|
|
|
|
|
it.toBack();
|
|
|
|
|
if (it.hasPrevious()) {
|
|
|
|
|
it.previous();
|
|
|
|
|
_quickFixes = it.value();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
delete state;
|
|
|
|
|
|
|
|
|
|
if (! _quickFixes.isEmpty())
|
|
|
|
|
return editable->position();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QuickFixCollector::completions(QList<TextEditor::CompletionItem> *quickFixItems)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < _quickFixes.size(); ++i) {
|
|
|
|
|
TextEditor::QuickFixOperation::Ptr op = _quickFixes.at(i);
|
|
|
|
|
|
|
|
|
|
TextEditor::CompletionItem item(this);
|
|
|
|
|
item.text = op->description();
|
|
|
|
|
item.data = QVariant::fromValue(i);
|
|
|
|
|
quickFixItems->append(item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-09 15:12:44 +02:00
|
|
|
void QuickFixCollector::fix(const TextEditor::CompletionItem &item)
|
2010-06-03 15:15:11 +02:00
|
|
|
{
|
|
|
|
|
const int index = item.data.toInt();
|
|
|
|
|
|
|
|
|
|
if (index < _quickFixes.size()) {
|
|
|
|
|
TextEditor::QuickFixOperation::Ptr quickFix = _quickFixes.at(index);
|
|
|
|
|
quickFix->perform();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QuickFixCollector::cleanup()
|
|
|
|
|
{
|
|
|
|
|
_quickFixes.clear();
|
|
|
|
|
}
|