Changeset View
Changeset View
Standalone View
Standalone View
addons/externaltools/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/Application> | ||||
23 | #include <KTextEditor/Document> | ||||
24 | #include <KTextEditor/Editor> | ||||
25 | #include <KTextEditor/View> | ||||
26 | #include <KLocalizedString> | ||||
27 | | ||||
28 | #include <QDir> | ||||
29 | #include <QFileInfo> | ||||
30 | #include <QDate> | ||||
31 | #include <QTime> | ||||
32 | #include <QUuid> | ||||
33 | | ||||
34 | #include <algorithm> | ||||
35 | | ||||
36 | MacroExpander::MacroExpander(KTextEditor::View* view) | ||||
37 | : KWordMacroExpander() | ||||
38 | , m_view(view) | ||||
39 | , m_url(view ? view->document()->url().toLocalFile() : QString()) | ||||
40 | { | ||||
41 | registerExactMatch(Variable(QStringLiteral("CurrentDocument:FileBaseName"), i18n("Current document: File base name without path and suffix."), [](const QStringView&, KTextEditor::View* view) { | ||||
42 | const auto url = view ? view->document()->url().toLocalFile() : QString(); | ||||
43 | return QFileInfo(url).baseName(); | ||||
44 | })); | ||||
45 | registerExactMatch(Variable(QStringLiteral("CurrentDocument:FileExtension"), i18n("Current document: File extension."), [](const QStringView&, KTextEditor::View* view) { | ||||
46 | const auto url = view ? view->document()->url().toLocalFile() : QString(); | ||||
47 | return QFileInfo(url).completeSuffix(); | ||||
48 | })); | ||||
49 | registerExactMatch(Variable(QStringLiteral("CurrentDocument:FileName"), i18n("Current document: File name without path."), [](const QStringView&, KTextEditor::View* view) { | ||||
50 | const auto url = view ? view->document()->url().toLocalFile() : QString(); | ||||
51 | return QFileInfo(url).fileName(); | ||||
52 | })); | ||||
53 | registerExactMatch(Variable(QStringLiteral("CurrentDocument:FilePath"), i18n("Current document: Full path including file name."), [](const QStringView&, KTextEditor::View* view) { | ||||
54 | const auto url = view ? view->document()->url().toLocalFile() : QString(); | ||||
55 | return QFileInfo(url).absoluteFilePath(); | ||||
56 | })); | ||||
57 | registerExactMatch(Variable(QStringLiteral("CurrentDocument:Text"), i18n("Current document: Contents of entire file."), [](const QStringView&, KTextEditor::View* view) { | ||||
58 | return view ? view->document()->text() : QString(); | ||||
59 | })); | ||||
60 | registerExactMatch(Variable(QStringLiteral("CurrentDocument:Path"), i18n("Current document: Full path excluding file name."), [](const QStringView&, KTextEditor::View* view) { | ||||
61 | const auto url = view ? view->document()->url().toLocalFile() : QString(); | ||||
62 | return QFileInfo(url).absolutePath(); | ||||
63 | })); | ||||
64 | registerExactMatch(Variable(QStringLiteral("CurrentDocument:NativeFilePath"), i18n("Current document: Full path including file name, with native path separator (backslash on Windows)."), [](const QStringView&, KTextEditor::View* view) { | ||||
65 | const auto url = view ? view->document()->url().toLocalFile() : QString(); | ||||
66 | return url.isEmpty() ? QString() : QDir::toNativeSeparators(QFileInfo(url).absoluteFilePath()); | ||||
67 | })); | ||||
68 | registerExactMatch(Variable(QStringLiteral("CurrentDocument:NativePath"), i18n("Current document: Full path excluding file name, with native path separator (backslash on Windows)."), [](const QStringView&, KTextEditor::View* view) { | ||||
69 | const auto url = view ? view->document()->url().toLocalFile() : QString(); | ||||
70 | return url.isEmpty() ? QString() : QDir::toNativeSeparators(QFileInfo(url).absolutePath()); | ||||
71 | })); | ||||
72 | registerExactMatch(Variable(QStringLiteral("CurrentDocument:Cursor:Line"), i18n("Line number of the text cursor position in current document (starts with 0)."), [](const QStringView&, KTextEditor::View* view) { | ||||
73 | return view ? QString::number(view->cursorPosition().line()) : QString(); | ||||
74 | })); | ||||
75 | registerExactMatch(Variable(QStringLiteral("CurrentDocument:Cursor:Column"), i18n("Column number of the text cursor position in current document (starts with 0)."), [](const QStringView&, KTextEditor::View* view) { | ||||
76 | return view ? QString::number(view->cursorPosition().column()) : QString(); | ||||
77 | })); | ||||
78 | registerExactMatch(Variable(QStringLiteral("CurrentDocument:Cursor:XPos"), i18n("X component in global screen coordinates of the cursor position."), [](const QStringView&, KTextEditor::View* view) { | ||||
79 | return view ? QString::number(view->mapToGlobal(view->cursorPositionCoordinates()).x()) : QString(); | ||||
80 | })); | ||||
81 | registerExactMatch(Variable(QStringLiteral("CurrentDocument:Cursor:YPos"), i18n("Y component in global screen coordinates of the cursor position."), [](const QStringView&, KTextEditor::View* view) { | ||||
82 | return view ? QString::number(view->mapToGlobal(view->cursorPositionCoordinates()).y()) : QString(); | ||||
83 | })); | ||||
84 | registerExactMatch(Variable(QStringLiteral("CurrentDocument:Selection:Text"), i18n("Selection of current document."), [](const QStringView&, KTextEditor::View* view) { | ||||
85 | return (view && view->selection()) ? view->selectionText() : QString(); | ||||
86 | })); | ||||
87 | registerExactMatch(Variable(QStringLiteral("CurrentDocument:Selection:StartLine"), i18n("Start line of selected text of current document."), [](const QStringView&, KTextEditor::View* view) { | ||||
88 | return (view && view->selection()) ? QString::number(view->selectionRange().start().line()) : QString(); | ||||
89 | })); | ||||
90 | registerExactMatch(Variable(QStringLiteral("CurrentDocument:Selection:StartColumn"), i18n("Start column of selected text of current document."), [](const QStringView&, KTextEditor::View* view) { | ||||
91 | return (view && view->selection()) ? QString::number(view->selectionRange().start().column()) : QString(); | ||||
92 | })); | ||||
93 | registerExactMatch(Variable(QStringLiteral("CurrentDocument:Selection:EndLine"), i18n("End line of selected text of current document."), [](const QStringView&, KTextEditor::View* view) { | ||||
94 | return (view && view->selection()) ? QString::number(view->selectionRange().end().line()) : QString(); | ||||
95 | })); | ||||
96 | registerExactMatch(Variable(QStringLiteral("CurrentDocument:Selection:EndColumn"), i18n("End column of selected text of current document."), [](const QStringView&, KTextEditor::View* view) { | ||||
97 | return (view && view->selection()) ? QString::number(view->selectionRange().end().column()) : QString(); | ||||
98 | })); | ||||
99 | registerExactMatch(Variable(QStringLiteral("CurrentDocument:RowCount"), i18n("Number of rows of current document."), [](const QStringView&, KTextEditor::View* view) { | ||||
100 | return view ? QString::number(view->document()->lines()) : QString(); | ||||
101 | })); | ||||
102 | | ||||
103 | registerExactMatch(Variable(QStringLiteral("Date:Locale"), i18n("The current date in current locale format."), [](const QStringView&, KTextEditor::View*) { | ||||
104 | return QDate::currentDate().toString(Qt::DefaultLocaleShortDate); | ||||
105 | })); | ||||
106 | registerExactMatch(Variable(QStringLiteral("Date:ISO"), i18n("The current date (ISO)."), [](const QStringView&, KTextEditor::View*) { | ||||
107 | return QDate::currentDate().toString(Qt::ISODate); | ||||
108 | })); | ||||
109 | registerPrefix(Variable(QStringLiteral("Date:"), i18n("The current date (QDate formatstring)."), [](const QStringView& str, KTextEditor::View*) { | ||||
110 | return QDate::currentDate().toString(str.right(str.length() - 5)); | ||||
111 | })); | ||||
112 | | ||||
113 | registerExactMatch(Variable(QStringLiteral("Time:Locale"), i18n("The current time in current locale format."), [](const QStringView&, KTextEditor::View*) { | ||||
114 | return QTime::currentTime().toString(Qt::DefaultLocaleShortDate); | ||||
115 | })); | ||||
116 | registerExactMatch(Variable(QStringLiteral("Time:ISO"), i18n("The current time (ISO)."), [](const QStringView&, KTextEditor::View*) { | ||||
117 | return QTime::currentTime().toString(Qt::ISODate); | ||||
118 | })); | ||||
119 | registerPrefix(Variable(QStringLiteral("Time:"), i18n("The current time (QTime formatstring)."), [](const QStringView& str, KTextEditor::View*) { | ||||
120 | return QTime::currentTime().toString(str.right(str.length() - 5)); | ||||
121 | })); | ||||
122 | | ||||
123 | registerPrefix(Variable(QStringLiteral("ENV:"), i18n("Access environment variables."), [](const QStringView& str, KTextEditor::View*) { | ||||
124 | return QString::fromLocal8Bit(qgetenv(str.right(str.size() - 4).toLocal8Bit().constData())); | ||||
125 | })); | ||||
126 | registerPrefix(Variable(QStringLiteral("JS:"), i18n("Evaluate simple JavaScript statements. The statements may not contain '{' nor '}' characters."), [](const QStringView& str, KTextEditor::View*) { | ||||
127 | // FIXME | ||||
128 | Q_UNUSED(str) | ||||
129 | return QString(); | ||||
130 | })); | ||||
131 | | ||||
132 | registerExactMatch(Variable(QStringLiteral("UUID"), i18n("Generate a new UUID."), [](const QStringView&, KTextEditor::View*) { | ||||
133 | #if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0) | ||||
134 | return QUuid::createUuid().toString(QUuid::WithoutBraces); | ||||
135 | #else | ||||
136 | // LEGACY | ||||
137 | QString uuid = QUuid::createUuid().toString(); | ||||
138 | if (uuid.startsWith(QLatin1Char('{'))) | ||||
139 | uuid.remove(0, 1); | ||||
140 | if (uuid.endsWith(QLatin1Char('}'))) | ||||
141 | uuid.chop(1); | ||||
142 | return uuid; | ||||
143 | #endif | ||||
144 | })); | ||||
145 | | ||||
146 | } | ||||
147 | | ||||
148 | void MacroExpander::registerExactMatch(const Variable & variable) | ||||
149 | { | ||||
150 | m_exactMatches.push_back(variable); | ||||
151 | } | ||||
152 | | ||||
153 | void MacroExpander::registerPrefix(const Variable & variable) | ||||
154 | { | ||||
155 | m_prefixMatches.push_back(variable); | ||||
156 | } | ||||
157 | | ||||
158 | bool MacroExpander::expandMacro(const QString& str, QStringList& ret) | ||||
159 | { | ||||
160 | KTextEditor::View* view = m_view; | ||||
161 | if (!view) | ||||
162 | return false; | ||||
163 | | ||||
164 | // first try exact matches | ||||
165 | const auto it = std::find_if(m_exactMatches.cbegin(), m_exactMatches.cend(), [&str](const Variable& var) { | ||||
166 | return str == var.variable(); | ||||
167 | }); | ||||
168 | if (it != m_exactMatches.cend()) { | ||||
169 | ret += it->evaluate(str, view); | ||||
170 | return true; | ||||
171 | } | ||||
172 | | ||||
173 | // try prefix matching | ||||
174 | const int colonIndex = str.indexOf(QLatin1Char(':')); | ||||
175 | if (colonIndex >= 0) { | ||||
176 | const QString prefix = str.left(colonIndex + 1); | ||||
177 | const auto itPrefix = std::find_if(m_prefixMatches.cbegin(), m_prefixMatches.cend(), [&prefix](const Variable& var) { | ||||
178 | return prefix == var.variable(); | ||||
179 | }); | ||||
180 | if (itPrefix != m_prefixMatches.cend()) { | ||||
181 | ret += itPrefix->evaluate(str, view); | ||||
182 | return true; | ||||
183 | } | ||||
184 | } | ||||
185 | return false; | ||||
186 | } | ||||
187 | | ||||
188 | // kate: space-indent on; indent-width 4; replace-tabs on; |