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

KDE's Doxygen guidelines are available online.