KTextEditor

ktexteditor.cpp
1/*
2 SPDX-FileCopyrightText: 2001 Christoph Cullmann <cullmann@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "kateview.h"
8
9#include "cursor.h"
10
11#include "configpage.h"
12
13#include "editor.h"
14
15#include "document.h"
16
17#include "view.h"
18
19#include "plugin.h"
20
21#include "command.h"
22#include "inlinenote.h"
23#include "inlinenotedata.h"
24#include "inlinenoteprovider.h"
25#include "katerenderer.h"
26#include "katevariableexpansionmanager.h"
27#include "sessionconfiginterface.h"
28#include "texthintinterface.h"
29#include "variable.h"
30
31#include "abstractannotationitemdelegate.h"
32#include "annotationinterface.h"
33
34#include "katecmd.h"
35#include "kateconfig.h"
36#include "kateglobal.h"
37#include "katesyntaxmanager.h"
38
39using namespace KTextEditor;
40
42{
43 // parse format "(line, column)"
44 const int startIndex = str.indexOf(QLatin1Char('('));
45 const int endIndex = str.indexOf(QLatin1Char(')'));
46 const int commaIndex = str.indexOf(QLatin1Char(','));
47
48 if (startIndex < 0 || endIndex < 0 || commaIndex < 0 || commaIndex < startIndex || endIndex < commaIndex || endIndex < startIndex) {
49 return invalid();
50 }
51
52 bool ok1 = false;
53 bool ok2 = false;
54
55 const int line = str.mid(startIndex + 1, commaIndex - startIndex - 1).toInt(&ok1);
56 const int column = str.mid(commaIndex + 1, endIndex - commaIndex - 1).toInt(&ok2);
57
58 if (!ok1 || !ok2) {
59 return invalid();
60 }
61
62 return {line, column};
63}
64
66{
67 return QStringLiteral("(%1, %2)").arg(m_line).arg(m_column);
68}
69
71{
72 s.nospace() << "(" << cursor.line() << ", " << cursor.column() << ")";
73 return s.space();
74}
75
76size_t KTextEditor::qHash(KTextEditor::Cursor cursor, size_t seed) noexcept
77{
78 return qHashMulti(seed, cursor.line(), cursor.column());
79}
80
82 : QObject()
83 , d(impl)
84{
85}
86
87Editor::~Editor() = default;
88
90{
91 // Just use internal KTextEditor::EditorPrivate::self()
93}
94
96{
97 // return default encoding in global config object
98 return d->documentConfig()->encoding();
99}
100
101bool Editor::registerVariableMatch(const QString &name, const QString &description, ExpandFunction expansionFunc)
102{
103 const auto var = Variable(name, description, expansionFunc, false);
104 return d->variableExpansionManager()->addVariable(var);
105}
106
107bool Editor::registerVariablePrefix(const QString &prefix, const QString &description, ExpandFunction expansionFunc)
108{
109 const auto var = Variable(prefix, description, expansionFunc, true);
110 return d->variableExpansionManager()->addVariable(var);
111}
112
114{
115 return d->variableExpansionManager()->removeVariable(variable);
116}
117
118bool Editor::expandVariable(const QString &variable, KTextEditor::View *view, QString &output) const
119{
120 return d->variableExpansionManager()->expandVariable(variable, view, output);
121}
122
124{
125 return d->variableExpansionManager()->expandText(text, view);
126}
127
128void Editor::addVariableExpansion(const QList<QWidget *> &widgets, const QStringList &variables) const
129{
130 d->variableExpansionManager()->showDialog(widgets, variables);
131}
132
134{
135 return d->rendererConfig()->baseFont();
136}
137
139{
140 return KateHlManager::self()->repository().theme(d->rendererConfig()->schema());
141}
142
144{
145 return KateHlManager::self()->repository();
146}
147
148bool View::insertText(const QString &text)
149{
151 if (!doc) {
152 return false;
153 }
154 return doc->insertText(cursorPosition(), text, blockSelection());
155}
156
158{
159 // is the status bar around?
160 return !!d->statusBar();
161}
162
164{
165 // no state change, do nothing
166 if (enable == !!d->statusBar()) {
167 return;
168 }
169
170 // else toggle it
171 d->toggleStatusBar();
172}
173
174bool View::insertTemplate(KTextEditor::Cursor insertPosition, const QString &templateString, const QString &script)
175{
176 return d->insertTemplateInternal(insertPosition, templateString, script);
177}
178
180{
181 return KateHlManager::self()->repository().theme(d->rendererConfig()->schema());
182}
183
185{
186 d->setCursors(positions);
187}
188
190{
191 return d->cursors();
192}
193
195{
196 d->setSelections(ranges);
197}
198
200{
201 return d->selectionRanges();
202}
203
205 : QWidget(parent)
206 , d(nullptr)
207{
208}
209
210ConfigPage::~ConfigPage() = default;
211
213{
214 return name();
215}
216
218{
219 return QIcon::fromTheme(QStringLiteral("document-properties"));
220}
221
222View::View(ViewPrivate *impl, QWidget *parent)
223 : QWidget(parent)
224 , KXMLGUIClient()
225 , d(impl)
226{
227}
228
229View::~View() = default;
230
232 : QObject(parent)
233 , d(nullptr)
234{
235}
236
237Plugin::~Plugin() = default;
238
240{
241 return 0;
242}
243
245{
246 return nullptr;
247}
248
249SessionConfigInterface::SessionConfigInterface() = default;
250
252
254
256
258
260
261KateInlineNoteData::KateInlineNoteData(KTextEditor::InlineNoteProvider *provider,
262 const KTextEditor::View *view,
263 const KTextEditor::Cursor position,
264 int index,
265 bool underMouse,
266 const QFont &font,
267 int lineHeight)
268 : m_provider(provider)
269 , m_view(view)
270 , m_position(position)
271 , m_index(index)
272 , m_underMouse(underMouse)
273 , m_font(font)
274 , m_lineHeight(lineHeight)
275{
276}
277
279 : d(data)
280{
281}
282
283qreal InlineNote::width() const
284{
285 return d.m_provider->inlineNoteSize(*this).width();
286}
287
289{
290 return d.m_underMouse;
291}
292
294{
295 Q_UNUSED(note);
296 Q_UNUSED(buttons);
297 Q_UNUSED(globalPos);
298}
299
301{
302 Q_UNUSED(note);
303 Q_UNUSED(globalPos);
304}
305
310
312{
313 Q_UNUSED(note);
314 Q_UNUSED(globalPos);
315}
316
318{
319 return d.m_provider;
320}
321
323{
324 return d.m_view;
325}
326
328{
329 return d.m_font;
330}
331
333{
334 return d.m_index;
335}
336
338{
339 return d.m_lineHeight;
340}
341
343{
344 return d.m_position;
345}
346
348 : QObject(parent)
349 , m_cmds(cmds)
350 , d(nullptr)
351{
352 // register this command
353 static_cast<KTextEditor::EditorPrivate *>(KTextEditor::Editor::instance())->cmdManager()->registerCommand(this);
354}
355
357{
358 // unregister this command, if instance is still there!
360 static_cast<KTextEditor::EditorPrivate *>(KTextEditor::Editor::instance())->cmdManager()->unregisterCommand(this);
361 }
362}
363
365{
366 return false;
367}
368
370{
371 return nullptr;
372}
373
375{
376 return false;
377}
378
382
384{
385 d->setScrollPositionInternal(cursor);
386}
387
389{
390 d->setHorizontalScrollPositionInternal(x);
391}
392
394{
395 return d->maxScrollPositionInternal();
396}
397
399{
400 return d->firstDisplayedLineInternal(lineType);
401}
402
404{
405 return d->lastDisplayedLineInternal(lineType);
406}
407
409{
410 return d->textAreaRectInternal();
411}
412
413StyleOptionAnnotationItem::StyleOptionAnnotationItem()
414 : contentFontMetrics(QFont())
415{
416}
417
418StyleOptionAnnotationItem::StyleOptionAnnotationItem(const StyleOptionAnnotationItem &other)
419 : QStyleOption(Version, Type)
420 , contentFontMetrics(QFont())
421{
422 *this = other;
423}
424
425StyleOptionAnnotationItem::StyleOptionAnnotationItem(int version)
426 : QStyleOption(version, Type)
427 , contentFontMetrics(QFont())
428{
429}
430
431AbstractAnnotationItemDelegate::AbstractAnnotationItemDelegate(QObject *parent)
432 : QObject(parent)
433{
434}
435
436AbstractAnnotationItemDelegate::~AbstractAnnotationItemDelegate() = default;
437
438AnnotationModel::~AnnotationModel() = default;
439
440#include "moc_abstractannotationitemdelegate.cpp"
441#include "moc_annotationinterface.cpp"
442#include "moc_application.cpp"
443#include "moc_codecompletionmodel.cpp"
444#include "moc_command.cpp"
445#include "moc_configpage.cpp"
446#include "moc_document.cpp"
447#include "moc_editor.cpp"
448#include "moc_inlinenoteprovider.cpp"
449#include "moc_mainwindow.cpp"
450#include "moc_message.cpp"
451#include "moc_plugin.cpp"
452#include "moc_view.cpp"
Q_INVOKABLE KSyntaxHighlighting::Theme theme(const QString &themeName) const
virtual bool supportsRange(const QString &cmd)
Find out if a given command can act on a range.
Command(const QStringList &cmds, QObject *parent=nullptr)
Constructor with parent.
virtual bool wantsToProcessText(const QString &cmdname)
Check, whether the command wants to process text interactively for the given command with name cmdnam...
virtual void processText(KTextEditor::View *view, const QString &text)
This is called by the command line each time the argument text for the command changed,...
virtual KCompletion * completionObject(KTextEditor::View *view, const QString &cmdname)
Return a KCompletion object that will substitute the command line default one while typing the first ...
~Command() override
Virtual destructor.
Config page interface for the Editor and Plugins.
Definition configpage.h:44
virtual QString fullName() const
Get a readable full name for the config page.
virtual QIcon icon() const
Get an icon for the config page.
ConfigPage(QWidget *parent)
Constructor.
virtual QString name() const =0
Get a readable name for the config page.
~ConfigPage() override
Virtual destructor.
The Cursor represents a position in a Document.
Definition cursor.h:75
constexpr int column() const noexcept
Retrieve the column on which this cursor is situated.
Definition cursor.h:192
static Cursor fromString(QStringView str) noexcept
Returns a Cursor created from the string str containing the format "(line, column)".
QString toString() const
Returns the cursor position as string in the format "(line, column)".
constexpr int line() const noexcept
Retrieve the line on which this cursor is situated.
Definition cursor.h:174
A KParts derived class representing a text document.
Definition document.h:284
virtual bool insertText(KTextEditor::Cursor position, const QString &text, bool block=false)=0
Insert text at position.
KTextEditor::EditorPrivate One instance of this class is hold alive during a kate part session,...
Definition kateglobal.h:65
KateCmd * cmdManager()
command manager
Definition kateglobal.h:282
static KTextEditor::EditorPrivate * self()
Kate Part Internal stuff ;)
KateDocumentConfig * documentConfig()
fallback document config
Definition kateglobal.h:238
KateRendererConfig * rendererConfig()
fallback renderer config
Definition kateglobal.h:256
KateVariableExpansionManager * variableExpansionManager()
Returns the variable expansion manager.
Accessor interface for the KTextEditor framework.
Definition editor.h:91
QString(*)(const QStringView &text, KTextEditor::View *view) ExpandFunction
Function that is called to expand a variable in text.
Definition editor.h:309
KSyntaxHighlighting::Theme theme() const
Get the current global theme.
static Editor * instance()
Accessor to get the Editor instance.
bool expandVariable(const QString &variable, KTextEditor::View *view, QString &output) const
Expands a single variable, writing the expanded value to output.
QString expandText(const QString &text, KTextEditor::View *view) const
Expands arbitrary text that may contain arbitrary many variables.
~Editor() override
Virtual destructor.
bool unregisterVariable(const QString &variableName)
Unregisters a variable that was previously registered with registerVariableMatch() or registerVariabl...
const KSyntaxHighlighting::Repository & repository() const
Get read-only access to the syntax highlighting repository the editor uses.
QString defaultEncoding() const
Get the current default encoding for this Editor part.
bool registerVariableMatch(const QString &name, const QString &description, ExpandFunction expansionFunc)
Registers a variable called name for exact matches.
bool registerVariablePrefix(const QString &prefix, const QString &description, ExpandFunction expansionFunc)
Registers a variable for arbitrary text that matches the specified prefix.
QFont font() const
Get the current global editor font.
Editor(EditorPrivate *impl)
Constructor.
void addVariableExpansion(const QList< QWidget * > &widgets, const QStringList &variables=QStringList()) const
Adds a QAction to the widget in widgets that whenever focus is gained.
A source of inline notes for a document.
virtual void inlineNoteMouseMoveEvent(const InlineNote &note, const QPoint &globalPos)
Invoked when the mouse cursor moves inside the note.
virtual void inlineNoteFocusInEvent(const InlineNote &note, const QPoint &globalPos)
Invoked when the mouse cursor moves into the note when it was outside before.
virtual void inlineNoteFocusOutEvent(const InlineNote &note)
Invoked when the mouse cursor leaves the note.
virtual QSize inlineNoteSize(const InlineNote &note) const =0
Width to be reserved for the note in the text.
~InlineNoteProvider() override
Virtual destructor to allow inheritance.
virtual void inlineNoteActivated(const InlineNote &note, Qt::MouseButtons buttons, const QPoint &globalPos)
Invoked when a note is activated by the user.
InlineNoteProvider()
Default constructor.
Describes an inline note.
Definition inlinenote.h:40
qreal width() const
Returns the width of this note in pixels.
InlineNoteProvider * provider() const
The provider which created this note.
KTextEditor::Cursor position() const
The cursor position of this note.
int index() const
The index of this note, i.e.
bool underMouse() const
Returns whether the mouse cursor is currently over this note.
QFont font() const
The font of the text surrounding this note.
int lineHeight() const
The height of the line containing this note.
InlineNote(const KateInlineNoteData &data)
Constructs an inline note.
const KTextEditor::View * view() const
The View this note is shown in.
~Plugin() override
Virtual destructor.
virtual ConfigPage * configPage(int number, QWidget *parent)
Get the config page with the number, config pages from 0 to configPages()-1 are available if configPa...
virtual int configPages() const
Get the number of available config pages.
Plugin(QObject *parent)
Constructor.
virtual ~SessionConfigInterface()
Virtual destructor.
The style option set for an annotation item, as painted by AbstractAnnotationItemDelegate.
TextHintProvider()
Default constructor.
virtual ~TextHintProvider()
Virtual destructor to allow inheritance.
Variable for variable expansion.
Definition variable.h:35
A text widget with KXMLGUIClient that represents a Document.
Definition view.h:244
~View() override
Virtual destructor.
int lastDisplayedLine(LineType lineType=RealLine) const
Get the last displayed line in the view.
int firstDisplayedLine(LineType lineType=RealLine) const
Get the first displayed line in the view.
View(ViewPrivate *impl, QWidget *parent)
Constructor.
virtual Document * document() const =0
Get the view's document, that means the view is a view of the returned document.
virtual bool blockSelection() const =0
Get the status of the selection mode.
KSyntaxHighlighting::Theme theme() const
Get the current active theme of this view.
void setScrollPosition(KTextEditor::Cursor cursor)
Scroll view to cursor.
void setHorizontalScrollPosition(int x)
Horizontally scroll view to position.
void setSelections(const QList< KTextEditor::Range > &ranges)
Set the view's selection to the range selection.
virtual Cursor cursorPosition() const =0
Get the view's current cursor position.
QRect textAreaRect() const
Get the view's text area rectangle excluding border, scrollbars, etc.
virtual bool insertText(const QString &text)
This is a convenience function which inserts text at the view's current cursor position.
LineType
Possible line types.
Definition view.h:311
bool insertTemplate(KTextEditor::Cursor insertPosition, const QString &templateString, const QString &script=QString())
Insert a template into the document.
void setCursorPositions(const QList< KTextEditor::Cursor > &positions)
Set the view's new cursors to positions.
QList< KTextEditor::Range > selectionRanges() const
Get the ranges occupied by the current selections.
QList< KTextEditor::Cursor > cursorPositions() const
Get the view's current cursor positions.
bool isStatusBarEnabled() const
Is the status bar enabled?
KTextEditor::Cursor maxScrollPosition() const
Get the cursor corresponding to the maximum position the view can vertically scroll to.
void setStatusBarEnabled(bool enable)
Show/hide the status bar of the view.
Internal data container for KTextEditor::InlineNote interface.
bool addVariable(const KTextEditor::Variable &variable)
Adds variable to the expansion list view.
bool removeVariable(const QString &name)
Removes variable name.
KCOREADDONS_EXPORT unsigned int version()
The KTextEditor namespace contains all the public API that is required to use the KTextEditor compone...
KTEXTEDITOR_EXPORT size_t qHash(KTextEditor::Cursor cursor, size_t seed=0) noexcept
QHash function for KTextEditor::Cursor.
KTEXTEDITOR_EXPORT QDebug operator<<(QDebug s, const MovingCursor *cursor)
qDebug() stream operator.
QDebug & nospace()
QDebug & space()
QIcon fromTheme(const QString &name)
int width() const const
QString arg(Args &&... args) const const
typedef MouseButtons
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:01:56 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.