01: /*
02: * RegexpIndentRule.java
03: * :tabSize=8:indentSize=8:noTabs=false:
04: * :folding=explicit:collapseFolds=1:
05: *
06: * Copyright (C) 2005 Slava Pestov
07: *
08: * This program is free software; you can redistribute it and/or
09: * 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;
26: import java.util.regex.Matcher;
27: import java.util.regex.Pattern;
28: import java.util.regex.PatternSyntaxException;
29:
30: import org.gjt.sp.jedit.buffer.JEditBuffer;
31:
32: /**
33: * @author Slava Pestov
34: * @version $Id: RegexpIndentRule.java 9006 2007-02-22 05:09:45Z vanza $
35: */
36: public class RegexpIndentRule implements IndentRule {
37: //{{{ RegexpIndentRule constructor
38: /**
39: * @param collapse If true, then if the next indent rule is
40: * an opening bracket, this rule will not increase indent.
41: */
42: public RegexpIndentRule(String regexp, IndentAction prevPrev,
43: IndentAction prev, IndentAction this Line, boolean collapse)
44: throws PatternSyntaxException {
45: prevPrevAction = prevPrev;
46: prevAction = prev;
47: this Action = this Line;
48: this .regexp = Pattern.compile(regexp, Pattern.CASE_INSENSITIVE);
49: this .collapse = collapse;
50: } //}}}
51:
52: //{{{ apply() method
53: public void apply(JEditBuffer buffer, int this LineIndex,
54: int prevLineIndex, int prevPrevLineIndex,
55: List<IndentAction> indentActions) {
56: if (this Action != null
57: && isMatch(buffer.getLineText(this LineIndex))) {
58: indentActions.add(this Action);
59: }
60: if (prevAction != null && prevLineIndex != -1
61: && isMatch(buffer.getLineText(prevLineIndex))) {
62: indentActions.add(prevAction);
63: if (collapse)
64: indentActions.add(IndentAction.PrevCollapse);
65: }
66: if (prevPrevAction != null && prevPrevLineIndex != -1
67: && isMatch(buffer.getLineText(prevPrevLineIndex))) {
68: indentActions.add(prevPrevAction);
69: if (collapse)
70: indentActions.add(IndentAction.PrevPrevCollapse);
71: }
72: } //}}}
73:
74: //{{{ isMatch() method
75: public boolean isMatch(String line) {
76: Matcher m = regexp.matcher(line);
77: // return regexp.isMatch(line);
78: return m.matches();
79: } //}}}
80:
81: //{{{ toString() method
82: public String toString() {
83: return getClass().getName() + '[' + regexp + ']';
84: } //}}}
85:
86: private IndentAction prevPrevAction, prevAction, this Action;
87: private Pattern regexp;
88: private boolean collapse;
89: }
|