PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/bundles/plugins-trunk/XML/sidekick/css/CssSideKickParser.java

#
Java | 127 lines | 58 code | 27 blank | 42 comment | 4 complexity | 4fc9aea3cf6bb1d1e32d675c772b8b75 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /**
  2. * CssSideKickParser.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 errorlist.*;
  24. import java.util.regex.*;
  25. import javax.swing.tree.*;
  26. import org.gjt.sp.jedit.*;
  27. import org.gjt.sp.util.Log;
  28. import sidekick.*;
  29. import sidekick.SideKickParsedData;
  30. import sidekick.SideKickParser;
  31. import sidekick.enhanced.*;
  32. //}}}
  33. /**
  34. * @deprecated use CSS2SideKickParser instead.
  35. */
  36. public class CssSideKickParser extends SideKickParser {
  37. Pattern selRe = Pattern.compile("((^|[}{])\\s*)+(.+?)(\\s*\\{)", Pattern.DOTALL);
  38. Pattern commentRe = Pattern.compile("\\s*\\/\\*.*?\\*\\/\\s*|\n", Pattern.DOTALL);
  39. String text;
  40. //{{{ CssSideKickParser constructor
  41. public CssSideKickParser() {
  42. super("css");
  43. } //}}}
  44. //{{{ parse() method
  45. public SideKickParsedData parse(Buffer buffer, DefaultErrorSource errorSource) {
  46. int startPos;
  47. int endPos;
  48. Matcher selMatcher; // matcher for selectors
  49. Matcher commentMatcher; // matcher for coments
  50. String[] tlc; // top level commads - selectors, @rules
  51. SideKickParsedData data = new SideKickParsedData(buffer.getName());
  52. text = buffer.getText(0, buffer.getLength() -1);
  53. selMatcher = selRe.matcher(text);
  54. // selMatcher should find only selectors, but if there are some
  55. // at-rules like @import or @media, they are matched also.
  56. // @imports are almost OK, but for all at-rules is startPos incorrect
  57. // and first selector inside @media rule is not shown
  58. // TODO: correct parsing of at-rulles
  59. while (selMatcher.find()) {
  60. // Start position
  61. startPos = selMatcher.start() + selMatcher.group(1).length() + selMatcher.group(3).length();
  62. // Remove comments and redundant white spaces from selector list:
  63. // div#menu \n /* this is cool selector */ ul li ===> div#menu ul li
  64. commentMatcher = commentRe.matcher(selMatcher.group(3));
  65. tlc = commentMatcher.replaceAll(" ").trim().split("\\s*;\\s*");
  66. // Log.log(Log.DEBUG, CssSideKickParser.class, tlc);
  67. for (int i = 0; i < tlc.length; i++) {
  68. endPos = selMatcher.end() + buffer.getText(selMatcher.end(), buffer.getLength() - 1 - selMatcher.end()).indexOf("}");
  69. if (endPos == -1) {
  70. endPos = buffer.getLength() -1;
  71. }
  72. // Log.log(Log.DEBUG, CssSideKickParser.class, "Start: " + startPos + " End: " + endPos);
  73. SourceAsset asset = new SourceAsset(tlc[i], getLineNo(startPos), buffer.createPosition(startPos));
  74. IAsset iasset = (IAsset) asset;
  75. // Log.log(Log.DEBUG, CssSideKickParser.class, iasset);
  76. iasset.setStart(buffer.createPosition(startPos));
  77. iasset.setEnd(buffer.createPosition(endPos));
  78. data.root.add(new DefaultMutableTreeNode(iasset));
  79. }
  80. }
  81. return data;
  82. } //}}}
  83. //{{{ getLineNo() method
  84. public int getLineNo(int start) {
  85. return text.substring(0, start).split("\n").length;
  86. } //}}}
  87. //{{{ supportsCompletion() method
  88. public boolean supportsCompletion() {
  89. return true;
  90. } //}}}
  91. //{{{ canCompleteAnywhere() method
  92. public boolean canCompleteAnywhere() {
  93. return true;
  94. } //}}}
  95. //{{{ complete() method
  96. public SideKickCompletion complete(EditPane editPane, int caret) {
  97. CompletionRequest cr = new CompletionRequest(editPane, caret);
  98. return cr.getSideKickCompletion();
  99. } //}}}
  100. }