KTextEditor

katesedcmd.h
1 /*
2  SPDX-FileCopyrightText: 2003-2005 Anders Lund <[email protected]>
3  SPDX-FileCopyrightText: 2001-2010 Christoph Cullmann <[email protected]>
4  SPDX-FileCopyrightText: 2001 Charles Samuels <[email protected]>
5 
6  SPDX-License-Identifier: LGPL-2.0-or-later
7 */
8 
9 #ifndef KATE_SED_CMD_H
10 #define KATE_SED_CMD_H
11 
12 #include "kateregexpsearch.h"
13 #include <KTextEditor/Command>
14 
15 #include <QSharedPointer>
16 #include <QStringList>
17 
18 namespace KTextEditor
19 {
20 class DocumentPrivate;
21 class ViewPrivate;
22 }
23 
24 /**
25  * The KateCommands namespace collects subclasses of KTextEditor::Command
26  * for specific use in kate.
27  */
28 namespace KateCommands
29 {
30 /**
31  * Support vim/sed style search and replace
32  * @author Charles Samuels <[email protected]>
33  **/
35 {
36  static SedReplace *m_instance;
37 
38 protected:
39  SedReplace()
40  : KTextEditor::Command({QStringLiteral("s"), QStringLiteral("%s"), QStringLiteral("$s")})
41  {
42  }
43 
44 public:
45  ~SedReplace() override
46  {
47  m_instance = nullptr;
48  }
49 
50  /**
51  * Execute command. Valid command strings are:
52  * - s/search/replace/ find @c search, replace it with @c replace
53  * on this line
54  * - \%s/search/replace/ do the same to the whole file
55  * - s/search/replace/i do the search and replace case insensitively
56  * - $s/search/replace/ do the search are replacement to the
57  * selection only
58  *
59  * @note $s/// is currently unsupported
60  * @param view view to use for execution
61  * @param cmd cmd string
62  * @param errorMsg error to return if no success
63  * @return success
64  */
65  bool exec(class KTextEditor::View *view, const QString &cmd, QString &errorMsg, const KTextEditor::Range &r) override;
66 
67  bool supportsRange(const QString &) override
68  {
69  return true;
70  }
71 
72  /** This command does not have help. @see KTextEditor::Command::help */
73  bool help(class KTextEditor::View *, const QString &, QString &) override
74  {
75  return false;
76  }
77 
78  static SedReplace *self()
79  {
80  if (m_instance == nullptr) {
81  m_instance = new SedReplace();
82  }
83  return m_instance;
84  }
85 
86  /**
87  * Parses @c sedReplaceString to see if it is a valid sed replace expression (e.g. "s/find/replace/gi").
88  * If it is, returns true and sets @c delimiter to the delimiter used in the expression;
89  * @c destFindBeginPos and @c destFindEndPos to the positions in * @c sedReplaceString of the
90  * begin and end of the "find" term; and @c destReplaceBeginPos and @c destReplaceEndPos to the begin
91  * and end positions of the "replace" term.
92  */
93  static bool
94  parse(const QString &sedReplaceString, QString &destDelim, int &destFindBeginPos, int &destFindEndPos, int &destReplaceBeginPos, int &destReplaceEndPos);
95 
96  class InteractiveSedReplacer
97  {
98  public:
99  InteractiveSedReplacer(KTextEditor::DocumentPrivate *doc,
100  const QString &findPattern,
101  const QString &replacePattern,
102  bool caseSensitive,
103  bool onlyOnePerLine,
104  int startLine,
105  int endLine);
106  /**
107  * Will return invalid Range if there are no further matches.
108  */
109  KTextEditor::Range currentMatch();
110  void skipCurrentMatch();
111  void replaceCurrentMatch();
112  void replaceAllRemaining();
113  QString currentMatchReplacementConfirmationMessage();
114  QString finalStatusReportMessage();
115 
116  private:
117  const QString m_findPattern;
118  const QString m_replacePattern;
119  bool m_onlyOnePerLine;
120  int m_endLine;
121  KTextEditor::DocumentPrivate *m_doc;
122  KateRegExpSearch m_regExpSearch;
123  Qt::CaseSensitivity m_caseSensitive;
124 
125  int m_numReplacementsDone;
126  int m_numLinesTouched;
127  int m_lastChangedLineNum;
128 
129  KTextEditor::Cursor m_currentSearchPos;
130  const QVector<KTextEditor::Range> fullCurrentMatch();
131  QString replacementTextForCurrentMatch();
132  };
133 
134 protected:
135  virtual bool interactiveSedReplace(KTextEditor::ViewPrivate *kateView, QSharedPointer<InteractiveSedReplacer> interactiveSedReplace);
136 };
137 
138 } // namespace KateCommands
139 #endif
bool help(class KTextEditor::View *, const QString &, QString &) override
This command does not have help.
Definition: katesedcmd.h:73
Support vim/sed style search and replace.
Definition: katesedcmd.h:34
Object to help to search for regexp.
The Cursor represents a position in a Document.
Definition: cursor.h:71
bool supportsRange(const QString &) override
Find out if a given command can act on a range.
Definition: katesedcmd.h:67
CaseSensitivity
The KTextEditor namespace contains all the public API that is required to use the KTextEditor compone...
Definition: katetextblock.h:20
An object representing a section of text, from one Cursor to another.
An Editor command line command.
The KateCommands namespace collects subclasses of KTextEditor::Command for specific use in kate...
A text widget with KXMLGUIClient that represents a Document.
Definition: view.h:146
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Fri Jul 30 2021 22:58:08 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.