1
/* This file is part of the KDE libraries
2
Copyright (C) 2008 Niko Sams <niko.sams@gmail.com>
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.
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.
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.
20
#include "codecompletionmodelcontrollerinterface.h"
22
#include <ktexteditor/view.h>
23
#include <ktexteditor/document.h>
25
namespace KTextEditor {
27
CodeCompletionModelControllerInterface::CodeCompletionModelControllerInterface()
31
CodeCompletionModelControllerInterface::~CodeCompletionModelControllerInterface()
35
bool CodeCompletionModelControllerInterface::shouldStartCompletion(View* view, const QString &insertedText, const Cursor &position)
39
if(insertedText.isEmpty())
42
QChar lastChar = insertedText.at(insertedText.count() - 1);
43
if (lastChar.isLetter() || lastChar.isNumber() || lastChar == '.' || lastChar == '_' || lastChar == '>') {
49
Range CodeCompletionModelControllerInterface::completionRange(View* view, const Cursor &position)
51
Cursor end = position;
53
QString text = view->document()->line(end.line());
55
static QRegExp findWordStart( "\\b([_\\w]+)$" );
56
static QRegExp findWordEnd( "^([_\\w]*)\\b" );
60
if (findWordStart.lastIndexIn(text.left(end.column())) >= 0)
61
start.setColumn(findWordStart.pos(1));
63
if (findWordEnd.indexIn(text.mid(end.column())) >= 0)
64
end.setColumn(end.column() + findWordEnd.cap(1).length());
66
return Range(start, end);
69
void CodeCompletionModelControllerInterface::updateCompletionRange(View* view, SmartRange& range)
75
QString CodeCompletionModelControllerInterface::filterString(View* view, const SmartRange &range, const Cursor &position)
77
return view->document()->text(KTextEditor::Range(range.start(), position));
80
bool CodeCompletionModelControllerInterface::shouldAbortCompletion(View* view, const SmartRange &range, const QString ¤tCompletion)
84
static const QRegExp allowedText("^(\\w*)");
85
return !allowedText.exactMatch(currentCompletion);