Changeset View
Changeset View
Standalone View
Standalone View
src/utils/katemacroexpander.cpp
- This file was added.
1 | /* This file is part of the KDE project | ||||
---|---|---|---|---|---|
2 | * | ||||
3 | * Copyright 2019 Dominik Haumann <dhaumann@kde.org> | ||||
4 | * | ||||
5 | * This library is free software; you can redistribute it and/or | ||||
6 | * modify it under the terms of the GNU Library General Public | ||||
7 | * License as published by the Free Software Foundation; either | ||||
8 | * version 2 of the License, or (at your option) any later version. | ||||
9 | * | ||||
10 | * This library is distributed in the hope that it will be useful, | ||||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
13 | * Library General Public License for more details. | ||||
14 | * | ||||
15 | * You should have received a copy of the GNU Library General Public License | ||||
16 | * along with this library; see the file COPYING.LIB. If not, write to | ||||
17 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||||
18 | * Boston, MA 02110-1301, USA. | ||||
19 | */ | ||||
20 | #include "katemacroexpander.h" | ||||
21 | | ||||
22 | #include <KTextEditor/Editor> | ||||
23 | | ||||
24 | /** | ||||
25 | * Find closing bracket for @p str starting a position @p pos. | ||||
26 | */ | ||||
27 | static int findClosing(QStringView str, int pos = 0) | ||||
28 | { | ||||
29 | const int len = str.size(); | ||||
30 | int nesting = 0; | ||||
31 | | ||||
32 | while (pos < len) { | ||||
33 | ++pos; | ||||
34 | const QChar c = str[pos]; | ||||
35 | if (c == QLatin1Char('}')) { | ||||
36 | if (nesting == 0) { | ||||
37 | return pos; | ||||
38 | } | ||||
39 | nesting--; | ||||
40 | } else if (c == QLatin1Char('{')) { | ||||
41 | nesting++; | ||||
42 | } | ||||
43 | } | ||||
44 | return -1; | ||||
45 | } | ||||
46 | | ||||
47 | QString KateMacroExpander::expandMacro(const QString& input, KTextEditor::View* view) | ||||
48 | { | ||||
49 | QString output = input; | ||||
50 | QString oldStr; | ||||
51 | do { | ||||
52 | oldStr = output; | ||||
53 | const int startIndex = output.indexOf(QStringLiteral("%{")); | ||||
54 | if (startIndex < 0) { | ||||
55 | break; | ||||
56 | } | ||||
57 | | ||||
58 | const int endIndex = findClosing(output, startIndex + 2); | ||||
59 | if (endIndex <= startIndex) { | ||||
60 | break; | ||||
61 | } | ||||
62 | | ||||
63 | const int varLen = endIndex - (startIndex + 2); | ||||
64 | QString variable = output.mid(startIndex + 2, varLen); | ||||
65 | variable = expandMacro(variable, view); | ||||
66 | if (KTextEditor::Editor::instance()->expandVariable(variable, view, variable)) { | ||||
67 | output.replace(startIndex, endIndex - startIndex + 1, variable); | ||||
68 | } | ||||
69 | } while (output != oldStr); // str comparison guards against infinite loop | ||||
70 | return output; | ||||
71 | } | ||||
72 | | ||||
73 | // kate: space-indent on; indent-width 4; replace-tabs on; |