completion.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. ktigcc - TIGCC IDE for KDE
  3. Copyright (C) 2006 Kevin Kofler
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  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 Foundation,
  14. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include <qstring.h>
  17. #include <qvaluelist.h>
  18. #include <qpair.h>
  19. #include <qpoint.h>
  20. #include <qregexp.h>
  21. #include <kate/view.h>
  22. #include <kate/document.h>
  23. #include <ktexteditor/editinterfaceext.h>
  24. #include "completion.h"
  25. #include "preferences.h"
  26. // Maps file name to a CompletionInfo.
  27. QMap<QString,CompletionInfo> systemHeaderCompletion, projectCompletion;
  28. TemplatePopup::TemplatePopup(Kate::View *parent)
  29. : QPopupMenu(parent), view(parent)
  30. {
  31. connect(this,SIGNAL(activated(int)),this,SLOT(QPopupMenu_activated(int)));
  32. unsigned i=0;
  33. for (QValueList<QPair<QString,QString> >::ConstIterator it=preferences.templates.begin();
  34. it!=preferences.templates.end(); ++it, i++)
  35. insertItem((*it).first,i);
  36. QPoint pos=parent->cursorCoordinates();
  37. if (pos.x()<0 || pos.y()<0) {
  38. // Cursor outside of the view, so center on view instead.
  39. QSize parentSize=parent->size();
  40. QSize popupSize=sizeHint();
  41. pos.setX((parentSize.width()-popupSize.width())>>1);
  42. pos.setY((parentSize.height()-popupSize.height())>>1);
  43. }
  44. exec(parent->mapToGlobal(pos));
  45. deleteLater();
  46. }
  47. void TemplatePopup::QPopupMenu_activated(int id)
  48. {
  49. QString code=preferences.templates[id].second;
  50. QString indent=view->currentTextLine();
  51. // Remove everything starting from the first non-whitespace character.
  52. indent=indent.remove(QRegExp("(?!\\s).*$"));
  53. indent.prepend('\n');
  54. code.replace('\n',indent);
  55. int cursorPos=code.find('|');
  56. if (cursorPos>=0) {
  57. QString left=code.left(cursorPos);
  58. QString right=code.mid(cursorPos+1);
  59. unsigned row, col;
  60. KTextEditor::EditInterfaceExt *editExt=KTextEditor::editInterfaceExt(view->getDoc());
  61. editExt->editBegin();
  62. view->insertText(left);
  63. view->cursorPositionReal(&row,&col);
  64. view->insertText(right);
  65. editExt->editEnd();
  66. view->setCursorPositionReal(row,col);
  67. } else view->insertText(code);
  68. }