• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • applications API Reference
  • KDE Home
  • Contact Us
 

Kate

  • kde-4.14
  • applications
  • kate
  • part
  • vimode
katevicommandrangeexpressionparser.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries and the Kate part.
2  *
3  * Copyright (C) 2008 - 2009 Erlend Hamberg <ehamberg@gmail.com>
4  * Copyright (C) 2011 Svyatoslav Kuzmich <svatoslav1@gmail.com>
5  * Copyright (C) 2012 Vegard Øye
6  * Copyright (C) 2013 Simon St James <kdedevel@etotheipiplusone.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public License
19  * along with this library; see the file COPYING.LIB. If not, write to
20  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23 #include "katevicommandrangeexpressionparser.h"
24 
25 #include <kateview.h>
26 
27 #include <QtCore/QStringList>
28 
29 using KTextEditor::Range;
30 using KTextEditor::Cursor;
31 
32 namespace
33 {
34  CommandRangeExpressionParser rangeExpressionParser;
35 }
36 
37 CommandRangeExpressionParser::CommandRangeExpressionParser()
38 {
39  m_line.setPattern("\\d+");
40  m_lastLine.setPattern("\\$");
41  m_thisLine.setPattern("\\.");
42  m_mark.setPattern("\\'[0-9a-z><\\+\\*\\_]");
43  m_forwardSearch.setPattern("/([^/]*)/?");
44  m_forwardSearch2.setPattern("/[^/]*/?"); // no group
45  m_backwardSearch.setPattern("\\?([^?]*)\\??");
46  m_backwardSearch2.setPattern("\\?[^?]*\\??"); // no group
47  m_base.setPattern("(?:" + m_mark.pattern() + ")|(?:" +
48  m_line.pattern() + ")|(?:" +
49  m_thisLine.pattern() + ")|(?:" +
50  m_lastLine.pattern() + ")|(?:" +
51  m_forwardSearch2.pattern() + ")|(?:" +
52  m_backwardSearch2.pattern() + ")");
53  m_offset.setPattern("[+-](?:" + m_base.pattern() + ")?");
54 
55  // The position regexp contains two groups: the base and the offset.
56  // The offset may be empty.
57  m_position.setPattern("(" + m_base.pattern() + ")((?:" + m_offset.pattern() + ")*)");
58 
59  // The range regexp contains seven groups: the first is the start position, the second is
60  // the base of the start position, the third is the offset of the start position, the
61  // fourth is the end position including a leading comma, the fifth is end position
62  // without the comma, the sixth is the base of the end position, and the seventh is the
63  // offset of the end position. The third and fourth groups may be empty, and the
64  // fifth, sixth and seventh groups are contingent on the fourth group.
65  m_cmdRange.setPattern("^(" + m_position.pattern() + ")((?:,(" + m_position.pattern() + "))?)");
66 }
67 
68 Range CommandRangeExpressionParser::parseRangeExpression(const QString& command, KateView* view, QString& destRangeExpression, QString& destTransformedCommand)
69 {
70  return rangeExpressionParser.parseRangeExpression(command, destRangeExpression, destTransformedCommand, view);
71 }
72 
73 Range CommandRangeExpressionParser::parseRangeExpression(const QString& command, QString& destRangeExpression, QString& destTransformedCommand, KateView* view)
74 {
75  Range parsedRange(0, -1, 0, -1);
76  if (command.isEmpty())
77  {
78  return parsedRange;
79  }
80  QString commandTmp = command;
81  bool leadingRangeWasPercent = false;
82  // expand '%' to '1,$' ("all lines") if at the start of the line
83  if ( commandTmp.at( 0 ) == '%' ) {
84  commandTmp.replace( 0, 1, "1,$" );
85  leadingRangeWasPercent = true;
86  }
87  if (m_cmdRange.indexIn(commandTmp) != -1 && m_cmdRange.matchedLength() > 0) {
88  commandTmp.remove(m_cmdRange);
89 
90  QString position_string1 = m_cmdRange.capturedTexts().at(1);
91  QString position_string2 = m_cmdRange.capturedTexts().at(4);
92  int position1 = calculatePosition(position_string1, view);
93 
94  int position2;
95  if (!position_string2.isEmpty()) {
96  // remove the comma
97  position_string2 = m_cmdRange.capturedTexts().at(5);
98  position2 = calculatePosition(position_string2, view);
99  } else {
100  position2 = position1;
101  }
102 
103  // special case: if the command is just a number with an optional +/- prefix, rewrite to "goto"
104  if (commandTmp.isEmpty()) {
105  commandTmp = QString("goto %1").arg(position1);
106  } else {
107  parsedRange.setRange(KTextEditor::Range(position1 - 1, 0, position2 - 1, 0));
108  }
109 
110  destRangeExpression = (leadingRangeWasPercent ? "%" : m_cmdRange.cap(0));
111  destTransformedCommand = commandTmp;
112  }
113 
114  return parsedRange;
115 }
116 
117 int CommandRangeExpressionParser::calculatePosition(const QString& string, KateView* view ) {
118 
119  int pos = 0;
120  QList<bool> operators_list;
121  QStringList split = string.split(QRegExp("[-+](?!([+-]|$))"));
122  QList<int> values;
123 
124  foreach ( QString line, split ) {
125  pos += line.size();
126 
127  if ( pos < string.size() ) {
128  if ( string.at(pos) == '+' ) {
129  operators_list.push_back( true );
130  } else if ( string.at(pos) == '-' ) {
131  operators_list.push_back( false );
132  } else {
133  Q_ASSERT( false );
134  }
135  }
136 
137  ++pos;
138 
139  if ( m_line.exactMatch(line) ) {
140  values.push_back( line.toInt() );
141  } else if ( m_lastLine.exactMatch(line) ) {
142  values.push_back( view->doc()->lines() );
143  } else if ( m_thisLine.exactMatch(line) ) {
144  values.push_back( view->cursorPosition().line() + 1 );
145  } else if ( m_mark.exactMatch(line) ) {
146  values.push_back( view->getViInputModeManager()->getMarkPosition(line.at(1)).line() + 1 );
147  } else if ( m_forwardSearch.exactMatch(line) ) {
148  m_forwardSearch.indexIn(line);
149  QString pattern = m_forwardSearch.capturedTexts().at(1);
150  int match = view->doc()->searchText( Range( view->cursorPosition(), view->doc()->documentEnd() ),
151  pattern, KTextEditor::Search::Regex ).first().start().line();
152  values.push_back( (match < 0) ? -1 : match + 1 );
153  } else if ( m_backwardSearch.exactMatch(line) ) {
154  m_backwardSearch.indexIn(line);
155  QString pattern = m_backwardSearch.capturedTexts().at(1);
156  int match = view->doc()->searchText( Range( Cursor( 0, 0), view->cursorPosition() ),
157  pattern, KTextEditor::Search::Regex ).first().start().line();
158  values.push_back( (match < 0) ? -1 : match + 1 );
159  }
160  }
161 
162  if (values.isEmpty())
163  {
164  return -1;
165  }
166 
167  int result = values.at(0);
168  for (int i = 0; i < operators_list.size(); ++i) {
169  if ( operators_list.at(i) == true ) {
170  result += values.at(i + 1);
171  } else {
172  result -= values.at(i + 1);
173  }
174  }
175 
176  return result;
177 }
178 
QRegExp::cap
QString cap(int nth) const
kateview.h
QList::push_back
void push_back(const T &value)
katevicommandrangeexpressionparser.h
QList::at
const T & at(int i) const
QString::size
int size() const
QVector::first
T & first()
QString::remove
QString & remove(int position, int n)
KateView::getViInputModeManager
KateViInputModeManager * getViInputModeManager()
Definition: kateview.cpp:1587
CommandRangeExpressionParser
Definition: katevicommandrangeexpressionparser.h:32
QList::size
int size() const
CommandRangeExpressionParser::parseRangeExpression
static KTextEditor::Range parseRangeExpression(const QString &command, KateView *view, QString &destRangeExpression, QString &destTransformedCommand)
Attempt to parse any leading range expression (e.g.
Definition: katevicommandrangeexpressionparser.cpp:68
QRegExp::setPattern
void setPattern(const QString &pattern)
QRegExp::matchedLength
int matchedLength() const
QRegExp::indexIn
int indexIn(const QString &str, int offset, CaretMode caretMode) const
QRegExp
QRegExp::capturedTexts
QStringList capturedTexts() const
CommandRangeExpressionParser::CommandRangeExpressionParser
CommandRangeExpressionParser()
Definition: katevicommandrangeexpressionparser.cpp:37
QString::toInt
int toInt(bool *ok, int base) const
QList::isEmpty
bool isEmpty() const
QString::isEmpty
bool isEmpty() const
KateViInputModeManager::getMarkPosition
KTextEditor::Cursor getMarkPosition(const QChar &mark) const
Definition: kateviinputmodemanager.cpp:781
KateDocument::lines
virtual int lines() const
Definition: katedocument.cpp:753
KateDocument::documentEnd
virtual KTextEditor::Cursor documentEnd() const
Definition: katedocument.cpp:4681
QString
QList< bool >
QStringList
KateView
Definition: kateview.h:77
KateDocument::searchText
virtual QVector< KTextEditor::Range > searchText(const KTextEditor::Range &range, const QString &pattern, const KTextEditor::Search::SearchOptions options)
Definition: katedocument.cpp:1387
KateView::cursorPosition
KTextEditor::Cursor cursorPosition() const
Definition: kateview.cpp:2423
QString::replace
QString & replace(int position, int n, QChar after)
QString::at
const QChar at(int position) const
QRegExp::pattern
QString pattern() const
QStringList::split
QStringList split(const QString &sep, const QString &str, bool allowEmptyEntries)
KateView::doc
KateDocument * doc()
accessor to katedocument pointer
Definition: kateview.h:553
QRegExp::exactMatch
bool exactMatch(const QString &str) const
QString::arg
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat May 9 2020 03:56:58 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Kate

Skip menu "Kate"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

applications API Reference

Skip menu "applications API Reference"
  •   kate
  •       kate
  •   KTextEditor
  •   Kate
  • Konsole

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal