forked from qt-creator/qt-creator
TextEditor: add document content completer
Add completion based on words of the document. This provides basic assistance for programming languages without a code model. Task-number: QTCREATORBUG-13869 Change-Id: I3a9c59c741dfd6895442fc0524cfd1bd3b2b0111 Reviewed-by: Christian Stenger <christian.stenger@qt.io> Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
@@ -252,7 +252,6 @@ void CodeAssistantPrivate::requestProposal(AssistReason reason,
|
|||||||
case IAssistProvider::Asynchronous: {
|
case IAssistProvider::Asynchronous: {
|
||||||
processor->setAsyncCompletionAvailableHandler(
|
processor->setAsyncCompletionAvailableHandler(
|
||||||
[this, reason](IAssistProposal *newProposal){
|
[this, reason](IAssistProposal *newProposal){
|
||||||
QTC_CHECK(newProposal);
|
|
||||||
invalidateCurrentRequestData();
|
invalidateCurrentRequestData();
|
||||||
displayProposal(newProposal, reason);
|
displayProposal(newProposal, reason);
|
||||||
|
|
||||||
|
137
src/plugins/texteditor/codeassist/documentcontentcompletion.cpp
Normal file
137
src/plugins/texteditor/codeassist/documentcontentcompletion.cpp
Normal file
@@ -0,0 +1,137 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
**
|
||||||
|
** Copyright (C) 2018 The Qt Company Ltd.
|
||||||
|
** Contact: https://www.qt.io/licensing/
|
||||||
|
**
|
||||||
|
** 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 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.
|
||||||
|
**
|
||||||
|
** 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.
|
||||||
|
**
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include "documentcontentcompletion.h"
|
||||||
|
|
||||||
|
#include "assistinterface.h"
|
||||||
|
#include "assistproposalitem.h"
|
||||||
|
#include "genericproposal.h"
|
||||||
|
#include "genericproposalmodel.h"
|
||||||
|
#include "iassistprocessor.h"
|
||||||
|
#include "../snippets/snippetassistcollector.h"
|
||||||
|
|
||||||
|
#include "utils/asconst.h"
|
||||||
|
#include "utils/runextensions.h"
|
||||||
|
|
||||||
|
#include <QElapsedTimer>
|
||||||
|
#include <QRegularExpression>
|
||||||
|
#include <QSet>
|
||||||
|
#include <QTextBlock>
|
||||||
|
#include <QTextDocument>
|
||||||
|
|
||||||
|
using namespace TextEditor;
|
||||||
|
|
||||||
|
class DocumentContentCompletionProcessor : public IAssistProcessor
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
DocumentContentCompletionProcessor(const QString &snippetGroupId);
|
||||||
|
|
||||||
|
IAssistProposal *perform(const AssistInterface *interface) override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
TextEditor::SnippetAssistCollector m_snippetCollector;
|
||||||
|
IAssistProposal *createProposal(const AssistInterface *interface);
|
||||||
|
};
|
||||||
|
|
||||||
|
DocumentContentCompletionProvider::DocumentContentCompletionProvider(const QString &snippetGroup)
|
||||||
|
: m_snippetGroup(snippetGroup)
|
||||||
|
{ }
|
||||||
|
|
||||||
|
IAssistProvider::RunType DocumentContentCompletionProvider::runType() const
|
||||||
|
{
|
||||||
|
return Asynchronous;
|
||||||
|
}
|
||||||
|
|
||||||
|
IAssistProcessor *DocumentContentCompletionProvider::createProcessor() const
|
||||||
|
{
|
||||||
|
return new DocumentContentCompletionProcessor(m_snippetGroup);
|
||||||
|
}
|
||||||
|
|
||||||
|
DocumentContentCompletionProcessor::DocumentContentCompletionProcessor(const QString &snippetGroupId)
|
||||||
|
: m_snippetCollector(snippetGroupId, QIcon(":/texteditor/images/snippet.png"))
|
||||||
|
{ }
|
||||||
|
|
||||||
|
IAssistProposal *DocumentContentCompletionProcessor::perform(const AssistInterface *interface)
|
||||||
|
{
|
||||||
|
Utils::onResultReady(Utils::runAsync(
|
||||||
|
&DocumentContentCompletionProcessor::createProposal, this, interface),
|
||||||
|
[this](IAssistProposal *proposal){
|
||||||
|
setAsyncProposalAvailable(proposal);
|
||||||
|
});
|
||||||
|
setPerformWasApplicable(true);
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void generateProposalItems(const QString &text, QSet<QString> &words,
|
||||||
|
QList<AssistProposalItemInterface *> &items)
|
||||||
|
{
|
||||||
|
static const QRegularExpression wordRE("([a-zA-Z_][a-zA-Z0-9_]{2,})");
|
||||||
|
|
||||||
|
QRegularExpressionMatch match;
|
||||||
|
int index = text.indexOf(wordRE, 0, &match);
|
||||||
|
while (index >= 0) {
|
||||||
|
const QString &word = match.captured();
|
||||||
|
if (!words.contains(word)) {
|
||||||
|
auto item = new AssistProposalItem();
|
||||||
|
item->setText(word);
|
||||||
|
items.append(item);
|
||||||
|
words.insert(word);
|
||||||
|
}
|
||||||
|
index += word.size();
|
||||||
|
index = text.indexOf(wordRE, index, &match);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
IAssistProposal *DocumentContentCompletionProcessor::createProposal(
|
||||||
|
const AssistInterface *interface)
|
||||||
|
{
|
||||||
|
QScopedPointer<const AssistInterface> assistInterface(interface);
|
||||||
|
int pos = interface->position();
|
||||||
|
|
||||||
|
QChar chr;
|
||||||
|
// Skip to the start of a name
|
||||||
|
do {
|
||||||
|
chr = interface->characterAt(--pos);
|
||||||
|
} while (chr.isLetterOrNumber() || chr == '_');
|
||||||
|
|
||||||
|
++pos;
|
||||||
|
|
||||||
|
if (interface->reason() == IdleEditor) {
|
||||||
|
QChar characterUnderCursor = interface->characterAt(interface->position());
|
||||||
|
if (characterUnderCursor.isLetterOrNumber() || interface->position() - pos < 3)
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
QSet<QString> words;
|
||||||
|
QList<AssistProposalItemInterface *> items = m_snippetCollector.collect();
|
||||||
|
QTextBlock block = interface->textDocument()->firstBlock();
|
||||||
|
|
||||||
|
while (block.isValid()) {
|
||||||
|
generateProposalItems(block.text(), words, items);
|
||||||
|
block = block.next();
|
||||||
|
}
|
||||||
|
|
||||||
|
return new GenericProposal(pos, items);
|
||||||
|
}
|
@@ -0,0 +1,49 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
**
|
||||||
|
** Copyright (C) 2018 The Qt Company Ltd.
|
||||||
|
** Contact: https://www.qt.io/licensing/
|
||||||
|
**
|
||||||
|
** 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 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.
|
||||||
|
**
|
||||||
|
** 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.
|
||||||
|
**
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "completionassistprovider.h"
|
||||||
|
|
||||||
|
#include "texteditor/texteditorconstants.h"
|
||||||
|
|
||||||
|
namespace TextEditor {
|
||||||
|
|
||||||
|
class AssistInterface;
|
||||||
|
|
||||||
|
class TEXTEDITOR_EXPORT DocumentContentCompletionProvider : public CompletionAssistProvider
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
DocumentContentCompletionProvider(
|
||||||
|
const QString &snippetGroup = QString(Constants::TEXT_SNIPPET_GROUP_ID));
|
||||||
|
|
||||||
|
RunType runType() const override;
|
||||||
|
IAssistProcessor *createProcessor() const override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
QString m_snippetGroup;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // TextEditor
|
@@ -56,7 +56,7 @@
|
|||||||
#include <texteditor/codeassist/assistinterface.h>
|
#include <texteditor/codeassist/assistinterface.h>
|
||||||
#include <texteditor/codeassist/codeassistant.h>
|
#include <texteditor/codeassist/codeassistant.h>
|
||||||
#include <texteditor/codeassist/completionassistprovider.h>
|
#include <texteditor/codeassist/completionassistprovider.h>
|
||||||
#include <texteditor/codeassist/keywordscompletionassist.h>
|
#include <texteditor/codeassist/documentcontentcompletion.h>
|
||||||
#include <texteditor/generichighlighter/context.h>
|
#include <texteditor/generichighlighter/context.h>
|
||||||
#include <texteditor/generichighlighter/highlightdefinition.h>
|
#include <texteditor/generichighlighter/highlightdefinition.h>
|
||||||
#include <texteditor/generichighlighter/highlighter.h>
|
#include <texteditor/generichighlighter/highlighter.h>
|
||||||
@@ -8608,7 +8608,7 @@ void TextEditorFactory::setParenthesesMatchingEnabled(bool on)
|
|||||||
|
|
||||||
IEditor *TextEditorFactory::createEditor()
|
IEditor *TextEditorFactory::createEditor()
|
||||||
{
|
{
|
||||||
static KeywordsCompletionAssistProvider basicSnippetProvider;
|
static DocumentContentCompletionProvider basicSnippetProvider;
|
||||||
TextDocumentPtr doc(d->m_documentCreator());
|
TextDocumentPtr doc(d->m_documentCreator());
|
||||||
|
|
||||||
if (d->m_indenterCreator)
|
if (d->m_indenterCreator)
|
||||||
|
@@ -84,6 +84,7 @@ SOURCES += texteditorplugin.cpp \
|
|||||||
codeassist/genericproposalwidget.cpp \
|
codeassist/genericproposalwidget.cpp \
|
||||||
codeassist/iassistproposalmodel.cpp \
|
codeassist/iassistproposalmodel.cpp \
|
||||||
codeassist/textdocumentmanipulator.cpp \
|
codeassist/textdocumentmanipulator.cpp \
|
||||||
|
codeassist/documentcontentcompletion.cpp\
|
||||||
tabsettingswidget.cpp \
|
tabsettingswidget.cpp \
|
||||||
simplecodestylepreferences.cpp \
|
simplecodestylepreferences.cpp \
|
||||||
simplecodestylepreferenceswidget.cpp \
|
simplecodestylepreferenceswidget.cpp \
|
||||||
@@ -194,6 +195,7 @@ HEADERS += texteditorplugin.h \
|
|||||||
codeassist/iassistproposalmodel.h \
|
codeassist/iassistproposalmodel.h \
|
||||||
codeassist/textdocumentmanipulator.h \
|
codeassist/textdocumentmanipulator.h \
|
||||||
codeassist/textdocumentmanipulatorinterface.h \
|
codeassist/textdocumentmanipulatorinterface.h \
|
||||||
|
codeassist/documentcontentcompletion.h \
|
||||||
tabsettingswidget.h \
|
tabsettingswidget.h \
|
||||||
simplecodestylepreferences.h \
|
simplecodestylepreferences.h \
|
||||||
simplecodestylepreferenceswidget.h \
|
simplecodestylepreferenceswidget.h \
|
||||||
|
@@ -160,6 +160,8 @@ Project {
|
|||||||
"codeassistant.h",
|
"codeassistant.h",
|
||||||
"completionassistprovider.cpp",
|
"completionassistprovider.cpp",
|
||||||
"completionassistprovider.h",
|
"completionassistprovider.h",
|
||||||
|
"documentcontentcompletion.cpp",
|
||||||
|
"documentcontentcompletion.h",
|
||||||
"functionhintproposal.cpp",
|
"functionhintproposal.cpp",
|
||||||
"functionhintproposal.h",
|
"functionhintproposal.h",
|
||||||
"functionhintproposalwidget.cpp",
|
"functionhintproposalwidget.cpp",
|
||||||
|
Reference in New Issue
Block a user