• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

cantor/src/lib

  • sources
  • kde-4.14
  • kdeedu
  • cantor
  • src
  • lib
completionobject.cpp
Go to the documentation of this file.
1 /*
2  This program is free software; you can redistribute it and/or
3  modify it under the terms of the GNU General Public License
4  as published by the Free Software Foundation; either version 2
5  of the License, or (at your option) any later version.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software
14  Foundation, Inc., 51 Franklin Street, Fifth Floor,
15  Boston, MA 02110-1301, USA.
16 
17  ---
18  Copyright (C) 2009 Alexander Rieder <alexanderrieder@gmail.com>
19  */
20 
21 #include "completionobject.h"
22 using namespace Cantor;
23 
24 #include <QStringList>
25 #include <QTimer>
26 #include <KDebug>
27 
28 #include "session.h"
29 
30 class Cantor::CompletionObjectPrivate
31 {
32  public:
33  QStringList completions;
34  QString line;
35  QString command;
36  QString identifier;
37  QString completion;
38  int position;
39  Session* session;
40  bool parenCompletion;
41 };
42 
43 CompletionObject::CompletionObject(Session* session) :
44  d(new CompletionObjectPrivate)
45 {
46  setParent(session);
47  d->position = -1;
48  d->session=session;
49 
50  connect(this, SIGNAL(fetchingDone()), this, SLOT(findCompletion()));
51  connect(this, SIGNAL(fetchingTypeDone(IdentifierType)), this,
52  SLOT(completeLineWithType(IdentifierType)));
53  setCompletionMode(KGlobalSettings::CompletionShell);
54 }
55 
56 CompletionObject::~CompletionObject()
57 {
58  delete d;
59 }
60 
61 QString CompletionObject::command() const
62 {
63  return d->command;
64 }
65 
66 Session* CompletionObject::session() const
67 {
68  return d->session;
69 }
70 
71 QStringList CompletionObject::completions() const
72 {
73  return d->completions;
74 }
75 
76 QString CompletionObject::identifier() const
77 {
78  return d->identifier;
79 }
80 
81 QString CompletionObject::completion() const
82 {
83  return d->completion;
84 }
85 
86 void CompletionObject::setLine(const QString& line, int index)
87 {
88  d->parenCompletion = false;
89  d->line = line;
90  if (index < 0)
91  index = line.length();
92  if (index > 1 && line[index-1] == '(') {
93  --index; // move before the parenthesis
94  d->parenCompletion = true; // but remember it was there
95  }
96  int cmd_index = locateIdentifier(line, index-1);
97  if (cmd_index < 0)
98  cmd_index = index;
99  d->position=cmd_index;
100  d->command=line.mid(cmd_index, index-cmd_index);
101 
102  //start a delayed fetch
103  QTimer::singleShot(0, this, SLOT(fetchCompletions()));
104 }
105 
106 void CompletionObject::updateLine(const QString& line, int index)
107 {
108  d->line = line;
109  if (index < 0)
110  index = line.length();
111  int cmd_index = locateIdentifier(line, index-1);
112  if (cmd_index < 0)
113  cmd_index = index;
114  d->command=line.mid(cmd_index, index-cmd_index);
115 
116  // start a delayed fetch
117  // For some backends this is a lot of unnecessary work...
118  QTimer::singleShot(0, this, SLOT(fetchCompletions()));
119 }
120 
121 void CompletionObject::completeLine(const QString& comp, CompletionObject::LineCompletionMode mode)
122 {
123  d->identifier = comp;
124  if (comp.isEmpty()) {
125  int index = d->position + d->command.length();
126  emit lineDone(d->line, index);
127  } else if (mode == PreliminaryCompletion) {
128  completeUnknownLine();
129  } else /* mode == FinalCompletion */ {
130  QTimer::singleShot(0, this, SLOT(fetchIdentifierType()));
131  }
132 }
133 
134 void CompletionObject::fetchIdentifierType()
135 {
136  emit fetchingTypeDone(UnknownType);
137 }
138 
139 
140 void CompletionObject::setCompletions(const QStringList& completions)
141 {
142  d->completions=completions;
143  this->setItems(completions);
144 }
145 
146 void CompletionObject::setCommand(const QString& cmd)
147 {
148  d->command=cmd;
149 }
150 
151 int CompletionObject::locateIdentifier(const QString& cmd, int index) const
152 {
153  if (index < 0)
154  return -1;
155 
156  int i;
157  for (i=index; i>=0 && mayIdentifierContain(cmd[i]); --i)
158  {}
159 
160  if (i==index || !mayIdentifierBeginWith(cmd[i+1]))
161  return -1;
162  return i+1;
163 }
164 
165 bool CompletionObject::mayIdentifierContain(QChar c) const
166 {
167  return c.isLetter() || c.isDigit() || c == '_';
168 }
169 
170 bool CompletionObject::mayIdentifierBeginWith(QChar c) const
171 {
172  return c.isLetter() || c == '_';
173 }
174 
175 void CompletionObject::findCompletion()
176 {
177  if (d->parenCompletion) {
178  disconnect(this, SIGNAL(fetchingTypeDone(IdentifierType)), 0, 0);
179  connect(this, SIGNAL(fetchingTypeDone(IdentifierType)), this,
180  SLOT(handleParenCompletionWithType(IdentifierType)));
181  d->identifier = d->command;
182  fetchIdentifierType();
183  return;
184  }
185  d->completion = makeCompletion(command());
186  emit done();
187 }
188 
189 void CompletionObject::handleParenCompletionWithType(IdentifierType type)
190 {
191  disconnect(this, SIGNAL(fetchingTypeDone(IdentifierType)), 0, 0);
192  connect(this, SIGNAL(fetchingTypeDone(IdentifierType)), this,
193  SLOT(completeLineWithType(IdentifierType)));
194 
195  if (type == FunctionWithArguments || type == FunctionWithoutArguments) {
196  d->completion = d->command;
197  emit done();
198  }
199 }
200 
201 void CompletionObject::completeLineWithType(IdentifierType type)
202 {
203  switch(type) {
204  case VariableType:
205  completeVariableLine();
206  break;
207  case FunctionWithArguments:
208  case FunctionWithoutArguments:
209  completeFunctionLine(type);
210  break;
211  case KeywordType:
212  completeKeywordLine();
213  break;
214  case UnknownType:
215  completeUnknownLine();
216  break;
217  }
218 }
219 
220 void CompletionObject::completeFunctionLine(IdentifierType type)
221 {
222  QString newline;
223  int newindex;
224 
225  QString func = d->identifier;
226  int after_command = d->position + d->command.length();
227  QString part1 = d->line.left(d->position) + func;
228  int index = d->position + func.length() + 1;
229  if (after_command < d->line.length() && d->line.at(after_command) == '(') {
230  QString part2 = d->line.mid(after_command+1);
231  int i;
232  // search for next non-space position
233  for (i = after_command+1;
234  i < d->line.length() && d->line.at(i).isSpace();
235  ++i) {}
236  if (type == FunctionWithArguments) {
237  if (i < d->line.length()) {
238  newline = part1+'('+part2;
239  newindex = index;
240  } else {
241  newline = part1+"()"+part2;
242  newindex = index;
243  }
244  } else /*type == FunctionWithoutArguments*/ {
245  if (i < d->line.length() && d->line.at(i) == ')') {
246  newline = part1+'('+part2;
247  newindex = index+i-after_command;
248  } else {
249  newline = part1+"()"+part2;
250  newindex = index+1;
251  }
252  }
253  } else {
254  QString part2 = d->line.mid(after_command);
255  if (type == FunctionWithArguments) {
256  newline = part1+"()"+part2;
257  newindex = index;
258  } else /*type == FunctionWithoutArguments*/ {
259  newline = part1+"()"+part2;
260  newindex = index+1;
261  }
262  }
263  emit lineDone(newline, newindex);
264 }
265 
266 void CompletionObject::completeKeywordLine()
267 {
268  QString keyword = d->identifier;
269  int after_command = d->position + d->command.length();
270  int newindex = d->position + keyword.length() + 1;
271  QString part1 = d->line.left(d->position) + keyword;
272  QString part2 = d->line.mid(after_command);
273  if (after_command < d->line.length() && d->line.at(after_command) == ' ')
274  emit lineDone(part1+part2, newindex);
275  else
276  emit lineDone(part1+' '+part2, newindex);
277 }
278 
279 void CompletionObject::completeVariableLine()
280 {
281  QString var = d->identifier;
282  int after_command = d->position + d->command.length();
283  QString newline = d->line.left(d->position) + var + d->line.mid(after_command);
284  int newindex = d->position + var.length();
285  emit lineDone(newline, newindex);
286 }
287 
288 void CompletionObject::completeUnknownLine()
289 {
290  // identifiers of unknown type are completed like variables
291  completeVariableLine();
292 }
293 
294 #include "completionobject.moc"
Cantor::CompletionObject::FunctionWithoutArguments
a function that takes no arguments
Definition: completionobject.h:111
Cantor::CompletionObject::KeywordType
a keyword
Definition: completionobject.h:112
Cantor::CompletionObject::completeKeywordLine
void completeKeywordLine()
Completes line with keyword identifier and emits lineDone with the completed line.
Definition: completionobject.cpp:266
Cantor::CompletionObject::identifier
QString identifier() const
returns the identifier for fetchIdentifierType
Definition: completionobject.cpp:76
Cantor::CompletionObject::command
QString command() const
returns the command, this completion is for
Definition: completionobject.cpp:61
QChar
Cantor::CompletionObject::completeFunctionLine
void completeFunctionLine(IdentifierType type=FunctionWithArguments)
Completes line with function identifier and emits lineDone with the completed line.
Definition: completionobject.cpp:220
QChar::isDigit
bool isDigit() const
session.h
Cantor::CompletionObject::IdentifierType
IdentifierType
Definition: completionobject.h:107
Cantor::CompletionObject::mayIdentifierContain
virtual bool mayIdentifierContain(QChar c) const
return true if c may be used in identifier names
Definition: completionobject.cpp:165
completionobject.h
Cantor::CompletionObject::setCommand
void setCommand(const QString &cmd)
sets the command/command-part
Definition: completionobject.cpp:146
QChar::isLetter
bool isLetter() const
Cantor::CompletionObject::findCompletion
void findCompletion()
Find the completion.
Definition: completionobject.cpp:175
Cantor::CompletionObject::completeLine
void completeLine(const QString &comp, LineCompletionMode mode)
Takes a completion and a completion mode and triggers and calculates the new line with this completio...
Definition: completionobject.cpp:121
Cantor::CompletionObject::~CompletionObject
~CompletionObject()
Destructor.
Definition: completionobject.cpp:56
Cantor::CompletionObject::CompletionObject
CompletionObject(Session *parent)
Constructor.
Definition: completionobject.cpp:43
Cantor::CompletionObject::setCompletions
void setCompletions(const QStringList &completions)
Sets the completions.
Definition: completionobject.cpp:140
Cantor::CompletionObject::VariableType
a variable
Definition: completionobject.h:108
Cantor::CompletionObject::PreliminaryCompletion
Only insert the completion.
Definition: completionobject.h:54
QString::isEmpty
bool isEmpty() const
Cantor::CompletionObject::LineCompletionMode
LineCompletionMode
Definition: completionobject.h:53
QString
Cantor::CompletionObject::mayIdentifierBeginWith
virtual bool mayIdentifierBeginWith(QChar c) const
return true if identifier names can begin with c
Definition: completionobject.cpp:170
Cantor::CompletionObject::UnknownType
no identifier type was found
Definition: completionobject.h:113
QStringList
Cantor::CompletionObject::updateLine
void updateLine(const QString &line, int index)
Takes the changed line and updates the command accordingly.
Definition: completionobject.cpp:106
Cantor::CompletionObject::session
Session * session() const
returns the session, this object belongs to
Definition: completionobject.cpp:66
Cantor::CompletionObject::fetchIdentifierType
virtual void fetchIdentifierType()
Fetch the identifier type of d->identifier; reimplemented in the backends.
Definition: completionobject.cpp:134
Cantor::CompletionObject::locateIdentifier
virtual int locateIdentifier(const QString &cmd, int index) const
Find an identifier in cmd that ends at index.
Definition: completionobject.cpp:151
Cantor::CompletionObject::done
void done()
indicates that the possible completions and a common completion string have been found ...
Cantor::CompletionObject::FunctionWithArguments
a function that takes arguments
Definition: completionobject.h:109
QString::mid
QString mid(int position, int n) const
Cantor::CompletionObject::handleParenCompletionWithType
void handleParenCompletionWithType(IdentifierType type)
Handle a completion request after a opening parenthesis.
Definition: completionobject.cpp:189
Cantor::CompletionObject::completion
QString completion() const
Returns the last completion.
Definition: completionobject.cpp:81
Cantor::CompletionObject::lineDone
void lineDone(QString line, int index)
emitted when the line completion is done, passes the new line and the cursor index ...
QString::length
int length() const
Cantor::CompletionObject::completeLineWithType
void completeLineWithType(IdentifierType type)
Calls the appropriate complete*Line based on type.
Definition: completionobject.cpp:201
Cantor::CompletionObject::completions
QStringList completions() const
Returns a list of completions.
Definition: completionobject.cpp:71
Cantor::CompletionObject::setLine
void setLine(const QString &line, int index)
Sets the line and cursor index at which a completion should be found This triggers an asynchronous fe...
Definition: completionobject.cpp:86
Cantor::CompletionObject::fetchCompletions
virtual void fetchCompletions()=0
This function should be reimplemented to start the actual fetching of the completions.
Cantor::CompletionObject::fetchingTypeDone
void fetchingTypeDone(IdentifierType type)
indicates that the type of identifier() was found and passes the type as an argument ...
Cantor::CompletionObject::completeUnknownLine
void completeUnknownLine()
Completes line with identifier of unknown type and emits lineDone with the completed line...
Definition: completionobject.cpp:288
Cantor::CompletionObject::fetchingDone
void fetchingDone()
indicates that the fetching of completions is done
Cantor::CompletionObject::completeVariableLine
void completeVariableLine()
Completes line with variable identifier and emits lineDone with the completed line.
Definition: completionobject.cpp:279
QTimer::singleShot
singleShot
Cantor::Session
The Session object is the main class used to interact with a Backend.
Definition: session.h:50
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:16:33 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

cantor/src/lib

Skip menu "cantor/src/lib"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



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