/jEdit/tags/before-fast-scroll/org/gjt/sp/jedit/buffer/ExplicitFoldHandler.java

# · Java · 100 lines · 52 code · 8 blank · 40 comment · 8 complexity · 1ab3d63433f0d889a52a7bc2412929ea MD5 · raw file

  1. /*
  2. * ExplicitFoldHandler.java - Explicit fold handler
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2001 Slava Pestov
  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. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. package org.gjt.sp.jedit.buffer;
  23. import javax.swing.text.Segment;
  24. import org.gjt.sp.jedit.Buffer;
  25. /**
  26. * A fold handler that folds lines based on markers ("{{{" and "}}}")
  27. * embedded in the text.
  28. *
  29. * @author Slava Pestov
  30. * @version $Id: ExplicitFoldHandler.java 4918 2003-11-18 20:51:58Z spestov $
  31. * @since jEdit 4.0pre1
  32. */
  33. public class ExplicitFoldHandler extends FoldHandler
  34. {
  35. //{{{ ExplicitFoldHandler constructor
  36. public ExplicitFoldHandler()
  37. {
  38. super("explicit");
  39. } //}}}
  40. //{{{ getFoldLevel() method
  41. /**
  42. * Returns the fold level of the specified line.
  43. * @param buffer The buffer in question
  44. * @param lineIndex The line index
  45. * @param seg A segment the fold handler can use to obtain any
  46. * text from the buffer, if necessary
  47. * @return The fold level of the specified line
  48. * @since jEdit 4.0pre1
  49. */
  50. public int getFoldLevel(Buffer buffer, int lineIndex, Segment seg)
  51. {
  52. if(lineIndex == 0)
  53. return 0;
  54. else
  55. {
  56. int foldLevel = buffer.getFoldLevel(lineIndex - 1);
  57. buffer.getLineText(lineIndex - 1,seg);
  58. int offset = seg.offset;
  59. int count = seg.count;
  60. int openingBrackets = 0, closingBrackets = 0;
  61. for(int i = 0; i < count; i++)
  62. {
  63. switch(seg.array[offset + i])
  64. {
  65. case '{':
  66. closingBrackets = 0;
  67. openingBrackets++;
  68. if(openingBrackets == 3)
  69. {
  70. foldLevel++;
  71. openingBrackets = 0;
  72. }
  73. break;
  74. case '}':
  75. openingBrackets = 0;
  76. closingBrackets++;
  77. if(closingBrackets == 3)
  78. {
  79. if(foldLevel > 0)
  80. foldLevel--;
  81. closingBrackets = 0;
  82. }
  83. break;
  84. default:
  85. closingBrackets = openingBrackets = 0;
  86. break;
  87. }
  88. }
  89. return foldLevel;
  90. }
  91. } //}}}
  92. }