1 22 23 package org.gjt.sp.jedit.indent; 24 25 import org.gjt.sp.jedit.TextUtilities; 26 import org.gjt.sp.jedit.buffer.JEditBuffer; 27 28 import java.util.List ; 29 30 36 public class DeepIndentRule implements IndentRule 37 { 38 47 private static Parens getLastParens(String s, int pos) 48 { 49 int lastClose; 50 int lastOpen; 51 if (pos == -1) 52 { 53 lastClose = s.lastIndexOf(')'); 54 lastOpen = s.lastIndexOf('('); 55 } 56 else 57 { 58 lastClose = s.lastIndexOf(')', pos); 59 lastOpen = s.lastIndexOf('(', pos); 60 } 61 return new Parens(lastOpen, lastClose); 62 } 64 public void apply(JEditBuffer buffer, int thisLineIndex, 66 int prevLineIndex, int prevPrevLineIndex, 67 List <IndentAction> indentActions) 68 { 69 if (prevLineIndex == -1) 70 return; 71 72 int lineIndex = prevLineIndex; 73 int oldLineIndex = lineIndex; 74 String lineText = buffer.getLineText(lineIndex); 75 int searchPos = -1; 76 while (true) 77 { 78 if (lineIndex != oldLineIndex) 79 { 80 lineText = buffer.getLineText(lineIndex); 81 oldLineIndex = lineIndex; 82 } 83 Parens parens = getLastParens(lineText, searchPos); 84 if (parens.openOffset > parens.closeOffset) 85 { 86 int indent = parens.openOffset + TextUtilities.tabsToSpaces(lineText, buffer.getTabSize()).length() - lineText.length(); 88 indentActions.add(new IndentAction.AlignParameter(indent, lineText)); 89 return; 90 } 91 92 if (parens.openOffset == -1 && parens.closeOffset == -1) 94 { 95 return; 96 } 97 int openParenOffset = TextUtilities.findMatchingBracket(buffer, lineIndex, parens.closeOffset); 98 if (openParenOffset >= 0) 99 { 100 lineIndex = buffer.getLineOfOffset(openParenOffset); 101 searchPos = openParenOffset - buffer.getLineStartOffset(lineIndex) - 1; 102 if (searchPos < 0) 103 break; 104 } 105 else 106 break; 107 } 108 } 110 private static class Parens 112 { 113 final int openOffset; 114 final int closeOffset; 115 116 Parens(int openOffset, int closeOffset) 117 { 118 this.openOffset = openOffset; 119 this.closeOffset = closeOffset; 120 } 121 122 @Override 123 public String toString() 124 { 125 return "Parens(" + openOffset + ',' + closeOffset + ')'; 126 } 127 } } 129 130 | Popular Tags |