2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2011-04-15 16:19:23 +02:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2011-04-15 16:19:23 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2011-04-15 16:19:23 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** 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
|
2016-01-15 14:57:40 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2011-04-15 16:19:23 +02:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2011-04-15 16:19:23 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2011-04-15 16:19:23 +02:00
|
|
|
|
2014-09-04 00:04:18 +02:00
|
|
|
#include "assistproposalitem.h"
|
2011-04-15 16:19:23 +02:00
|
|
|
|
2014-09-26 09:14:03 +02:00
|
|
|
#include <texteditor/texteditor.h>
|
2011-04-15 16:19:23 +02:00
|
|
|
#include <texteditor/quickfix.h>
|
|
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QTextCursor>
|
2011-04-15 16:19:23 +02:00
|
|
|
|
2014-09-04 00:04:18 +02:00
|
|
|
namespace TextEditor {
|
2011-04-15 16:19:23 +02:00
|
|
|
|
2014-09-04 00:04:18 +02:00
|
|
|
/*!
|
|
|
|
|
\class TextEditor::AssistProposalItem
|
|
|
|
|
\brief The AssistProposalItem class acts as an interface for representing an assist
|
|
|
|
|
proposal item.
|
|
|
|
|
\ingroup CodeAssist
|
|
|
|
|
|
|
|
|
|
This is class is part of the CodeAssist API.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
\fn bool TextEditor::AssistProposalItem::implicitlyApplies() const
|
|
|
|
|
|
|
|
|
|
Returns whether this item should implicitly apply in the case it is the only proposal
|
|
|
|
|
item available.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
\fn bool TextEditor::AssistProposalItem::prematurelyApplies(const QChar &c) const
|
|
|
|
|
|
|
|
|
|
Returns whether the character \a c causes this item to be applied.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
\fn void TextEditor::AssistProposalItem::apply(BaseTextEditor *editor, int basePosition) const
|
|
|
|
|
|
|
|
|
|
This is the place to implement the actual application of the item.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
AssistProposalItem::AssistProposalItem()
|
2011-04-15 16:19:23 +02:00
|
|
|
: m_order(0)
|
|
|
|
|
{}
|
|
|
|
|
|
2014-09-04 00:04:18 +02:00
|
|
|
AssistProposalItem::~AssistProposalItem()
|
2011-04-15 16:19:23 +02:00
|
|
|
{}
|
|
|
|
|
|
2014-09-04 00:04:18 +02:00
|
|
|
void AssistProposalItem::setIcon(const QIcon &icon)
|
2011-04-15 16:19:23 +02:00
|
|
|
{
|
|
|
|
|
m_icon = icon;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-04 00:04:18 +02:00
|
|
|
const QIcon &AssistProposalItem::icon() const
|
2011-04-15 16:19:23 +02:00
|
|
|
{
|
|
|
|
|
return m_icon;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-04 00:04:18 +02:00
|
|
|
void AssistProposalItem::setText(const QString &text)
|
2011-04-15 16:19:23 +02:00
|
|
|
{
|
|
|
|
|
m_text = text;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-04 00:04:18 +02:00
|
|
|
QString AssistProposalItem::text() const
|
2011-04-15 16:19:23 +02:00
|
|
|
{
|
|
|
|
|
return m_text;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-04 00:04:18 +02:00
|
|
|
void AssistProposalItem::setDetail(const QString &detail)
|
2011-04-15 16:19:23 +02:00
|
|
|
{
|
|
|
|
|
m_detail = detail;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-04 00:04:18 +02:00
|
|
|
const QString &AssistProposalItem::detail() const
|
2011-04-15 16:19:23 +02:00
|
|
|
{
|
|
|
|
|
return m_detail;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-04 00:04:18 +02:00
|
|
|
void AssistProposalItem::setData(const QVariant &var)
|
2011-04-15 16:19:23 +02:00
|
|
|
{
|
|
|
|
|
m_data = var;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-04 00:04:18 +02:00
|
|
|
const QVariant &AssistProposalItem::data() const
|
2011-04-15 16:19:23 +02:00
|
|
|
{
|
|
|
|
|
return m_data;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-04 00:04:18 +02:00
|
|
|
int AssistProposalItem::order() const
|
2011-04-15 16:19:23 +02:00
|
|
|
{
|
|
|
|
|
return m_order;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-04 00:04:18 +02:00
|
|
|
void AssistProposalItem::setOrder(int order)
|
2011-04-15 16:19:23 +02:00
|
|
|
{
|
|
|
|
|
m_order = order;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-04 00:04:18 +02:00
|
|
|
bool AssistProposalItem::implicitlyApplies() const
|
2011-04-15 16:19:23 +02:00
|
|
|
{
|
|
|
|
|
return !data().canConvert<QString>() && !data().canConvert<QuickFixOperation::Ptr>();
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-04 00:04:18 +02:00
|
|
|
bool AssistProposalItem::prematurelyApplies(const QChar &c) const
|
2011-04-15 16:19:23 +02:00
|
|
|
{
|
|
|
|
|
Q_UNUSED(c);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-26 11:37:54 +02:00
|
|
|
void AssistProposalItem::apply(TextEditorWidget *editorWidget, int basePosition) const
|
2011-04-15 16:19:23 +02:00
|
|
|
{
|
2015-07-01 13:18:49 +09:00
|
|
|
if (data().canConvert<QString>()) {
|
2014-09-03 22:45:33 +02:00
|
|
|
applySnippet(editorWidget, basePosition);
|
2015-07-01 13:18:49 +09:00
|
|
|
} else if (data().canConvert<QuickFixOperation::Ptr>()) {
|
2014-09-03 22:45:33 +02:00
|
|
|
applyQuickFix(editorWidget, basePosition);
|
2015-07-01 13:18:49 +09:00
|
|
|
} else {
|
2014-09-03 22:45:33 +02:00
|
|
|
applyContextualContent(editorWidget, basePosition);
|
2015-07-01 13:18:49 +09:00
|
|
|
editorWidget->encourageApply();
|
|
|
|
|
}
|
2011-04-15 16:19:23 +02:00
|
|
|
}
|
|
|
|
|
|
2014-09-26 11:37:54 +02:00
|
|
|
void AssistProposalItem::applyContextualContent(TextEditorWidget *editorWidget, int basePosition) const
|
2011-04-15 16:19:23 +02:00
|
|
|
{
|
2014-09-03 22:45:33 +02:00
|
|
|
const int currentPosition = editorWidget->position();
|
|
|
|
|
editorWidget->setCursorPosition(basePosition);
|
|
|
|
|
editorWidget->replace(currentPosition - basePosition, text());
|
2011-04-15 16:19:23 +02:00
|
|
|
}
|
|
|
|
|
|
2014-09-26 11:37:54 +02:00
|
|
|
void AssistProposalItem::applySnippet(TextEditorWidget *editorWidget, int basePosition) const
|
2011-04-15 16:19:23 +02:00
|
|
|
{
|
2014-09-03 22:45:33 +02:00
|
|
|
QTextCursor tc = editorWidget->textCursor();
|
2011-04-15 16:19:23 +02:00
|
|
|
tc.setPosition(basePosition, QTextCursor::KeepAnchor);
|
2014-09-03 22:45:33 +02:00
|
|
|
editorWidget->insertCodeSnippet(tc, data().toString());
|
2011-04-15 16:19:23 +02:00
|
|
|
}
|
|
|
|
|
|
2014-09-26 11:37:54 +02:00
|
|
|
void AssistProposalItem::applyQuickFix(TextEditorWidget *editorWidget, int basePosition) const
|
2011-04-15 16:19:23 +02:00
|
|
|
{
|
2014-09-03 22:45:33 +02:00
|
|
|
Q_UNUSED(editorWidget)
|
2011-04-15 16:19:23 +02:00
|
|
|
Q_UNUSED(basePosition)
|
|
|
|
|
|
|
|
|
|
QuickFixOperation::Ptr op = data().value<QuickFixOperation::Ptr>();
|
|
|
|
|
op->perform();
|
|
|
|
|
}
|
2014-09-04 00:04:18 +02:00
|
|
|
|
|
|
|
|
} // namespace TextEditor
|