diff --git a/src/plugins/texteditor/snippetsparser.cpp b/src/plugins/texteditor/snippetsparser.cpp index 63ddc93aeee881fdc215a6cfe15e8fdaeaefa733..7e30cd94d3776021039a8e7f2085ab76a0e97e2b 100644 --- a/src/plugins/texteditor/snippetsparser.cpp +++ b/src/plugins/texteditor/snippetsparser.cpp @@ -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(" ")); - { - 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(" ")); + { + 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;