KTextEditor

katetemplatehandler.h
1 /*
2  SPDX-FileCopyrightText: 2004, 2010 Joseph Wenninger <[email protected]>
3  SPDX-FileCopyrightText: 2009 Milian Wolff <[email protected]>
4  SPDX-FileCopyrightText: 2014 Sven Brauch <[email protected]>
5 
6  SPDX-License-Identifier: LGPL-2.0-or-later
7 */
8 
9 #ifndef _KATE_TEMPLATE_HANDLER_H_
10 #define _KATE_TEMPLATE_HANDLER_H_
11 
12 #include <QMap>
13 #include <QObject>
14 #include <QPointer>
15 #include <QString>
16 #include <QVector>
17 
18 #include <katescript.h>
19 
20 class KateUndoManager;
21 
22 namespace KTextEditor
23 {
24 class DocumentPrivate;
25 class ViewPrivate;
26 class MovingCursor;
27 class MovingRange;
28 }
29 
53 {
54  Q_OBJECT
55 
56 public:
63  KateTemplateHandler(KTextEditor::ViewPrivate *view, KTextEditor::Cursor position, const QString &templateString, const QString &script, KateUndoManager *undoManager);
64 
65  ~KateTemplateHandler() override;
66 
67 protected:
83  bool eventFilter(QObject *object, QEvent *event) override;
84 
85 private:
91  void initializeTemplate();
92 
96  void parseFields(const QString &templateText);
97 
102  void setupFieldRanges();
103 
112  void setupDefaultValues();
113 
120  void setupEventHandler(KTextEditor::View *view);
121 
127  void jumpToPreviousRange();
128 
134  void jumpToNextRange();
135 
141  void jump(int by, bool initial = false);
142 
147  void jumpToFinalCursorPosition();
148 
155  void updateRangeBehaviours();
156 
161  void sortFields();
162 
163 private Q_SLOTS:
169  void slotTemplateInserted(KTextEditor::Document *document, const KTextEditor::Range &range);
170 
174  void slotViewCreated(KTextEditor::Document *document, KTextEditor::View *view);
175 
179  void updateDependentFields(KTextEditor::Document *document, const KTextEditor::Range &oldRange);
180 
181 public:
182  KTextEditor::ViewPrivate *view() const;
183  KTextEditor::DocumentPrivate *doc() const;
184 
185 private:
187  KTextEditor::ViewPrivate *m_view;
189  KateUndoManager *const m_undoManager;
190 
191  // Describes a single template field, e.g. ${foo}.
192  struct TemplateField {
193  // up-to-date range for the field
195  // contents of the field, i.e. identifier or function to call
196  QString identifier;
197  // default value, if applicable; else empty
198  QString defaultValue;
199  enum Kind {
200  Invalid, // not an actual field
201  Editable, // normal, user-editable field (green by default) [non-dependent field]
202  Mirror, // field mirroring contents of another field [dependent field]
203  FunctionCall, // field containing the up-to-date result of a function call [dependent field]
204  FinalCursorPosition // field marking the final cursor position
205  };
206  Kind kind = Invalid;
207  // true if this field was edited by the user before
208  bool touched = false;
209  bool operator==(const TemplateField &other)
210  {
211  return range == other.range;
212  }
213  };
214  // List of all template fields in the inserted snippet. @see sortFields()
215  QVector<TemplateField> m_fields;
216 
217  // Get the template field which contains @p range.
218  const TemplateField fieldForRange(const KTextEditor::Range &range) const;
219 
221  KateScript::FieldMap fieldMap() const;
222 
225  QSharedPointer<KTextEditor::MovingRange> m_wholeTemplateRange;
226 
228  bool m_internalEdit;
229 
231  KateScript m_templateScript;
232 };
233 
234 #endif
KateScript objects represent a script that can be executed and inspected.
Definition: katescript.h:106
Inserts a template and offers advanced snippet features, like navigation and mirroring.
KateUndoManager implements a document&#39;s history.
The Cursor represents a position in a Document.
Definition: cursor.h:71
A KParts derived class representing a text document.
Definition: document.h:185
The KTextEditor namespace contains all the public API that is required to use the KTextEditor compone...
An object representing a section of text, from one Cursor to another.
A text widget with KXMLGUIClient that represents a Document.
Definition: view.h:143
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Thu Nov 26 2020 22:56:12 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.