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