• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 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
27namespace KTextEditor {
28
29//BEGIN OLD
30
31CodeCompletionModelControllerInterface::CodeCompletionModelControllerInterface()
32{
33}
34
35CodeCompletionModelControllerInterface::~CodeCompletionModelControllerInterface()
36{
37}
38
39bool 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
54Range 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
75void 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
83QString CodeCompletionModelControllerInterface::filterString(View* view, const SmartRange &range, const Cursor &position)
84{
85 return view->document()->text(KTextEditor::Range(range.start(), position));
86}
87
88bool 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
100void CodeCompletionModelControllerInterface::aborted(KTextEditor::View* view) {
101 Q_UNUSED(view);
102}
103
104bool CodeCompletionModelControllerInterface::shouldExecute(const QModelIndex& index, QChar inserted) {
105 Q_UNUSED(index);
106 Q_UNUSED(inserted);
107 return false;
108}
109
110KTextEditor::CodeCompletionModelControllerInterface2::MatchReaction CodeCompletionModelControllerInterface2::matchingItem(const QModelIndex& selected) {
111 Q_UNUSED(selected)
112 return HideListIfAutomaticInvocation;
113}
114
115//END OLD
116
117//BEGIN V3
118
119CodeCompletionModelControllerInterface3::CodeCompletionModelControllerInterface3()
120{
121}
122
123CodeCompletionModelControllerInterface3::~CodeCompletionModelControllerInterface3()
124{
125}
126
127bool 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
142Range 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
163Range 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
173QString CodeCompletionModelControllerInterface3::filterString(View* view, const Range &range, const Cursor &position)
174{
175 return view->document()->text(KTextEditor::Range(range.start(), position));
176}
177
178bool 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
190void CodeCompletionModelControllerInterface3::aborted(KTextEditor::View* view) {
191 Q_UNUSED(view);
192}
193
194bool CodeCompletionModelControllerInterface3::shouldExecute(const QModelIndex& index, QChar inserted) {
195 Q_UNUSED(index);
196 Q_UNUSED(inserted);
197 return false;
198}
199
200KTextEditor::CodeCompletionModelControllerInterface3::MatchReaction CodeCompletionModelControllerInterface3::matchingItem(const QModelIndex& selected) {
201 Q_UNUSED(selected)
202 return HideListIfAutomaticInvocation;
203}
204//END V3
205
206
207}
KTextEditor::CodeCompletionModelControllerInterface2::MatchReaction
MatchReaction
Definition: codecompletionmodelcontrollerinterface.h:172
KTextEditor::CodeCompletionModelControllerInterface2::HideListIfAutomaticInvocation
@ 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::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
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::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::aborted
virtual void aborted(View *view)
Notification that completion for this model has been aborted.
Definition: codecompletionmodelcontrollerinterface.cpp:190
KTextEditor::CodeCompletionModelControllerInterface3::CodeCompletionModelControllerInterface3
CodeCompletionModelControllerInterface3()
Definition: codecompletionmodelcontrollerinterface.cpp:119
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::CodeCompletionModelControllerInterface3::MatchReaction
MatchReaction
Definition: codecompletionmodelcontrollerinterface.h:326
KTextEditor::CodeCompletionModelControllerInterface3::HideListIfAutomaticInvocation
@ HideListIfAutomaticInvocation
Definition: codecompletionmodelcontrollerinterface.h:328
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
KTextEditor::CodeCompletionModelControllerInterface3::~CodeCompletionModelControllerInterface3
virtual ~CodeCompletionModelControllerInterface3()
Definition: codecompletionmodelcontrollerinterface.cpp:123
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::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
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::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::aborted
virtual void aborted(View *view)
Notification that completion for this model has been aborted.
Definition: codecompletionmodelcontrollerinterface.cpp:100
KTextEditor::CodeCompletionModelControllerInterface::updateCompletionRange
virtual void updateCompletionRange(View *view, SmartRange &range)
This function lets the CompletionModel dynamically modify the range.
Definition: codecompletionmodelcontrollerinterface.cpp:75
KTextEditor::CodeCompletionModelControllerInterface::CodeCompletionModelControllerInterface
CodeCompletionModelControllerInterface()
Definition: codecompletionmodelcontrollerinterface.cpp:31
KTextEditor::CodeCompletionModelControllerInterface::~CodeCompletionModelControllerInterface
virtual ~CodeCompletionModelControllerInterface()
Definition: codecompletionmodelcontrollerinterface.cpp:35
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::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
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
KTextEditor::Cursor
An object which represents a position in a Document.
Definition: cursor.h:62
KTextEditor::Cursor::setColumn
virtual void setColumn(int column)
Set the cursor column to column.
Definition: cursor.cpp:84
KTextEditor::Document::line
virtual QString line(int line) const =0
Get a single text line.
KTextEditor::Document::text
virtual QString text() const =0
Get the document content.
KTextEditor::Document::textLines
virtual QStringList textLines(const Range &range, bool block=false) const =0
Get the document content within the given range.
KTextEditor::Range
An object representing a section of text, from one Cursor to another.
Definition: range.h:55
KTextEditor::Range::start
Cursor & start()
Get the start position of this range.
Definition: range.cpp:296
KTextEditor::Range::end
Cursor & end()
Get the end position of this range.
Definition: range.cpp:306
KTextEditor::SmartRange
A Range which is bound to a specific Document, and maintains its position.
Definition: smartrange.h:95
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
A text widget with KXMLGUIClient that represents a Document.
Definition: view.h:146
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::View::cursorPosition
virtual Cursor cursorPosition() const =0
Get the view's current cursor position.
codecompletionmodelcontrollerinterface.h
document.h
end
const KShortcut & end()
KTextEditor
Namespace for the KDE Text Editor Interfaces.
Definition: annotationinterface.h:31
view.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 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.14.38 API Reference

Skip menu "kdelibs-4.14.38 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