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

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

#
Java | 85 lines | 39 code | 8 blank | 38 comment | 5 complexity | a187583a2f553c70fc904f144d1b8826 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 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 3996 2002-01-26 01:36:25Z spestov $
  29. * @since jEdit 4.0pre1
  30. */
  31. public class IndentFoldHandler implements FoldHandler
  32. {
  33. //{{{ getFoldLevel() method
  34. /**
  35. * Returns the fold level of the specified line.
  36. * @param buffer The buffer in question
  37. * @param lineIndex The line index
  38. * @param seg A segment the fold handler can use to obtain any
  39. * text from the buffer, if necessary
  40. * @return The fold level of the specified line
  41. * @since jEdit 4.0pre1
  42. */
  43. public int getFoldLevel(Buffer buffer, int lineIndex, Segment seg)
  44. {
  45. int tabSize = buffer.getTabSize();
  46. buffer.getLineText(lineIndex,seg);
  47. int offset = seg.offset;
  48. int count = seg.count;
  49. int whitespace = 0;
  50. if(count == 0)
  51. {
  52. // empty line. inherit previous line's fold level
  53. if(lineIndex != 0)
  54. whitespace = buffer.getFoldLevel(lineIndex - 1);
  55. else
  56. whitespace = 0;
  57. }
  58. else
  59. {
  60. loop: for(int i = 0; i < count; i++)
  61. {
  62. switch(seg.array[offset + i])
  63. {
  64. case ' ':
  65. whitespace++;
  66. break;
  67. case '\t':
  68. whitespace += (tabSize - whitespace % tabSize);
  69. break;
  70. default:
  71. break loop;
  72. }
  73. }
  74. }
  75. return whitespace;
  76. } //}}}
  77. }