KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > indent > BracketIndentRule


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
23 package org.gjt.sp.jedit.indent;
24
25 /**
26  * @author Slava Pestov
27  * @version $Id: BracketIndentRule.java 5579 2006-07-13 10:53:15Z kpouer $
28  */

29 public abstract class BracketIndentRule implements IndentRule
30 {
31     //{{{ BracketIndentRule constructor
32
public BracketIndentRule(char openBracket, char closeBracket)
33     {
34         this.openBracket = openBracket;
35         this.closeBracket = closeBracket;
36     } //}}}
37

38     //{{{ Brackets class
39
public static class Brackets
40     {
41         int openCount;
42         int closeCount;
43     } //}}}
44

45     //{{{ getBrackets() method
46
public Brackets getBrackets(String JavaDoc line)
47     {
48         Brackets brackets = new Brackets();
49
50         for(int i = 0; i < line.length(); i++)
51         {
52             char ch = line.charAt(i);
53             if(ch == openBracket)
54             {
55                 /* Don't increase indent when we see
56                 an explicit fold. */

57                 if(line.length() - i >= 3)
58                 {
59                     if(line.substring(i,i+3).equals("{{{")) /* }}} */
60                     {
61                         i += 2;
62                         continue;
63                     }
64                 }
65                 brackets.openCount++;
66             }
67             else if(ch == closeBracket)
68             {
69                 if(brackets.openCount != 0)
70                     brackets.openCount--;
71                 else
72                     brackets.closeCount++;
73             }
74         }
75
76         return brackets;
77     } //}}}
78

79     //{{{ toString() method
80
public String JavaDoc toString()
81     {
82         return getClass().getName() + "[" + openBracket + ","
83             + closeBracket + "]";
84     } //}}}
85

86     protected char openBracket, closeBracket;
87 }
88
Popular Tags