• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

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 CodeCompletionModelControllerInterface::CodeCompletionModelControllerInterface()
00030 {
00031 }
00032 
00033 CodeCompletionModelControllerInterface::~CodeCompletionModelControllerInterface()
00034 {
00035 }
00036 
00037 bool CodeCompletionModelControllerInterface::shouldStartCompletion(View* view, const QString &insertedText, bool userInsertion, const Cursor &position)
00038 {
00039     Q_UNUSED(view);
00040     Q_UNUSED(position);
00041     if(insertedText.isEmpty())
00042         return false;
00043 
00044     QChar lastChar = insertedText.at(insertedText.count() - 1);
00045     if ((userInsertion && (lastChar.isLetter() || lastChar.isNumber() || lastChar == '_')) || lastChar == '.' || insertedText.endsWith("->")) {
00046         return true;
00047     }
00048     return false;
00049 }
00050 
00051 Range CodeCompletionModelControllerInterface::completionRange(View* view, const Cursor &position)
00052 {
00053     Cursor end = position;
00054 
00055     QString text = view->document()->line(end.line());
00056 
00057     static QRegExp findWordStart( "\\b([_\\w]+)$" );
00058     static QRegExp findWordEnd( "^([_\\w]*)\\b" );
00059 
00060     Cursor start = end;
00061 
00062     if (findWordStart.lastIndexIn(text.left(end.column())) >= 0)
00063         start.setColumn(findWordStart.pos(1));
00064 
00065     if (findWordEnd.indexIn(text.mid(end.column())) >= 0)
00066         end.setColumn(end.column() + findWordEnd.cap(1).length());
00067 
00068     return Range(start, end);
00069 }
00070 
00071 void CodeCompletionModelControllerInterface::updateCompletionRange(View* view, SmartRange& range)
00072 {
00073     Q_UNUSED(view);
00074     if(!range.text().isEmpty() && range.text().count() == 1 && range.text().first().trimmed().isEmpty())
00075       //When inserting a newline behind an empty completion-range,, move the range forward to its end
00076       range.start() = range.end();
00077 }
00078 
00079 QString CodeCompletionModelControllerInterface::filterString(View* view, const SmartRange &range, const Cursor &position)
00080 {
00081     return view->document()->text(KTextEditor::Range(range.start(), position));
00082 }
00083 
00084 bool CodeCompletionModelControllerInterface::shouldAbortCompletion(View* view, const SmartRange &range, const QString &currentCompletion)
00085 {
00086     if(view->cursorPosition() < range.start() || view->cursorPosition() > range.end())
00087       return true; //Always abort when the completion-range has been left
00088     //Do not abort completions when the text has been empty already before and a newline has been instead
00089     
00090     Q_UNUSED(view);
00091     Q_UNUSED(range);
00092     static const QRegExp allowedText("^(\\w*)");
00093     return !allowedText.exactMatch(currentCompletion);
00094 }
00095 
00096 void CodeCompletionModelControllerInterface::aborted(KTextEditor::View* view) {
00097     Q_UNUSED(view);
00098 }
00099 
00100 bool CodeCompletionModelControllerInterface::shouldExecute(const QModelIndex& index, QChar inserted) {
00101   Q_UNUSED(index);
00102   Q_UNUSED(inserted);
00103   return false;
00104 }
00105 
00106 }

KTextEditor

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

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal