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 "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 
39 using 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 
76 size_t qHash(KTextEditor::Cursor cursor, size_t seed) noexcept
77 {
78  return qHash(qMakePair(cursor.line(), cursor.column()), seed);
79 }
80 
82  : QObject()
83  , d(impl)
84 {
85 }
86 
87 Editor::~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 
101 bool 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 
107 bool 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 {
116 }
117 
119 {
121 }
122 
124 {
125  return d->variableExpansionManager()->expandVariable(variable, view, output);
126 }
127 
128 void Editor::expandText(const QString &text, KTextEditor::View *view, QString &output) const
129 {
130  output = d->variableExpansionManager()->expandText(text, view);
131 }
132 
133 void Editor::addVariableExpansion(const QVector<QWidget *> &widgets, const QStringList &variables) const
134 {
135  d->variableExpansionManager()->showDialog(widgets, variables);
136 }
137 
139 {
140  return d->rendererConfig()->baseFont();
141 }
142 
144 {
145  return KateHlManager::self()->repository().theme(d->rendererConfig()->schema());
146 }
147 
149 {
150  return KateHlManager::self()->repository();
151 }
152 
153 bool View::insertText(const QString &text)
154 {
156  if (!doc) {
157  return false;
158  }
159  return doc->insertText(cursorPosition(), text, blockSelection());
160 }
161 
163 {
164  // is the status bar around?
165  return !!d->statusBar();
166 }
167 
168 void View::setStatusBarEnabled(bool enable)
169 {
170  // no state change, do nothing
171  if (enable == !!d->statusBar()) {
172  return;
173  }
174 
175  // else toggle it
176  d->toggleStatusBar();
177 }
178 
179 bool View::insertTemplate(KTextEditor::Cursor insertPosition, const QString &templateString, const QString &script)
180 {
181  return d->insertTemplateInternal(insertPosition, templateString, script);
182 }
183 
185 {
186  d->setInputMode(inputMode);
187 }
188 
190 {
191  return KateHlManager::self()->repository().theme(d->renderer()->config()->schema());
192 }
193 
195 {
196  d->setCursors(positions);
197 }
198 
200 {
201  return d->cursors();
202 }
203 
205 {
206  d->setSelections(ranges);
207 }
208 
210 {
211  return d->selectionRanges();
212 }
213 
215  : QWidget(parent)
216  , d(nullptr)
217 {
218 }
219 
220 ConfigPage::~ConfigPage() = default;
221 
223 {
224  return name();
225 }
226 
228 {
229  return QIcon::fromTheme(QStringLiteral("document-properties"));
230 }
231 
232 View::View(ViewPrivate *impl, QWidget *parent)
233  : QWidget(parent)
234  , KXMLGUIClient()
235  , d(impl)
236 {
237 }
238 
239 View::~View() = default;
240 
242  : QObject(parent)
243  , d(nullptr)
244 {
245 }
246 
247 Plugin::~Plugin() = default;
248 
250 {
251  return 0;
252 }
253 
255 {
256  return nullptr;
257 }
258 
259 SessionConfigInterface::SessionConfigInterface() = default;
260 
262 
264 
266 
268 
270 
271 KateInlineNoteData::KateInlineNoteData(KTextEditor::InlineNoteProvider *provider,
272  const KTextEditor::View *view,
273  const KTextEditor::Cursor position,
274  int index,
275  bool underMouse,
276  const QFont &font,
277  int lineHeight)
278  : m_provider(provider)
279  , m_view(view)
280  , m_position(position)
281  , m_index(index)
282  , m_underMouse(underMouse)
283  , m_font(font)
284  , m_lineHeight(lineHeight)
285 {
286 }
287 
289  : d(data)
290 {
291 }
292 
293 qreal InlineNote::width() const
294 {
295  return d.m_provider->inlineNoteSize(*this).width();
296 }
297 
299 {
300  return d.m_underMouse;
301 }
302 
304 {
305  Q_UNUSED(note);
306  Q_UNUSED(buttons);
307  Q_UNUSED(globalPos);
308 }
309 
311 {
312  Q_UNUSED(note);
313  Q_UNUSED(globalPos);
314 }
315 
317 {
318  Q_UNUSED(note);
319 }
320 
322 {
323  Q_UNUSED(note);
324  Q_UNUSED(globalPos);
325 }
326 
328 {
329  return d.m_provider;
330 }
331 
333 {
334  return d.m_view;
335 }
336 
338 {
339  return d.m_font;
340 }
341 
342 int InlineNote::index() const
343 {
344  return d.m_index;
345 }
346 
348 {
349  return d.m_lineHeight;
350 }
351 
353 {
354  return d.m_position;
355 }
356 
357 Command::Command(const QStringList &cmds, QObject *parent)
358  : QObject(parent)
359  , m_cmds(cmds)
360  , d(nullptr)
361 {
362  // register this command
363  static_cast<KTextEditor::EditorPrivate *>(KTextEditor::Editor::instance())->cmdManager()->registerCommand(this);
364 }
365 
367 {
368  // unregister this command, if instance is still there!
370  static_cast<KTextEditor::EditorPrivate *>(KTextEditor::Editor::instance())->cmdManager()->unregisterCommand(this);
371  }
372 }
373 
375 {
376  return false;
377 }
378 
380 {
381  return nullptr;
382 }
383 
385 {
386  return false;
387 }
388 
390 {
391 }
392 
394 {
395  d->setScrollPositionInternal(cursor);
396 }
397 
399 {
400  d->setHorizontalScrollPositionInternal(x);
401 }
402 
404 {
405  return d->maxScrollPositionInternal();
406 }
407 
409 {
410  return d->firstDisplayedLineInternal(lineType);
411 }
412 
414 {
415  return d->lastDisplayedLineInternal(lineType);
416 }
417 
419 {
420  return d->textAreaRectInternal();
421 }
422 
423 StyleOptionAnnotationItem::StyleOptionAnnotationItem()
424  : contentFontMetrics(QFont())
425 {
426 }
427 
428 StyleOptionAnnotationItem::StyleOptionAnnotationItem(const StyleOptionAnnotationItem &other)
429  : QStyleOption(Version, Type)
430  , contentFontMetrics(QFont())
431 {
432  *this = other;
433 }
434 
435 StyleOptionAnnotationItem::StyleOptionAnnotationItem(int version)
437  , contentFontMetrics(QFont())
438 {
439 }
440 
441 AbstractAnnotationItemDelegate::AbstractAnnotationItemDelegate(QObject *parent)
442  : QObject(parent)
443 {
444 }
445 
446 AbstractAnnotationItemDelegate::~AbstractAnnotationItemDelegate() = default;
447 
448 AnnotationModel::~AnnotationModel() = 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: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:284
~Plugin() override
Virtual destructor.
KCALENDARCORE_EXPORT QDataStream & operator<<(QDataStream &out, const KCalendarCore::Alarm::Ptr &)
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.
bool insertTemplate(KTextEditor::Cursor insertPosition, const QString &templateString, const QString &script=QString())
Insert a template into the document.
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.
QDebug & nospace()
QString defaultEncoding() const
Get the current default encoding for this Editor part.
Definition: ktexteditor.cpp:95
constexpr int line() const noexcept
Retrieve the line on which this cursor is situated.
Definition: cursor.h:174
QVector< KTextEditor::Range > selectionRanges() const
Get the ranges occupied by the current selections.
QIcon fromTheme(const QString &name)
QDebug & space()
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
~View() override
Virtual destructor.
qreal width() const
Returns the width of this note in pixels.
virtual bool insertText(KTextEditor::Cursor position, const QString &text, bool block=false)=0
Insert text at position.
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.
void setScrollPosition(KTextEditor::Cursor cursor)
Scroll view to cursor.
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:74
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.
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:241
LineType
Possible line types.
Definition: view.h:309
int toInt(bool *ok, int base) const const
QString toString() const
Returns the cursor position as string in the format "(line, column)".
Definition: ktexteditor.cpp:65
KCALENDARCORE_EXPORT uint qHash(const KCalendarCore::Period &key)
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
KDB_EXPORT KDbVersionInfo version()
virtual ~TextHintProvider()
Virtual destructor to allow inheritance.
const KTextEditor::View * view() const
The View this note is shown in.
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
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 Cursor fromString(QStringView str) noexcept
Returns a Cursor created from the string str containing the format "(line, column)".
Definition: ktexteditor.cpp:41
static Editor * instance()
Accessor to get the Editor instance.
Definition: ktexteditor.cpp:89
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:81
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.
constexpr int column() const noexcept
Retrieve the column on which this cursor is situated.
Definition: cursor.h:192
virtual QString fullName() const
Get a readable full name for the config page.
QFont font() const
Get the current global editor font.
TextHintProvider()
Default constructor.
KateDocumentConfig * documentConfig()
fallback document config
Definition: kateglobal.h:250
A KParts derived class representing a text document.
Definition: document.h:284
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sun Jun 4 2023 03:50:33 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.