• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.10.3 API Reference
  • KDE Home
  • Contact Us
 

KTextEditor

  • interfaces
  • 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 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat May 18 2013 11:45:14 by doxygen 1.8.3.1 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

kdelibs-4.10.3 API Reference

Skip menu "kdelibs-4.10.3 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
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