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 | | ||||
27 | #include <QDir> | ||||
28 | #include <QFileInfo> | ||||
29 | #include <QDate> | ||||
30 | #include <QTime> | ||||
31 | #include <QUuid> | ||||
32 | | ||||
33 | using StringFunction = QString (*)(KTextEditor::View* view); | ||||
34 | class Variable | ||||
35 | { | ||||
36 | public: | ||||
37 | Variable(const QString& variable, const QString& description, StringFunction func) | ||||
38 | : m_variable(variable) | ||||
39 | , m_description(description) | ||||
40 | , m_function(func) | ||||
41 | {} | ||||
42 | | ||||
43 | QString variable() const | ||||
44 | { | ||||
45 | return m_variable; | ||||
46 | } | ||||
47 | | ||||
48 | QString description() const | ||||
49 | { | ||||
50 | return m_description; | ||||
51 | } | ||||
52 | | ||||
53 | QString value(KTextEditor::View * view) const | ||||
54 | { | ||||
55 | return m_function(view); | ||||
56 | } | ||||
57 | | ||||
58 | private: | ||||
59 | QString m_variable; | ||||
60 | QString m_description; | ||||
61 | StringFunction m_function; | ||||
62 | }; | ||||
63 | | ||||
64 | MacroExpander::MacroExpander(KTextEditor::View* view) | ||||
65 | : KWordMacroExpander() | ||||
66 | , m_view(view) | ||||
67 | , m_url(view ? view->document()->url().toLocalFile() : QString()) | ||||
68 | { | ||||
69 | } | ||||
70 | | ||||
71 | bool MacroExpander::expandMacro(const QString& str, QStringList& ret) | ||||
72 | { | ||||
73 | KTextEditor::View* view = m_view; | ||||
74 | if (!view) | ||||
75 | return false; | ||||
76 | | ||||
77 | KTextEditor::Document* doc = view->document(); | ||||
78 | const QUrl url = doc->url(); | ||||
79 | | ||||
80 | if (str == QStringLiteral("CurrentDocument:FileBaseName")) // Current document: File base name without path and suffix. | ||||
81 | ret += QFileInfo(m_url).baseName(); | ||||
82 | else if (str == QStringLiteral("CurrentDocument:FileExtension")) // Current document: File extension. | ||||
83 | ret += QFileInfo(m_url).completeSuffix(); | ||||
84 | else if (str == QStringLiteral("CurrentDocument:FileName")) // Current document: File name without path. | ||||
85 | ret += QFileInfo(m_url).fileName(); | ||||
86 | else if (str == QStringLiteral("CurrentDocument:FilePath")) // Current document: Full path including file name | ||||
87 | ret += QFileInfo(m_url).absoluteFilePath(); | ||||
88 | else if (str == QStringLiteral("CurrentDocument:Text")) // Current document: Contents of entire file | ||||
89 | ret += doc->text(); | ||||
90 | else if (str == QStringLiteral("CurrentDocument:Path")) // Current document: Full path excluding file name | ||||
91 | ret += QFileInfo(m_url).absolutePath(); | ||||
92 | else if (str == QStringLiteral("CurrentDocument:NativeFilePath")) // Current document: Full path including file name, with native path separator (backslash on Windows). | ||||
93 | ret += m_url.isEmpty() ? QString() : QDir::toNativeSeparators(QFileInfo(m_url).absoluteFilePath()); | ||||
94 | else if (str == QStringLiteral("CurrentDocument:NativePath")) // Current document: Full path excluding file name, with native path separator (backslash on Windows). | ||||
95 | ret += m_url.isEmpty() ? QString() : QDir::toNativeSeparators(QFileInfo(m_url).absolutePath()); | ||||
96 | | ||||
97 | else if (str == QStringLiteral("CurrentDocument:Cursor:Line")) // Line number of the text cursor position in current document (starts with 0). | ||||
98 | ret += QString::number(view->cursorPosition().line()); | ||||
99 | else if (str == QStringLiteral("CurrentDocument:Cursor:Column")) // Column number of the text cursor position in current document (starts with 0). | ||||
100 | ret += QString::number(view->cursorPosition().column()); | ||||
101 | else if (str == QStringLiteral("CurrentDocument:Cursor:XPos")) // X component in global screen coordinates of the cursor position. | ||||
102 | ret += QString::number(view->mapToGlobal(view->cursorPositionCoordinates()).x()); | ||||
103 | else if (str == QStringLiteral("CurrentDocument:Cursor:YPos")) // Y component in global screen coordinates of the cursor position. | ||||
104 | ret += QString::number(view->mapToGlobal(view->cursorPositionCoordinates()).y()); | ||||
105 | else if (str == QStringLiteral("CurrentDocument:Selection:Text")) // Current document: Full path excluding file name. | ||||
106 | ret += view->selectionText(); | ||||
107 | else if (str == QStringLiteral("CurrentDocument:Selection:StartLine")) | ||||
108 | ret += QString::number(view->selectionRange().start().line()); | ||||
109 | else if (str == QStringLiteral("CurrentDocument:Selection:StartColumn")) | ||||
110 | ret += QString::number(view->selectionRange().start().column()); | ||||
111 | else if (str == QStringLiteral("CurrentDocument:Selection:EndLine")) | ||||
112 | ret += QString::number(view->selectionRange().end().line()); | ||||
113 | else if (str == QStringLiteral("CurrentDocument:Selection:EndColumn")) | ||||
114 | ret += QString::number(view->selectionRange().end().column()); | ||||
115 | else if (str == QStringLiteral("CurrentDocument:RowCount")) | ||||
116 | ret += QString::number(view->document()->lines()); | ||||
117 | | ||||
118 | else if (str == QStringLiteral("Date:Locale")) // The current date in current locale format. | ||||
119 | ret += QDate::currentDate().toString(Qt::DefaultLocaleShortDate); | ||||
120 | else if (str == QStringLiteral("Date:ISO")) // The current date (ISO). | ||||
121 | ret += QDate::currentDate().toString(Qt::ISODate); | ||||
122 | else if (str.startsWith(QStringLiteral("Date:"))) // The current date (QDate formatstring). | ||||
123 | ret += QDate::currentDate().toString(QStringView(str.constData() + 5, str.length() - 5)); | ||||
124 | | ||||
125 | else if (str == QStringLiteral("Time:Locale")) // The current time in current locale format. | ||||
126 | ret += QTime::currentTime().toString(Qt::DefaultLocaleShortDate); | ||||
127 | else if (str == QStringLiteral("Time:ISO")) // The current time (ISO). | ||||
128 | ret += QTime::currentTime().toString(Qt::ISODate); | ||||
129 | else if (str.startsWith(QStringLiteral("Time:"))) // The current time (QTime formatstring). | ||||
130 | ret += QTime::currentTime().toString(QStringView(str.constData() + 5, str.length() - 5)); | ||||
131 | | ||||
132 | else if (str.startsWith(QStringLiteral("ENV:"))) // Access environment variables. | ||||
133 | ret += QString::fromLocal8Bit(qgetenv(str.rightRef(str.size() - 4).toLocal8Bit().constData())); | ||||
134 | else if (str.startsWith(QStringLiteral("JS:"))) // Evaluate simple JavaScript statements. The statements may not contain '{' nor '}' characters. | ||||
135 | ; // FIXME | ||||
136 | else if (str == QStringLiteral("UUID")) // Generate a new UUID. | ||||
137 | #if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0) | ||||
138 | ret += QUuid::createUuid().toString(QUuid::WithoutBraces); | ||||
139 | #else | ||||
140 | { // LEGACY | ||||
141 | QString uuid = QUuid::createUuid().toString(); | ||||
142 | if (uuid.startsWith(QLatin1Char('{'))) | ||||
143 | uuid.remove(0, 1); | ||||
144 | if (uuid.endsWith(QLatin1Char('}'))) | ||||
145 | uuid.chop(1); | ||||
146 | ret += uuid; | ||||
147 | } | ||||
148 | #endif | ||||
149 | else | ||||
150 | return false; | ||||
151 | | ||||
152 | return true; | ||||
153 | } | ||||
154 | | ||||
155 | // kate: space-indent on; indent-width 4; replace-tabs on; |