PageRenderTime 79ms CodeModel.GetById 75ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/indent/WhitespaceRule.java

#
Java | 80 lines | 32 code | 8 blank | 40 comment | 6 complexity | 5cbd1ed845372e5bd12f5f615f83fee9 MD5 | raw file
 1/*
 2 * WhitespaceRule.java
 3 * :tabSize=8:indentSize=8:noTabs=false:
 4 * :folding=explicit:collapseFolds=1:
 5 *
 6 * Copyright (C) 2007 Marcelo Vanzin
 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.indent;
24
25import java.util.List;
26
27import org.gjt.sp.jedit.buffer.JEditBuffer;
28
29/**
30 * Whitespace rule. This rule cancels all indent actions in the
31 * following case:
32 *
33 * <ul>
34 * <li>The previous line is all whitespace</li>
35 * <li>The current line is not empty</li>
36 * </ul>
37 *
38 * <p>The result is that this rule won't allow the indentation to be
39 * increased, only decreased (by rules triggered by unindentThisLine).
40 * If the requirements above do not apply, this rule does nothing.</p>
41 *
42 * @author Marcelo Vanzin
43 * @version $Id: WhitespaceRule.java 15614 2009-06-30 07:21:56Z voituk $
44 * @since jEdit 4.3pre10
45 */
46public class WhitespaceRule implements IndentRule
47{
48
49	public void apply(JEditBuffer buffer, int thisLineIndex,
50			  int prevLineIndex, int prevPrevLineIndex,
51			  List<IndentAction> indentActions)
52	{
53		/* Don't apply this rule if the current line is empty. */
54		CharSequence current = buffer.getLineSegment(thisLineIndex);
55		boolean found = false;
56		for (int i = 0; i < current.length(); i++)
57		{
58			if (!Character.isWhitespace(current.charAt(i)))
59			{
60				found = true;
61				break;
62			}
63		}
64		if (!found)
65			return;
66
67		/* Check if the previous line is empty. */
68		if (prevLineIndex >= 0) {
69			CharSequence previous = buffer.getLineSegment(prevLineIndex);
70			for (int i = 0; i < previous.length(); i++)
71			{
72				if (!Character.isWhitespace(previous.charAt(i)))
73					return;
74			}
75		}
76		indentActions.add(new IndentAction.NoIncrease());
77	}
78
79}
80