KTextEditor
codecompletionmodelcontrollerinterface.cpp
Go to the documentation of this file.
00001 /* This file is part of the KDE libraries 00002 Copyright (C) 2008 Niko Sams <niko.sams@gmail.com> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 Boston, MA 02110-1301, USA. 00018 */ 00019 00020 #include "codecompletionmodelcontrollerinterface.h" 00021 00022 #include <QtCore/QModelIndex> 00023 00024 #include <ktexteditor/view.h> 00025 #include <ktexteditor/document.h> 00026 00027 namespace KTextEditor { 00028 00029 //BEGIN OLD 00030 00031 CodeCompletionModelControllerInterface::CodeCompletionModelControllerInterface() 00032 { 00033 } 00034 00035 CodeCompletionModelControllerInterface::~CodeCompletionModelControllerInterface() 00036 { 00037 } 00038 00039 bool CodeCompletionModelControllerInterface::shouldStartCompletion(View* view, const QString &insertedText, bool userInsertion, const Cursor &position) 00040 { 00041 Q_UNUSED(view); 00042 Q_UNUSED(position); 00043 if(insertedText.isEmpty()) 00044 return false; 00045 00046 QChar lastChar = insertedText.at(insertedText.count() - 1); 00047 if ((userInsertion && (lastChar.isLetter() || lastChar.isNumber() || lastChar == '_')) || 00048 lastChar == '.' || insertedText.endsWith(QLatin1String("->"))) { 00049 return true; 00050 } 00051 return false; 00052 } 00053 00054 Range CodeCompletionModelControllerInterface::completionRange(View* view, const Cursor &position) 00055 { 00056 Cursor end = position; 00057 00058 QString text = view->document()->line(end.line()); 00059 00060 static QRegExp findWordStart( "\\b([_\\w]+)$" ); 00061 static QRegExp findWordEnd( "^([_\\w]*)\\b" ); 00062 00063 Cursor start = end; 00064 00065 if (findWordStart.lastIndexIn(text.left(end.column())) >= 0) 00066 start.setColumn(findWordStart.pos(1)); 00067 00068 if (findWordEnd.indexIn(text.mid(end.column())) >= 0) 00069 end.setColumn(end.column() + findWordEnd.cap(1).length()); 00070 00071 //kDebug()<<"returning:"<<Range(start,end); 00072 return Range(start, end); 00073 } 00074 00075 void CodeCompletionModelControllerInterface::updateCompletionRange(View* view, SmartRange& range) 00076 { 00077 Q_UNUSED(view); 00078 if(!range.text().isEmpty() && range.text().count() == 1 && range.text().first().trimmed().isEmpty()) 00079 //When inserting a newline behind an empty completion-range,, move the range forward to its end 00080 range.start() = range.end(); 00081 } 00082 00083 QString CodeCompletionModelControllerInterface::filterString(View* view, const SmartRange &range, const Cursor &position) 00084 { 00085 return view->document()->text(KTextEditor::Range(range.start(), position)); 00086 } 00087 00088 bool CodeCompletionModelControllerInterface::shouldAbortCompletion(View* view, const SmartRange &range, const QString ¤tCompletion) 00089 { 00090 //kDebug()<<view->cursorPosition(); 00091 //kDebug()<<range; 00092 if(view->cursorPosition() < range.start() || view->cursorPosition() > range.end()) 00093 return true; //Always abort when the completion-range has been left 00094 //Do not abort completions when the text has been empty already before and a newline has been entered 00095 00096 static const QRegExp allowedText("^(\\w*)"); 00097 return !allowedText.exactMatch(currentCompletion); 00098 } 00099 00100 void CodeCompletionModelControllerInterface::aborted(KTextEditor::View* view) { 00101 Q_UNUSED(view); 00102 } 00103 00104 bool CodeCompletionModelControllerInterface::shouldExecute(const QModelIndex& index, QChar inserted) { 00105 Q_UNUSED(index); 00106 Q_UNUSED(inserted); 00107 return false; 00108 } 00109 00110 KTextEditor::CodeCompletionModelControllerInterface2::MatchReaction CodeCompletionModelControllerInterface2::matchingItem(const QModelIndex& selected) { 00111 Q_UNUSED(selected) 00112 return HideListIfAutomaticInvocation; 00113 } 00114 00115 //END OLD 00116 00117 //BEGIN V3 00118 00119 CodeCompletionModelControllerInterface3::CodeCompletionModelControllerInterface3() 00120 { 00121 } 00122 00123 CodeCompletionModelControllerInterface3::~CodeCompletionModelControllerInterface3() 00124 { 00125 } 00126 00127 bool CodeCompletionModelControllerInterface3::shouldStartCompletion(View* view, const QString &insertedText, bool userInsertion, const Cursor &position) 00128 { 00129 Q_UNUSED(view); 00130 Q_UNUSED(position); 00131 if(insertedText.isEmpty()) 00132 return false; 00133 00134 QChar lastChar = insertedText.at(insertedText.count() - 1); 00135 if ((userInsertion && (lastChar.isLetter() || lastChar.isNumber() || lastChar == '_')) || 00136 lastChar == '.' || insertedText.endsWith(QLatin1String("->"))) { 00137 return true; 00138 } 00139 return false; 00140 } 00141 00142 Range CodeCompletionModelControllerInterface3::completionRange(View* view, const Cursor &position) 00143 { 00144 Cursor end = position; 00145 00146 QString text = view->document()->line(end.line()); 00147 00148 static QRegExp findWordStart( "\\b([_\\w]+)$" ); 00149 static QRegExp findWordEnd( "^([_\\w]*)\\b" ); 00150 00151 Cursor start = end; 00152 00153 if (findWordStart.lastIndexIn(text.left(end.column())) >= 0) 00154 start.setColumn(findWordStart.pos(1)); 00155 00156 if (findWordEnd.indexIn(text.mid(end.column())) >= 0) 00157 end.setColumn(end.column() + findWordEnd.cap(1).length()); 00158 00159 //kDebug()<<"returning:"<<Range(start,end); 00160 return Range(start, end); 00161 } 00162 00163 Range CodeCompletionModelControllerInterface3::updateCompletionRange(View* view, const Range& range) 00164 { 00165 QStringList text=view->document()->textLines(range,false); 00166 if(!text.isEmpty() && text.count() == 1 && text.first().trimmed().isEmpty()) 00167 //When inserting a newline behind an empty completion-range,, move the range forward to its end 00168 return Range(range.end(),range.end()); 00169 00170 return range; 00171 } 00172 00173 QString CodeCompletionModelControllerInterface3::filterString(View* view, const Range &range, const Cursor &position) 00174 { 00175 return view->document()->text(KTextEditor::Range(range.start(), position)); 00176 } 00177 00178 bool CodeCompletionModelControllerInterface3::shouldAbortCompletion(View* view, const Range &range, const QString ¤tCompletion) 00179 { 00180 //kDebug()<<view->cursorPosition(); 00181 //kDebug()<<range; 00182 if(view->cursorPosition() < range.start() || view->cursorPosition() > range.end()) 00183 return true; //Always abort when the completion-range has been left 00184 //Do not abort completions when the text has been empty already before and a newline has been entered 00185 00186 static const QRegExp allowedText("^(\\w*)"); 00187 return !allowedText.exactMatch(currentCompletion); 00188 } 00189 00190 void CodeCompletionModelControllerInterface3::aborted(KTextEditor::View* view) { 00191 Q_UNUSED(view); 00192 } 00193 00194 bool CodeCompletionModelControllerInterface3::shouldExecute(const QModelIndex& index, QChar inserted) { 00195 Q_UNUSED(index); 00196 Q_UNUSED(inserted); 00197 return false; 00198 } 00199 00200 KTextEditor::CodeCompletionModelControllerInterface3::MatchReaction CodeCompletionModelControllerInterface3::matchingItem(const QModelIndex& selected) { 00201 Q_UNUSED(selected) 00202 return HideListIfAutomaticInvocation; 00203 } 00204 //END V3 00205 00206 }
KDE 4.6 API Reference