PageRenderTime 156ms CodeModel.GetById 151ms app.highlight 3ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-3-pre5/org/gjt/sp/jedit/indent/BracketIndentRule.java

#
Java | 85 lines | 49 code | 9 blank | 27 comment | 10 complexity | 5ea291280013af1639839be69c1bf26e MD5 | raw file
 1/*
 2 * BracketIndentRule.java
 3 * :tabSize=8:indentSize=8:noTabs=false:
 4 * :folding=explicit:collapseFolds=1:
 5 *
 6 * Copyright (C) 2005 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.indent;
24
25import org.gjt.sp.jedit.Buffer;
26
27public abstract class BracketIndentRule implements IndentRule
28{
29	//{{{ BracketIndentRule constructor
30	public BracketIndentRule(char openBracket, char closeBracket)
31	{
32		this.openBracket = openBracket;
33		this.closeBracket = closeBracket;
34	} //}}}
35
36	//{{{ Brackets class
37	public static class Brackets
38	{
39		int openCount;
40		int closeCount;
41	} //}}}
42
43	//{{{ getBrackets() method
44	public Brackets getBrackets(String line)
45	{
46		Brackets brackets = new Brackets();
47
48		for(int i = 0; i < line.length(); i++)
49		{
50			char ch = line.charAt(i);
51			if(ch == openBracket)
52			{
53				/* Don't increase indent when we see
54				an explicit fold. */
55				if(line.length() - i >= 3)
56				{
57					if(line.substring(i,i+3).equals("{{{")) /* }}} */
58					{
59						i += 2;
60						continue;
61					}
62				}
63				brackets.openCount++;
64			}
65			else if(ch == closeBracket)
66			{
67				if(brackets.openCount != 0)
68					brackets.openCount--;
69				else
70					brackets.closeCount++;
71			}
72		}
73
74		return brackets;
75	} //}}}
76
77	//{{{ toString() method
78	public String toString()
79	{
80		return getClass().getName() + "[" + openBracket + ","
81			+ closeBracket + "]";
82	} //}}}
83
84	protected char openBracket, closeBracket;
85}