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 {
107  return d->variableExpansionManager()->removeVariable(variable);
108 }
109 
111 {
112  return d->variableExpansionManager()->removeVariable(variable);
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 {
147  KTextEditor::Document *doc = document();
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  : QWidget(parent)
188  , d(nullptr)
189 {
190 }
191 
192 ConfigPage::~ConfigPage() = default;
193 
195 {
196  return name();
197 }
198 
200 {
201  return QIcon::fromTheme(QStringLiteral("document-properties"));
202 }
203 
204 View::View(ViewPrivate *impl, QWidget *parent)
205  : QWidget(parent)
206  , KXMLGUIClient()
207  , d(impl)
208 {
209 }
210 
211 View::~View() = default;
212 
214  : QObject(parent)
215  , d(nullptr)
216 {
217 }
218 
219 Plugin::~Plugin() = default;
220 
222 {
223  return 0;
224 }
225 
227 {
228  return nullptr;
229 }
230 
231 MarkInterface::MarkInterface() = default;
232 
234 
235 ModificationInterface::ModificationInterface() = default;
236 
238 
239 SessionConfigInterface::SessionConfigInterface() = default;
240 
242 
243 TextHintInterface::TextHintInterface() = default;
244 
245 TextHintInterface::~TextHintInterface() = default;
246 
248 
250 
251 InlineNoteInterface::InlineNoteInterface() = default;
252 
253 InlineNoteInterface::~InlineNoteInterface() = default;
254 
256 
258 
259 KateInlineNoteData::KateInlineNoteData(KTextEditor::InlineNoteProvider *provider,
260  const KTextEditor::View *view,
261  const KTextEditor::Cursor &position,
262  int index,
263  bool underMouse,
264  const QFont &font,
265  int lineHeight)
266  : m_provider(provider)
267  , m_view(view)
268  , m_position(position)
269  , m_index(index)
270  , m_underMouse(underMouse)
271  , m_font(font)
272  , m_lineHeight(lineHeight)
273 {
274 }
275 
277  : d(data)
278 {
279 }
280 
281 qreal InlineNote::width() const
282 {
283  return d.m_provider->inlineNoteSize(*this).width();
284 }
285 
287 {
288  return d.m_underMouse;
289 }
290 
292 {
293  Q_UNUSED(note);
294  Q_UNUSED(buttons);
295  Q_UNUSED(globalPos);
296 }
297 
299 {
300  Q_UNUSED(note);
301  Q_UNUSED(globalPos);
302 }
303 
305 {
306  Q_UNUSED(note);
307 }
308 
310 {
311  Q_UNUSED(note);
312  Q_UNUSED(globalPos);
313 }
314 
316 {
317  return d.m_provider;
318 }
319 
321 {
322  return d.m_view;
323 }
324 
326 {
327  return d.m_font;
328 }
329 
330 int InlineNote::index() const
331 {
332  return d.m_index;
333 }
334 
336 {
337  return d.m_lineHeight;
338 }
339 
341 {
342  return d.m_position;
343 }
344 
345 Command::Command(const QStringList &cmds, QObject *parent)
346  : QObject(parent)
347  , m_cmds(cmds)
348  , d(nullptr)
349 {
350  // register this command
351  static_cast<KTextEditor::EditorPrivate *>(KTextEditor::Editor::instance())->cmdManager()->registerCommand(this);
352 }
353 
355 {
356  // unregister this command, if instance is still there!
358  static_cast<KTextEditor::EditorPrivate *>(KTextEditor::Editor::instance())->cmdManager()->unregisterCommand(this);
359  }
360 }
361 
363 {
364  return false;
365 }
366 
368 {
369  return nullptr;
370 }
371 
373 {
374  return false;
375 }
376 
378 {
379 }
380 
382 {
383  d->setScrollPositionInternal(cursor);
384 }
385 
387 {
388  d->setHorizontalScrollPositionInternal(x);
389 }
390 
392 {
393  return d->maxScrollPositionInternal();
394 }
395 
397 {
398  return d->firstDisplayedLineInternal(lineType);
399 }
400 
402 {
403  return d->lastDisplayedLineInternal(lineType);
404 }
405 
407 {
408  return d->textAreaRectInternal();
409 }
410 
411 StyleOptionAnnotationItem::StyleOptionAnnotationItem()
412  : contentFontMetrics(QFont())
413 {
414 }
415 
416 StyleOptionAnnotationItem::StyleOptionAnnotationItem(const StyleOptionAnnotationItem &other)
417  : QStyleOption(Version, Type)
418  , contentFontMetrics(QFont())
419 {
420  *this = other;
421 }
422 
423 StyleOptionAnnotationItem::StyleOptionAnnotationItem(int version)
424  : QStyleOption(version, Type)
425  , contentFontMetrics(QFont())
426 {
427 }
428 
429 AbstractAnnotationItemDelegate::AbstractAnnotationItemDelegate(QObject *parent)
430  : QObject(parent)
431 {
432 }
433 
434 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:34
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.
Definition: ktexteditor.cpp:99
Editor(EditorPrivate *impl)
Constructor.
Definition: ktexteditor.cpp:73
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:87
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:81
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:267
KateVariableExpansionManager * variableExpansionManager()
Returns the variable expansion manager.
Definition: kateglobal.cpp:413
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:93
KSyntaxHighlighting::Theme theme() const
Get the current active theme of this view.
virtual ~Editor()
Virtual destructor.
virtual ~MarkInterface()
Virtual destructor.
KateDocumentConfig * documentConfig()
fallback document config
Definition: kateglobal.h:249
virtual ~View()
Virtual destructor.
The KTextEditor namespace contains all the public API that is required to use the KTextEditor compone...
Definition: katetextblock.h:20
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:491
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:63
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:349
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 Sep 23 2021 22:58:48 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.