TextEditor: move ParsedSnippet out of snippet

Change-Id: I4e9c7d810fdd0904386f8d3b3481c0a4855c2021
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
David Schulz
2021-04-19 14:56:14 +02:00
parent c471b5a3ea
commit a3c7adecd1
3 changed files with 22 additions and 21 deletions

View File

@@ -160,12 +160,12 @@ struct SnippetReplacement
int posDelta = 0;
};
static SnippetReplacement replacementAt(int pos, Snippet::ParsedSnippet &parsedSnippet)
static SnippetReplacement replacementAt(int pos, ParsedSnippet &parsedSnippet)
{
static const char kOpenBold[] = "<b>";
static const char kCloseBold[] = "</b>";
auto mangledText = [](const QString &text, const Snippet::ParsedSnippet::Range &range) {
auto mangledText = [](const QString &text, const ParsedSnippet::Range &range) {
if (range.length == 0)
return QString("...");
if (NameMangler *mangler = range.mangler)
@@ -174,7 +174,7 @@ static SnippetReplacement replacementAt(int pos, Snippet::ParsedSnippet &parsedS
};
if (!parsedSnippet.ranges.isEmpty() && parsedSnippet.ranges.first().start == pos) {
Snippet::ParsedSnippet::Range range = parsedSnippet.ranges.takeFirst();
ParsedSnippet::Range range = parsedSnippet.ranges.takeFirst();
return {kOpenBold + mangledText(parsedSnippet.text, range) + kCloseBold, range.length};
}
return {};
@@ -215,13 +215,13 @@ QString Snippet::generateTip() const
return tip;
}
Snippet::ParsedSnippet Snippet::parse(const QString &snippet)
ParsedSnippet Snippet::parse(const QString &snippet)
{
static UppercaseMangler ucMangler;
static LowercaseMangler lcMangler;
static TitlecaseMangler tcMangler;
Snippet::ParsedSnippet result;
ParsedSnippet result;
QString errorMessage;
QString preprocessedSnippet
@@ -418,7 +418,7 @@ void Internal::TextEditorPlugin::testSnippetParsing()
Q_ASSERT(ranges_start.count() == ranges_length.count()); // sanity check for the test data
Q_ASSERT(ranges_start.count() == ranges_mangler.count()); // sanity check for the test data
Snippet::ParsedSnippet result = Snippet::parse(input);
ParsedSnippet result = Snippet::parse(input);
QCOMPARE(result.text, text);
QCOMPARE(result.success, success);

View File

@@ -44,6 +44,21 @@ public:
virtual QString mangle(const QString &unmangled) const = 0;
};
class TEXTEDITOR_EXPORT ParsedSnippet
{
public:
QString text;
QString errorMessage;
bool success;
struct Range {
Range(int s, int l, NameMangler *m) : start(s), length(l), mangler(m) { }
int start;
int length;
NameMangler *mangler;
};
QList<Range> ranges;
};
class TEXTEDITOR_EXPORT Snippet
{
public:
@@ -76,20 +91,6 @@ public:
static const QChar kVariableDelimiter;
static const QChar kEscapeChar;
class ParsedSnippet {
public:
QString text;
QString errorMessage;
bool success;
struct Range {
Range(int s, int l, NameMangler *m) : start(s), length(l), mangler(m) { }
int start;
int length;
NameMangler *mangler;
};
QList<Range> ranges;
};
static ParsedSnippet parse(const QString &snippet);
private:

View File

@@ -2700,7 +2700,7 @@ void TextEditorWidget::keyPressEvent(QKeyEvent *e)
void TextEditorWidget::insertCodeSnippet(const QTextCursor &cursor_arg, const QString &snippet)
{
Snippet::ParsedSnippet data = Snippet::parse(snippet);
ParsedSnippet data = Snippet::parse(snippet);
if (!data.success) {
QString message = QString::fromLatin1("Cannot parse snippet \"%1\".").arg(snippet);