Commit fa15d41d authored by Leandro Melo's avatar Leandro Melo
Browse files

Snippets: Check if file exists.

parent d972308c
......@@ -46,93 +46,96 @@ const QList<CompletionItem> &SnippetsParser::execute(ICompletionCollector *colle
const QIcon &icon,
int order)
{
const QDateTime &lastModified = QFileInfo(m_fileName).lastModified();
if (m_lastTrackedFileChange.isNull() || m_lastTrackedFileChange != lastModified) {
if (!QFile::exists(m_fileName)) {
m_snippets.clear();
} else {
const QDateTime &lastModified = QFileInfo(m_fileName).lastModified();
if (m_lastTrackedFileChange.isNull() || m_lastTrackedFileChange != lastModified) {
m_snippets.clear();
QFile file(m_fileName);
file.open(QIODevice::ReadOnly);
QXmlStreamReader xml(&file);
if (xml.readNextStartElement()) {
if (xml.name() == QLatin1String("snippets")) {
while (xml.readNextStartElement()) {
if (xml.name() == QLatin1String("snippet")) {
TextEditor::CompletionItem item(collector);
QString title;
QString data;
QString description = xml.attributes().value("description").toString();
QFile file(m_fileName);
file.open(QIODevice::ReadOnly);
QXmlStreamReader xml(&file);
if (xml.readNextStartElement()) {
if (xml.name() == QLatin1String("snippets")) {
while (xml.readNextStartElement()) {
if (xml.name() == QLatin1String("snippet")) {
TextEditor::CompletionItem item(collector);
QString title;
QString data;
QString description = xml.attributes().value("description").toString();
while (!xml.atEnd()) {
xml.readNext();
if (xml.isEndElement()) {
int i = 0;
while (i < data.size() && data.at(i).isLetterOrNumber())
++i;
title = data.left(i);
item.text = title;
if (!description.isEmpty()) {
item.text += QLatin1Char(' ');
item.text += description;
}
item.data = QVariant::fromValue(data);
while (!xml.atEnd()) {
xml.readNext();
if (xml.isEndElement()) {
int i = 0;
while (i < data.size() && data.at(i).isLetterOrNumber())
++i;
title = data.left(i);
item.text = title;
if (!description.isEmpty()) {
item.text += QLatin1Char(' ');
item.text += description;
}
item.data = QVariant::fromValue(data);
QString infotip = data;
while (infotip.size() && infotip.at(infotip.size()-1).isSpace())
infotip.chop(1);
infotip.replace(QLatin1Char('\n'), QLatin1String("<br>"));
infotip.replace(QLatin1Char(' '), QLatin1String("&nbsp;"));
{
QString s = QLatin1String("<nobr>");
int count = 0;
for (int i = 0; i < infotip.count(); ++i) {
if (infotip.at(i) != QChar::ObjectReplacementCharacter) {
s += infotip.at(i);
continue;
}
if (++count % 2) {
s += QLatin1String("<b>");
} else {
if (infotip.at(i-1) == QChar::ObjectReplacementCharacter)
s += QLatin1String("...");
s += QLatin1String("</b>");
QString infotip = data;
while (infotip.size() && infotip.at(infotip.size()-1).isSpace())
infotip.chop(1);
infotip.replace(QLatin1Char('\n'), QLatin1String("<br>"));
infotip.replace(QLatin1Char(' '), QLatin1String("&nbsp;"));
{
QString s = QLatin1String("<nobr>");
int count = 0;
for (int i = 0; i < infotip.count(); ++i) {
if (infotip.at(i) != QChar::ObjectReplacementCharacter) {
s += infotip.at(i);
continue;
}
if (++count % 2) {
s += QLatin1String("<b>");
} else {
if (infotip.at(i-1) == QChar::ObjectReplacementCharacter)
s += QLatin1String("...");
s += QLatin1String("</b>");
}
}
infotip = s;
}
infotip = s;
}
item.details = infotip;
item.details = infotip;
item.icon = icon;
item.order = order;
item.isSnippet = true;
m_snippets.append(item);
break;
}
item.icon = icon;
item.order = order;
item.isSnippet = true;
m_snippets.append(item);
break;
}
if (xml.isCharacters())
data += xml.text();
else if (xml.isStartElement()) {
if (xml.name() != QLatin1String("tab"))
xml.raiseError(QLatin1String("invalid snippets file"));
else {
data += QChar::ObjectReplacementCharacter;
data += xml.readElementText();
data += QChar::ObjectReplacementCharacter;
if (xml.isCharacters())
data += xml.text();
else if (xml.isStartElement()) {
if (xml.name() != QLatin1String("tab"))
xml.raiseError(QLatin1String("invalid snippets file"));
else {
data += QChar::ObjectReplacementCharacter;
data += xml.readElementText();
data += QChar::ObjectReplacementCharacter;
}
}
}
} else {
xml.skipCurrentElement();
}
} else {
xml.skipCurrentElement();
}
} else {
xml.skipCurrentElement();
}
} else {
xml.skipCurrentElement();
}
}
if (xml.hasError())
qWarning() << m_fileName << xml.errorString() << xml.lineNumber() << xml.columnNumber();
file.close();
if (xml.hasError())
qWarning() << m_fileName << xml.errorString() << xml.lineNumber() << xml.columnNumber();
file.close();
m_lastTrackedFileChange = lastModified;
m_lastTrackedFileChange = lastModified;
}
}
return m_snippets;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment