1 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 36 public class RegexpIndentRule implements IndentRule 37 { 38 43 public RegexpIndentRule(String regexp, IndentAction prevPrev, 44 IndentAction prev, IndentAction thisLine, boolean collapse) 45 throws PatternSyntaxException 46 { 47 prevPrevAction = prevPrev; 48 prevAction = prev; 49 thisAction = thisLine; 50 this.regexp = Pattern.compile(regexp, Pattern.CASE_INSENSITIVE ); 51 this.collapse = collapse; 52 } 54 public void apply(JEditBuffer buffer, int thisLineIndex, 56 int prevLineIndex, int prevPrevLineIndex, 57 List <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 } 86 public boolean isMatch(String line) 88 { 89 Matcher m = regexp.matcher(line); 90 return m.matches(); 92 } 94 public String toString() 96 { 97 return getClass().getName() + '[' + regexp + ']'; 98 } 100 private IndentAction prevPrevAction, prevAction, thisAction; 101 private Pattern regexp; 102 private boolean collapse; 103 } 104 | Popular Tags |