forked from qt-creator/qt-creator
Copilot: add Apply Line action to tooltip
Task-number: QTCREATORBUG-31418 Change-Id: Iec4b2d47d47356df851d8949e59243817aafe075 Reviewed-by: Marcus Tillmanns <marcus.tillmanns@qt.io>
This commit is contained in:
@@ -47,11 +47,13 @@ public:
|
||||
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()));
|
||||
auto applyLine = addAction(Tr::tr("Apply Line"));
|
||||
|
||||
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);
|
||||
connect(applyLine, &QAction::triggered, this, &CopilotCompletionToolTip::applyLine);
|
||||
|
||||
updateLabels();
|
||||
}
|
||||
@@ -108,6 +110,15 @@ private:
|
||||
ToolTip::hide();
|
||||
}
|
||||
|
||||
void applyLine()
|
||||
{
|
||||
if (TextSuggestion *suggestion = m_editor->currentSuggestion()) {
|
||||
if (!suggestion->applyLine(m_editor))
|
||||
return;
|
||||
}
|
||||
ToolTip::hide();
|
||||
}
|
||||
|
||||
QLabel *m_numberLabel;
|
||||
QList<Completion> m_completions;
|
||||
int m_currentCompletion = 0;
|
||||
|
@@ -42,6 +42,26 @@ bool CopilotSuggestion::apply()
|
||||
}
|
||||
|
||||
bool CopilotSuggestion::applyWord(TextEditorWidget *widget)
|
||||
{
|
||||
return applyPart(Word, widget);
|
||||
}
|
||||
|
||||
bool CopilotSuggestion::applyLine(TextEditor::TextEditorWidget *widget)
|
||||
{
|
||||
return applyPart(Line, widget);
|
||||
}
|
||||
|
||||
void CopilotSuggestion::reset()
|
||||
{
|
||||
m_start.removeSelectedText();
|
||||
}
|
||||
|
||||
int CopilotSuggestion::position()
|
||||
{
|
||||
return m_start.selectionEnd();
|
||||
}
|
||||
|
||||
bool CopilotSuggestion::applyPart(Part part, TextEditor::TextEditorWidget *widget)
|
||||
{
|
||||
Completion completion = m_completions.value(m_currentCompletion);
|
||||
const Range range = completion.range();
|
||||
@@ -50,11 +70,13 @@ bool CopilotSuggestion::applyWord(TextEditorWidget *widget)
|
||||
const QString text = completion.text();
|
||||
const int startPos = currentCursor.positionInBlock() - cursor.positionInBlock()
|
||||
+ (cursor.selectionEnd() - cursor.selectionStart());
|
||||
const int next = endOfNextWord(text, startPos);
|
||||
int next = part == Word ? endOfNextWord(text, startPos) : text.indexOf('\n', startPos);
|
||||
|
||||
if (next == -1)
|
||||
return apply();
|
||||
|
||||
if (part == Line)
|
||||
++next;
|
||||
QString subText = text.mid(startPos, next - startPos);
|
||||
if (subText.isEmpty())
|
||||
return false;
|
||||
@@ -78,15 +100,5 @@ bool CopilotSuggestion::applyWord(TextEditorWidget *widget)
|
||||
return false;
|
||||
}
|
||||
|
||||
void CopilotSuggestion::reset()
|
||||
{
|
||||
m_start.removeSelectedText();
|
||||
}
|
||||
|
||||
int CopilotSuggestion::position()
|
||||
{
|
||||
return m_start.selectionEnd();
|
||||
}
|
||||
|
||||
} // namespace Copilot::Internal
|
||||
|
||||
|
@@ -18,6 +18,7 @@ public:
|
||||
|
||||
bool apply() final;
|
||||
bool applyWord(TextEditor::TextEditorWidget *widget) final;
|
||||
bool applyLine(TextEditor::TextEditorWidget *widget) final;
|
||||
void reset() final;
|
||||
int position() final;
|
||||
|
||||
@@ -25,6 +26,9 @@ public:
|
||||
int currentCompletion() const { return m_currentCompletion; }
|
||||
|
||||
private:
|
||||
enum Part {Word, Line};
|
||||
bool applyPart(Part part, TextEditor::TextEditorWidget *widget);
|
||||
|
||||
QList<Completion> m_completions;
|
||||
int m_currentCompletion = 0;
|
||||
QTextCursor m_start;
|
||||
|
@@ -134,6 +134,8 @@ public:
|
||||
return false;
|
||||
}
|
||||
|
||||
virtual bool applyLine(TextEditor::TextEditorWidget *widget) override { return false; }
|
||||
|
||||
virtual void reset() override { m_start.removeSelectedText(); }
|
||||
|
||||
virtual int position() override { return m_start.selectionEnd(); }
|
||||
|
@@ -52,6 +52,7 @@ public:
|
||||
virtual bool apply() = 0;
|
||||
// Returns true if the suggestion was applied completely, false if it was only partially applied.
|
||||
virtual bool applyWord(TextEditorWidget *widget) = 0;
|
||||
virtual bool applyLine(TextEditorWidget *widget) = 0;
|
||||
virtual void reset() = 0;
|
||||
virtual int position() = 0;
|
||||
|
||||
|
Reference in New Issue
Block a user