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

KTextEditor

  • kde-4.14
  • applications
  • kate
  • ktexteditor
codecompletionmodelcontrollerinterface.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries
2  Copyright (C) 2008 Niko Sams <niko.sams@gmail.com>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 #include "codecompletionmodelcontrollerinterface.h"
21 
22 #include <QtCore/QModelIndex>
23 
24 #include <ktexteditor/view.h>
25 #include <ktexteditor/document.h>
26 
27 namespace KTextEditor {
28 
29 //BEGIN OLD
30 
31 CodeCompletionModelControllerInterface::CodeCompletionModelControllerInterface()
32 {
33 }
34 
35 CodeCompletionModelControllerInterface::~CodeCompletionModelControllerInterface()
36 {
37 }
38 
39 bool CodeCompletionModelControllerInterface::shouldStartCompletion(View* view, const QString &insertedText, bool userInsertion, const Cursor &position)
40 {
41  Q_UNUSED(view);
42  Q_UNUSED(position);
43  if(insertedText.isEmpty())
44  return false;
45 
46  QChar lastChar = insertedText.at(insertedText.count() - 1);
47  if ((userInsertion && (lastChar.isLetter() || lastChar.isNumber() || lastChar == '_')) ||
48  lastChar == '.' || insertedText.endsWith(QLatin1String("->"))) {
49  return true;
50  }
51  return false;
52 }
53 
54 Range CodeCompletionModelControllerInterface::completionRange(View* view, const Cursor &position)
55 {
56  Cursor end = position;
57 
58  QString text = view->document()->line(end.line());
59 
60  static QRegExp findWordStart( "\\b([_\\w]+)$" );
61  static QRegExp findWordEnd( "^([_\\w]*)\\b" );
62 
63  Cursor start = end;
64 
65  if (findWordStart.lastIndexIn(text.left(end.column())) >= 0)
66  start.setColumn(findWordStart.pos(1));
67 
68  if (findWordEnd.indexIn(text.mid(end.column())) >= 0)
69  end.setColumn(end.column() + findWordEnd.cap(1).length());
70 
71  //kDebug()<<"returning:"<<Range(start,end);
72  return Range(start, end);
73 }
74 
75 void CodeCompletionModelControllerInterface::updateCompletionRange(View* view, SmartRange& range)
76 {
77  Q_UNUSED(view);
78  if(!range.text().isEmpty() && range.text().count() == 1 && range.text().first().trimmed().isEmpty())
79  //When inserting a newline behind an empty completion-range,, move the range forward to its end
80  range.start() = range.end();
81 }
82 
83 QString CodeCompletionModelControllerInterface::filterString(View* view, const SmartRange &range, const Cursor &position)
84 {
85  return view->document()->text(KTextEditor::Range(range.start(), position));
86 }
87 
88 bool CodeCompletionModelControllerInterface::shouldAbortCompletion(View* view, const SmartRange &range, const QString &currentCompletion)
89 {
90  //kDebug()<<view->cursorPosition();
91  //kDebug()<<range;
92  if(view->cursorPosition() < range.start() || view->cursorPosition() > range.end())
93  return true; //Always abort when the completion-range has been left
94  //Do not abort completions when the text has been empty already before and a newline has been entered
95 
96  static const QRegExp allowedText("^(\\w*)");
97  return !allowedText.exactMatch(currentCompletion);
98 }
99 
100 void CodeCompletionModelControllerInterface::aborted(KTextEditor::View* view) {
101  Q_UNUSED(view);
102 }
103 
104 bool CodeCompletionModelControllerInterface::shouldExecute(const QModelIndex& index, QChar inserted) {
105  Q_UNUSED(index);
106  Q_UNUSED(inserted);
107  return false;
108 }
109 
110 KTextEditor::CodeCompletionModelControllerInterface2::MatchReaction CodeCompletionModelControllerInterface2::matchingItem(const QModelIndex& selected) {
111  Q_UNUSED(selected)
112  return HideListIfAutomaticInvocation;
113 }
114 
115 //END OLD
116 
117 //BEGIN V3
118 
119 CodeCompletionModelControllerInterface3::CodeCompletionModelControllerInterface3()
120 {
121 }
122 
123 CodeCompletionModelControllerInterface3::~CodeCompletionModelControllerInterface3()
124 {
125 }
126 
127 bool CodeCompletionModelControllerInterface3::shouldStartCompletion(View* view, const QString &insertedText, bool userInsertion, const Cursor &position)
128 {
129  Q_UNUSED(view);
130  Q_UNUSED(position);
131  if(insertedText.isEmpty())
132  return false;
133 
134  QChar lastChar = insertedText.at(insertedText.count() - 1);
135  if ((userInsertion && (lastChar.isLetter() || lastChar.isNumber() || lastChar == '_')) ||
136  lastChar == '.' || insertedText.endsWith(QLatin1String("->"))) {
137  return true;
138  }
139  return false;
140 }
141 
142 Range CodeCompletionModelControllerInterface3::completionRange(View* view, const Cursor &position)
143 {
144  Cursor end = position;
145 
146  QString text = view->document()->line(end.line());
147 
148  static QRegExp findWordStart( "\\b([_\\w]+)$" );
149  static QRegExp findWordEnd( "^([_\\w]*)\\b" );
150 
151  Cursor start = end;
152 
153  if (findWordStart.lastIndexIn(text.left(end.column())) >= 0)
154  start.setColumn(findWordStart.pos(1));
155 
156  if (findWordEnd.indexIn(text.mid(end.column())) >= 0)
157  end.setColumn(end.column() + findWordEnd.cap(1).length());
158 
159  //kDebug()<<"returning:"<<Range(start,end);
160  return Range(start, end);
161 }
162 
163 Range CodeCompletionModelControllerInterface3::updateCompletionRange(View* view, const Range& range)
164 {
165  QStringList text=view->document()->textLines(range,false);
166  if(!text.isEmpty() && text.count() == 1 && text.first().trimmed().isEmpty())
167  //When inserting a newline behind an empty completion-range,, move the range forward to its end
168  return Range(range.end(),range.end());
169 
170  return range;
171 }
172 
173 QString CodeCompletionModelControllerInterface3::filterString(View* view, const Range &range, const Cursor &position)
174 {
175  return view->document()->text(KTextEditor::Range(range.start(), position));
176 }
177 
178 bool CodeCompletionModelControllerInterface3::shouldAbortCompletion(View* view, const Range &range, const QString &currentCompletion)
179 {
180  //kDebug()<<view->cursorPosition();
181  //kDebug()<<range;
182  if(view->cursorPosition() < range.start() || view->cursorPosition() > range.end())
183  return true; //Always abort when the completion-range has been left
184  //Do not abort completions when the text has been empty already before and a newline has been entered
185 
186  static const QRegExp allowedText("^(\\w*)");
187  return !allowedText.exactMatch(currentCompletion);
188 }
189 
190 void CodeCompletionModelControllerInterface3::aborted(KTextEditor::View* view) {
191  Q_UNUSED(view);
192 }
193 
194 bool CodeCompletionModelControllerInterface3::shouldExecute(const QModelIndex& index, QChar inserted) {
195  Q_UNUSED(index);
196  Q_UNUSED(inserted);
197  return false;
198 }
199 
200 KTextEditor::CodeCompletionModelControllerInterface3::MatchReaction CodeCompletionModelControllerInterface3::matchingItem(const QModelIndex& selected) {
201  Q_UNUSED(selected)
202  return HideListIfAutomaticInvocation;
203 }
204 //END V3
205 
206 
207 }
QRegExp::pos
int pos(int nth) const
KTextEditor::Range::start
Cursor & start()
Get the start position of this range.
Definition: range.cpp:296
QModelIndex
QRegExp::cap
QString cap(int nth) const
KTextEditor::CodeCompletionModelControllerInterface2::MatchReaction
MatchReaction
Definition: codecompletionmodelcontrollerinterface.h:172
KTextEditor::CodeCompletionModelControllerInterface3::MatchReaction
MatchReaction
Definition: codecompletionmodelcontrollerinterface.h:326
QChar
KTextEditor::CodeCompletionModelControllerInterface::shouldExecute
virtual bool shouldExecute(const QModelIndex &selected, QChar inserted)
When an item within this model is currently selected in the completion-list, and the user inserted th...
Definition: codecompletionmodelcontrollerinterface.cpp:104
KTextEditor::CodeCompletionModelControllerInterface2::HideListIfAutomaticInvocation
If this is returned, the completion-list is hidden if it was invoked automatically.
Definition: codecompletionmodelcontrollerinterface.h:174
KTextEditor::CodeCompletionModelControllerInterface2::matchingItem
virtual MatchReaction matchingItem(const QModelIndex &matched)
Called whenever an item in the completion-list perfectly matches the current filter text...
Definition: codecompletionmodelcontrollerinterface.cpp:110
KTextEditor::SmartRange
A Range which is bound to a specific Document, and maintains its position.
Definition: smartrange.h:94
KTextEditor::CodeCompletionModelControllerInterface::aborted
virtual void aborted(View *view)
Notification that completion for this model has been aborted.
Definition: codecompletionmodelcontrollerinterface.cpp:100
KTextEditor::Cursor
An object which represents a position in a Document.
Definition: cursor.h:55
KTextEditor::CodeCompletionModelControllerInterface::filterString
virtual QString filterString(View *view, const SmartRange &range, const Cursor &position)
This function returns the filter-text used for the current completion.
Definition: codecompletionmodelcontrollerinterface.cpp:83
KTextEditor::CodeCompletionModelControllerInterface::shouldStartCompletion
virtual bool shouldStartCompletion(View *view, const QString &insertedText, bool userInsertion, const Cursor &position)
This function decides if the automatic completion should be started when the user entered some text...
Definition: codecompletionmodelcontrollerinterface.cpp:39
QChar::isLetter
bool isLetter() const
KTextEditor::CodeCompletionModelControllerInterface3::HideListIfAutomaticInvocation
Definition: codecompletionmodelcontrollerinterface.h:328
QRegExp::indexIn
int indexIn(const QString &str, int offset, CaretMode caretMode) const
QRegExp
QList::count
int count(const T &value) const
KTextEditor::CodeCompletionModelControllerInterface3::aborted
virtual void aborted(View *view)
Notification that completion for this model has been aborted.
Definition: codecompletionmodelcontrollerinterface.cpp:190
view.h
QList::isEmpty
bool isEmpty() const
QString::isEmpty
bool isEmpty() const
KTextEditor::CodeCompletionModelControllerInterface::completionRange
virtual Range completionRange(View *view, const Cursor &position)
This function returns the completion range that will be used for the current completion.
Definition: codecompletionmodelcontrollerinterface.cpp:54
KTextEditor::View::document
virtual Document * document() const =0
Get the view's document, that means the view is a view of the returned document.
KTextEditor::CodeCompletionModelControllerInterface3::shouldExecute
virtual bool shouldExecute(const QModelIndex &selected, QChar inserted)
When an item within this model is currently selected in the completion-list, and the user inserted th...
Definition: codecompletionmodelcontrollerinterface.cpp:194
QString::endsWith
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const
document.h
QList::first
T & first()
QString
KTextEditor::CodeCompletionModelControllerInterface::CodeCompletionModelControllerInterface
CodeCompletionModelControllerInterface()
Definition: codecompletionmodelcontrollerinterface.cpp:31
KTextEditor::SmartRange::text
virtual QStringList text(bool block=false) const
Retrieve the text which is contained within this range.
Definition: smartrange.cpp:641
KTextEditor::View::cursorPosition
virtual Cursor cursorPosition() const =0
Get the view's current cursor position.
QStringList
KTextEditor::CodeCompletionModelControllerInterface3::updateCompletionRange
virtual Range updateCompletionRange(View *view, const Range &range)
This function lets the CompletionModel dynamically modify the range.
Definition: codecompletionmodelcontrollerinterface.cpp:163
KTextEditor::Range
An object representing a section of text, from one Cursor to another.
Definition: range.h:54
KTextEditor::Document::text
virtual QString text() const =0
Get the document content.
KTextEditor::CodeCompletionModelControllerInterface3::matchingItem
virtual MatchReaction matchingItem(const QModelIndex &matched)
Called whenever an item in the completion-list perfectly matches the current filter text...
Definition: codecompletionmodelcontrollerinterface.cpp:200
KTextEditor::Document::line
virtual QString line(int line) const =0
Get a single text line.
codecompletionmodelcontrollerinterface.h
KTextEditor::Cursor::line
virtual int line() const
Retrieve the line on which this cursor is situated.
Definition: cursor.cpp:62
QRegExp::lastIndexIn
int lastIndexIn(const QString &str, int offset, CaretMode caretMode) const
QString::mid
QString mid(int position, int n) const
QLatin1String
KTextEditor::CodeCompletionModelControllerInterface::shouldAbortCompletion
virtual bool shouldAbortCompletion(View *view, const SmartRange &range, const QString &currentCompletion)
This function decides if the completion should be aborted.
Definition: codecompletionmodelcontrollerinterface.cpp:88
QString::count
int count() const
KTextEditor::Range::end
Cursor & end()
Get the end position of this range.
Definition: range.cpp:306
QString::at
const QChar at(int position) const
KTextEditor::CodeCompletionModelControllerInterface3::shouldAbortCompletion
virtual bool shouldAbortCompletion(View *view, const Range &range, const QString &currentCompletion)
This function decides if the completion should be aborted.
Definition: codecompletionmodelcontrollerinterface.cpp:178
QString::length
int length() const
QString::left
QString left(int n) const
KTextEditor::CodeCompletionModelControllerInterface3::filterString
virtual QString filterString(View *view, const Range &range, const Cursor &position)
This function returns the filter-text used for the current completion.
Definition: codecompletionmodelcontrollerinterface.cpp:173
KTextEditor::CodeCompletionModelControllerInterface::updateCompletionRange
virtual void updateCompletionRange(View *view, SmartRange &range)
This function lets the CompletionModel dynamically modify the range.
Definition: codecompletionmodelcontrollerinterface.cpp:75
KTextEditor::View
A text widget with KXMLGUIClient that represents a Document.
Definition: view.h:145
KTextEditor::CodeCompletionModelControllerInterface::~CodeCompletionModelControllerInterface
virtual ~CodeCompletionModelControllerInterface()
Definition: codecompletionmodelcontrollerinterface.cpp:35
KTextEditor::CodeCompletionModelControllerInterface3::shouldStartCompletion
virtual bool shouldStartCompletion(View *view, const QString &insertedText, bool userInsertion, const Cursor &position)
This function decides if the automatic completion should be started when the user entered some text...
Definition: codecompletionmodelcontrollerinterface.cpp:127
QRegExp::exactMatch
bool exactMatch(const QString &str) const
KTextEditor::Document::textLines
virtual QStringList textLines(const Range &range, bool block=false) const =0
Get the document content within the given range.
KTextEditor::Cursor::setColumn
virtual void setColumn(int column)
Set the cursor column to column.
Definition: cursor.cpp:84
KTextEditor::Cursor::column
int column() const
Retrieve the column on which this cursor is situated.
Definition: cursor.cpp:79
QChar::isNumber
bool isNumber() const
KTextEditor::CodeCompletionModelControllerInterface3::CodeCompletionModelControllerInterface3
CodeCompletionModelControllerInterface3()
Definition: codecompletionmodelcontrollerinterface.cpp:119
KTextEditor::CodeCompletionModelControllerInterface3::completionRange
virtual Range completionRange(View *view, const Cursor &position)
This function returns the completion range that will be used for the current completion.
Definition: codecompletionmodelcontrollerinterface.cpp:142
KTextEditor::CodeCompletionModelControllerInterface3::~CodeCompletionModelControllerInterface3
virtual ~CodeCompletionModelControllerInterface3()
Definition: codecompletionmodelcontrollerinterface.cpp:123
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat May 9 2020 03:56:48 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KTextEditor

Skip menu "KTextEditor"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • 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