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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
#include "copilothoverhandler.h"
#include "copilotclient.h"
#include "copilotsuggestion.h"
#include "copilottr.h"
#include <texteditor/textdocument.h>
#include <texteditor/textdocumentlayout.h>
#include <texteditor/texteditor.h>
#include <utils/tooltip/tooltip.h>
#include <utils/utilsicons.h>
#include <QPushButton>
#include <QToolBar>
#include <QToolButton>
using namespace TextEditor;
using namespace LanguageServerProtocol;
using namespace Utils;
namespace Copilot::Internal {
class CopilotCompletionToolTip : public QToolBar
{
public:
CopilotCompletionToolTip(QList<Completion> completions,
int currentCompletion,
TextEditorWidget *editor)
: m_numberLabel(new QLabel)
, m_completions(completions)
, m_currentCompletion(std::max(0, std::min<int>(currentCompletion, completions.size() - 1)))
, m_editor(editor)
{
auto prev = addAction(Utils::Icons::PREV_TOOLBAR.icon(),
Tr::tr("Select Previous Copilot Suggestion"));
prev->setEnabled(m_completions.size() > 1);
addWidget(m_numberLabel);
auto next = addAction(Utils::Icons::NEXT_TOOLBAR.icon(),
Tr::tr("Select Next Copilot Suggestion"));
next->setEnabled(m_completions.size() > 1);
auto apply = addAction(Tr::tr("Apply (%1)").arg(QKeySequence(Qt::Key_Tab).toString()));
auto applyWord = addAction(
Tr::tr("Apply Word (%1)").arg(QKeySequence(QKeySequence::MoveToNextWord).toString()));
connect(prev, &QAction::triggered, this, &CopilotCompletionToolTip::selectPrevious);
connect(next, &QAction::triggered, this, &CopilotCompletionToolTip::selectNext);
connect(apply, &QAction::triggered, this, &CopilotCompletionToolTip::apply);
connect(applyWord, &QAction::triggered, this, &CopilotCompletionToolTip::applyWord);
updateLabels();
}
private:
void updateLabels()
{
m_numberLabel->setText(Tr::tr("%1 of %2")
.arg(m_currentCompletion + 1)
.arg(m_completions.count()));
}
void selectPrevious()
{
--m_currentCompletion;
if (m_currentCompletion < 0)
m_currentCompletion = m_completions.size() - 1;
setCurrentCompletion();
}
void selectNext()
{
++m_currentCompletion;
if (m_currentCompletion >= m_completions.size())
m_currentCompletion = 0;
setCurrentCompletion();
}
void setCurrentCompletion()
{
updateLabels();
if (TextSuggestion *suggestion = m_editor->currentSuggestion())
suggestion->reset();
m_editor->insertSuggestion(std::make_unique<CopilotSuggestion>(m_completions,
m_editor->document(),
m_currentCompletion));
}
void apply()
{
if (TextSuggestion *suggestion = m_editor->currentSuggestion()) {
if (!suggestion->apply())
return;
}
ToolTip::hide();
}
void applyWord()
{
if (TextSuggestion *suggestion = m_editor->currentSuggestion()) {
if (!suggestion->applyWord(m_editor))
return;
}
ToolTip::hide();
}
QLabel *m_numberLabel;
QList<Completion> m_completions;
int m_currentCompletion = 0;
TextEditorWidget *m_editor;
};
void CopilotHoverHandler::identifyMatch(TextEditorWidget *editorWidget,
int pos,
ReportPriority report)
{
auto reportNone = qScopeGuard([&] { report(Priority_None); });
if (!editorWidget->suggestionVisible())
return;
QTextCursor cursor(editorWidget->document());
cursor.setPosition(pos);
m_block = cursor.block();
auto *suggestion = dynamic_cast<CopilotSuggestion *>(TextDocumentLayout::suggestion(m_block));
if (!suggestion)
return;
const QList<Completion> completions = suggestion->completions();
if (completions.isEmpty())
return;
reportNone.dismiss();
report(Priority_Suggestion);
}
void CopilotHoverHandler::operateTooltip(TextEditorWidget *editorWidget, const QPoint &point)
{
auto *suggestion = dynamic_cast<CopilotSuggestion *>(TextDocumentLayout::suggestion(m_block));
if (!suggestion)
return;
auto tooltipWidget = new CopilotCompletionToolTip(suggestion->completions(),
suggestion->currentCompletion(),
editorWidget);
const qreal deltay = 2 * editorWidget->textDocument()->fontSettings().lineSpacing();
ToolTip::show(point - QPoint{0, int(deltay)}, tooltipWidget, editorWidget);
}
} // namespace Copilot::Internal
|