/plugins/PHPParser/tags/PHPParser-1.4.0/src/gatchan/phpparser/sidekick/PHPSideKickCompletion.java

# · Java · 172 lines · 127 code · 11 blank · 34 comment · 25 complexity · a701d667adbd74dfefae2f0d50888f7b MD5 · raw file

  1. /*
  2. * PHPSideKickCompletion.java - The PHP Parser
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2003, 2010 Matthieu Casanova
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or any later version.
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. */
  21. package gatchan.phpparser.sidekick;
  22. import gatchan.phpparser.project.itemfinder.PHPItemCellRenderer;
  23. import net.sourceforge.phpdt.internal.compiler.ast.ClassDeclaration;
  24. import net.sourceforge.phpdt.internal.compiler.ast.ClassHeader;
  25. import net.sourceforge.phpdt.internal.compiler.ast.MethodDeclaration;
  26. import net.sourceforge.phpdt.internal.compiler.ast.MethodHeader;
  27. import net.sourceforge.phpdt.internal.compiler.parser.Outlineable;
  28. import org.gjt.sp.jedit.jEdit;
  29. import org.gjt.sp.jedit.textarea.Selection;
  30. import sidekick.SideKickCompletion;
  31. import javax.swing.*;
  32. import java.util.List;
  33. /**
  34. * @author Matthieu Casanova
  35. */
  36. public class PHPSideKickCompletion extends SideKickCompletion
  37. {
  38. private final String lastWord;
  39. //{{{ PHPSideKickCompletion constructor
  40. public PHPSideKickCompletion(String word, String lastWord)
  41. {
  42. super(jEdit.getActiveView(), word);
  43. this.lastWord = lastWord;
  44. } //}}}
  45. //{{{ addItem() method
  46. public void addItem(Object item, String word)
  47. {
  48. boolean caseSensitive = !(item instanceof MethodDeclaration);
  49. if (item.toString().regionMatches(caseSensitive, 0, word, 0, word.length()))
  50. {
  51. if (!items.contains(item))
  52. {
  53. items.add(item);
  54. }
  55. }
  56. } //}}}
  57. //{{{ getRenderer() method
  58. @Override
  59. public ListCellRenderer getRenderer()
  60. {
  61. return new PHPItemCellRenderer();
  62. } //}}}
  63. //{{{ getItemsCount()
  64. public int getItemsCount()
  65. {
  66. return items.size();
  67. } //}}}
  68. //{{{ addOutlineableList() method
  69. public void addOutlineableList(List items, String word)
  70. {
  71. for (int i = 0; i < items.size(); i++)
  72. {
  73. addItem(items.get(i), word);
  74. }
  75. } //}}}
  76. //{{{ insert()
  77. @Override
  78. public void insert(int index)
  79. {
  80. Object object = items.get(index);
  81. int caret = textArea.getCaretPosition();
  82. if (text.length() != 0)
  83. {
  84. Selection selection = textArea.getSelectionAtOffset(caret);
  85. if (selection == null)
  86. {
  87. selection = new Selection.Range(caret - text.length(), caret);
  88. }
  89. else
  90. {
  91. int start = selection.getStart();
  92. int end = selection.getEnd();
  93. selection = new Selection.Range(start - text.length(), end);
  94. }
  95. textArea.setSelection(selection);
  96. }
  97. String insertText;
  98. if (object instanceof Outlineable)
  99. {
  100. insertText = ((Outlineable) object).getName();
  101. if (object instanceof MethodDeclaration ||
  102. (object instanceof ClassDeclaration &&
  103. "new".equals(lastWord)))
  104. {
  105. insertText += "()";
  106. caret--; //to go between the parenthesis
  107. }
  108. }
  109. else if (object instanceof ClassHeader)
  110. {
  111. insertText = ((ClassHeader) object).getName();
  112. if ("new".equals(lastWord))
  113. {
  114. insertText += "()";
  115. caret--; //to go between the parenthesis
  116. }
  117. }
  118. else if (object instanceof MethodHeader)
  119. {
  120. insertText = ((MethodHeader) object).getName();
  121. }
  122. else
  123. {
  124. insertText = (String) object;
  125. }
  126. caret += insertText.length();
  127. textArea.setSelectedText(insertText);
  128. // textArea.setCaretPosition(caret);
  129. } //}}}
  130. //{{{ getTokenLength() method
  131. @Override
  132. public int getTokenLength()
  133. {
  134. return text.length();
  135. } //}}}
  136. //{{{ handleKeystroke() method
  137. @Override
  138. public boolean handleKeystroke(int selectedIndex, char keyChar)
  139. {
  140. if (keyChar == '\n' || keyChar == ' ' || keyChar == '\t')
  141. {
  142. insert(selectedIndex);
  143. if (keyChar == ' ')
  144. {
  145. //inserting the space after the insertion
  146. textArea.userInput(' ');
  147. }
  148. else if (keyChar == '\t')
  149. {
  150. //removing the end of the word
  151. textArea.deleteWord();
  152. }
  153. return false;
  154. }
  155. else
  156. {
  157. textArea.userInput(keyChar);
  158. return true;
  159. }
  160. } //}}}
  161. }