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
|
|
|
|
|
|
|
|
#include "codeassistant.h"
|
|
|
|
|
#include "completionassistprovider.h"
|
|
|
|
|
#include "iassistprocessor.h"
|
|
|
|
|
#include "iassistproposal.h"
|
2015-07-29 10:58:34 +02:00
|
|
|
#include "iassistproposalmodel.h"
|
2011-04-15 16:19:23 +02:00
|
|
|
#include "iassistproposalwidget.h"
|
2014-09-04 00:04:18 +02:00
|
|
|
#include "assistinterface.h"
|
|
|
|
|
#include "assistproposalitem.h"
|
2011-04-15 16:19:23 +02:00
|
|
|
#include "runner.h"
|
2016-01-19 14:54:59 +01:00
|
|
|
#include "textdocumentmanipulator.h"
|
2011-04-15 16:19:23 +02:00
|
|
|
|
2018-11-08 11:39:48 +01:00
|
|
|
#include <texteditor/textdocument.h>
|
2014-09-26 09:14:03 +02:00
|
|
|
#include <texteditor/texteditor.h>
|
2011-04-15 16:19:23 +02:00
|
|
|
#include <texteditor/texteditorsettings.h>
|
|
|
|
|
#include <texteditor/completionsettings.h>
|
2013-09-12 00:21:12 +03:00
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
2011-04-15 16:19:23 +02:00
|
|
|
#include <extensionsystem/pluginmanager.h>
|
2019-05-17 14:37:06 +02:00
|
|
|
#include <utils/algorithm.h>
|
2013-03-05 13:47:29 +01:00
|
|
|
#include <utils/qtcassert.h>
|
2011-04-15 16:19:23 +02:00
|
|
|
|
2013-12-22 08:08:59 +02:00
|
|
|
#include <QKeyEvent>
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QList>
|
2013-12-22 08:08:59 +02:00
|
|
|
#include <QObject>
|
|
|
|
|
#include <QScopedPointer>
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QTimer>
|
2011-04-15 16:19:23 +02:00
|
|
|
|
2014-09-19 14:08:11 +02:00
|
|
|
using namespace TextEditor::Internal;
|
2011-04-15 16:19:23 +02:00
|
|
|
|
|
|
|
|
namespace TextEditor {
|
|
|
|
|
|
|
|
|
|
class CodeAssistantPrivate : public QObject
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
CodeAssistantPrivate(CodeAssistant *assistant);
|
|
|
|
|
|
2014-09-26 11:37:54 +02:00
|
|
|
void configure(TextEditorWidget *editorWidget);
|
2011-04-15 16:19:23 +02:00
|
|
|
bool isConfigured() const;
|
|
|
|
|
|
2018-02-14 12:25:15 +01:00
|
|
|
void invoke(AssistKind kind, IAssistProvider *provider = nullptr);
|
2011-04-15 16:19:23 +02:00
|
|
|
void process();
|
2018-02-14 12:25:15 +01:00
|
|
|
void requestProposal(AssistReason reason, AssistKind kind, IAssistProvider *provider = nullptr);
|
2011-04-15 16:19:23 +02:00
|
|
|
void cancelCurrentRequest();
|
|
|
|
|
void invalidateCurrentRequestData();
|
|
|
|
|
void displayProposal(IAssistProposal *newProposal, AssistReason reason);
|
|
|
|
|
bool isDisplayingProposal() const;
|
|
|
|
|
bool isWaitingForProposal() const;
|
|
|
|
|
|
|
|
|
|
void notifyChange();
|
|
|
|
|
bool hasContext() const;
|
|
|
|
|
void destroyContext();
|
|
|
|
|
|
2017-08-18 12:21:45 +02:00
|
|
|
QVariant userData() const;
|
|
|
|
|
void setUserData(const QVariant &data);
|
|
|
|
|
|
2011-04-15 16:19:23 +02:00
|
|
|
CompletionAssistProvider *identifyActivationSequence();
|
|
|
|
|
|
|
|
|
|
void stopAutomaticProposalTimer();
|
|
|
|
|
void startAutomaticProposalTimer();
|
2014-09-19 14:08:11 +02:00
|
|
|
void automaticProposalTimeout();
|
|
|
|
|
void clearAbortedPosition();
|
2014-12-05 09:59:15 +01:00
|
|
|
void updateFromCompletionSettings(const TextEditor::CompletionSettings &settings);
|
2011-04-15 16:19:23 +02:00
|
|
|
|
2018-07-11 07:31:38 +02:00
|
|
|
bool eventFilter(QObject *o, QEvent *e) override;
|
2011-04-15 16:19:23 +02:00
|
|
|
|
2014-09-12 01:25:42 +02:00
|
|
|
private:
|
2020-09-23 12:48:17 +02:00
|
|
|
bool requestActivationCharProposal();
|
2016-02-01 14:51:01 +01:00
|
|
|
void processProposalItem(AssistProposalItemInterface *proposalItem);
|
2011-04-15 16:19:23 +02:00
|
|
|
void handlePrefixExpansion(const QString &newPrefix);
|
|
|
|
|
void finalizeProposal();
|
2013-09-12 00:21:12 +03:00
|
|
|
void explicitlyAborted();
|
2018-03-02 13:32:34 +01:00
|
|
|
bool isDestroyEvent(int key, const QString &keyText);
|
2011-04-15 16:19:23 +02:00
|
|
|
|
|
|
|
|
private:
|
2018-02-14 12:25:15 +01:00
|
|
|
CodeAssistant *q = nullptr;
|
|
|
|
|
TextEditorWidget *m_editorWidget = nullptr;
|
|
|
|
|
Internal::ProcessorRunner *m_requestRunner = nullptr;
|
2016-03-16 13:32:26 +01:00
|
|
|
QMetaObject::Connection m_runnerConnection;
|
2018-02-14 12:25:15 +01:00
|
|
|
IAssistProvider *m_requestProvider = nullptr;
|
|
|
|
|
IAssistProcessor *m_asyncProcessor = nullptr;
|
|
|
|
|
AssistKind m_assistKind = TextEditor::Completion;
|
|
|
|
|
IAssistProposalWidget *m_proposalWidget = nullptr;
|
2011-04-15 16:19:23 +02:00
|
|
|
QScopedPointer<IAssistProposal> m_proposal;
|
2018-02-14 12:25:15 +01:00
|
|
|
bool m_receivedContentWhileWaiting = false;
|
2011-04-15 16:19:23 +02:00
|
|
|
QTimer m_automaticProposalTimer;
|
|
|
|
|
CompletionSettings m_settings;
|
2018-02-14 12:25:15 +01:00
|
|
|
int m_abortedBasePosition = -1;
|
2011-05-20 14:08:34 +02:00
|
|
|
static const QChar m_null;
|
2017-08-18 12:21:45 +02:00
|
|
|
QVariant m_userData;
|
2011-04-15 16:19:23 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// --------------------
|
|
|
|
|
// CodeAssistantPrivate
|
|
|
|
|
// --------------------
|
2011-05-20 14:08:34 +02:00
|
|
|
const QChar CodeAssistantPrivate::m_null;
|
|
|
|
|
|
2011-04-15 16:19:23 +02:00
|
|
|
CodeAssistantPrivate::CodeAssistantPrivate(CodeAssistant *assistant)
|
2014-09-19 14:08:11 +02:00
|
|
|
: q(assistant)
|
2011-04-15 16:19:23 +02:00
|
|
|
{
|
|
|
|
|
m_automaticProposalTimer.setSingleShot(true);
|
2014-09-19 14:08:11 +02:00
|
|
|
connect(&m_automaticProposalTimer, &QTimer::timeout,
|
|
|
|
|
this, &CodeAssistantPrivate::automaticProposalTimeout);
|
2011-04-15 16:19:23 +02:00
|
|
|
|
2016-05-19 12:25:16 +02:00
|
|
|
m_settings = TextEditorSettings::completionSettings();
|
2014-09-19 14:08:11 +02:00
|
|
|
connect(TextEditorSettings::instance(), &TextEditorSettings::completionSettingsChanged,
|
2014-12-05 09:59:15 +01:00
|
|
|
this, &CodeAssistantPrivate::updateFromCompletionSettings);
|
2014-09-19 14:08:11 +02:00
|
|
|
|
|
|
|
|
connect(Core::EditorManager::instance(), &Core::EditorManager::currentEditorChanged,
|
|
|
|
|
this, &CodeAssistantPrivate::clearAbortedPosition);
|
2011-09-16 13:10:06 +02:00
|
|
|
}
|
2011-04-15 16:19:23 +02:00
|
|
|
|
2014-09-26 11:37:54 +02:00
|
|
|
void CodeAssistantPrivate::configure(TextEditorWidget *editorWidget)
|
2011-04-15 16:19:23 +02:00
|
|
|
{
|
2014-09-03 22:45:33 +02:00
|
|
|
m_editorWidget = editorWidget;
|
|
|
|
|
m_editorWidget->installEventFilter(this);
|
2013-08-30 12:55:06 +02:00
|
|
|
}
|
|
|
|
|
|
2011-04-15 16:19:23 +02:00
|
|
|
bool CodeAssistantPrivate::isConfigured() const
|
|
|
|
|
{
|
2018-02-14 12:25:15 +01:00
|
|
|
return m_editorWidget != nullptr;
|
2011-04-15 16:19:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CodeAssistantPrivate::invoke(AssistKind kind, IAssistProvider *provider)
|
|
|
|
|
{
|
|
|
|
|
if (!isConfigured())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
stopAutomaticProposalTimer();
|
|
|
|
|
|
|
|
|
|
if (isDisplayingProposal() && m_assistKind == kind && !m_proposal->isFragile()) {
|
|
|
|
|
m_proposalWidget->setReason(ExplicitlyInvoked);
|
2014-09-19 14:08:11 +02:00
|
|
|
m_proposalWidget->updateProposal(m_editorWidget->textAt(
|
2013-04-18 18:21:17 +02:00
|
|
|
m_proposal->basePosition(),
|
2014-09-03 22:45:33 +02:00
|
|
|
m_editorWidget->position() - m_proposal->basePosition()));
|
2011-04-15 16:19:23 +02:00
|
|
|
} else {
|
|
|
|
|
destroyContext();
|
|
|
|
|
requestProposal(ExplicitlyInvoked, kind, provider);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-23 12:48:17 +02:00
|
|
|
bool CodeAssistantPrivate::requestActivationCharProposal()
|
|
|
|
|
{
|
|
|
|
|
if (m_assistKind == Completion && m_settings.m_completionTrigger != ManualCompletion) {
|
|
|
|
|
if (CompletionAssistProvider *provider = identifyActivationSequence()) {
|
|
|
|
|
if (isWaitingForProposal())
|
|
|
|
|
cancelCurrentRequest();
|
|
|
|
|
requestProposal(ActivationCharacter, Completion, provider);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-15 16:19:23 +02:00
|
|
|
void CodeAssistantPrivate::process()
|
|
|
|
|
{
|
|
|
|
|
if (!isConfigured())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
stopAutomaticProposalTimer();
|
|
|
|
|
|
2013-10-24 14:32:14 +02:00
|
|
|
if (m_assistKind == TextEditor::Completion) {
|
2020-09-23 12:48:17 +02:00
|
|
|
if (!requestActivationCharProposal())
|
|
|
|
|
startAutomaticProposalTimer();
|
2020-07-31 16:50:03 +02:00
|
|
|
} else if (m_assistKind != FunctionHint){
|
2013-10-24 14:32:14 +02:00
|
|
|
m_assistKind = TextEditor::Completion;
|
|
|
|
|
}
|
2011-04-15 16:19:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CodeAssistantPrivate::requestProposal(AssistReason reason,
|
|
|
|
|
AssistKind kind,
|
|
|
|
|
IAssistProvider *provider)
|
|
|
|
|
{
|
2013-03-05 13:47:29 +01:00
|
|
|
QTC_ASSERT(!isWaitingForProposal(), return);
|
2011-04-15 16:19:23 +02:00
|
|
|
|
2014-09-03 22:45:33 +02:00
|
|
|
if (m_editorWidget->hasBlockSelection())
|
2014-06-16 14:20:36 +02:00
|
|
|
return; // TODO
|
|
|
|
|
|
2011-04-15 16:19:23 +02:00
|
|
|
if (!provider) {
|
2013-08-30 12:55:06 +02:00
|
|
|
if (kind == Completion)
|
2014-10-01 22:39:47 +02:00
|
|
|
provider = m_editorWidget->textDocument()->completionAssistProvider();
|
2020-07-31 16:50:03 +02:00
|
|
|
else if (kind == FunctionHint)
|
|
|
|
|
provider = m_editorWidget->textDocument()->functionHintAssistProvider();
|
2016-03-16 13:04:05 +01:00
|
|
|
else
|
|
|
|
|
provider = m_editorWidget->textDocument()->quickFixAssistProvider();
|
2011-04-15 16:19:23 +02:00
|
|
|
|
|
|
|
|
if (!provider)
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-04 00:04:18 +02:00
|
|
|
AssistInterface *assistInterface = m_editorWidget->createAssistInterface(kind, reason);
|
2016-03-14 13:10:19 +01:00
|
|
|
if (!assistInterface)
|
2011-04-15 16:19:23 +02:00
|
|
|
return;
|
2016-03-14 13:10:19 +01:00
|
|
|
|
|
|
|
|
m_assistKind = kind;
|
2018-07-13 12:33:46 +02:00
|
|
|
m_requestProvider = provider;
|
2016-03-14 13:10:19 +01:00
|
|
|
IAssistProcessor *processor = provider->createProcessor();
|
2011-04-15 16:19:23 +02:00
|
|
|
|
2015-04-30 11:58:20 +02:00
|
|
|
switch (provider->runType()) {
|
|
|
|
|
case IAssistProvider::Synchronous: {
|
|
|
|
|
if (IAssistProposal *newProposal = processor->perform(assistInterface))
|
|
|
|
|
displayProposal(newProposal, reason);
|
|
|
|
|
delete processor;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case IAssistProvider::AsynchronousWithThread: {
|
2013-09-18 09:07:21 +02:00
|
|
|
if (IAssistProposal *newProposal = processor->immediateProposal(assistInterface))
|
|
|
|
|
displayProposal(newProposal, reason);
|
|
|
|
|
|
2013-09-17 13:25:39 +02:00
|
|
|
m_requestRunner = new ProcessorRunner;
|
2016-03-16 13:32:26 +01:00
|
|
|
m_runnerConnection = connect(m_requestRunner, &ProcessorRunner::finished,
|
|
|
|
|
this, [this, reason](){
|
|
|
|
|
// Since the request runner is a different thread, there's still a gap in which the
|
|
|
|
|
// queued signal could be processed after an invalidation of the current request.
|
|
|
|
|
if (!m_requestRunner || m_requestRunner != sender())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
IAssistProposal *proposal = m_requestRunner->proposal();
|
|
|
|
|
invalidateCurrentRequestData();
|
|
|
|
|
displayProposal(proposal, reason);
|
|
|
|
|
emit q->finished();
|
|
|
|
|
});
|
2014-09-12 01:25:42 +02:00
|
|
|
connect(m_requestRunner, &ProcessorRunner::finished,
|
2016-03-16 13:32:26 +01:00
|
|
|
m_requestRunner, &ProcessorRunner::deleteLater);
|
2013-09-17 13:25:39 +02:00
|
|
|
assistInterface->prepareForAsyncUse();
|
|
|
|
|
m_requestRunner->setProcessor(processor);
|
|
|
|
|
m_requestRunner->setAssistInterface(assistInterface);
|
|
|
|
|
m_requestRunner->start();
|
2015-04-30 11:58:20 +02:00
|
|
|
break;
|
2011-04-15 16:19:23 +02:00
|
|
|
}
|
2015-04-30 11:58:20 +02:00
|
|
|
case IAssistProvider::Asynchronous: {
|
2020-05-14 08:18:32 +02:00
|
|
|
processor->setAsyncCompletionAvailableHandler([this, reason, processor](IAssistProposal *newProposal) {
|
2020-03-26 09:21:57 +01:00
|
|
|
// do not delete this processor directly since this function is called from within the processor
|
2020-05-14 08:18:32 +02:00
|
|
|
QTimer::singleShot(0, [processor]() { delete processor; });
|
|
|
|
|
if (processor != m_asyncProcessor)
|
|
|
|
|
return;
|
|
|
|
|
invalidateCurrentRequestData();
|
|
|
|
|
if (processor && processor->needsRestart() && m_receivedContentWhileWaiting) {
|
|
|
|
|
delete newProposal;
|
|
|
|
|
m_receivedContentWhileWaiting = false;
|
|
|
|
|
requestProposal(reason, m_assistKind, m_requestProvider);
|
|
|
|
|
} else {
|
|
|
|
|
displayProposal(newProposal, reason);
|
|
|
|
|
emit q->finished();
|
|
|
|
|
}
|
2015-04-30 11:58:20 +02:00
|
|
|
});
|
2011-04-15 16:19:23 +02:00
|
|
|
|
2015-04-30 11:58:20 +02:00
|
|
|
// If there is a proposal, nothing asynchronous happened...
|
|
|
|
|
if (IAssistProposal *newProposal = processor->perform(assistInterface)) {
|
|
|
|
|
displayProposal(newProposal, reason);
|
|
|
|
|
delete processor;
|
2018-03-01 12:33:48 +01:00
|
|
|
} else if (!processor->running()) {
|
2015-07-29 12:15:15 +02:00
|
|
|
delete processor;
|
2015-12-01 13:21:02 +01:00
|
|
|
} else { // ...async request was triggered
|
2020-05-14 08:18:32 +02:00
|
|
|
QTC_CHECK(!m_asyncProcessor);
|
2015-12-01 13:21:02 +01:00
|
|
|
m_asyncProcessor = processor;
|
2015-04-30 11:58:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} // switch
|
2011-04-15 16:19:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CodeAssistantPrivate::cancelCurrentRequest()
|
|
|
|
|
{
|
2015-04-30 11:58:20 +02:00
|
|
|
if (m_requestRunner) {
|
|
|
|
|
m_requestRunner->setDiscardProposal(true);
|
2016-03-16 13:32:26 +01:00
|
|
|
disconnect(m_runnerConnection);
|
2015-04-30 11:58:20 +02:00
|
|
|
}
|
2020-03-26 09:21:57 +01:00
|
|
|
if (m_asyncProcessor) {
|
2020-02-12 14:06:45 +01:00
|
|
|
m_asyncProcessor->cancel();
|
2020-05-14 08:18:32 +02:00
|
|
|
delete m_asyncProcessor;
|
2020-03-26 09:21:57 +01:00
|
|
|
}
|
2011-04-15 16:19:23 +02:00
|
|
|
invalidateCurrentRequestData();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CodeAssistantPrivate::displayProposal(IAssistProposal *newProposal, AssistReason reason)
|
|
|
|
|
{
|
|
|
|
|
if (!newProposal)
|
|
|
|
|
return;
|
|
|
|
|
|
2017-06-06 13:29:51 +02:00
|
|
|
// TODO: The proposal should own the model until someone takes it explicitly away.
|
2011-04-15 16:19:23 +02:00
|
|
|
QScopedPointer<IAssistProposal> proposalCandidate(newProposal);
|
|
|
|
|
|
2017-04-27 15:44:37 +02:00
|
|
|
bool destroyCurrentContext = false;
|
2011-04-15 16:19:23 +02:00
|
|
|
if (isDisplayingProposal()) {
|
2013-09-18 09:07:21 +02:00
|
|
|
if (!m_proposal->isFragile())
|
2011-04-15 16:19:23 +02:00
|
|
|
return;
|
2017-04-27 15:44:37 +02:00
|
|
|
destroyCurrentContext = true;
|
2011-04-15 16:19:23 +02:00
|
|
|
}
|
|
|
|
|
|
2013-09-12 00:21:12 +03:00
|
|
|
int basePosition = proposalCandidate->basePosition();
|
2017-04-27 15:44:37 +02:00
|
|
|
if (m_editorWidget->position() < basePosition) {
|
|
|
|
|
if (destroyCurrentContext)
|
|
|
|
|
destroyContext();
|
2011-04-15 16:19:23 +02:00
|
|
|
return;
|
2017-04-27 15:44:37 +02:00
|
|
|
}
|
2011-04-15 16:19:23 +02:00
|
|
|
|
2017-04-27 15:44:37 +02:00
|
|
|
if (m_abortedBasePosition == basePosition && reason != ExplicitlyInvoked) {
|
|
|
|
|
if (destroyCurrentContext)
|
|
|
|
|
destroyContext();
|
2013-09-12 00:21:12 +03:00
|
|
|
return;
|
2017-04-27 15:44:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const QString prefix = m_editorWidget->textAt(basePosition,
|
|
|
|
|
m_editorWidget->position() - basePosition);
|
2017-06-08 16:14:41 +02:00
|
|
|
if (!newProposal->hasItemsToPropose(prefix, reason)) {
|
|
|
|
|
if (newProposal->isCorrective(m_editorWidget))
|
|
|
|
|
newProposal->makeCorrection(m_editorWidget);
|
2017-04-27 15:44:37 +02:00
|
|
|
return;
|
2017-06-08 16:14:41 +02:00
|
|
|
}
|
2017-04-27 15:44:37 +02:00
|
|
|
|
|
|
|
|
if (destroyCurrentContext)
|
|
|
|
|
destroyContext();
|
2013-09-12 00:21:12 +03:00
|
|
|
|
|
|
|
|
clearAbortedPosition();
|
2011-04-15 16:19:23 +02:00
|
|
|
m_proposal.reset(proposalCandidate.take());
|
|
|
|
|
|
2017-03-23 14:00:09 +01:00
|
|
|
if (m_proposal->isCorrective(m_editorWidget))
|
2014-09-03 22:45:33 +02:00
|
|
|
m_proposal->makeCorrection(m_editorWidget);
|
2011-04-15 16:19:23 +02:00
|
|
|
|
2016-06-01 10:19:59 +02:00
|
|
|
m_editorWidget->keepAutoCompletionHighlight(true);
|
2013-09-12 00:21:12 +03:00
|
|
|
basePosition = m_proposal->basePosition();
|
2011-04-15 16:19:23 +02:00
|
|
|
m_proposalWidget = m_proposal->createWidget();
|
2014-09-12 01:25:42 +02:00
|
|
|
connect(m_proposalWidget, &QObject::destroyed,
|
|
|
|
|
this, &CodeAssistantPrivate::finalizeProposal);
|
|
|
|
|
connect(m_proposalWidget, &IAssistProposalWidget::prefixExpanded,
|
|
|
|
|
this, &CodeAssistantPrivate::handlePrefixExpansion);
|
|
|
|
|
connect(m_proposalWidget, &IAssistProposalWidget::proposalItemActivated,
|
|
|
|
|
this, &CodeAssistantPrivate::processProposalItem);
|
|
|
|
|
connect(m_proposalWidget, &IAssistProposalWidget::explicitlyAborted,
|
|
|
|
|
this, &CodeAssistantPrivate::explicitlyAborted);
|
2014-09-19 14:08:11 +02:00
|
|
|
m_proposalWidget->setAssistant(q);
|
2011-04-15 16:19:23 +02:00
|
|
|
m_proposalWidget->setReason(reason);
|
2011-06-23 15:04:01 +02:00
|
|
|
m_proposalWidget->setKind(m_assistKind);
|
2017-08-18 12:21:45 +02:00
|
|
|
m_proposalWidget->setBasePosition(basePosition);
|
2014-09-03 22:45:33 +02:00
|
|
|
m_proposalWidget->setUnderlyingWidget(m_editorWidget);
|
2018-02-14 14:32:51 +01:00
|
|
|
m_proposalWidget->setModel(m_proposal->model());
|
2014-09-03 22:45:33 +02:00
|
|
|
m_proposalWidget->setDisplayRect(m_editorWidget->cursorRect(basePosition));
|
2016-03-16 13:45:36 +01:00
|
|
|
m_proposalWidget->setIsSynchronized(!m_receivedContentWhileWaiting);
|
2017-04-27 15:44:37 +02:00
|
|
|
m_proposalWidget->showProposal(prefix);
|
2011-04-15 16:19:23 +02:00
|
|
|
}
|
|
|
|
|
|
2016-02-01 14:51:01 +01:00
|
|
|
void CodeAssistantPrivate::processProposalItem(AssistProposalItemInterface *proposalItem)
|
2011-04-15 16:19:23 +02:00
|
|
|
{
|
2013-03-05 13:47:29 +01:00
|
|
|
QTC_ASSERT(m_proposal, return);
|
2016-01-19 14:54:59 +01:00
|
|
|
TextDocumentManipulator manipulator(m_editorWidget);
|
|
|
|
|
proposalItem->apply(manipulator, m_proposal->basePosition());
|
2011-04-15 16:19:23 +02:00
|
|
|
destroyContext();
|
2018-08-15 09:39:22 +02:00
|
|
|
m_editorWidget->encourageApply();
|
2020-09-23 12:48:17 +02:00
|
|
|
if (!proposalItem->isSnippet())
|
|
|
|
|
requestActivationCharProposal();
|
2011-04-15 16:19:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CodeAssistantPrivate::handlePrefixExpansion(const QString &newPrefix)
|
|
|
|
|
{
|
2013-03-05 13:47:29 +01:00
|
|
|
QTC_ASSERT(m_proposal, return);
|
2017-05-17 15:53:47 +02:00
|
|
|
|
|
|
|
|
QTextCursor cursor(m_editorWidget->document());
|
|
|
|
|
cursor.setPosition(m_proposal->basePosition());
|
|
|
|
|
cursor.movePosition(QTextCursor::EndOfWord);
|
|
|
|
|
|
2017-06-28 09:51:53 +02:00
|
|
|
int currentPosition = m_editorWidget->position();
|
|
|
|
|
const QString textAfterCursor = m_editorWidget->textAt(currentPosition,
|
|
|
|
|
cursor.position() - currentPosition);
|
|
|
|
|
if (!textAfterCursor.startsWith(newPrefix)) {
|
|
|
|
|
if (newPrefix.indexOf(textAfterCursor, currentPosition - m_proposal->basePosition()) >= 0)
|
|
|
|
|
currentPosition = cursor.position();
|
2020-09-07 14:44:41 +02:00
|
|
|
const QStringView prefixAddition = QStringView(newPrefix).mid(currentPosition
|
|
|
|
|
- m_proposal->basePosition());
|
2017-06-28 09:51:53 +02:00
|
|
|
// If remaining string starts with the prefix addition
|
|
|
|
|
if (textAfterCursor.startsWith(prefixAddition))
|
|
|
|
|
currentPosition += prefixAddition.size();
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-03 22:45:33 +02:00
|
|
|
m_editorWidget->setCursorPosition(m_proposal->basePosition());
|
2017-06-28 09:51:53 +02:00
|
|
|
m_editorWidget->replace(currentPosition - m_proposal->basePosition(), newPrefix);
|
2011-04-15 16:19:23 +02:00
|
|
|
notifyChange();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CodeAssistantPrivate::finalizeProposal()
|
|
|
|
|
{
|
2013-08-21 23:22:11 +03:00
|
|
|
stopAutomaticProposalTimer();
|
2011-04-15 16:19:23 +02:00
|
|
|
m_proposal.reset();
|
2018-02-14 12:25:15 +01:00
|
|
|
m_proposalWidget = nullptr;
|
2011-04-15 16:19:23 +02:00
|
|
|
if (m_receivedContentWhileWaiting)
|
|
|
|
|
m_receivedContentWhileWaiting = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CodeAssistantPrivate::isDisplayingProposal() const
|
|
|
|
|
{
|
2020-09-23 12:48:17 +02:00
|
|
|
return m_proposalWidget != nullptr && m_proposalWidget->isVisible();
|
2011-04-15 16:19:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CodeAssistantPrivate::isWaitingForProposal() const
|
|
|
|
|
{
|
2018-02-14 12:25:15 +01:00
|
|
|
return m_requestRunner != nullptr || m_asyncProcessor != nullptr;
|
2011-04-15 16:19:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CodeAssistantPrivate::invalidateCurrentRequestData()
|
|
|
|
|
{
|
2018-02-14 12:25:15 +01:00
|
|
|
m_asyncProcessor = nullptr;
|
|
|
|
|
m_requestRunner = nullptr;
|
|
|
|
|
m_requestProvider = nullptr;
|
2011-04-15 16:19:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CompletionAssistProvider *CodeAssistantPrivate::identifyActivationSequence()
|
|
|
|
|
{
|
2019-05-17 14:37:06 +02:00
|
|
|
auto checkActivationSequence = [this](CompletionAssistProvider *provider) {
|
|
|
|
|
if (!provider)
|
|
|
|
|
return false;
|
|
|
|
|
const int length = provider->activationCharSequenceLength();
|
|
|
|
|
if (!length)
|
|
|
|
|
return false;
|
|
|
|
|
QString sequence = m_editorWidget->textAt(m_editorWidget->position() - length, length);
|
|
|
|
|
// In pretty much all cases the sequence will have the appropriate length. Only in the
|
|
|
|
|
// case of typing the very first characters in the document for providers that request a
|
|
|
|
|
// length greater than 1 (currently only C++, which specifies 3), the sequence needs to
|
|
|
|
|
// be prepended so it has the expected length.
|
|
|
|
|
const int lengthDiff = length - sequence.length();
|
|
|
|
|
for (int j = 0; j < lengthDiff; ++j)
|
|
|
|
|
sequence.prepend(m_null);
|
|
|
|
|
return provider->isActivationCharSequence(sequence);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
auto provider = {
|
|
|
|
|
m_editorWidget->textDocument()->completionAssistProvider(),
|
|
|
|
|
m_editorWidget->textDocument()->functionHintAssistProvider()
|
|
|
|
|
};
|
|
|
|
|
return Utils::findOrDefault(provider, checkActivationSequence);
|
2011-04-15 16:19:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CodeAssistantPrivate::notifyChange()
|
|
|
|
|
{
|
|
|
|
|
stopAutomaticProposalTimer();
|
|
|
|
|
|
|
|
|
|
if (isDisplayingProposal()) {
|
2013-03-05 13:47:29 +01:00
|
|
|
QTC_ASSERT(m_proposal, return);
|
2014-09-03 22:45:33 +02:00
|
|
|
if (m_editorWidget->position() < m_proposal->basePosition()) {
|
2011-04-15 16:19:23 +02:00
|
|
|
destroyContext();
|
2018-10-23 13:42:58 +02:00
|
|
|
} else if (m_proposal->supportsPrefix()) {
|
2011-04-15 16:19:23 +02:00
|
|
|
m_proposalWidget->updateProposal(
|
2014-09-12 01:25:42 +02:00
|
|
|
m_editorWidget->textAt(m_proposal->basePosition(),
|
2014-09-03 22:45:33 +02:00
|
|
|
m_editorWidget->position() - m_proposal->basePosition()));
|
2020-09-23 12:48:17 +02:00
|
|
|
if (!isDisplayingProposal())
|
|
|
|
|
requestActivationCharProposal();
|
2018-07-13 12:33:46 +02:00
|
|
|
} else {
|
|
|
|
|
destroyContext();
|
|
|
|
|
requestProposal(ExplicitlyInvoked, m_assistKind, m_requestProvider);
|
2012-04-13 21:47:11 +04:00
|
|
|
}
|
2011-04-15 16:19:23 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CodeAssistantPrivate::hasContext() const
|
|
|
|
|
{
|
2015-04-30 11:58:20 +02:00
|
|
|
return m_requestRunner || m_asyncProcessor || m_proposalWidget;
|
2011-04-15 16:19:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CodeAssistantPrivate::destroyContext()
|
|
|
|
|
{
|
|
|
|
|
stopAutomaticProposalTimer();
|
|
|
|
|
|
|
|
|
|
if (isWaitingForProposal()) {
|
|
|
|
|
cancelCurrentRequest();
|
|
|
|
|
} else if (isDisplayingProposal()) {
|
2016-06-01 10:19:59 +02:00
|
|
|
m_editorWidget->keepAutoCompletionHighlight(false);
|
2011-04-15 16:19:23 +02:00
|
|
|
m_proposalWidget->closeProposal();
|
2014-09-12 01:25:42 +02:00
|
|
|
disconnect(m_proposalWidget, &QObject::destroyed,
|
|
|
|
|
this, &CodeAssistantPrivate::finalizeProposal);
|
2011-04-15 16:19:23 +02:00
|
|
|
finalizeProposal();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-18 12:21:45 +02:00
|
|
|
QVariant CodeAssistantPrivate::userData() const
|
|
|
|
|
{
|
|
|
|
|
return m_userData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CodeAssistantPrivate::setUserData(const QVariant &data)
|
|
|
|
|
{
|
|
|
|
|
m_userData = data;
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-15 16:19:23 +02:00
|
|
|
void CodeAssistantPrivate::startAutomaticProposalTimer()
|
|
|
|
|
{
|
|
|
|
|
if (m_settings.m_completionTrigger == AutomaticCompletion)
|
|
|
|
|
m_automaticProposalTimer.start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CodeAssistantPrivate::automaticProposalTimeout()
|
|
|
|
|
{
|
2017-04-27 15:44:37 +02:00
|
|
|
if (isWaitingForProposal() || (isDisplayingProposal() && !m_proposal->isFragile()))
|
2011-04-15 16:19:23 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
requestProposal(IdleEditor, Completion);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CodeAssistantPrivate::stopAutomaticProposalTimer()
|
|
|
|
|
{
|
|
|
|
|
if (m_automaticProposalTimer.isActive())
|
|
|
|
|
m_automaticProposalTimer.stop();
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-05 09:59:15 +01:00
|
|
|
void CodeAssistantPrivate::updateFromCompletionSettings(
|
|
|
|
|
const TextEditor::CompletionSettings &settings)
|
2011-04-15 16:19:23 +02:00
|
|
|
{
|
|
|
|
|
m_settings = settings;
|
2014-12-05 09:59:15 +01:00
|
|
|
m_automaticProposalTimer.setInterval(m_settings.m_automaticProposalTimeoutInMs);
|
2011-04-15 16:19:23 +02:00
|
|
|
}
|
|
|
|
|
|
2013-09-12 00:21:12 +03:00
|
|
|
void CodeAssistantPrivate::explicitlyAborted()
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(m_proposal, return);
|
|
|
|
|
m_abortedBasePosition = m_proposal->basePosition();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CodeAssistantPrivate::clearAbortedPosition()
|
|
|
|
|
{
|
|
|
|
|
m_abortedBasePosition = -1;
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-02 13:32:34 +01:00
|
|
|
bool CodeAssistantPrivate::isDestroyEvent(int key, const QString &keyText)
|
|
|
|
|
{
|
|
|
|
|
if (keyText.isEmpty())
|
|
|
|
|
return key != Qt::LeftArrow && key != Qt::RightArrow && key != Qt::Key_Shift;
|
2018-09-20 01:16:01 +03:00
|
|
|
if (auto provider = qobject_cast<CompletionAssistProvider *>(m_requestProvider))
|
2018-03-02 13:32:34 +01:00
|
|
|
return !provider->isContinuationChar(keyText.at(0));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-15 16:19:23 +02:00
|
|
|
bool CodeAssistantPrivate::eventFilter(QObject *o, QEvent *e)
|
|
|
|
|
{
|
2019-07-23 10:58:00 +02:00
|
|
|
Q_UNUSED(o)
|
2011-04-15 16:19:23 +02:00
|
|
|
|
|
|
|
|
if (isWaitingForProposal()) {
|
|
|
|
|
QEvent::Type type = e->type();
|
|
|
|
|
if (type == QEvent::FocusOut) {
|
|
|
|
|
destroyContext();
|
|
|
|
|
} else if (type == QEvent::KeyPress) {
|
2018-09-20 01:16:01 +03:00
|
|
|
auto keyEvent = static_cast<QKeyEvent *>(e);
|
2011-04-15 16:19:23 +02:00
|
|
|
const QString &keyText = keyEvent->text();
|
2013-09-17 13:25:39 +02:00
|
|
|
|
2018-03-02 13:32:34 +01:00
|
|
|
if (isDestroyEvent(keyEvent->key(), keyText))
|
2011-04-15 16:19:23 +02:00
|
|
|
destroyContext();
|
2018-03-02 13:32:34 +01:00
|
|
|
else if (!keyText.isEmpty() && !m_receivedContentWhileWaiting)
|
2011-04-15 16:19:23 +02:00
|
|
|
m_receivedContentWhileWaiting = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -------------
|
|
|
|
|
// CodeAssistant
|
|
|
|
|
// -------------
|
2011-09-16 13:10:06 +02:00
|
|
|
CodeAssistant::CodeAssistant() : d(new CodeAssistantPrivate(this))
|
2013-03-19 11:41:40 +01:00
|
|
|
{
|
|
|
|
|
}
|
2011-04-15 16:19:23 +02:00
|
|
|
|
|
|
|
|
CodeAssistant::~CodeAssistant()
|
2011-09-16 13:10:06 +02:00
|
|
|
{
|
2015-09-01 15:40:04 +02:00
|
|
|
destroyContext();
|
2011-09-16 13:10:06 +02:00
|
|
|
delete d;
|
|
|
|
|
}
|
2011-04-15 16:19:23 +02:00
|
|
|
|
2014-09-26 11:37:54 +02:00
|
|
|
void CodeAssistant::configure(TextEditorWidget *editorWidget)
|
2011-04-15 16:19:23 +02:00
|
|
|
{
|
2014-09-03 22:45:33 +02:00
|
|
|
d->configure(editorWidget);
|
2011-04-15 16:19:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CodeAssistant::process()
|
|
|
|
|
{
|
2011-09-16 13:10:06 +02:00
|
|
|
d->process();
|
2011-04-15 16:19:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CodeAssistant::notifyChange()
|
|
|
|
|
{
|
2011-09-16 13:10:06 +02:00
|
|
|
d->notifyChange();
|
2011-04-15 16:19:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CodeAssistant::hasContext() const
|
|
|
|
|
{
|
2011-09-16 13:10:06 +02:00
|
|
|
return d->hasContext();
|
2011-04-15 16:19:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CodeAssistant::destroyContext()
|
|
|
|
|
{
|
2011-09-16 13:10:06 +02:00
|
|
|
d->destroyContext();
|
2011-04-15 16:19:23 +02:00
|
|
|
}
|
|
|
|
|
|
2017-08-18 12:21:45 +02:00
|
|
|
QVariant CodeAssistant::userData() const
|
|
|
|
|
{
|
|
|
|
|
return d->userData();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CodeAssistant::setUserData(const QVariant &data)
|
|
|
|
|
{
|
|
|
|
|
d->setUserData(data);
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-15 16:19:23 +02:00
|
|
|
void CodeAssistant::invoke(AssistKind kind, IAssistProvider *provider)
|
|
|
|
|
{
|
2011-09-16 13:10:06 +02:00
|
|
|
d->invoke(kind, provider);
|
2011-04-15 16:19:23 +02:00
|
|
|
}
|
|
|
|
|
|
2014-09-19 14:08:11 +02:00
|
|
|
} // namespace TextEditor
|