4545#include < QTextStream>
4646#include " CodeEditor/codeeditor.h"
4747
48- CodeEditor::CodeEditor (QWidget *parent) : QPlainTextEdit(parent) {
48+ CodeEditor::CodeEditor (QWidget *parent) : QPlainTextEdit(parent), c( 0 ) {
4949 lineNumberArea = new LineNumberArea (this );
5050
5151 connect (this , SIGNAL (blockCountChanged (int )), this ,
@@ -60,6 +60,50 @@ CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent) {
6060 p.setColor (QPalette::Text, Qt::white);
6161 this ->setPalette (p);
6262}
63+
64+ void CodeEditor::setCompleter (QCompleter *completer) {
65+ if (c)
66+ QObject::disconnect (c, 0 , this , 0 );
67+
68+ c = completer;
69+
70+ if (!c)
71+ return ;
72+
73+ c->setWidget (this );
74+ // c->setCompletionMode(QCompleter::PopupCompletion);
75+ // c->setCaseSensitivity(Qt::CaseInsensitive);
76+ QObject::connect (c, SIGNAL (activated (QString)), this ,
77+ SLOT (insertCompletion (QString)));
78+ }
79+
80+ QCompleter *CodeEditor::completer () const { return c; }
81+
82+ void CodeEditor::insertCompletion (const QString &completion) {
83+ if (c->widget () != this )
84+ return ;
85+ QTextCursor tc = textCursor ();
86+ int extra = c->completionPrefix ().length ();
87+ tc.movePosition (QTextCursor::Left, QTextCursor::MoveAnchor, extra);
88+ tc.movePosition (QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
89+ tc.insertText (completion);
90+ tc.movePosition (QTextCursor::Right);
91+ setTextCursor (tc);
92+ }
93+
94+ QString CodeEditor::textUnderCursor () const {
95+
96+ QTextCursor tc = textCursor ();
97+ tc.select (QTextCursor::WordUnderCursor);
98+ return tc.selectedText ();
99+ }
100+
101+ void CodeEditor::focusInEvent (QFocusEvent *e) {
102+ if (c)
103+ c->setWidget (this );
104+ QPlainTextEdit::focusInEvent (e);
105+ }
106+
63107int CodeEditor::lineNumberAreaWidth () {
64108 int digits = 1 ;
65109 int max = qMax (1 , blockCount ());
@@ -149,50 +193,50 @@ bool CodeEditor::KeepIndent() {
149193 return true ;
150194}
151195void CodeEditor::keyPressEvent (QKeyEvent *e) {
152- switch (e->key ()) {
153- case Qt::Key_Backtab:
154- SelectLineMarginBlock ();
155- {
156- QString text (" " );
157- QStringList lines = this ->textCursor ().selection ().toPlainText ().split (
158- QRegExp (" \n |\r\n |\r " ));
159- foreach (QString line, lines) {
160- line.replace (QRegExp (" ^( | | | )(.*)" ), " \\ 2" );
161- text.append (line);
162- text.append (" \n " );
163- }
164- text.truncate (text.length () - 1 );
165- this ->textCursor ().insertText (text);
166- }
167- break ;
168- case Qt::Key_Tab:
169- if (this ->textCursor ().hasSelection ()) {
170- SelectLineMarginBlock ();
171- {
172- QString text (" " );
173- QStringList lines = this ->textCursor ().selection ().toPlainText ().split (
174- QRegExp (" \n |\r\n |\r " ));
175- foreach (QString line, lines) {
176- text.append (" " );
177- text.append (line);
178- text.append (" \n " );
179- }
180- text.truncate (text.length () - 1 );
181- this ->textCursor ().insertText (text);
182- }
183- } else {
184- this ->insertPlainText (" " );
185- }
186- break ;
187- case Qt::Key_Enter:
188- case Qt::Key_Return:
189- if (!KeepIndent ()) {
190- QPlainTextEdit::keyPressEvent (e);
196+ if (c && c->popup ()->isVisible ()) {
197+ // The following keys are forwarded by the completer to the widget
198+ switch (e->key ()) {
199+ case Qt::Key_Enter:
200+ case Qt::Key_Return:
201+ case Qt::Key_Escape:
202+ case Qt::Key_Tab:
203+ case Qt::Key_Backtab:
204+ e->ignore ();
205+ return ; // let the completer do default behavior
206+ default :
207+ break ;
191208 }
192- break ;
193- default :
209+ }
210+
211+ bool isShortcut = ((e->modifiers () & Qt::ControlModifier) &&
212+ e->key () == Qt::Key_Space); // CTRL+Space
213+ if (!c || !isShortcut)
194214 QPlainTextEdit::keyPressEvent (e);
215+
216+ const bool ctrlOrShift =
217+ e->modifiers () & (Qt::ControlModifier | Qt::ShiftModifier);
218+
219+ if (!c || (ctrlOrShift && e->text ().isEmpty ())) {
220+ return ;
221+ }
222+
223+ bool hasModifier = (e->modifiers () != Qt::NoModifier) && !ctrlOrShift;
224+ QString completionPrefix = textUnderCursor ();
225+
226+ if (!isShortcut &&
227+ (hasModifier || e->text ().isEmpty () || completionPrefix.length () < 2 )) {
228+ c->popup ()->hide ();
229+ return ;
230+ }
231+
232+ if (completionPrefix != c->completionPrefix ()) {
233+ c->setCompletionPrefix (completionPrefix);
234+ c->popup ()->setCurrentIndex (c->completionModel ()->index (0 , 0 ));
195235 }
236+ QRect cr = cursorRect ();
237+ cr.setWidth (c->popup ()->sizeHintForColumn (0 ) +
238+ c->popup ()->verticalScrollBar ()->sizeHint ().width ());
239+ c->complete (cr);
196240}
197241
198242void CodeEditor::lineNumberAreaPaintEvent (QPaintEvent *event) {
0 commit comments