summaryrefslogtreecommitdiff
path: root/src/plugins/texteditor/codeassist/documentcontentcompletion.cpp
blob: 44506678e027cf69072a666ee5bf7fbc85104f9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright (C) 2018 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "documentcontentcompletion.h"

#include "assistinterface.h"
#include "assistproposalitem.h"
#include "asyncprocessor.h"
#include "genericproposal.h"
#include "iassistprocessor.h"
#include "../snippets/snippetassistcollector.h"
#include "../completionsettings.h"
#include "../texteditorsettings.h"

#include <utils/algorithm.h>

#include <QElapsedTimer>
#include <QRegularExpression>
#include <QSet>
#include <QTextBlock>
#include <QTextDocument>

using namespace TextEditor;

class DocumentContentCompletionProcessor final : public AsyncProcessor
{
public:
    DocumentContentCompletionProcessor(const QString &snippetGroupId);
    ~DocumentContentCompletionProcessor() final;

    IAssistProposal *performAsync() override;

private:
    QString m_snippetGroup;
};

DocumentContentCompletionProvider::DocumentContentCompletionProvider(const QString &snippetGroup)
    : m_snippetGroup(snippetGroup)
{ }

IAssistProcessor *DocumentContentCompletionProvider::createProcessor(const AssistInterface *) const
{
    return new DocumentContentCompletionProcessor(m_snippetGroup);
}

DocumentContentCompletionProcessor::DocumentContentCompletionProcessor(const QString &snippetGroupId)
    : m_snippetGroup(snippetGroupId)
{ }

DocumentContentCompletionProcessor::~DocumentContentCompletionProcessor()
{
    cancel();
}

IAssistProposal *DocumentContentCompletionProcessor::performAsync()
{
    int pos = interface()->position();

    QChar chr;
    // Skip to the start of a name
    do {
        chr = interface()->characterAt(--pos);
    } while (chr.isLetterOrNumber() || chr == '_');

    ++pos;
    int length = interface()->position() - pos;

    if (interface()->reason() == IdleEditor) {
        QChar characterUnderCursor = interface()->characterAt(interface()->position());
        if (characterUnderCursor.isLetterOrNumber()
                || length < TextEditorSettings::completionSettings().m_characterThreshold) {
            return nullptr;
        }
    }

    const TextEditor::SnippetAssistCollector snippetCollector(
                m_snippetGroup, QIcon(":/texteditor/images/snippet.png"));
    QList<AssistProposalItemInterface *> items = snippetCollector.collect();

    const QString wordUnderCursor = interface()->textAt(pos, length);
    const QString text = interface()->textDocument()->toPlainText();

    const QRegularExpression wordRE("([\\p{L}_][\\p{L}0-9_]{2,})");
    QSet<QString> words;
    QRegularExpressionMatchIterator it = wordRE.globalMatch(text);
    int wordUnderCursorFound = 0;
    while (it.hasNext()) {
        if (isCanceled())
            return nullptr;
        QRegularExpressionMatch match = it.next();
        const QString &word = match.captured();
        if (word == wordUnderCursor) {
            // Only add the word under cursor if it
            // already appears elsewhere in the text
            if (++wordUnderCursorFound < 2)
                continue;
        }

        if (!words.contains(word)) {
            auto item = new AssistProposalItem();
            item->setText(word);
            items.append(item);
            words.insert(word);
        }
    }

    return new GenericProposal(pos, items);
}