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 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
45 Cursor Cursor::fromString(const QStringRef &str) Q_DECL_NOEXCEPT
46 {
47  return fromString(QStringView(str));
48 }
49 #endif
50 
52 {
53  // parse format "(line, column)"
54  const int startIndex = str.indexOf(QLatin1Char('('));
55  const int endIndex = str.indexOf(QLatin1Char(')'));
56  const int commaIndex = str.indexOf(QLatin1Char(','));
57 
58  if (startIndex < 0 || endIndex < 0 || commaIndex < 0 || commaIndex < startIndex || endIndex < commaIndex || endIndex < startIndex) {
59  return invalid();
60  }
61 
62  bool ok1 = false;
63  bool ok2 = false;
64 
65  const int line = str.mid(startIndex + 1, commaIndex - startIndex - 1).toString().toInt(&ok1); // FIXME KF6, Qt 5.15.2 and higher
66  const int column = str.mid(commaIndex + 1, endIndex - commaIndex - 1).toString().toInt(&ok2); // FIXME KF6, Qt 5.15.2 and higher
67 
68  if (!ok1 || !ok2) {
69  return invalid();
70  }
71 
72  return {line, column};
73 }
74 
76  : QObject()
77  , d(impl)
78 {
79 }
80 
81 Editor::~Editor() = default;
82 
84 {
85  // Just use internal KTextEditor::EditorPrivate::self()
87 }
88 
90 {
91  // return default encoding in global config object
92  return d->documentConfig()->encoding();
93 }
94 
95 bool Editor::registerVariableMatch(const QString &name, const QString &description, ExpandFunction expansionFunc)
96 {
97  const auto var = Variable(name, description, expansionFunc, false);
98  return d->variableExpansionManager()->addVariable(var);
99 }
100 
101 bool Editor::registerVariablePrefix(const QString &prefix, const QString &description, ExpandFunction expansionFunc)
102 {
103  const auto var = Variable(prefix, description, expansionFunc, true);
104  return d->variableExpansionManager()->addVariable(var);
105 }
106 
108 {
110 }
111 
113 {
115 }
116 
118 {
119  return d->variableExpansionManager()->expandVariable(variable, view, output);
120 }
121 
122 void Editor::expandText(const QString &text, KTextEditor::View *view, QString &output) const
123 {
124  output = d->variableExpansionManager()->expandText(text, view);
125 }
126 
127 void Editor::addVariableExpansion(const QVector<QWidget *> &widgets, const QStringList &variables) const
128 {
129  d->variableExpansionManager()->showDialog(widgets, variables);
130 }
131 
133 {
134  return d->rendererConfig()->baseFont();
135 }
136 
138 {
139  return KateHlManager::self()->repository().theme(d->rendererConfig()->schema());
140 }
141 
143 {
144  return KateHlManager::self()->repository();
145 }
146 
147 bool View::insertText(const QString &text)
148 {
150  if (!doc) {
151  return false;
152  }
153  return doc->insertText(cursorPosition(), text, blockSelection());
154 }
155 
157 {
158  // is the status bar around?
159  return !!d->statusBar();
160 }
161 
162 void View::setStatusBarEnabled(bool enable)
163 {
164  // no state change, do nothing
165  if (enable == !!d->statusBar()) {
166  return;
167  }
168 
169  // else toggle it
170  d->toggleStatusBar();
171 }
172 
173 bool View::insertTemplate(const KTextEditor::Cursor &insertPosition, const QString &templateString, const QString &script)
174 {
175  return d->insertTemplateInternal(insertPosition, templateString, script);
176 }
177 
179 {
180  d->setInputMode(inputMode);
181 }
182 
184 {
185  return KateHlManager::self()->repository().theme(d->renderer()->config()->schema());
186 }
187 
189 {
190  d->setCursors(positions);
191 }
192 
194 {
195  return d->cursors();
196 }
197 
199 {
200  d->setSelections(ranges);
201 }
202 
204 {
205  return d->selectionRanges();
206 }
207 
209  : QWidget(parent)
210  , d(nullptr)
211 {
212 }
213 
214 ConfigPage::~ConfigPage() = default;
215 
217 {
218  return name();
219 }
220 
222 {
223  return QIcon::fromTheme(QStringLiteral("document-properties"));
224 }
225 
226 View::View(ViewPrivate *impl, QWidget *parent)
227  : QWidget(parent)
228  , KXMLGUIClient()
229  , d(impl)
230 {
231 }
232 
233 View::~View() = default;
234 
236  : QObject(parent)
237  , d(nullptr)
238 {
239 }
240 
241 Plugin::~Plugin() = default;
242 
244 {
245  return 0;
246 }
247 
249 {
250  return nullptr;
251 }
252 
253 MarkInterface::MarkInterface() = default;
254 
256 
257 ModificationInterface::ModificationInterface() = default;
258 
260 
261 SessionConfigInterface::SessionConfigInterface() = default;
262 
264 
265 TextHintInterface::TextHintInterface() = default;
266 
267 TextHintInterface::~TextHintInterface() = default;
268 
270 
272 
273 InlineNoteInterface::InlineNoteInterface() = default;
274 
275 InlineNoteInterface::~InlineNoteInterface() = default;
276 
278 
280 
281 KateInlineNoteData::KateInlineNoteData(KTextEditor::InlineNoteProvider *provider,
282  const KTextEditor::View *view,
283  const KTextEditor::Cursor position,
284  int index,
285  bool underMouse,
286  const QFont &font,
287  int lineHeight)
288  : m_provider(provider)
289  , m_view(view)
290  , m_position(position)
291  , m_index(index)
292  , m_underMouse(underMouse)
293  , m_font(font)
294  , m_lineHeight(lineHeight)
295 {
296 }
297 
299  : d(data)
300 {
301 }
302 
303 qreal InlineNote::width() const
304 {
305  return d.m_provider->inlineNoteSize(*this).width();
306 }
307 
309 {
310  return d.m_underMouse;
311 }
312 
314 {
315  Q_UNUSED(note);
316  Q_UNUSED(buttons);
317  Q_UNUSED(globalPos);
318 }
319 
321 {
322  Q_UNUSED(note);
323  Q_UNUSED(globalPos);
324 }
325 
327 {
328  Q_UNUSED(note);
329 }
330 
332 {
333  Q_UNUSED(note);
334  Q_UNUSED(globalPos);
335 }
336 
338 {
339  return d.m_provider;
340 }
341 
343 {
344  return d.m_view;
345 }
346 
348 {
349  return d.m_font;
350 }
351 
352 int InlineNote::index() const
353 {
354  return d.m_index;
355 }
356 
358 {
359  return d.m_lineHeight;
360 }
361 
363 {
364  return d.m_position;
365 }
366 
367 Command::Command(const QStringList &cmds, QObject *parent)
368  : QObject(parent)
369  , m_cmds(cmds)
370  , d(nullptr)
371 {
372  // register this command
373  static_cast<KTextEditor::EditorPrivate *>(KTextEditor::Editor::instance())->cmdManager()->registerCommand(this);
374 }
375 
377 {
378  // unregister this command, if instance is still there!
380  static_cast<KTextEditor::EditorPrivate *>(KTextEditor::Editor::instance())->cmdManager()->unregisterCommand(this);
381  }
382 }
383 
385 {
386  return false;
387 }
388 
390 {
391  return nullptr;
392 }
393 
395 {
396  return false;
397 }
398 
400 {
401 }
402 
404 {
405  d->setScrollPositionInternal(cursor);
406 }
407 
409 {
410  d->setHorizontalScrollPositionInternal(x);
411 }
412 
414 {
415  return d->maxScrollPositionInternal();
416 }
417 
419 {
420  return d->firstDisplayedLineInternal(lineType);
421 }
422 
424 {
425  return d->lastDisplayedLineInternal(lineType);
426 }
427 
429 {
430  return d->textAreaRectInternal();
431 }
432 
433 StyleOptionAnnotationItem::StyleOptionAnnotationItem()
434  : contentFontMetrics(QFont())
435 {
436 }
437 
438 StyleOptionAnnotationItem::StyleOptionAnnotationItem(const StyleOptionAnnotationItem &other)
439  : QStyleOption(Version, Type)
440  , contentFontMetrics(QFont())
441 {
442  *this = other;
443 }
444 
445 StyleOptionAnnotationItem::StyleOptionAnnotationItem(int version)
447  , contentFontMetrics(QFont())
448 {
449 }
450 
451 AbstractAnnotationItemDelegate::AbstractAnnotationItemDelegate(QObject *parent)
452  : QObject(parent)
453 {
454 }
455 
456 AbstractAnnotationItemDelegate::~AbstractAnnotationItemDelegate() = default;
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:267
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.
static Cursor fromString(QStringView str) Q_DECL_NOEXCEPT
Returns a Cursor created from the string str containing the format "(line, column)".
Definition: ktexteditor.cpp:51
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:89
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:95
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.
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:83
~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:75
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.
KateDocumentConfig * documentConfig()
fallback document config
Definition: kateglobal.h:249
A KParts derived class representing a text document.
Definition: document.h:185
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Wed Jun 29 2022 03:54:12 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.