/plugins/XML/tags/release-2-0-8/sidekick/css/CompletionRequest.java

# · Java · 218 lines · 119 code · 40 blank · 59 comment · 20 complexity · aedccc0c262dc9eeb61057931bb7aa2c MD5 · raw file

  1. /**
  2. * CompletionRequest.java
  3. * :folding=explicit:collapseFolds=1:
  4. *
  5. * Copyright (C) 2006 Jakub Roztocil
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or any later version.
  11. *
  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 sidekick.css;
  22. //{{{ Imports
  23. import java.util.*;
  24. import java.util.regex.*;
  25. import org.gjt.sp.jedit.*;
  26. import org.gjt.sp.jedit.buffer.JEditBuffer;
  27. import org.gjt.sp.util.Log;
  28. import sidekick.*;
  29. //}}}
  30. public class CompletionRequest {
  31. //{{{ CompletionRequest constructor
  32. public CompletionRequest(EditPane editPane, int caret) {
  33. if (!CssSideKickCompletion.initialized()) {
  34. CssSideKickCompletion.initialize();
  35. }
  36. completionList = new ArrayList();
  37. buffer = editPane.getBuffer();
  38. textBeforeCaret = buffer.getText(0, caret);
  39. word = getWord(textBeforeCaret);
  40. // Log.log(Log.DEBUG, CompletionRequest.class, "[" + textBeforeCaret + "]");
  41. }
  42. //}}}
  43. //{{{ getSideKickCompletion() method
  44. public SideKickCompletion getSideKickCompletion() {
  45. boolean selectedProperty = false;
  46. if (!canComplete(textBeforeCaret)) {
  47. return null;
  48. }
  49. if (canAddAllUnits(textBeforeCaret)) {
  50. // user is typing number
  51. word = "";
  52. addMatchedToCompletionList(CssSideKickCompletion.getCssUnits());
  53. } else if (canCompleteUnit(word)) {
  54. // user is typing unit for number
  55. word = getUnitStart(word);
  56. addMatchedToCompletionList(CssSideKickCompletion.getCssUnits(), word);
  57. } else if (canCompleteCssValues(textBeforeCaret)) {
  58. // user has typed property
  59. String currProperty = getCurrentCssProperty(textBeforeCaret);
  60. ArrayList values = getPropertyValues(currProperty);
  61. if (word.length() == 0) {
  62. // and has not yet typed start of value, show him all possible values
  63. addMatchedToCompletionList(values);
  64. } else {
  65. // started type value, show matched
  66. addMatchedToCompletionList(values, word);
  67. }
  68. } else {
  69. selectedProperty = true;
  70. addMatchedToCompletionList(findMatchedProperties(word));
  71. }
  72. if (completionList.size() > 0) {
  73. return new CssSideKickCompletion(completionList, word, selectedProperty);
  74. }
  75. return null;
  76. } //}}}
  77. //{{{ Private members
  78. private List completionList;
  79. private JEditBuffer buffer;
  80. private String textBeforeCaret;
  81. private String word;
  82. // {{{ Static fields
  83. private static ArrayList emptyArrayList = new ArrayList(0);
  84. // patterns
  85. private static Pattern CURR_PROP = Pattern.compile("([\\w-]+):[^:]*$");
  86. private static Pattern GET_WORD = Pattern.compile("[^\\s:;{]*$");
  87. private static Pattern CAN_COMPLETE = Pattern.compile("[{;][^}]*([\\w-]|:\\s+)$");
  88. private static Pattern CAN_COMPLETE_VALUES = Pattern.compile(":[^;}]*$");
  89. private static Pattern CAN_COMPLETE_UNITS = Pattern.compile("^\\d+[a-z]+$");
  90. private static Pattern CAN_COMPLETE_ALL_UNITS = Pattern.compile("[;:{\\s.]\\d+$");
  91. private static Pattern UNIT_START = Pattern.compile("[a-z]+$");
  92. // }}}
  93. //{{{ findMatchedProperties() method
  94. private ArrayList findMatchedProperties(String startsWith) {
  95. ArrayList found = new ArrayList();
  96. Iterator it = CssSideKickCompletion.getCssProperties().keySet().iterator();
  97. while (it.hasNext()) {
  98. String n = (String) it.next();
  99. if (!startsWith.equals(n) && n.startsWith(startsWith)) {
  100. found.add(n);
  101. }
  102. }
  103. Collections.sort(found);
  104. return found;
  105. } //}}}
  106. //{{{ addMatchedToCompletionList() method
  107. private void addMatchedToCompletionList(ArrayList cantidateCompletions, String startsWith) {
  108. Iterator it = cantidateCompletions.iterator();
  109. String value;
  110. while (it.hasNext()) {
  111. value = (String)it.next();
  112. if (value.length() > startsWith.length() && value.startsWith(startsWith)) {
  113. completionList.add(value);
  114. }
  115. }
  116. }
  117. //}}}
  118. //{{{ addMatchedToCompletionList() method
  119. private void addMatchedToCompletionList(ArrayList completions) {
  120. Iterator it = completions.iterator();
  121. while (it.hasNext()) {
  122. completionList.add((String)it.next());
  123. }
  124. }
  125. //}}}
  126. //{{{ getPropertyValues() method
  127. private ArrayList getPropertyValues(String property) {
  128. if (CssSideKickCompletion.getCssProperties().containsKey(property)) {
  129. return (ArrayList)CssSideKickCompletion.getCssProperties().get(property);
  130. }
  131. return emptyArrayList;
  132. }
  133. //}}}
  134. //{{{ getWord() method
  135. private String getWord(String text) {
  136. Matcher wordM = GET_WORD.matcher(text);
  137. wordM.find();
  138. // Log.log(Log.DEBUG, CompletionRequest.class, "Word: '" + wordM.group(0) + "'");
  139. return wordM.group(0);
  140. }
  141. //}}}
  142. //{{{ getUnitStart() method
  143. private String getUnitStart(String word) {
  144. Matcher m = UNIT_START.matcher(word);
  145. m.find();
  146. return m.group(0);
  147. }
  148. //}}}
  149. //{{{ getCurrentCssProperty() method
  150. private String getCurrentCssProperty(String text) {
  151. Matcher m = CURR_PROP.matcher(text);
  152. m.find();
  153. // Log.log(Log.DEBUG, CompletionRequest.class, "getCurrentCssProperty() = '" + m.group(1) + "'");
  154. return m.group(1);
  155. }
  156. //}}}
  157. //{{{ canComplete() method
  158. private boolean canComplete(String text) {
  159. return CAN_COMPLETE.matcher(text).find();
  160. }
  161. //}}}
  162. //{{{ canCompleteCssValues() method
  163. private boolean canCompleteCssValues(String text) {
  164. return CAN_COMPLETE_VALUES.matcher(text).find();
  165. }
  166. //}}}
  167. //{{{ canAddAllUnits() method
  168. private boolean canAddAllUnits(String text) {
  169. return CAN_COMPLETE_ALL_UNITS.matcher(text).find();
  170. }
  171. //}}}
  172. //{{{ canCompleteUnit() method
  173. private boolean canCompleteUnit(String word) {
  174. return CAN_COMPLETE_UNITS.matcher(word).find();
  175. }
  176. //}}}
  177. //}}}
  178. }