KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * RegexpIndentRule.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 import java.util.List JavaDoc;
26 import java.util.regex.Matcher JavaDoc;
27 import java.util.regex.Pattern JavaDoc;
28 import java.util.regex.PatternSyntaxException JavaDoc;
29
30 import org.gjt.sp.jedit.buffer.JEditBuffer;
31
32 /**
33  * @author Slava Pestov
34  * @version $Id: RegexpIndentRule.java 5579 2006-07-13 10:53:15Z kpouer $
35  */

36 public class RegexpIndentRule implements IndentRule
37 {
38     //{{{ RegexpIndentRule constructor
39
/**
40      * @param collapse If true, then if the next indent rule is
41      * an opening bracket, this rule will not increase indent.
42      */

43     public RegexpIndentRule(String JavaDoc regexp, IndentAction prevPrev,
44         IndentAction prev, IndentAction thisLine, boolean collapse)
45     throws PatternSyntaxException JavaDoc
46     {
47         prevPrevAction = prevPrev;
48         prevAction = prev;
49         thisAction = thisLine;
50         this.regexp = Pattern.compile(regexp, Pattern.CASE_INSENSITIVE );
51         this.collapse = collapse;
52     } //}}}
53

54     //{{{ apply() method
55
public void apply(JEditBuffer buffer, int thisLineIndex,
56         int prevLineIndex, int prevPrevLineIndex,
57         List JavaDoc<IndentAction> indentActions)
58     {
59         boolean match = false;
60
61         if(thisAction != null
62             && isMatch(buffer.getLineText(thisLineIndex)))
63         {
64             indentActions.add(thisAction);
65             match = true;
66         }
67         if(prevAction != null
68             && prevLineIndex != -1
69             && isMatch(buffer.getLineText(prevLineIndex)))
70         {
71             indentActions.add(prevAction);
72             match = true;
73         }
74         if(prevPrevAction != null
75             && prevPrevLineIndex != -1
76             && isMatch(buffer.getLineText(prevPrevLineIndex)))
77         {
78             indentActions.add(prevPrevAction);
79             match = true;
80         }
81
82         if(match && collapse)
83             indentActions.add(new IndentAction.Collapse());
84     } //}}}
85

86     //{{{ isMatch() method
87
public boolean isMatch(String JavaDoc line)
88     {
89         Matcher JavaDoc m = regexp.matcher(line);
90 // return regexp.isMatch(line);
91
return m.matches();
92     } //}}}
93

94     //{{{ toString() method
95
public String JavaDoc toString()
96     {
97         return getClass().getName() + '[' + regexp + ']';
98     } //}}}
99

100     private IndentAction prevPrevAction, prevAction, thisAction;
101     private Pattern JavaDoc regexp;
102     private boolean collapse;
103 }
104
Popular Tags