Changeset View
Changeset View
Standalone View
Standalone View
src/utils/kateglobal.cpp
Show First 20 Lines • Show All 50 Lines • ▼ Show 20 Line(s) | |||||
51 | #include <KAboutData> | 51 | #include <KAboutData> | ||
52 | #include <KPageDialog> | 52 | #include <KPageDialog> | ||
53 | #include <KPageWidgetModel> | 53 | #include <KPageWidgetModel> | ||
54 | #include <KConfigGroup> | 54 | #include <KConfigGroup> | ||
55 | 55 | | |||
56 | #include <QBoxLayout> | 56 | #include <QBoxLayout> | ||
57 | #include <QApplication> | 57 | #include <QApplication> | ||
58 | #include <QClipboard> | 58 | #include <QClipboard> | ||
59 | #include <QDir> | ||||
60 | #include <QFileInfo> | ||||
61 | #include <QJSEngine> | ||||
59 | #include <QPushButton> | 62 | #include <QPushButton> | ||
60 | #include <QStringListModel> | 63 | #include <QStringListModel> | ||
64 | #include <QUuid> | ||||
61 | 65 | | |||
62 | #if LIBGIT2_FOUND | 66 | #if LIBGIT2_FOUND | ||
63 | #include <git2.h> | 67 | #include <git2.h> | ||
64 | #endif | 68 | #endif | ||
65 | 69 | | |||
70 | namespace { | ||||
71 | | ||||
72 | void registerVariables(KTextEditor::Editor * editor) | ||||
73 | { | ||||
74 | editor->registerVariableMatch(QStringLiteral("CurrentDocument:FileBaseName"), i18n("Current document: File base name without path and suffix."), [](const QStringView&, KTextEditor::View* view) { | ||||
75 | const auto url = view ? view->document()->url().toLocalFile() : QString(); | ||||
76 | return QFileInfo(url).baseName(); | ||||
77 | }); | ||||
78 | editor->registerVariableMatch(QStringLiteral("CurrentDocument:FileExtension"), i18n("Current document: File extension."), [](const QStringView&, KTextEditor::View* view) { | ||||
79 | const auto url = view ? view->document()->url().toLocalFile() : QString(); | ||||
80 | return QFileInfo(url).completeSuffix(); | ||||
81 | }); | ||||
82 | editor->registerVariableMatch(QStringLiteral("CurrentDocument:FileName"), i18n("Current document: File name without path."), [](const QStringView&, KTextEditor::View* view) { | ||||
83 | const auto url = view ? view->document()->url().toLocalFile() : QString(); | ||||
84 | return QFileInfo(url).fileName(); | ||||
85 | }); | ||||
86 | editor->registerVariableMatch(QStringLiteral("CurrentDocument:FilePath"), i18n("Current document: Full path including file name."), [](const QStringView&, KTextEditor::View* view) { | ||||
87 | const auto url = view ? view->document()->url().toLocalFile() : QString(); | ||||
88 | return QFileInfo(url).absoluteFilePath(); | ||||
89 | }); | ||||
90 | editor->registerVariableMatch(QStringLiteral("CurrentDocument:Text"), i18n("Current document: Contents of entire file."), [](const QStringView&, KTextEditor::View* view) { | ||||
91 | return view ? view->document()->text() : QString(); | ||||
92 | }); | ||||
93 | editor->registerVariableMatch(QStringLiteral("CurrentDocument:Path"), i18n("Current document: Full path excluding file name."), [](const QStringView&, KTextEditor::View* view) { | ||||
94 | const auto url = view ? view->document()->url().toLocalFile() : QString(); | ||||
95 | return QFileInfo(url).absolutePath(); | ||||
96 | }); | ||||
97 | editor->registerVariableMatch(QStringLiteral("CurrentDocument:NativeFilePath"), i18n("Current document: Full path including file name, with native path separator (backslash on Windows)."), [](const QStringView&, KTextEditor::View* view) { | ||||
98 | const auto url = view ? view->document()->url().toLocalFile() : QString(); | ||||
99 | return url.isEmpty() ? QString() : QDir::toNativeSeparators(QFileInfo(url).absoluteFilePath()); | ||||
100 | }); | ||||
101 | editor->registerVariableMatch(QStringLiteral("CurrentDocument:NativePath"), i18n("Current document: Full path excluding file name, with native path separator (backslash on Windows)."), [](const QStringView&, KTextEditor::View* view) { | ||||
102 | const auto url = view ? view->document()->url().toLocalFile() : QString(); | ||||
103 | return url.isEmpty() ? QString() : QDir::toNativeSeparators(QFileInfo(url).absolutePath()); | ||||
104 | }); | ||||
105 | editor->registerVariableMatch(QStringLiteral("CurrentDocument:Cursor:Line"), i18n("Line number of the text cursor position in current document (starts with 0)."), [](const QStringView&, KTextEditor::View* view) { | ||||
106 | return view ? QString::number(view->cursorPosition().line()) : QString(); | ||||
107 | }); | ||||
108 | editor->registerVariableMatch(QStringLiteral("CurrentDocument:Cursor:Column"), i18n("Column number of the text cursor position in current document (starts with 0)."), [](const QStringView&, KTextEditor::View* view) { | ||||
109 | return view ? QString::number(view->cursorPosition().column()) : QString(); | ||||
110 | }); | ||||
111 | editor->registerVariableMatch(QStringLiteral("CurrentDocument:Cursor:XPos"), i18n("X component in global screen coordinates of the cursor position."), [](const QStringView&, KTextEditor::View* view) { | ||||
112 | return view ? QString::number(view->mapToGlobal(view->cursorPositionCoordinates()).x()) : QString(); | ||||
113 | }); | ||||
114 | editor->registerVariableMatch(QStringLiteral("CurrentDocument:Cursor:YPos"), i18n("Y component in global screen coordinates of the cursor position."), [](const QStringView&, KTextEditor::View* view) { | ||||
115 | return view ? QString::number(view->mapToGlobal(view->cursorPositionCoordinates()).y()) : QString(); | ||||
116 | }); | ||||
117 | editor->registerVariableMatch(QStringLiteral("CurrentDocument:Selection:Text"), i18n("Selection of current document."), [](const QStringView&, KTextEditor::View* view) { | ||||
118 | return (view && view->selection()) ? view->selectionText() : QString(); | ||||
119 | }); | ||||
120 | editor->registerVariableMatch(QStringLiteral("CurrentDocument:Selection:StartLine"), i18n("Start line of selected text of current document."), [](const QStringView&, KTextEditor::View* view) { | ||||
121 | return (view && view->selection()) ? QString::number(view->selectionRange().start().line()) : QString(); | ||||
122 | }); | ||||
123 | editor->registerVariableMatch(QStringLiteral("CurrentDocument:Selection:StartColumn"), i18n("Start column of selected text of current document."), [](const QStringView&, KTextEditor::View* view) { | ||||
124 | return (view && view->selection()) ? QString::number(view->selectionRange().start().column()) : QString(); | ||||
125 | }); | ||||
126 | editor->registerVariableMatch(QStringLiteral("CurrentDocument:Selection:EndLine"), i18n("End line of selected text of current document."), [](const QStringView&, KTextEditor::View* view) { | ||||
127 | return (view && view->selection()) ? QString::number(view->selectionRange().end().line()) : QString(); | ||||
128 | }); | ||||
129 | editor->registerVariableMatch(QStringLiteral("CurrentDocument:Selection:EndColumn"), i18n("End column of selected text of current document."), [](const QStringView&, KTextEditor::View* view) { | ||||
130 | return (view && view->selection()) ? QString::number(view->selectionRange().end().column()) : QString(); | ||||
131 | }); | ||||
132 | editor->registerVariableMatch(QStringLiteral("CurrentDocument:RowCount"), i18n("Number of rows of current document."), [](const QStringView&, KTextEditor::View* view) { | ||||
133 | return view ? QString::number(view->document()->lines()) : QString(); | ||||
134 | }); | ||||
135 | | ||||
136 | editor->registerVariableMatch(QStringLiteral("Date:Locale"), i18n("The current date in current locale format."), [](const QStringView&, KTextEditor::View*) { | ||||
137 | return QDate::currentDate().toString(Qt::DefaultLocaleShortDate); | ||||
138 | }); | ||||
139 | editor->registerVariableMatch(QStringLiteral("Date:ISO"), i18n("The current date (ISO)."), [](const QStringView&, KTextEditor::View*) { | ||||
140 | return QDate::currentDate().toString(Qt::ISODate); | ||||
141 | }); | ||||
142 | editor->registerVariablePrefix(QStringLiteral("Date:"), i18n("The current date (QDate formatstring)."), [](const QStringView& str, KTextEditor::View*) { | ||||
143 | return QDate::currentDate().toString(str.right(str.length() - 5)); | ||||
144 | }); | ||||
145 | | ||||
146 | editor->registerVariableMatch(QStringLiteral("Time:Locale"), i18n("The current time in current locale format."), [](const QStringView&, KTextEditor::View*) { | ||||
147 | return QTime::currentTime().toString(Qt::DefaultLocaleShortDate); | ||||
148 | }); | ||||
149 | editor->registerVariableMatch(QStringLiteral("Time:ISO"), i18n("The current time (ISO)."), [](const QStringView&, KTextEditor::View*) { | ||||
150 | return QTime::currentTime().toString(Qt::ISODate); | ||||
151 | }); | ||||
152 | editor->registerVariablePrefix(QStringLiteral("Time:"), i18n("The current time (QTime formatstring)."), [](const QStringView& str, KTextEditor::View*) { | ||||
153 | return QTime::currentTime().toString(str.right(str.length() - 5)); | ||||
154 | }); | ||||
155 | | ||||
156 | editor->registerVariablePrefix(QStringLiteral("ENV:"), i18n("Access environment variables."), [](const QStringView& str, KTextEditor::View*) { | ||||
157 | return QString::fromLocal8Bit(qgetenv(str.right(str.size() - 4).toLocal8Bit().constData())); | ||||
158 | }); | ||||
159 | | ||||
160 | editor->registerVariablePrefix(QStringLiteral("JS:"), i18n("Evaluate simple JavaScript statements. The statements may not contain '{' nor '}' characters."), [](const QStringView& str, KTextEditor::View*) { | ||||
161 | QJSEngine jsEngine; | ||||
162 | const QJSValue out = jsEngine.evaluate(str.toString()); | ||||
163 | return out.toString(); | ||||
164 | }); | ||||
165 | | ||||
166 | editor->registerVariableMatch(QStringLiteral("UUID"), i18n("Generate a new UUID."), [](const QStringView&, KTextEditor::View*) { | ||||
167 | #if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0) | ||||
168 | return QUuid::createUuid().toString(QUuid::WithoutBraces); | ||||
169 | #else | ||||
170 | // LEGACY | ||||
171 | QString uuid = QUuid::createUuid().toString(); | ||||
172 | if (uuid.startsWith(QLatin1Char('{'))) | ||||
173 | uuid.remove(0, 1); | ||||
174 | if (uuid.endsWith(QLatin1Char('}'))) | ||||
175 | uuid.chop(1); | ||||
176 | return uuid; | ||||
177 | #endif | ||||
178 | }); | ||||
179 | } | ||||
180 | } | ||||
181 | | ||||
66 | //BEGIN unit test mode | 182 | //BEGIN unit test mode | ||
67 | static bool kateUnitTestMode = false; | 183 | static bool kateUnitTestMode = false; | ||
68 | 184 | | |||
69 | void KTextEditor::EditorPrivate::enableUnitTestMode() | 185 | void KTextEditor::EditorPrivate::enableUnitTestMode() | ||
70 | { | 186 | { | ||
71 | kateUnitTestMode = true; | 187 | kateUnitTestMode = true; | ||
72 | } | 188 | } | ||
73 | 189 | | |||
▲ Show 20 Lines • Show All 147 Lines • ▼ Show 20 Line(s) | 312 | #endif | |||
221 | // global word completion model | 337 | // global word completion model | ||
222 | m_wordCompletionModel = new KateWordCompletionModel(this); | 338 | m_wordCompletionModel = new KateWordCompletionModel(this); | ||
223 | 339 | | |||
224 | // global keyword completion model | 340 | // global keyword completion model | ||
225 | m_keywordCompletionModel = new KateKeywordCompletionModel (this); | 341 | m_keywordCompletionModel = new KateKeywordCompletionModel (this); | ||
226 | 342 | | |||
227 | // tap to QApplication object for color palette changes | 343 | // tap to QApplication object for color palette changes | ||
228 | qApp->installEventFilter(this); | 344 | qApp->installEventFilter(this); | ||
345 | | ||||
346 | // register default variables for expansion | ||||
347 | registerVariables(this); | ||||
229 | } | 348 | } | ||
230 | 349 | | |||
231 | KTextEditor::EditorPrivate::~EditorPrivate() | 350 | KTextEditor::EditorPrivate::~EditorPrivate() | ||
232 | { | 351 | { | ||
233 | delete m_globalConfig; | 352 | delete m_globalConfig; | ||
234 | delete m_documentConfig; | 353 | delete m_documentConfig; | ||
235 | delete m_viewConfig; | 354 | delete m_viewConfig; | ||
236 | delete m_rendererConfig; | 355 | delete m_rendererConfig; | ||
▲ Show 20 Lines • Show All 304 Lines • Show Last 20 Lines |