File: phpcodecompletion.cpp

package info (click to toggle)
kdevelop3 4%3A3.2.0-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 58,220 kB
  • ctags: 33,997
  • sloc: cpp: 278,404; ansic: 48,238; sh: 23,721; tcl: 19,882; perl: 5,498; makefile: 4,717; ruby: 1,610; python: 1,549; awk: 1,484; xml: 563; java: 359; php: 20; asm: 14; ada: 5; pascal: 4; fortran: 4; haskell: 2; sql: 1
file content (550 lines) | stat: -rw-r--r-- 18,550 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
/***************************************************************************
                          phpcodecompletion.cpp  -  description
                             -------------------
    begin                : Tue Jul 17 2001
    copyright            : (C) 2001 by Sandy Meier
    email                : smeier@kdevelop.org
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "phpcodecompletion.h"
#include "phpsupportpart.h"
#include "phpconfigdata.h"

#include <kdevcore.h>
#include <kinstance.h>
#include <kstandarddirs.h>
#include <kdebug.h>

#include <qfile.h>
#include <qtextstream.h>
#include <qregexp.h>

#include <iostream>

using namespace std;

PHPCodeCompletion::PHPCodeCompletion(PHPConfigData *config,KDevCore* core,CodeModel* model){
  m_config = config;
  m_core = core;
  m_model = model;
  m_argWidgetShow = false;
  m_completionBoxShow=false;

  readGlobalPHPFunctionsFile();
}

PHPCodeCompletion::~PHPCodeCompletion(){
}

void PHPCodeCompletion::readGlobalPHPFunctionsFile(){
  KStandardDirs *dirs = PHPSupportFactory::instance()->dirs();
  QString phpFuncFile = dirs->findResource("data","kdevphpsupport/phpfunctions");
  QRegExp lineReg(":([0-9A-Za-z_]+) ([0-9A-Za-z_]+)(\\(.*\\))");
  FunctionCompletionEntry e;
  QFile f(phpFuncFile);
  if ( f.open(IO_ReadOnly) ) {    // file opened successfully
      QTextStream t( &f );        // use a text stream
      QString s;
      while ( !t.eof() ) {        // until end of file...
	  s = t.readLine();       // line of text excluding '\n'
	  if(lineReg.search(s.local8Bit()) != -1){
	      e.prefix = lineReg.cap(1);
	      e.text = lineReg.cap(2);
	      //	      if(QString(lineReg.cap(3)) == "void"){
		e.postfix ="()";
		//	      }
		//	      else{
		//	      e.postfix ="(...)";
		//	      }
	      e.prototype = QString(lineReg.cap(1)) + " " + QString(lineReg.cap(2)) +
		  "(" + QString(lineReg.cap(3)) + ")";
	      m_globalFunctions.append(e);
	  }

      }
      f.close();
  }

}
void PHPCodeCompletion::argHintHided(){
  kdDebug(9018) << "PHPCodeCompletion::argHintHided"  << endl;
  m_argWidgetShow = false;
}
void PHPCodeCompletion::completionBoxHided(){
  kdDebug(9018) << "PHPCodeCompletion::completionBoxHided()" << endl;
  m_completionBoxShow=false;
}

void PHPCodeCompletion::setActiveEditorPart(KParts::Part *part)
{
  if (!part || !part->widget())
    return;

  kdDebug(9018) << "PHPCodeCompletion::setActiveEditorPart"  << endl;

  if(!(m_config->getCodeCompletion() || m_config->getCodeHinting())){
    return; // no help
  }

  m_editInterface = dynamic_cast<KTextEditor::EditInterface*>(part);
  if (!m_editInterface)
  {
    kdDebug(9018) << "editor doesn't support the EditDocumentIface" << endl;
    return;
  }

  m_cursorInterface = dynamic_cast<KTextEditor::ViewCursorInterface*>(part->widget());
  if (!m_cursorInterface)
  {
    kdDebug(9018) << "editor does not support the ViewCursorInterface" << endl;
    return;
  }

  m_codeInterface = dynamic_cast<KTextEditor::CodeCompletionInterface*>(part->widget());
  if (!m_codeInterface) { // no CodeCompletionDocument available
    kdDebug(9018) << "editor doesn't support the CodeCompletionDocumentIface" << endl;
    return;
  }

  m_selectionInterface = dynamic_cast<KTextEditor::SelectionInterface*>(part);
  if(!m_selectionInterface) {
	kdDebug(9018) << "editor doesn't support the SelectionInterface" << endl;
	return;
  }


  disconnect(part->widget(), 0, this, 0 ); // to make sure that it is't connected twice
//  connect(part->widget(), SIGNAL(cursorPositionChanged()), this, SLOT(cursorPositionChanged()));
  connect( part, SIGNAL(textChanged()), this, SLOT(cursorPositionChanged()) );
  connect(part->widget(), SIGNAL(argHintHidden()), this, SLOT(argHintHided()));
  connect(part->widget(), SIGNAL(completionAborted()), this, SLOT(completionBoxHided()));
  connect(part->widget(), SIGNAL(completionDone()), this, SLOT(completionBoxHided()));

}

void PHPCodeCompletion::cursorPositionChanged(){
  uint line, col;
  m_cursorInterface->cursorPositionReal(&line, &col);
  kdDebug(9018) << "PHPCodeCompletion::cursorPositionChanged:" << line << ":" << col  << endl;

  m_currentLine = line;
  QString lineStr = m_editInterface->textLine(line);
  if(lineStr.isNull() || lineStr.isEmpty()) return; // nothing to do
  //kdDebug(9018) << "ZEILE:" << lineStr <<":" << endl;
  //  kdDebug(9018) << "Length:" << lineStr.length() <<":" << endl;
  //  lineStr.replace(QRegExp("\t"),"_");
  //  kdDebug(9018) << "ZEILEohneTAB:" << lineStr <<":" << endl;

  if(m_selectionInterface->hasSelection()){
	kdDebug(9018) << "No CodeCompletion/ArgHinting at the moment, because text is selected" << endl;
	return;
  }

  if(m_config->getCodeHinting()){
    if(checkForNewInstanceArgHint(lineStr,col,line)){
      return;
    }

    if(checkForMethodArgHint(lineStr,col,line)){
      return;
    }

    if(checkForGlobalFunctionArgHint(lineStr,col,line)){
      return;
    }
  }

  if(m_config->getCodeCompletion()){
    QString restLine = lineStr.mid(col);
    if(restLine.left(1) != " " && restLine.left(1) != "\t" && !restLine.isNull()){
      kdDebug(9018) << "no codecompletion because no empty character after cursor:" << restLine << ":" << endl;
      return;
    }

    if(checkForVariable(lineStr,col,line)){
      return;
    }

    // $test = new XXX
    if(checkForNewInstance(lineStr,col,line)){
      return;
    }


    if(checkForGlobalFunction(lineStr,col)) {
      return;
    }
  }


}

bool PHPCodeCompletion::checkForMethodArgHint(QString lineStr,int col,int /*line*/){
  kdDebug(9018) << "enter checkForMethodArgHint" << endl;
  if(m_argWidgetShow){
    return false; //nothing to do
  }
  QString methodStart = lineStr.left(col);
  int leftBracket = methodStart.findRev("(");
  methodStart = methodStart.left(leftBracket);
  int varStart = methodStart.findRev("$");
  if(varStart ==-1){
    //cerr << "checkForMethodArgHint: no '$' (variable start) found" << endl;
    return false;
  }
  QString variableLine = methodStart.mid(varStart+1);
  if(variableLine.isNull()){ return false;}
  //  cerr << "VarLine:" << variableLine << endl;
  QString className = "";
  QStringList vars = QStringList::split("->",variableLine);
  QString methodName = vars.last();
  //cerr << "methodname:" << methodName << endl;
  vars.remove(vars.fromLast()); // remove the methodname
  for ( QStringList::Iterator it = vars.begin(); it != vars.end(); ++it ) {
    className = this->getClassName("$" + (*it),className);
  }
  //  cerr << "Classname:" << className << endl;

  if( m_model->globalNamespace()->hasClass(className) ){
    ClassDom pClass =  m_model->globalNamespace()->classByName(className)[ 0 ];
    FunctionList methodList = pClass->functionList();
    FunctionList::Iterator methodIt;

    for (methodIt = methodList.begin(); methodIt != methodList.end(); ++methodIt) {
      if ((*methodIt)->name() == methodName){
	ArgumentDom pArg = (*methodIt)->argumentList().first();
	m_argWidgetShow = true;
	QValueList <QString> functionList;
	if(pArg){
	  functionList.append(methodName + "(" + pArg->type() +")");
	}
	m_codeInterface->showArgHint ( functionList, "()", "," );
	return true;
      }
    }
  }

  return false;
}
bool PHPCodeCompletion::checkForVariable(QString lineStr,int col,int /*line*/){
  kdDebug(9018)  << "enter checkForVariable()" << endl;
  QString methodStart = lineStr.left(col);
  if(methodStart.right(2) != "->"){
    kdDebug(9018)  << "checkForVariable: no '->' found" << endl;
    return false;
  }
  int varStart = methodStart.findRev("$");
  if(varStart ==-1){
    kdDebug(9018) << "checkForVariable: no '$' (variable start) found" << endl;
    return false;
  }
  QString variableLine = methodStart.mid(varStart+1);
  kdDebug(9018) << "VarLine:" << variableLine << ":" << endl;
  QString className ="";
  QStringList vars = QStringList::split("->",variableLine);
  for ( QStringList::Iterator it = vars.begin(); it != vars.end(); ++it ) {
    className = this->getClassName("$" + (*it),className);
  }
  kdDebug(9018) << "Classname:" << className << endl;

  QValueList<KTextEditor::CompletionEntry> list = this->getClassMethodsAndVariables(className);
  if(list.count()>0){
    m_completionBoxShow=true;
    m_codeInterface->showCompletionBox(list);
    return true;
  }
  return false;
}

QString PHPCodeCompletion::getClassName(QString varName,QString maybeInstanceOf){
  kdDebug(9018) << "enter PHPCodeCompletion::getClassName:" << varName << ":" << maybeInstanceOf << ":" << endl;
  if(varName == "$this"){
    return this->searchCurrentClassName();
  }
  if(maybeInstanceOf.isEmpty()){
    // ok, we need to search it
    return this->searchClassNameForVariable(varName);
  }
  if(m_model->globalNamespace()->hasClass(maybeInstanceOf) !=0){
    ClassDom pClass = m_model->globalNamespace()->classByName(maybeInstanceOf)[ 0 ];
    VariableList attrList = pClass->variableList();
    VariableList::Iterator attrIt;

    for (attrIt = attrList.begin(); attrIt != attrList.end(); ++attrIt) {
      if ((*attrIt)->name() == varName){
	return (*attrIt)->type();
      }
    }
  }
  return "";
}

QString PHPCodeCompletion::searchClassNameForVariable(QString varName){
  kdDebug(9018) << "enter PHPCodeCompletion::searchClassNameForVariable:" << varName << ":" << endl;
  QRegExp createVarRe(QString("\\$" + varName.mid(1) + "[ \t]*=[& \t]*new[ \t]+([0-9A-Za-z_]+)").local8Bit());
  for(int i=m_currentLine;i>=0;i--){
    QString lineStr = m_editInterface->textLine(i);
    if(!lineStr.isNull()){
      if(createVarRe.search(lineStr.local8Bit()) != -1) { // ok found
	//      cerr << endl << "match in searchClassNameForVariable:";
	return createVarRe.cap(1);
      }
    }
  }
  return QString::null;
}

QString PHPCodeCompletion::searchCurrentClassName(){
  kdDebug(9018) << "enter PHPCodeCompletion::searchCurrentClassName:" << endl;
  QRegExp classre("^[ \t]*class[ \t]+([A-Za-z_]+)[ \t]*(extends[ \t]*([A-Za-z_]+))?.*$");
  for(int i=m_currentLine;i>=0;i--){
    QString lineStr = m_editInterface->textLine(i);
    if(!lineStr.isNull()){
      if(classre.search(lineStr.local8Bit()) != -1) { // ok found
          return classre.cap(1);
      }
    }
  }
  return QString::null;
}

bool PHPCodeCompletion::checkForGlobalFunctionArgHint(QString lineStr,int col,int /*line*/){
  kdDebug(9018) << "enter checkForGlobalFunctionArgHint" << endl;
  if(m_argWidgetShow){
    return false; //nothing to do
  }

  QString methodStart = lineStr.left(col);
  int leftBracket = methodStart.findRev("(");
  int rightBracket = methodStart.findRev(")");
  kdDebug(9018)   << "col: " << col << endl;
  kdDebug(9018)   << "leftBracket: " << leftBracket << endl;
  kdDebug(9018)   << "rightBracket: " << rightBracket << endl;
  kdDebug(9018)   << "methodStart: " << methodStart.latin1() << endl;
  if(leftBracket == -1) return false; // ok not found
  if(rightBracket>leftBracket) return false; // we are out of (..)
  methodStart = methodStart.left(leftBracket+1);
  //  cerr << methodStart << endl;
  QRegExp functionre("([A-Za-z_]+)[ \t]*\\(");
  if(functionre.search(methodStart.local8Bit()) != -1){ // check for global functions
    QString name = functionre.cap(1);
    int startMethod = lineStr.findRev(name,col);
    QString startString = lineStr.mid(0,startMethod);
    if(startString.right(2) != "->"){
      QValueList <QString> functionList;
      //cerr << "PHPCodeCompletion::checkForArgHint() found global function" << endl ;
      QValueList<FunctionCompletionEntry>::Iterator it;
      for( it = m_globalFunctions.begin(); it != m_globalFunctions.end(); ++it ){
	if((*it).text == name){
	  functionList.append((*it).prototype);
	}
      }
      FunctionList methodList = m_model->globalNamespace()->functionList();
      FunctionList::Iterator methodIt;
      for (methodIt = methodList.begin(); methodIt != methodList.end(); ++methodIt) {
	if((*methodIt)->name() == name){
	  ArgumentDom pArg = (*methodIt)->argumentList().first();
	  functionList.append(name+"("+ pArg->type()+")");
	}
      }
      if(functionList.count() >0){
	m_argWidgetShow = true;
	if (m_codeInterface)
	  m_codeInterface->showArgHint ( functionList, "()", "," );
	return true;
      }
    }
  }
  return false;
}
bool PHPCodeCompletion::checkForGlobalFunction(QString lineStr,int col){
  kdDebug(9018)   << "enter checkForGlobalFunction(" + lineStr + "," << col << endl;
  QString methodStart ="";
  if(lineStr.length()==2){
    return doGlobalMethodCompletion(lineStr);
  }
  if(col==2){
    QString startStr =lineStr.mid(col-2,2);
    return doGlobalMethodCompletion(startStr);
  }

  // normal case
  QString startStr =lineStr.mid(col-3,3);
  if(startStr.isNull()){
    kdDebug(9018)   << "not enough letters" << endl;
    return false; // not enough letters
  }
  //dDebug(9018)  << "StartStr:" << startStr  << ":" << endl;
  QString extraChar = " \t+-=/*;)(}{";
  if(extraChar.find( startStr[0] ) != -1){
    methodStart = startStr.right(2);
  }

  //kdDebug(9018)  << "Methodstart:" << methodStart  << ":" << endl;
  if(!methodStart.isEmpty()){
    return doGlobalMethodCompletion(methodStart);
  }
  return false;
}

bool PHPCodeCompletion::doGlobalMethodCompletion(QString methodStart){
  //kdDebug(9018)  << "doGlobalMethodCompletion:" << methodStart  << ":" << endl;
  QValueList<KTextEditor::CompletionEntry> list;
  QValueList<FunctionCompletionEntry>::Iterator it;
  for( it = m_globalFunctions.begin(); it != m_globalFunctions.end(); ++it ){
    if((*it).text.startsWith(methodStart)){
      KTextEditor::CompletionEntry e;
      e = (*it);
      list.append(e);
    }
  }

  FunctionList methodList = m_model->globalNamespace()->functionList();
  FunctionList::Iterator methodIt;
  for (methodIt = methodList.begin(); methodIt != methodList.end(); ++methodIt) {
    if ((*methodIt)->name().startsWith(methodStart)){
      KTextEditor::CompletionEntry e;
      e.text = (*methodIt)->name();
      e.postfix ="()";
      list.append(e);
    }
  }

  if(list.count() >0){
    m_completionBoxShow=true;
    m_codeInterface->showCompletionBox(list,2);
    return true;
  }
  return false;
}



bool PHPCodeCompletion::checkForNewInstanceArgHint(QString lineStr,int col,int /*line*/){
  //  cerr  << "enter checkForNewInstanceArgHint" << endl;
  if(m_argWidgetShow){
    return false; //nothing to do
  }

  QString start = lineStr.left(col);
  int leftBracket = start.findRev("(");
  int rightBracket = start.findRev(")");
  int equal = start.findRev("=");
  if(equal == -1) return false; // ok not found
  if(leftBracket == -1) return false; // ok not found
  if(rightBracket>leftBracket) return false; // we are out of (..)
  start = start.mid(equal,leftBracket-equal+1);
  //  cerr << "NEW: " << start << endl;
  QRegExp newre("=[& \t]*new[ \t]+([A-Za-z_]+)[ \t]*\\(");
  if(newre.search(start.local8Bit()) != -1){
    if( m_model->globalNamespace()->hasClass(newre.cap(1)) ){ // exists this class?
      ClassDom pClass = m_model->globalNamespace()->classByName(newre.cap(1))[ 0 ];
      FunctionList methodList = pClass->functionList();
      FunctionList::Iterator methodIt;
      for (methodIt = methodList.begin(); methodIt != methodList.end(); ++methodIt) {
        if((*methodIt)->name() == newre.cap(1)){
	  ArgumentDom pArg = (*methodIt)->argumentList().first();
	  m_argWidgetShow = true;
	  QValueList <QString> functionList;
	  if(pArg){
	    functionList.append((*methodIt)->name()+"("+ pArg->type()+")");
	  }
	  m_codeInterface->showArgHint ( functionList, "()", "," );
	  return true;
	}
      }
    }
  }
  return false;
}
bool PHPCodeCompletion::checkForNewInstance(QString lineStr,int col,int /*line*/){
  //  cerr  << "enter checkForNewInstance" << endl;
  QString start = lineStr.left(col);
  QRegExp newre("=[& \t]*new[ \t]+([A-Za-z_]+)");
  if(newre.search(start.local8Bit()) != -1){
    QString classStart = newre.cap(1);
    if(start.right(2) == classStart){
      QValueList<KTextEditor::CompletionEntry> list;

      ClassList classList = m_model->globalNamespace()->classList();
      ClassList::Iterator classIt;
      for (classIt = classList.begin(); classIt != classList.end(); ++classIt) {
	if((*classIt)->name().startsWith(classStart)){
	  KTextEditor::CompletionEntry e;
	  e.text = (*classIt)->name();
	  list.append(e);
	}
      }
      if(classStart == "ob") {
	KTextEditor::CompletionEntry e;
	e.text = "object";
	list.append(e);
      }
      if(classStart == "ar") {
	KTextEditor::CompletionEntry e;
	e.text = "array";
	list.append(e);
      }
      if(list.count() >0){
	m_completionBoxShow=true;
	m_codeInterface->showCompletionBox(list,2);
	return true;
      }
    }
  }
  return false;
}

QValueList<KTextEditor::CompletionEntry> PHPCodeCompletion::getClassMethodsAndVariables(QString className){
  QValueList<KTextEditor::CompletionEntry> list;
  ClassDom pClass;
  do {
    if(m_model->globalNamespace()->hasClass(className) ){
      pClass = m_model->globalNamespace()->classByName(className)[ 0 ];
      FunctionList methodList = pClass->functionList();
      FunctionList::Iterator methodIt;
      for (methodIt = methodList.begin(); methodIt != methodList.end(); ++methodIt) {
	KTextEditor::CompletionEntry e;
	e.text = (*methodIt)->name();
	//	ParsedArgument* pArg = pMethod->arguments.first();
	//	if(pArg->type() == ""){
	e.postfix ="()";
	//	}else{
	//      e.postfix ="(...)";
	//	}
	list.append(e);
      }
      VariableList attrList = pClass->variableList();
      VariableList::Iterator attrIt;
      for (attrIt = attrList.begin(); attrIt != attrList.end(); ++attrIt) {
	KTextEditor::CompletionEntry e;
	QString name = (*attrIt)->name();
	e.text = name;
	e.postfix ="";
	list.append(e);
      }


      if(pClass->baseClassList().count() !=0){
	className = pClass->baseClassList().first();
      }
      else{
	className ="";
      }
    } else {
        pClass = 0;
    }
  } while (pClass != 0);
  return list;
}

#include "phpcodecompletion.moc"