Merge pull request #150 from Tucos/autospace
authorManuel Nickschas <sputnick@quassel-irc.org>
Thu, 8 Oct 2015 22:07:54 +0000 (00:07 +0200)
committerManuel Nickschas <sputnick@quassel-irc.org>
Thu, 8 Oct 2015 22:07:54 +0000 (00:07 +0200)
Automatically remove automatically added space

src/uisupport/multilineedit.cpp
src/uisupport/multilineedit.h
src/uisupport/tabcompleter.cpp

index ed0dbb6..7e89f49 100644 (file)
@@ -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(" ");
+}
+
index 7591a51..f83c4de 100644 (file)
@@ -35,9 +35,6 @@
 #  define MultiLineEditParent QTextEdit
 #endif
 
-class QKeyEvent;
-class TabCompleter;
-
 class MultiLineEdit : public MultiLineEditParent
 {
     Q_OBJECT
@@ -76,6 +73,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 +100,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 +123,7 @@ private:
     bool _scrollBarsEnabled;
     bool _pasteProtectionEnabled;
     bool _emacsMode;
+    int _completionSpace;
 
     QSize _sizeHint;
     qreal _lastDocumentHeight;
index 130095d..b0e61fd 100644 (file)
@@ -152,7 +152,7 @@ void TabCompleter::complete()
             _lastCompletionLength += _nickSuffix.length();
         }
         else if (s.addSpaceMidSentence()) {
-            _lineEdit->insert(" ");
+            _lineEdit->addCompletionSpace();
             _lastCompletionLength++;
         }