From 5748fecfcb2cf979fe5f787a6e5245a29f12ac29 Mon Sep 17 00:00:00 2001 From: Bas Pape Date: Thu, 24 Sep 2015 20:11:48 +0200 Subject: [PATCH] Remove the tabcompletion space at end of text The "add space after nick when completing mid-sentence" option introduced by 84381d6 left the space even if it turned out to be at the end. Remove it again if the text hasn't changed when sending the text. --- src/uisupport/multilineedit.cpp | 18 +++++++++++++++++- src/uisupport/multilineedit.h | 5 ++++- src/uisupport/tabcompleter.cpp | 2 +- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/uisupport/multilineedit.cpp b/src/uisupport/multilineedit.cpp index ed0dbb60..7e89f499 100644 --- a/src/uisupport/multilineedit.cpp +++ b/src/uisupport/multilineedit.cpp @@ -41,6 +41,7 @@ MultiLineEdit::MultiLineEdit(QWidget *parent) _scrollBarsEnabled(true), _pasteProtectionEnabled(true), _emacsMode(false), + _completionSpace(0), _lastDocumentHeight(-1) { document()->setDocumentMargin(0); @@ -667,8 +668,12 @@ void MultiLineEdit::on_returnPressed() } -void MultiLineEdit::on_returnPressed(const QString &text) +void MultiLineEdit::on_returnPressed(QString text) { + if (_completionSpace && text.endsWith(" ")) { + text.chop(1); + } + if (!text.isEmpty()) { foreach(const QString &line, text.split('\n', QString::SkipEmptyParts)) { if (line.isEmpty()) @@ -687,6 +692,8 @@ void MultiLineEdit::on_returnPressed(const QString &text) void MultiLineEdit::on_textChanged() { + _completionSpace = qMax(_completionSpace - 1, 0); + QString newText = text(); newText.replace("\r\n", "\n"); newText.replace('\r', '\n'); @@ -769,3 +776,12 @@ void MultiLineEdit::showHistoryEntry() setTextCursor(cursor); updateScrollBars(); } + + +void MultiLineEdit::addCompletionSpace() +{ + // Inserting the space emits textChanged, which should not disable removal + _completionSpace = 2; + insertPlainText(" "); +} + diff --git a/src/uisupport/multilineedit.h b/src/uisupport/multilineedit.h index 7591a510..c041013e 100644 --- a/src/uisupport/multilineedit.h +++ b/src/uisupport/multilineedit.h @@ -76,6 +76,8 @@ public: inline qint32 idx() const { return _idx; } inline bool emacsMode() const { return _emacsMode; } + void addCompletionSpace(); + public slots: void setMode(Mode mode); void setMinHeight(int numLines); @@ -101,7 +103,7 @@ protected: private slots: void on_returnPressed(); - void on_returnPressed(const QString &text); + void on_returnPressed(QString text); void on_textChanged(); void on_documentHeightChanged(qreal height); @@ -124,6 +126,7 @@ private: bool _scrollBarsEnabled; bool _pasteProtectionEnabled; bool _emacsMode; + int _completionSpace; QSize _sizeHint; qreal _lastDocumentHeight; diff --git a/src/uisupport/tabcompleter.cpp b/src/uisupport/tabcompleter.cpp index 130095d5..b0e61fd7 100644 --- a/src/uisupport/tabcompleter.cpp +++ b/src/uisupport/tabcompleter.cpp @@ -152,7 +152,7 @@ void TabCompleter::complete() _lastCompletionLength += _nickSuffix.length(); } else if (s.addSpaceMidSentence()) { - _lineEdit->insert(" "); + _lineEdit->addCompletionSpace(); _lastCompletionLength++; } -- 2.20.1