PageRenderTime 67ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/buffer/IndentFoldHandler.java

#
Java | 136 lines | 68 code | 9 blank | 59 comment | 10 complexity | 2535a1190463d89eef356749bacfe855 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. * IndentFoldHandler.java - Indent-based fold handler
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2001, 2002 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 java.util.ArrayList;
  24. import java.util.List;
  25. import javax.swing.text.Segment;
  26. /**
  27. * A fold handler that folds lines based on their indent level.
  28. * @author Slava Pestov
  29. * @version $Id: IndentFoldHandler.java 16133 2009-08-31 21:07:15Z shlomy $
  30. * @since jEdit 4.0pre1
  31. */
  32. public class IndentFoldHandler extends FoldHandler
  33. {
  34. public IndentFoldHandler()
  35. {
  36. super("indent");
  37. }
  38. // Returns the width of leading whitespace in the given segment
  39. // if it contains non-whitespace characters, or (-1) otherwise.
  40. private int getLeadingWhitespaceWidth(Segment seg, int tabSize)
  41. {
  42. int offset = seg.offset;
  43. int count = seg.count;
  44. int whitespace = 0;
  45. for(int i = 0; i < count; i++)
  46. {
  47. switch(seg.array[offset + i])
  48. {
  49. case ' ':
  50. whitespace++;
  51. break;
  52. case '\t':
  53. whitespace += (tabSize - whitespace % tabSize);
  54. break;
  55. default:
  56. return whitespace;
  57. }
  58. }
  59. return (-1);
  60. }
  61. //{{{ getFoldLevel() method
  62. /**
  63. * Returns the fold level of the specified line. For a whitespace-only
  64. * line, returns the fold level of the next non-whitespace line, or
  65. * the level of the previous line if no non-whitespace line follows or if
  66. * the level of the previous line is higher.
  67. * @param buffer The buffer in question
  68. * @param lineIndex The line index
  69. * @param seg A segment the fold handler can use to obtain any
  70. * text from the buffer, if necessary
  71. * @return The fold level of the specified line
  72. * @since jEdit 4.0pre1
  73. */
  74. public int getFoldLevel(JEditBuffer buffer, int lineIndex, Segment seg)
  75. {
  76. int tabSize = buffer.getTabSize();
  77. // Look for first non-whitespace line starting at lineIndex
  78. int prevLevel = 0;
  79. for (int index = lineIndex; index < buffer.getLineCount(); index++)
  80. {
  81. buffer.getLineText(index,seg);
  82. int whitespace = getLeadingWhitespaceWidth(seg,tabSize);
  83. if(whitespace >= 0) // Non-whitespace found on line
  84. return (whitespace > prevLevel) ? whitespace : prevLevel;
  85. if(index == 0)
  86. return 0;
  87. if(index == lineIndex)
  88. prevLevel = buffer.getFoldLevel(lineIndex - 1);
  89. }
  90. // All lines from lineIndex are whitespace-only - use fold
  91. // level of previous line.
  92. return prevLevel;
  93. } //}}}
  94. //{{{ getPrecedingFoldLevels() method
  95. /**
  96. * Returns the fold levels of the lines preceding the specified line,
  97. * which depend on the specified line.
  98. * @param buffer The buffer in question
  99. * @param lineIndex The line index
  100. * @param seg A segment the fold handler can use to obtain any
  101. * @param lineFoldLevel The fold level of the specified line
  102. * @return The fold levels of the preceding lines, in decreasing line
  103. * number order (i.e. bottomost line first).
  104. * @since jEdit 4.3pre18
  105. */
  106. public List<Integer> getPrecedingFoldLevels(JEditBuffer buffer,
  107. int lineIndex, Segment seg, int lineFoldLevel)
  108. {
  109. List<Integer> precedingFoldLevels = new ArrayList<Integer>();
  110. int tabSize = buffer.getTabSize();
  111. int whitespace = 0;
  112. int index;
  113. // Find previous non-whitespace-only line
  114. for (index = lineIndex - 1; index > 0; index--)
  115. {
  116. buffer.getLineText(index,seg);
  117. whitespace = getLeadingWhitespaceWidth(seg,tabSize);
  118. if (whitespace >= 0)
  119. break;
  120. }
  121. int max = (lineFoldLevel > whitespace) ? lineFoldLevel : whitespace;
  122. for (index++; index < lineIndex; index++)
  123. precedingFoldLevels.add(Integer.valueOf(max));
  124. return precedingFoldLevels;
  125. }
  126. //}}}
  127. }