File: qlanguagefactory.cpp

package info (click to toggle)
texstudio 2.8.4%2Bdebian-3
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 39,108 kB
  • ctags: 13,390
  • sloc: cpp: 84,770; xml: 10,109; ansic: 6,865; sh: 150; makefile: 82
file content (329 lines) | stat: -rw-r--r-- 8,452 bytes parent folder | download | duplicates (2)
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
319
320
321
322
323
324
325
326
327
328
329
/****************************************************************************
**
** 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 "qlanguagefactory.h"

/*!
	\file qlanguagefactory.cpp
	\brief Implementation of QLanguageFactory

	\see QLanguageFactory
*/

/*!
	\ingroup language
	@{

	\class QLanguageFactory
	\brief A class managing language definitions.

	It stores language definitions, added programmatically or found in XML files,
	in specified locations and only if generic components are built-in. From
	these definitions, QLanguageFactory generates matchers, indenters and
	highlighters for a text editor, according to a file name.

	\see QLanguageDefinition
*/

#include "qeditor.h"
#include "qformatscheme.h"

#include "qlanguagedefinition.h"
#include "qcodecompletionengine.h"

#ifdef QNFA_BUILD
#include "qnfadefinition.h"
#endif

/*!
	\brief Empty constructor
*/
QLanguageFactory::QLanguageFactory(QFormatScheme *fmt, QObject *p)
 : QObject(p), m_defaultFormatScheme(fmt)
{

}

/*!
	\brief Empty destructor
*/
QLanguageFactory::~QLanguageFactory()
{
	foreach ( const LangData& d, m_data.values() )
		deleteLangData(d);
}

/*!
	\return a list of languages supported by this factory
*/
QStringList QLanguageFactory::languages() const
{
	return m_languages;
}

/*!
	\return a list of file filters supported by this factory

	\note This list is NEVER empty and the last item is always "All files (*)"
*/
QStringList QLanguageFactory::fileFilters() const
{
	QStringList l;

	foreach ( QString lng, m_languages )
		l << tr("%1 files (*.%2)").arg(lng).arg(m_data[lng].extensions.join(" *."));

	l << tr("All files (*)");

	return l;
}

/*!
	\param e target editor
	\param file filename displayed by the editor

	The \a file parameter may actuall be either a filename, an extension or the name of the language,
	checked in that order.

	If it is a filename, complete extension as higher priority than simple extension
	(see QFileInfo suffix() and completeSuffix()).

	Matches are first done case-sensitively.

	If no matching language definition is found for all three possible interpretations of the \a file
	parameter, the same search is done case-insensitively.

	If no matching language is found the previous language definition/completion engine of the editor
	are removed, leaving it blank, and the format scheme of the document is set to the defaultFormatScheme()
*/
void QLanguageFactory::setLanguage(QEditor *e, const QString& file)
{
	QString lang;
	QFileInfo inf(file);
	const QString ext = inf.suffix(),
				cext = inf.completeSuffix();

	//qDebug("suff:%s; compSuff:%s", qPrintable(ext), qPrintable(cext));

	if ( file.count() )
	{
		QList<Qt::CaseSensitivity> lcs;
		lcs << Qt::CaseSensitive << Qt::CaseInsensitive;

		foreach ( Qt::CaseSensitivity cs, lcs )
		{
			int n = 0, idx = -1;
			QStringList ext_langs, cext_langs, fcext_langs;

			foreach ( QString lang, m_languages )
			{
				const QStringList& exts = m_data[lang].extensions;

				//qDebug("%s in (%s) ?", qPrintable(ext), qPrintable(exts.join(" ")));

				foreach ( QString x, exts )
				{
					if ( !x.compare(ext, cs) )
						ext_langs << lang;

					if ( !x.compare(cext, cs) )
						cext_langs << lang;

					if ( !x.compare(file, cs) )
						fcext_langs << lang;
				}

				if ( !lang.compare(file, cs) )
					idx = n;

				++n;
			}

			if ( cext_langs.count() )
			{
				// TODO : use MIME types to resolve ambiguity
				lang = cext_langs.first();
			} else if ( ext_langs.count() ) {
				// TODO : use MIME types to resolve ambiguity
				lang = ext_langs.first();
			} else if ( fcext_langs.count() ) {
				// TODO : use MIME types to resolve ambiguity
				lang = fcext_langs.first();
			} else if ( idx != -1 ) {
				lang = m_languages.at(idx);
			}

			if ( lang.count() )
				break;
		}
	}
	
	setLanguageFromName(e, lang);
}

	
void QLanguageFactory::setLanguageFromName(QEditor *e, const QString& lang){
	QLanguageDefinition *oldLang = e->languageDefinition();

	if ( lang.isEmpty() && !m_data.contains(lang))
	{
		//qDebug("no lang match for %s", qPrintable(file));
		e->setLanguageDefinition(0);
		e->setCompletionEngine(0);
		e->document()->setFormatScheme(m_defaultFormatScheme);

		if ( oldLang )
			e->highlight();

	} else {
		//qDebug("lang match for %s : %s", qPrintable(file), qPrintable(lang));
		const LangData& data = m_data[lang];

		e->setLanguageDefinition(data.d);
		e->setCompletionEngine(data.e ? data.e->clone() : 0);
		e->document()->setFormatScheme(data.s ? data.s : m_defaultFormatScheme);

		if ( oldLang != data.d )
			e->highlight();
	}
}

/*!
	\brief Adds a language to the factory

	\param d language data

	\note The language data will overwrite any existing one for the same language
*/
void QLanguageFactory::addLanguage(const QLanguageFactory::LangData& d)
{
//TODO: disable, it causes a memory leak, enabled it crashes when an embed definition is used
//	if (m_data.contains(d.lang))
//		deleteLangData(m_data.value(d.lang));

	m_data.insert(d.lang, d);

	if ( !d.e )
	{
		foreach ( QCodeCompletionEngine *e, m_unusedEngines )
		{
			if ( e->language() == d.lang )
			{
				m_data[d.lang].e = e;
				break;
			}
		}
	}

	if ( !m_languages.contains(d.lang) )
		m_languages << d.lang;
}

/*!
	\brief Lookup language data for a matching language

	The primary purpose of this function is to make it easy to create configuration dialogs (mainly for
	format schemes). Beware though : some language may use the default format scheme. It is recommended
	to check for that before modifying a format scheme or users might be surprised...

	\warning This function will lead to crashes if you pass it a language name not contained in languages().
*/
const QLanguageFactory::LangData& QLanguageFactory::languageData(const QString& lang)
{
	return m_data[lang];
}

/*!
	\brief Registers a new completion engine

	\note This engine will NOT be used if there are no language definition for the
	language it supports...
*/
void QLanguageFactory::addLanguageDefinition(QLanguageDefinition *l)
{
	Q_UNUSED(l)
	
	qWarning("New design does not allow this sorry...");
}

/*!
	\brief Registers a new completion engine

	\note This engine will NOT be used if there are no language definition for the
	language it supports...
*/
void QLanguageFactory::addCompletionEngine(QCodeCompletionEngine *e)
{
	foreach ( QString l, m_languages )
	{
		if ( l == e->language() )
		{
			m_data[l].e = e;
			return;
		}
	}

	m_unusedEngines << e;
}

/*!
	\brief Fetches syntax definitions from files in \a path
*/
void QLanguageFactory::addDefinitionPath(const QString& path)
{
	QDir d(path);

	foreach ( QString f, d.entryList(QDir::Files | QDir::Readable) )
	{
		#ifdef QNFA_BUILD
		if ( f.endsWith(".qnfa") )
		{
			//qDebug("loading file %s", qPrintable(f));
			QFileInfo info(d.filePath(f));
			QString specificFormatScheme = QDir(info.path()).filePath(info.baseName() + ".qxf");

			QFormatScheme *scheme = m_defaultFormatScheme;

			if ( QFile::exists(specificFormatScheme) )
			{
				scheme = new QFormatScheme(specificFormatScheme);
			}

			LangData data;
			QNFADefinition::load(d.filePath(f), &data, scheme);

			//qDebug() << "load: "<<data.d;

			//qDebug("%s : (%s | %s)", qPrintable(data.lang), qPrintable(data.mime), qPrintable(data.extensions.join(", ")));
			addLanguage(data);
			//addLanguageDefinition(new QNFADefinition(d.filePath(f), this));
		}
		#endif
	}
}

void QLanguageFactory::deleteLangData(const LangData& d){
{
	if ( d.s != m_defaultFormatScheme && d.s)
		delete d.s;

	//qDebug() << "delete: "<<d.d;
	if (d.d)
		delete d.d;
	//delete d.e;
}
}

/*! @} */