PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/org/gjt/sp/jedit/buffer/IndentFoldHandler.java

#
Java | 90 lines | 42 code | 10 blank | 38 comment | 4 complexity | ca257cab1875a706816a4950d85d0659 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 javax.swing.text.Segment;
  24. import org.gjt.sp.jedit.Buffer;
  25. /**
  26. * A fold handler that folds lines based on their indent level.
  27. * @author Slava Pestov
  28. * @version $Id: IndentFoldHandler.java 4092 2002-03-17 01:28:44Z spestov $
  29. * @since jEdit 4.0pre1
  30. */
  31. public class IndentFoldHandler extends FoldHandler
  32. {
  33. public IndentFoldHandler()
  34. {
  35. super("indent");
  36. }
  37. //{{{ getFoldLevel() method
  38. /**
  39. * Returns the fold level of the specified line.
  40. * @param buffer The buffer in question
  41. * @param lineIndex The line index
  42. * @param seg A segment the fold handler can use to obtain any
  43. * text from the buffer, if necessary
  44. * @return The fold level of the specified line
  45. * @since jEdit 4.0pre1
  46. */
  47. public int getFoldLevel(Buffer buffer, int lineIndex, Segment seg)
  48. {
  49. int tabSize = buffer.getTabSize();
  50. buffer.getLineText(lineIndex,seg);
  51. int offset = seg.offset;
  52. int count = seg.count;
  53. int whitespace = 0;
  54. boolean seenNonWhiteSpace = false;
  55. loop: for(int i = 0; i < count; i++)
  56. {
  57. switch(seg.array[offset + i])
  58. {
  59. case ' ':
  60. whitespace++;
  61. break;
  62. case '\t':
  63. whitespace += (tabSize - whitespace % tabSize);
  64. break;
  65. default:
  66. seenNonWhiteSpace = true;
  67. break loop;
  68. }
  69. }
  70. if(!seenNonWhiteSpace)
  71. {
  72. // empty line. inherit previous line's fold level
  73. if(lineIndex != 0)
  74. return buffer.getFoldLevel(lineIndex - 1);
  75. else
  76. return 0;
  77. }
  78. return whitespace;
  79. } //}}}
  80. }