File: qcodecompletionengine.cpp

package info (click to toggle)
texstudio 2.11.2%2Bdebian-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 41,292 kB
  • ctags: 12,405
  • sloc: cpp: 93,072; xml: 10,217; ansic: 4,153; sh: 145; makefile: 56
file content (318 lines) | stat: -rw-r--r-- 6,713 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/****************************************************************************
**
** Copyright (C) 2006-2009 fullmetalcoder <fullmetalcoder@hotmail.fr>
**
** This file is part of the Edyuk project <http://edyuk.org>
**
** This file may be used under the terms of the GNU General Public License
** version 3 as published by the Free Software Foundation and appearing in the
** file GPL.txt included in the packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/

#include "qcodecompletionengine.h"

/*!
	\file qcompletionengine.cpp
	\brief Implementation of the QCodeCompletionEngine class.
*/

#include "qeditor.h"

#ifdef _QCODE_MODEL_
#include "qcodebuffer.h"
#endif

/*!

*/
QCodeCompletionEngine::QCodeCompletionEngine(QObject *p)
 : QObject(p), m_max(0)
{
	pForcedTrigger = new QAction(tr("&Trigger completion"), this);

	connect(pForcedTrigger	, SIGNAL( triggered() ),
			this			, SLOT  ( complete() ) );

}

/*!

*/
QCodeCompletionEngine::~QCodeCompletionEngine()
{

}

/*!
	\return
*/
QAction* QCodeCompletionEngine::triggerAction() const
{
	return pForcedTrigger;
}

/*!

*/
void QCodeCompletionEngine::retranslate()
{
	pForcedTrigger->setText(tr("&Trigger completion"));
}

/*!

*/
QStringList QCodeCompletionEngine::triggers() const
{
	return m_triggers;
}

/*!

*/
void QCodeCompletionEngine::addTrigger(const QString& s)
{
	if ( m_triggers.contains(s) )
		return;

	if ( s.count() > m_max )
		m_max = s.count();

	m_triggers << s;
}

/*!

*/
void QCodeCompletionEngine::removeTrigger(const QString& s)
{
	m_triggers.removeAll(s);
}

/*!

*/
void QCodeCompletionEngine::setCodeModel(QCodeModel *m)
{
	Q_UNUSED(m)
}

/*!

*/
QEditor* QCodeCompletionEngine::editor() const
{
	return pEdit;
}

/*!
	\brief Attach the completion engine instance to a new editor object
*/
void QCodeCompletionEngine::setEditor(QEditor *e)
{
	if ( pEdit )
	{
		pEdit->removeAction(pForcedTrigger, "&Edit");
		//pEdit->removeEventFilter(this);

		disconnect(	pEdit	, SIGNAL( textEdited(QKeyEvent*) ),
					this	, SLOT  ( textEdited(QKeyEvent*) ) );
	}

	pEdit = e;

	if ( pEdit )
	{
		//pEdit->installEventFilter(this);
		pEdit->addAction(pForcedTrigger, "&Edit");

		connect(pEdit	, SIGNAL( textEdited(QKeyEvent*) ),
				this	, SLOT  ( textEdited(QKeyEvent*) ) );
	}
}

/*!
	\internal
*/
void QCodeCompletionEngine::run()
{
	if ( m_cur.isNull() )
		return;

	//qDebug("complete!");

	complete(m_cur, m_trig);

	m_cur = QDocumentCursor();
	m_trig.clear();
}

/*!
	\brief Forced completion trigger
*/
void QCodeCompletionEngine::complete()
{
	complete(editor()->cursor(), QString());
}

/*!
	\brief Standard completion entry point for QEditor
	\param e QKeyEvent that caused a modification of the text

	\note This slot is only called when editing happens without
	any cursor mirrors
*/
void QCodeCompletionEngine::textEdited(QKeyEvent *k)
{
	QString s, txt = s = k->text();
	QDocumentCursor cur = editor()->cursor();

	int count = txt.count();

	if ( txt.isEmpty() || m_triggers.isEmpty() )
		return;

	//qDebug("should trigger completion? (bis)");

	if ( count > m_max )
	{
		txt = txt.right(m_max);

	} else if ( count < m_max ) {

		QDocumentCursor c(cur);
		c.movePosition(m_max, QDocumentCursor::Left, QDocumentCursor::KeepAnchor);

		//qDebug("prev text : %s", qPrintable(c.selectedText()));

		txt = c.selectedText();
	}

	//qDebug("text : %s", qPrintable(txt));

	foreach ( QString trig, m_triggers )
	{
		if ( txt.endsWith(trig) )
		{
			cur = editor()->cursor();
			cur.movePosition(trig.count(), QDocumentCursor::PreviousCharacter);

			// notify completion trigger
			emit completionTriggered(trig);

			//get rid of previous calltips/completions
			editor()->setFocus();

			// trigger completion
			complete(cur, trig);
		}
	}
}

/*!
	\internal
*/
bool QCodeCompletionEngine::eventFilter(QObject *o, QEvent *e)
{
	if ( !e || !o || (e->type() != QEvent::KeyPress) || (o != pEdit) )
		return false;

	//qDebug("should trigger completion?");

	QDocumentCursor cur = editor()->cursor();
	QKeyEvent *k = static_cast<QKeyEvent*>(e);

	QString s, txt = s = k->text();

	int count = txt.count();

	if ( txt.isEmpty() || m_triggers.isEmpty() )
		return false; // QThread::eventFilter(o, e);

	//qDebug("should trigger completion? (bis)");

	if ( count > m_max )
	{
		txt = txt.right(m_max);

	} else if ( count < m_max ) {

		QDocumentCursor c(cur);
		c.movePosition(m_max - count, QDocumentCursor::Left, QDocumentCursor::KeepAnchor);

		//qDebug("prev text : %s", qPrintable(c.selectedText()));

		txt.prepend(c.selectedText());
	}

	//qDebug("text : %s", qPrintable(txt));

	foreach ( QString trig, m_triggers )
	{
		if ( txt.endsWith(trig) )
		{
			editor()->write(s);

			cur = editor()->cursor();
			cur.movePosition(trig.count(), QDocumentCursor::PreviousCharacter);

			// notify completion trigger
			emit completionTriggered(trig);

			//get rid of previous calltips/completions
			editor()->setFocus();

			// trigger completion
			complete(cur, trig);

			return true;
		}
	}

	return false;
}

/*!
	\brief Completion callback
*/
void QCodeCompletionEngine::complete(const QDocumentCursor& c, const QString& trigger)
{
	#ifdef _QCODE_MODEL_
	// TODO :
	//	* use a more efficient design by avoiding deep copy of the data
	//	* only lex the requested part (stop at cursor or topmost frame required for proper class hierarchy)

	QDocumentCursor cc = c;
	cc.movePosition(1, QDocumentCursor::Start, QDocumentCursor::KeepAnchor);

	//qDebug("%s", qPrintable(cc.selectedText()));

	QCodeBuffer buffer(cc.selectedText());
	//QCodeBuffer buffer(c.document()->text());
	complete(&buffer, trigger);
	#else
	Q_UNUSED(c)
	Q_UNUSED(trigger)
	qWarning("From complete(QDocumentCursor, QString)");
	qWarning("QCodeCompletionEngine is not self-sufficient : subclasses should "
			"reimplement at least on of the complete() method...");
	#endif
}

/*!
	\overload
	\brief Overloaded completion callback
*/
void QCodeCompletionEngine::complete(QCodeStream *s, const QString& trigger)
{
	Q_UNUSED(s)
	Q_UNUSED(trigger)
	
	qWarning("From complete(QCodeStream*, QString)");
	qWarning("QCodeCompletionEngine is not self-sufficient : subclasses should"
			"reimplement at least on of the complete() method...");
}