[Gui] Make autocompletion global

Now auto complete is in all TextEdit objects. Fixes #12850
This commit is contained in:
Benjamin Bræstrup Sayoc
2024-10-06 23:54:20 +02:00
committed by Chris Hennes
parent 44221711f8
commit 81a27a9252
5 changed files with 65 additions and 33 deletions

View File

@@ -31,6 +31,7 @@ class QPlainTextEdit;
namespace Py {
class Object;
class List;
class String;
}
namespace Gui {

View File

@@ -43,7 +43,6 @@
#include "PythonConsolePy.h"
#include "PythonTracing.h"
#include "Application.h"
#include "CallTips.h"
#include "FileDialog.h"
#include "MainWindow.h"
#include "Tools.h"
@@ -90,7 +89,6 @@ struct PythonConsoleP
CopyType type;
PyObject *_stdoutPy=nullptr, *_stderrPy=nullptr, *_stdinPy=nullptr, *_stdin=nullptr;
InteractiveInterpreter* interpreter=nullptr;
CallTipsList* callTipsList=nullptr;
ConsoleHistory history;
QString output, error, info, historyFile;
QStringList statements;
@@ -439,17 +437,6 @@ PythonConsole::PythonConsole(QWidget *parent)
setVisibleLineNumbers(false);
setEnabledHighlightCurrentLine(false);
// create the window for call tips
d->callTipsList = new CallTipsList(this);
d->callTipsList->setFrameStyle(QFrame::Box);
d->callTipsList->setFrameShadow(QFrame::Raised);
d->callTipsList->setLineWidth(2);
installEventFilter(d->callTipsList);
viewport()->installEventFilter(d->callTipsList);
d->callTipsList->setSelectionMode( QAbstractItemView::SingleSelection );
d->callTipsList->hide();
QFont serifFont(QLatin1String("Courier"), 10, QFont::Normal);
setFont(serifFont);
@@ -536,6 +523,18 @@ void PythonConsole::OnChange(Base::Subject<const char*> &rCaller, const char* sR
}
}
int PythonConsole::getInputStringPosition()
{
QString rawLine = textCursor().block().text();
return textCursor().positionInBlock() - promptLength(rawLine);
}
QString PythonConsole::getInputString()
{
QString rawLine = textCursor().block().text();
return stripPromptFrom(rawLine);
}
/**
* Checks the input of the console to make the correct indentations.
* After a command is prompted completely the Python interpreter is started.
@@ -625,22 +624,6 @@ void PythonConsole::keyPressEvent(QKeyEvent * e)
runSource( inputStrg ); //< commit input string
} break;
case Qt::Key_Period:
{
// In Qt 4.8 there is a strange behaviour because when pressing ":"
// then key is also set to 'Period' instead of 'Colon'. So we have
// to make sure we only handle the period.
if (e->text() == QLatin1String(".")) {
// analyse context and show available call tips
int contextLength = cursor.position() - inputLineBegin.position();
PythonTextEditor::keyPressEvent(e);
d->callTipsList->showTips( inputStrg.left( contextLength ) );
}
else {
PythonTextEditor::keyPressEvent(e);
}
} break;
case Qt::Key_Home:
{
QTextCursor::MoveMode mode = (e->modifiers() & Qt::ShiftModifier)? QTextCursor::KeepAnchor
@@ -690,10 +673,6 @@ void PythonConsole::keyPressEvent(QKeyEvent * e)
PythonTextEditor::keyPressEvent(e);
} break;
}
// This can't be done in CallTipsList::eventFilter() because we must first perform
// the event and afterwards update the list widget
if (d->callTipsList->isVisible())
{ d->callTipsList->validateCursor(); }
// disable history restart if input line changed
restartHistory &= (inputLine != inputBlock.text());

View File

@@ -115,6 +115,8 @@ public:
void OnChange( Base::Subject<const char*> &rCaller,const char* rcReason ) override;
void printStatement( const QString& cmd );
QString readline( );
int getInputStringPosition() override;
QString getInputString() override;
public Q_SLOTS:
void onSaveHistoryAs();

View File

@@ -32,6 +32,7 @@
# include <QTextCursor>
#endif
#include "CallTips.h"
#include "TextEdit.h"
#include "SyntaxHighlighter.h"
#include "Tools.h"
@@ -46,6 +47,16 @@ using namespace Gui;
TextEdit::TextEdit(QWidget* parent)
: QPlainTextEdit(parent), cursorPosition(0), listBox(nullptr)
{
// create the window for call tips
callTipsList = new CallTipsList(this);
callTipsList->setFrameStyle(QFrame::Box);
callTipsList->setFrameShadow(QFrame::Raised);
callTipsList->setLineWidth(2);
installEventFilter(callTipsList);
viewport()->installEventFilter(callTipsList);
callTipsList->setSelectionMode( QAbstractItemView::SingleSelection );
callTipsList->hide();
//Note: Set the correct context to this shortcut as we may use several instances of this
//class at a time
auto shortcut = new QShortcut(this);
@@ -77,7 +88,9 @@ TextEdit::~TextEdit() = default;
*/
void TextEdit::keyPressEvent(QKeyEvent* e)
{
// We want input to be appended to document before doing calltips
QPlainTextEdit::keyPressEvent(e);
// This can't be done in CompletionList::eventFilter() because we must first perform
// the event and afterwards update the list widget
if (listBox && listBox->isVisible()) {
@@ -94,6 +107,35 @@ void TextEdit::keyPressEvent(QKeyEvent* e)
listBox->keyboardSearch(cursor.selectedText());
cursor.clearSelection();
}
if (e->key() == Qt::Key_Period)
{
// QTextCursor cursor = this->textCursor();
// In Qt 4.8 there is a strange behaviour because when pressing ":"
// then key is also set to 'Period' instead of 'Colon'. So we have
// to make sure we only handle the period.
if (e->text() == QLatin1String(".")) {
// analyse context and show available call tips
// TODO: idk why we need to remove the . from the input string (- 1). This shouldn't be needed
QString textToBeCompleted = getInputString().left(getInputStringPosition() - 1);
callTipsList->showTips( textToBeCompleted );
}
}
// This can't be done in CallTipsList::eventFilter() because we must first perform
// the event and afterwards update the list widget
if (callTipsList->isVisible()) {
callTipsList->validateCursor();
}
}
int TextEdit::getInputStringPosition() {
return textCursor().positionInBlock();
}
QString TextEdit::getInputString() {
return textCursor().block().text();
}
void TextEdit::wheelEvent(QWheelEvent* e)

View File

@@ -27,6 +27,8 @@
#include <QListWidget>
#include <QPlainTextEdit>
#include "CallTips.h"
#include "Window.h"
@@ -60,6 +62,11 @@ public:
explicit TextEdit(QWidget *parent = nullptr);
~TextEdit() override;
//! Get the cursor position of the current line being edited
virtual int getInputStringPosition();
//! Get the text of the current line being edited
virtual QString getInputString();
private Q_SLOTS:
void complete();
@@ -71,6 +78,7 @@ Q_SIGNALS:
protected:
void keyPressEvent(QKeyEvent *) override;
void wheelEvent(QWheelEvent* e) override;
CallTipsList* callTipsList = nullptr;
private:
void createListBox();