KTextEditor

katetemplatehandler.h
1 /* SPDX-License-Identifier: LGPL-2.0-or-later
2 
3  Copyright (C) 2004,2010 Joseph Wenninger <[email protected]>
4  Copyright (C) 2009 Milian Wolff <[email protected]>
5  Copyright (C) 2014 Sven Brauch <[email protected]>
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public License
18  along with this library; see the file COPYING.LIB. If not, write to
19  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  Boston, MA 02110-1301, USA.
21 */
22 
23 #ifndef _KATE_TEMPLATE_HANDLER_H_
24 #define _KATE_TEMPLATE_HANDLER_H_
25 
26 #include <QMap>
27 #include <QObject>
28 #include <QPointer>
29 #include <QString>
30 #include <QVector>
31 
32 #include <katescript.h>
33 
34 class KateUndoManager;
35 
36 namespace KTextEditor
37 {
38 class DocumentPrivate;
39 class ViewPrivate;
40 class MovingCursor;
41 class MovingRange;
42 }
43 
67 {
68  Q_OBJECT
69 
70 public:
77  KateTemplateHandler(KTextEditor::ViewPrivate *view, KTextEditor::Cursor position, const QString &templateString, const QString &script, KateUndoManager *undoManager);
78 
79  ~KateTemplateHandler() override;
80 
81 protected:
97  bool eventFilter(QObject *object, QEvent *event) override;
98 
99 private:
105  void initializeTemplate();
106 
110  void parseFields(const QString &templateText);
111 
116  void setupFieldRanges();
117 
126  void setupDefaultValues();
127 
134  void setupEventHandler(KTextEditor::View *view);
135 
141  void jumpToPreviousRange();
142 
148  void jumpToNextRange();
149 
155  void jump(int by, bool initial = false);
156 
161  void jumpToFinalCursorPosition();
162 
169  void updateRangeBehaviours();
170 
175  void sortFields();
176 
177 private Q_SLOTS:
183  void slotTemplateInserted(KTextEditor::Document *document, const KTextEditor::Range &range);
184 
188  void slotViewCreated(KTextEditor::Document *document, KTextEditor::View *view);
189 
193  void updateDependentFields(KTextEditor::Document *document, const KTextEditor::Range &oldRange);
194 
195 public:
196  KTextEditor::ViewPrivate *view() const;
197  KTextEditor::DocumentPrivate *doc() const;
198 
199 private:
201  KTextEditor::ViewPrivate *m_view;
203  KateUndoManager *const m_undoManager;
204 
205  // Describes a single template field, e.g. ${foo}.
206  struct TemplateField {
207  // up-to-date range for the field
209  // contents of the field, i.e. identifier or function to call
210  QString identifier;
211  // default value, if applicable; else empty
212  QString defaultValue;
213  enum Kind {
214  Invalid, // not an actual field
215  Editable, // normal, user-editable field (green by default) [non-dependent field]
216  Mirror, // field mirroring contents of another field [dependent field]
217  FunctionCall, // field containing the up-to-date result of a function call [dependent field]
218  FinalCursorPosition // field marking the final cursor position
219  };
220  Kind kind = Invalid;
221  // true if this field was edited by the user before
222  bool touched = false;
223  bool operator==(const TemplateField &other)
224  {
225  return range == other.range;
226  }
227  };
228  // List of all template fields in the inserted snippet. @see sortFields()
229  QVector<TemplateField> m_fields;
230 
231  // Get the template field which contains @p range.
232  const TemplateField fieldForRange(const KTextEditor::Range &range) const;
233 
235  KateScript::FieldMap fieldMap() const;
236 
239  QSharedPointer<KTextEditor::MovingRange> m_wholeTemplateRange;
240 
242  bool m_internalEdit;
243 
245  KateScript m_templateScript;
246 };
247 
248 #endif
KateScript objects represent a script that can be executed and inspected.
Definition: katescript.h:120
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:83
A KParts derived class representing a text document.
Definition: document.h:197
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:153
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sun May 24 2020 23:10:49 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.