PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/jEdit/tags/jedit-4-3-pre5/org/gjt/sp/jedit/buffer/ExplicitFoldHandler.java

#
Java | 99 lines | 51 code | 8 blank | 40 comment | 8 complexity | c32f1f32da0dd879c5d0beb2be23fb15 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. * 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. /**
  25. * A fold handler that folds lines based on markers ("{{{" and "}}}")
  26. * embedded in the text.
  27. *
  28. * @author Slava Pestov
  29. * @version $Id: ExplicitFoldHandler.java 5339 2006-01-25 23:12:07Z spestov $
  30. * @since jEdit 4.0pre1
  31. */
  32. public class ExplicitFoldHandler extends FoldHandler
  33. {
  34. //{{{ ExplicitFoldHandler constructor
  35. public ExplicitFoldHandler()
  36. {
  37. super("explicit");
  38. } //}}}
  39. //{{{ getFoldLevel() method
  40. /**
  41. * Returns the fold level of the specified line.
  42. * @param buffer The buffer in question
  43. * @param lineIndex The line index
  44. * @param seg A segment the fold handler can use to obtain any
  45. * text from the buffer, if necessary
  46. * @return The fold level of the specified line
  47. * @since jEdit 4.0pre1
  48. */
  49. public int getFoldLevel(JEditBuffer buffer, int lineIndex, Segment seg)
  50. {
  51. if(lineIndex == 0)
  52. return 0;
  53. else
  54. {
  55. int foldLevel = buffer.getFoldLevel(lineIndex - 1);
  56. buffer.getLineText(lineIndex - 1,seg);
  57. int offset = seg.offset;
  58. int count = seg.count;
  59. int openingBrackets = 0, closingBrackets = 0;
  60. for(int i = 0; i < count; i++)
  61. {
  62. switch(seg.array[offset + i])
  63. {
  64. case '{':
  65. closingBrackets = 0;
  66. openingBrackets++;
  67. if(openingBrackets == 3)
  68. {
  69. foldLevel++;
  70. openingBrackets = 0;
  71. }
  72. break;
  73. case '}':
  74. openingBrackets = 0;
  75. closingBrackets++;
  76. if(closingBrackets == 3)
  77. {
  78. if(foldLevel > 0)
  79. foldLevel--;
  80. closingBrackets = 0;
  81. }
  82. break;
  83. default:
  84. closingBrackets = openingBrackets = 0;
  85. break;
  86. }
  87. }
  88. return foldLevel;
  89. }
  90. } //}}}
  91. }