forked from qt-creator/qt-creator
Snippet: show mangled replacements in snippets tooltip
Fixes: QTCREATORBUG-24317 Change-Id: Ibaeacc13f49a933836ccf9c906309219e3533564 Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
@@ -26,6 +26,7 @@
|
|||||||
#include "snippet.h"
|
#include "snippet.h"
|
||||||
|
|
||||||
#include <utils/algorithm.h>
|
#include <utils/algorithm.h>
|
||||||
|
#include <utils/qtcassert.h>
|
||||||
#include <utils/templateengine.h>
|
#include <utils/templateengine.h>
|
||||||
|
|
||||||
#include <QTextDocument>
|
#include <QTextDocument>
|
||||||
@@ -153,37 +154,64 @@ bool Snippet::isModified() const
|
|||||||
return m_isModified;
|
return m_isModified;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct SnippetReplacement
|
||||||
|
{
|
||||||
|
QString text;
|
||||||
|
int posDelta = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
static SnippetReplacement replacementAt(int pos, Snippet::ParsedSnippet &parsedSnippet)
|
||||||
|
{
|
||||||
|
static const char kOpenBold[] = "<b>";
|
||||||
|
static const char kCloseBold[] = "</b>";
|
||||||
|
|
||||||
|
auto mangledText = [](const QString &text, const Snippet::ParsedSnippet::Range &range) {
|
||||||
|
if (range.length == 0)
|
||||||
|
return QString("...");
|
||||||
|
if (NameMangler *mangler = range.mangler)
|
||||||
|
return mangler->mangle(text.mid(range.start, range.length));
|
||||||
|
return text.mid(range.start, range.length);
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!parsedSnippet.ranges.isEmpty() && parsedSnippet.ranges.first().start == pos) {
|
||||||
|
Snippet::ParsedSnippet::Range range = parsedSnippet.ranges.takeFirst();
|
||||||
|
return {kOpenBold + mangledText(parsedSnippet.text, range) + kCloseBold, range.length};
|
||||||
|
}
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
QString Snippet::generateTip() const
|
QString Snippet::generateTip() const
|
||||||
{
|
{
|
||||||
static const QLatin1Char kNewLine('\n');
|
static const QHash<QChar, QString> replacements = {{'\n', "<br>"},
|
||||||
static const QLatin1Char kSpace(' ');
|
{' ', " "},
|
||||||
static const QLatin1String kBr("<br>");
|
{'"', """},
|
||||||
static const QLatin1String kNbsp(" ");
|
{'&', "&"},
|
||||||
static const QLatin1String kNoBr("<nobr>");
|
{'<', "<"},
|
||||||
static const QLatin1String kOpenBold("<b>");
|
{'>', ">"}};
|
||||||
static const QLatin1String kCloseBold("</b>");
|
|
||||||
static const QLatin1String kEllipsis("...");
|
|
||||||
|
|
||||||
QString escapedContent(m_content.toHtmlEscaped());
|
ParsedSnippet parsedSnippet = Snippet::parse(m_content);
|
||||||
escapedContent.replace(kNewLine, kBr);
|
|
||||||
escapedContent.replace(kSpace, kNbsp);
|
|
||||||
|
|
||||||
QString tip(kNoBr);
|
QString tip("<nobr>");
|
||||||
int count = 0;
|
int pos = 0;
|
||||||
for (int i = 0; i < escapedContent.count(); ++i) {
|
for (int end = parsedSnippet.text.count(); pos < end;) {
|
||||||
if (escapedContent.at(i) != kVariableDelimiter) {
|
const SnippetReplacement &replacement = replacementAt(pos, parsedSnippet);
|
||||||
tip += escapedContent.at(i);
|
if (!replacement.text.isEmpty()) {
|
||||||
continue;
|
tip += replacement.text;
|
||||||
}
|
pos += replacement.posDelta;
|
||||||
if (++count % 2) {
|
|
||||||
tip += kOpenBold;
|
|
||||||
} else {
|
} else {
|
||||||
if (escapedContent.at(i-1) == kVariableDelimiter)
|
const QChar ¤tChar = parsedSnippet.text.at(pos);
|
||||||
tip += kEllipsis;
|
tip += replacements.value(currentChar, currentChar);
|
||||||
tip += kCloseBold;
|
++pos;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
SnippetReplacement replacement = replacementAt(pos, parsedSnippet);
|
||||||
|
while (!replacement.text.isEmpty()) {
|
||||||
|
tip += replacement.text;
|
||||||
|
pos += replacement.posDelta;
|
||||||
|
replacement = replacementAt(pos, parsedSnippet);
|
||||||
|
}
|
||||||
|
|
||||||
|
QTC_CHECK(parsedSnippet.ranges.isEmpty());
|
||||||
return tip;
|
return tip;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user