KTextEditor

codecompletionmodelcontrollerinterface.cpp
1 /* SPDX-License-Identifier: LGPL-2.0-or-later
2 
3  Copyright (C) 2008 Niko Sams <[email protected]>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 
21 #include "codecompletionmodelcontrollerinterface.h"
22 
23 #include <QModelIndex>
24 #include <QRegularExpression>
25 
26 #include <ktexteditor/document.h>
27 #include <ktexteditor/view.h>
28 
29 namespace KTextEditor
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 
47  QChar lastChar = insertedText.at(insertedText.count() - 1);
48  if ((userInsertion && (lastChar.isLetter() || lastChar.isNumber() || lastChar == QLatin1Char('_'))) || lastChar == QLatin1Char('.') || insertedText.endsWith(QLatin1String("->"))) {
49  return true;
50  }
51  return false;
52 }
53 
55 {
56  Cursor end = position;
57 
58  QString text = view->document()->line(end.line());
59 
60  static const QRegularExpression findWordStart(QStringLiteral("\\b[_\\w]+$"));
61  static const QRegularExpression findWordEnd(QStringLiteral("^[_\\w]*\\b"));
62 
63  Cursor start = end;
64 
65  int pos = text.left(end.column()).lastIndexOf(findWordStart);
66  if (pos >= 0) {
67  start.setColumn(pos);
68  }
69 
71  pos = text.mid(end.column()).indexOf(findWordEnd, 0, &match);
72  if (pos >= 0) {
73  end.setColumn(end.column() + match.capturedLength());
74  }
75 
76  return Range(start, end);
77 }
78 
80 {
81  QStringList text = view->document()->textLines(range, false);
82  if (!text.isEmpty() && text.count() == 1 && text.first().trimmed().isEmpty())
83  // When inserting a newline behind an empty completion-range,, move the range forward to its end
84  {
85  return Range(range.end(), range.end());
86  }
87 
88  return range;
89 }
90 
92 {
93  return view->document()->text(KTextEditor::Range(range.start(), position));
94 }
95 
96 bool CodeCompletionModelControllerInterface::shouldAbortCompletion(View *view, const Range &range, const QString &currentCompletion)
97 {
98  if (view->cursorPosition() < range.start() || view->cursorPosition() > range.end()) {
99  return true; // Always abort when the completion-range has been left
100  }
101  // Do not abort completions when the text has been empty already before and a newline has been entered
102 
103  static const QRegularExpression allowedText(QStringLiteral("^\\w*$"));
104  return !allowedText.match(currentCompletion).hasMatch();
105 }
106 
108 {
109  Q_UNUSED(view);
110 }
111 
113 {
114  Q_UNUSED(index);
115  Q_UNUSED(inserted);
116  return false;
117 }
118 
120 {
121  Q_UNUSED(selected)
122  return HideListIfAutomaticInvocation;
123 }
124 
126 {
127  return false;
128 }
129 
130 }
QRegularExpressionMatch match(const QString &subject, int offset, QRegularExpression::MatchType matchType, QRegularExpression::MatchOptions matchOptions) const const
virtual Document * document() const =0
Get the view&#39;s document, that means the view is a view of the returned document.
virtual QString text() const =0
Get the document content.
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...
virtual QStringList textLines(const Range &range, bool block=false) const =0
Get the document content within the given range.
virtual void aborted(View *view)
Notification that completion for this model has been aborted.
The Cursor represents a position in a Document.
Definition: cursor.h:85
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...
bool isLetter() const const
virtual bool shouldAbortCompletion(View *view, const Range &range, const QString &currentCompletion)
This function decides if the completion should be aborted.
int count(const T &value) const const
virtual QString line(int line) const =0
Get a single text line.
bool isEmpty() const const
bool isEmpty() const const
virtual Range completionRange(View *view, const Cursor &position)
This function returns the completion range that will be used for the current completion.
virtual Range updateCompletionRange(View *view, const Range &range)
This function lets the CompletionModel dynamically modify the range.
bool hasMatch() const const
virtual Cursor cursorPosition() const =0
Get the view&#39;s current cursor position.
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const const
constexpr int column() const Q_DECL_NOEXCEPT
Retrieve the column on which this cursor is situated.
Definition: cursor.h:217
T & first()
The KTextEditor namespace contains all the public API that is required to use the KTextEditor compone...
constexpr Cursor start() const Q_DECL_NOEXCEPT
Get the start position of this range.
An object representing a section of text, from one Cursor to another.
virtual MatchReaction matchingItem(const QModelIndex &matched)
Called whenever an item in the completion-list perfectly matches the current filter text...
QString mid(int position, int n) const const
constexpr Cursor end() const Q_DECL_NOEXCEPT
Get the end position of this range.
int count() const const
const QChar at(int position) const const
constexpr int line() const Q_DECL_NOEXCEPT
Retrieve the line on which this cursor is situated.
Definition: cursor.h:199
QString left(int n) const const
virtual bool shouldHideItemsWithEqualNames() const
When multiple completion models are used at the same time, it may happen that multiple models add ite...
A text widget with KXMLGUIClient that represents a Document.
Definition: view.h:155
virtual QString filterString(View *view, const Range &range, const Cursor &position)
This function returns the filter-text used for the current completion.
int capturedLength(int nth) const const
bool isNumber() const const
void setColumn(int column) Q_DECL_NOEXCEPT
Set the cursor column to column.
Definition: cursor.h:226
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Fri Jun 5 2020 22:55:00 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.