KTextEditor

ktexteditor.cpp
1 /*
2  SPDX-FileCopyrightText: 2001 Christoph Cullmann <[email protected]>
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 "inlinenoteinterface.h"
25 #include "inlinenoteprovider.h"
26 #include "katerenderer.h"
27 #include "katevariableexpansionmanager.h"
28 #include "markinterface.h"
29 #include "modificationinterface.h"
30 #include "sessionconfiginterface.h"
31 #include "texthintinterface.h"
32 #include "variable.h"
33 
34 #include "abstractannotationitemdelegate.h"
35 #include "annotationinterface.h"
36 
37 #include "katecmd.h"
38 #include "kateconfig.h"
39 #include "kateglobal.h"
40 #include "katesyntaxmanager.h"
41 
42 using namespace KTextEditor;
43 
44 Cursor Cursor::fromString(const QStringRef &str) Q_DECL_NOEXCEPT
45 {
46  return fromString(QStringView(str));
47 }
48 
49 Cursor Cursor::fromString(QStringView str) Q_DECL_NOEXCEPT
50 {
51  // parse format "(line, column)"
52  const int startIndex = str.indexOf(QLatin1Char('('));
53  const int endIndex = str.indexOf(QLatin1Char(')'));
54  const int commaIndex = str.indexOf(QLatin1Char(','));
55 
56  if (startIndex < 0 || endIndex < 0 || commaIndex < 0 || commaIndex < startIndex || endIndex < commaIndex || endIndex < startIndex) {
57  return invalid();
58  }
59 
60  bool ok1 = false;
61  bool ok2 = false;
62 
63  const int line = str.mid(startIndex + 1, commaIndex - startIndex - 1).toString().toInt(&ok1); // FIXME KF6, Qt 5.15.2 and higher
64  const int column = str.mid(commaIndex + 1, endIndex - commaIndex - 1).toString().toInt(&ok2); // FIXME KF6, Qt 5.15.2 and higher
65 
66  if (!ok1 || !ok2) {
67  return invalid();
68  }
69 
70  return {line, column};
71 }
72 
74  : QObject()
75  , d(impl)
76 {
77 }
78 
79 Editor::~Editor() = default;
80 
82 {
83  // Just use internal KTextEditor::EditorPrivate::self()
85 }
86 
88 {
89  // return default encoding in global config object
90  return d->documentConfig()->encoding();
91 }
92 
93 bool Editor::registerVariableMatch(const QString &name, const QString &description, ExpandFunction expansionFunc)
94 {
95  const auto var = Variable(name, description, expansionFunc, false);
96  return d->variableExpansionManager()->addVariable(var);
97 }
98 
99 bool Editor::registerVariablePrefix(const QString &prefix, const QString &description, ExpandFunction expansionFunc)
100 {
101  const auto var = Variable(prefix, description, expansionFunc, true);
102  return d->variableExpansionManager()->addVariable(var);
103 }
104 
106 {
108 }
109 
111 {
113 }
114 
116 {
117  return d->variableExpansionManager()->expandVariable(variable, view, output);
118 }
119 
120 void Editor::expandText(const QString &text, KTextEditor::View *view, QString &output) const
121 {
122  output = d->variableExpansionManager()->expandText(text, view);
123 }
124 
125 void Editor::addVariableExpansion(const QVector<QWidget *> &widgets, const QStringList &variables) const
126 {
127  d->variableExpansionManager()->showDialog(widgets, variables);
128 }
129 
131 {
132  return d->rendererConfig()->baseFont();
133 }
134 
136 {
137  return KateHlManager::self()->repository().theme(d->rendererConfig()->schema());
138 }
139 
141 {
142  return KateHlManager::self()->repository();
143 }
144 
145 bool View::insertText(const QString &text)
146 {
148  if (!doc) {
149  return false;
150  }
151  return doc->insertText(cursorPosition(), text, blockSelection());
152 }
153 
155 {
156  // is the status bar around?
157  return !!d->statusBar();
158 }
159 
160 void View::setStatusBarEnabled(bool enable)
161 {
162  // no state change, do nothing
163  if (enable == !!d->statusBar()) {
164  return;
165  }
166 
167  // else toggle it
168  d->toggleStatusBar();
169 }
170 
171 bool View::insertTemplate(const KTextEditor::Cursor &insertPosition, const QString &templateString, const QString &script)
172 {
173  return d->insertTemplateInternal(insertPosition, templateString, script);
174 }
175 
177 {
178  d->setInputMode(inputMode);
179 }
180 
182 {
183  return KateHlManager::self()->repository().theme(d->renderer()->config()->schema());
184 }
185 
187 {
188  d->setCursors(positions);
189 }
190 
192 {
193  return d->cursors();
194 }
195 
197 {
198  d->setSelections(ranges);
199 }
200 
202 {
203  return d->selectionRanges();
204 }
205 
207  : QWidget(parent)
208  , d(nullptr)
209 {
210 }
211 
212 ConfigPage::~ConfigPage() = default;
213 
215 {
216  return name();
217 }
218 
220 {
221  return QIcon::fromTheme(QStringLiteral("document-properties"));
222 }
223 
224 View::View(ViewPrivate *impl, QWidget *parent)
225  : QWidget(parent)
226  , KXMLGUIClient()
227  , d(impl)
228 {
229 }
230 
231 View::~View() = default;
232 
234  : QObject(parent)
235  , d(nullptr)
236 {
237 }
238 
239 Plugin::~Plugin() = default;
240 
242 {
243  return 0;
244 }
245 
247 {
248  return nullptr;
249 }
250 
251 MarkInterface::MarkInterface() = default;
252 
254 
255 ModificationInterface::ModificationInterface() = default;
256 
258 
259 SessionConfigInterface::SessionConfigInterface() = default;
260 
262 
263 TextHintInterface::TextHintInterface() = default;
264 
265 TextHintInterface::~TextHintInterface() = default;
266 
268 
270 
271 InlineNoteInterface::InlineNoteInterface() = default;
272 
273 InlineNoteInterface::~InlineNoteInterface() = default;
274 
276 
278 
279 KateInlineNoteData::KateInlineNoteData(KTextEditor::InlineNoteProvider *provider,
280  const KTextEditor::View *view,
281  const KTextEditor::Cursor position,
282  int index,
283  bool underMouse,
284  const QFont &font,
285  int lineHeight)
286  : m_provider(provider)
287  , m_view(view)
288  , m_position(position)
289  , m_index(index)
290  , m_underMouse(underMouse)
291  , m_font(font)
292  , m_lineHeight(lineHeight)
293 {
294 }
295 
297  : d(data)
298 {
299 }
300 
301 qreal InlineNote::width() const
302 {
303  return d.m_provider->inlineNoteSize(*this).width();
304 }
305 
307 {
308  return d.m_underMouse;
309 }
310 
312 {
313  Q_UNUSED(note);
314  Q_UNUSED(buttons);
315  Q_UNUSED(globalPos);
316 }
317 
319 {
320  Q_UNUSED(note);
321  Q_UNUSED(globalPos);
322 }
323 
325 {
326  Q_UNUSED(note);
327 }
328 
330 {
331  Q_UNUSED(note);
332  Q_UNUSED(globalPos);
333 }
334 
336 {
337  return d.m_provider;
338 }
339 
341 {
342  return d.m_view;
343 }
344 
346 {
347  return d.m_font;
348 }
349 
350 int InlineNote::index() const
351 {
352  return d.m_index;
353 }
354 
356 {
357  return d.m_lineHeight;
358 }
359 
361 {
362  return d.m_position;
363 }
364 
365 Command::Command(const QStringList &cmds, QObject *parent)
366  : QObject(parent)
367  , m_cmds(cmds)
368  , d(nullptr)
369 {
370  // register this command
371  static_cast<KTextEditor::EditorPrivate *>(KTextEditor::Editor::instance())->cmdManager()->registerCommand(this);
372 }
373 
375 {
376  // unregister this command, if instance is still there!
378  static_cast<KTextEditor::EditorPrivate *>(KTextEditor::Editor::instance())->cmdManager()->unregisterCommand(this);
379  }
380 }
381 
383 {
384  return false;
385 }
386 
388 {
389  return nullptr;
390 }
391 
393 {
394  return false;
395 }
396 
398 {
399 }
400 
402 {
403  d->setScrollPositionInternal(cursor);
404 }
405 
407 {
408  d->setHorizontalScrollPositionInternal(x);
409 }
410 
412 {
413  return d->maxScrollPositionInternal();
414 }
415 
417 {
418  return d->firstDisplayedLineInternal(lineType);
419 }
420 
422 {
423  return d->lastDisplayedLineInternal(lineType);
424 }
425 
427 {
428  return d->textAreaRectInternal();
429 }
430 
431 StyleOptionAnnotationItem::StyleOptionAnnotationItem()
432  : contentFontMetrics(QFont())
433 {
434 }
435 
436 StyleOptionAnnotationItem::StyleOptionAnnotationItem(const StyleOptionAnnotationItem &other)
437  : QStyleOption(Version, Type)
438  , contentFontMetrics(QFont())
439 {
440  *this = other;
441 }
442 
443 StyleOptionAnnotationItem::StyleOptionAnnotationItem(int version)
445  , contentFontMetrics(QFont())
446 {
447 }
448 
449 AbstractAnnotationItemDelegate::AbstractAnnotationItemDelegate(QObject *parent)
450  : QObject(parent)
451 {
452 }
453 
454 AbstractAnnotationItemDelegate::~AbstractAnnotationItemDelegate() = default;
455 
456 #include "moc_abstractannotationitemdelegate.cpp"
457 #include "moc_annotationinterface.cpp"
458 #include "moc_application.cpp"
459 #include "moc_codecompletionmodel.cpp"
460 #include "moc_command.cpp"
461 #include "moc_configpage.cpp"
462 #include "moc_document.cpp"
463 #include "moc_editor.cpp"
464 #include "moc_inlinenoteprovider.cpp"
465 #include "moc_mainwindow.cpp"
466 #include "moc_message.cpp"
467 #include "moc_plugin.cpp"
468 #include "moc_view.cpp"
virtual Document * document() const =0
Get the view's document, that means the view is a view of the returned document.
KateRendererConfig * rendererConfig()
fallback renderer config
Definition: kateglobal.h:268
int firstDisplayedLine(LineType lineType=RealLine) const
Get the first displayed line in the view.
virtual QString name() const =0
Get a readable name for the config page.
KTextEditor::Cursor maxScrollPosition() const
Get the cursor corresponding to the maximum position the view can vertically scroll to.
virtual int configPages() const
Get the number of available config pages.
InlineNoteProvider * provider() const
The provider which created this note.
KTextEditor::Cursor position() const
The cursor position of this note.
InputMode
Possible input modes.
Definition: view.h:189
~Plugin() override
Virtual destructor.
int lastDisplayedLine(LineType lineType=RealLine) const
Get the last displayed line in the view.
bool isStatusBarEnabled() const
Is the status bar enabled?
virtual bool supportsRange(const QString &cmd)
Find out if a given command can act on a range.
ConfigPage(QWidget *parent)
Constructor.
QVector< KTextEditor::Cursor > cursorPositions() const
Get the view's current cursor positions.
QFont font() const
The font of the text surrounding this note.
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 ...
const KSyntaxHighlighting::Repository & repository() const
Get read-only access to the syntax highlighting repository the editor uses.
virtual void inlineNoteMouseMoveEvent(const InlineNote &note, const QPoint &globalPos)
Invoked when the mouse cursor moves inside the note.
~Command() override
Virtual destructor.
QRect textAreaRect() const
Get the view's text area rectangle excluding border, scrollbars, etc.
QString defaultEncoding() const
Get the current default encoding for this Editor part.
Definition: ktexteditor.cpp:87
QVector< KTextEditor::Range > selectionRanges() const
Get the ranges occupied by the current selections.
QIcon fromTheme(const QString &name)
virtual bool insertText(const Cursor &position, const QString &text, bool block=false)=0
Insert text at position.
int width() const const
virtual void inlineNoteFocusInEvent(const InlineNote &note, const QPoint &globalPos)
Invoked when the mouse cursor moves into the note when it was outside before.
typedef MouseButtons
qreal width() const
Returns the width of this note in pixels.
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,...
~Editor() override
Virtual destructor.
virtual QIcon icon() const
Get an icon for the config page.
Command(const QStringList &cmds, QObject *parent=nullptr)
Constructor with parent.
KSyntaxHighlighting::Theme theme() const
Get the current active theme of this view.
void addVariableExpansion(const QVector< QWidget * > &widgets, const QStringList &variables=QStringList()) const
Adds a QAction to the widget in widgets that whenever focus is gained.
bool removeVariable(const QString &name)
Removes variable name.
void setHorizontalScrollPosition(int x)
Horizontally scroll view to position.
bool underMouse() const
Returns whether the mouse cursor is currently over this note.
Config page interface for the Editor and Plugins.
Definition: configpage.h:43
bool expandVariable(const QString &variable, KTextEditor::View *view, QString &output) const
Expands a single variable, writing the expanded value to output.
bool addVariable(const KTextEditor::Variable &variable)
Adds variable to the expansion list view.
The Cursor represents a position in a Document.
Definition: cursor.h:71
virtual void inlineNoteFocusOutEvent(const InlineNote &note)
Invoked when the mouse cursor leaves the note.
View(ViewPrivate *impl, QWidget *parent)
Constructor.
virtual bool wantsToProcessText(const QString &cmdname)
Check, whether the command wants to process text interactively for the given command with name cmdnam...
bool registerVariableMatch(const QString &name, const QString &description, ExpandFunction expansionFunc)
Registers a variable called name for exact matches.
Definition: ktexteditor.cpp:93
virtual QString variable(const QString &name) const
Returns the value for the variable name.
~ConfigPage() override
Virtual destructor.
bool unregisterVariableMatch(const QString &variable)
Unregisters a variable that was previously registered with registerVariableMatch().
A text widget with KXMLGUIClient that represents a Document.
Definition: view.h:146
LineType
Possible line types.
Definition: view.h:214
int toInt(bool *ok, int base) const const
Variable for variable expansion.
Definition: variable.h:34
Plugin(QObject *parent)
Constructor.
void setStatusBarEnabled(bool enable)
Show/hide the status bar of the view.
Accessor interface for the KTextEditor framework.
Definition: editor.h:90
bool unregisterVariablePrefix(const QString &variable)
Unregisters a prefix of variable that was previously registered with registerVariableMatch().
KSyntaxHighlighting::Theme theme() const
Get the current global theme.
The KTextEditor namespace contains all the public API that is required to use the KTextEditor compone...
Definition: katetextblock.h:22
int lineHeight() const
The height of the line containing this note.
void setSelections(const QVector< KTextEditor::Range > &ranges)
Set the view's selection to the range selection.
virtual void inlineNoteActivated(const InlineNote &note, Qt::MouseButtons buttons, const QPoint &globalPos)
Invoked when a note is activated by the user.
static KTextEditor::EditorPrivate * self()
Kate Part Internal stuff ;)
Definition: kateglobal.cpp:392
KateVariableExpansionManager * variableExpansionManager()
Returns the variable expansion manager.
Definition: kateglobal.cpp:456
void setViewInputMode(InputMode inputMode)
Set the view's new input mode.
QString(*)(const QStringView &text, KTextEditor::View *view) ExpandFunction
Function that is called to expand a variable in text.
Definition: editor.h:309
virtual ~MarkInterface()
Virtual destructor.
KDB_EXPORT KDbVersionInfo version()
virtual ~TextHintProvider()
Virtual destructor to allow inheritance.
const KTextEditor::View * view() const
The View this note is shown in.
bool insertTemplate(const KTextEditor::Cursor &insertPosition, const QString &templateString, const QString &script=QString())
Insert a template into the document.
bool registerVariablePrefix(const QString &prefix, const QString &description, ExpandFunction expansionFunc)
Registers a variable for arbitrary text that matches the specified prefix.
Definition: ktexteditor.cpp:99
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 bool blockSelection() const =0
Get the status of the selection mode.
KTextEditor::EditorPrivate One instance of this class is hold alive during a kate part session,...
Definition: kateglobal.h:63
int index() const
The index of this note, i.e.
InlineNoteProvider()
Default constructor.
static Editor * instance()
Accessor to get the Editor instance.
Definition: ktexteditor.cpp:81
~View() override
Virtual destructor.
virtual QSize inlineNoteSize(const InlineNote &note) const =0
Width to be reserved for the note in the text.
InlineNote(const KateInlineNoteData &data)
Constructs an inline note.
~InlineNoteProvider() override
Virtual destructor to allow inheritance.
Internal data container for KTextEditor::InlineNote interface.
virtual bool insertText(const QString &text)
This is a convenience function which inserts text at the view's current cursor position.
Editor(EditorPrivate *impl)
Constructor.
Definition: ktexteditor.cpp:73
void setCursorPositions(const QVector< KTextEditor::Cursor > &positions)
Set the view's new cursors to positions.
virtual Cursor cursorPosition() const =0
Get the view's current cursor position.
QString mid(int position, int n) const const
void expandText(const QString &text, KTextEditor::View *view, QString &output) const
Expands arbitrary text that may contain arbitrary many variables.
A source of inline notes for a document.
virtual ~SessionConfigInterface()
Virtual destructor.
void setScrollPosition(KTextEditor::Cursor &cursor)
Scroll view to cursor.
virtual QString fullName() const
Get a readable full name for the config page.
virtual ~ModificationInterface()
Virtual destructor.
QFont font() const
Get the current global editor font.
TextHintProvider()
Default constructor.
static Cursor fromString(const QString &str) Q_DECL_NOEXCEPT
Returns a Cursor created from the string str containing the format "(line, column)".
Definition: cursor.h:138
KateDocumentConfig * documentConfig()
fallback document config
Definition: kateglobal.h:250
A KParts derived class representing a text document.
Definition: document.h:185
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Wed Dec 6 2023 03:52:04 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.