1 19 20 package org.netbeans.editor.ext; 21 22 import javax.swing.text.BadLocationException ; 23 import org.netbeans.editor.Analyzer; 24 import org.netbeans.editor.FinderFactory; 25 import org.netbeans.editor.BaseDocument; 26 import org.netbeans.editor.Utilities; 27 28 34 35 public class ExtFinderFactory { 36 37 41 public static abstract class LineFwdFinder extends FinderFactory.AbstractFinder { 42 43 private int origStartPos; 44 45 private int origLimitPos; 46 47 public LineFwdFinder() { 48 } 49 50 public int adjustStartPos(BaseDocument doc, int startPos) { 51 origStartPos = startPos; 52 try { 53 return Utilities.getRowStart(doc, startPos); 54 } catch (BadLocationException e) { 55 return startPos; 56 } 57 } 58 59 public int adjustLimitPos(BaseDocument doc, int limitPos) { 60 origLimitPos = limitPos; 61 try { 62 return Utilities.getRowEnd(doc, limitPos); 63 } catch (BadLocationException e) { 64 return limitPos; 65 } 66 } 67 68 69 public int find(int bufferStartPos, char buffer[], 70 int offset1, int offset2, int reqPos, int limitPos) { 71 int offset = reqPos - bufferStartPos; while (true) { 73 int lfOffset = Analyzer.findFirstLFOffset(buffer, offset, offset2 - offset); 74 boolean lfFound = (lfOffset >= 0); 75 if (!lfFound) { 76 lfOffset = offset2; 77 } 78 79 int lineOffset = lineFound(buffer, offset, lfOffset, 80 Math.max(origStartPos - bufferStartPos, offset), 81 Math.min(origLimitPos - bufferStartPos, lfOffset)); 82 if (lineOffset >= 0) { 83 found = true; 84 return bufferStartPos + offset + lineOffset; 85 } 86 87 if (lfFound) { 88 offset = lfOffset + 1; } else { 90 break; 91 } 92 } 93 return bufferStartPos + offset2; 94 } 95 96 105 protected abstract int lineFound(char[] buffer, int lineStartOffset, int lineEndOffset, 106 int startOffset, int endOffset); 107 108 } 109 110 114 public static abstract class LineBwdFinder extends FinderFactory.AbstractFinder { 115 116 private int origStartPos; 117 118 private int origLimitPos; 119 120 public LineBwdFinder() { 121 } 122 123 public int adjustStartPos(BaseDocument doc, int startPos) { 124 origStartPos = startPos; 125 try { 126 return Utilities.getRowEnd(doc, startPos); 127 } catch (BadLocationException e) { 128 return startPos; 129 } 130 } 131 132 public int adjustLimitPos(BaseDocument doc, int limitPos) { 133 origLimitPos = limitPos; 134 try { 135 return Utilities.getRowStart(doc, limitPos); 136 } catch (BadLocationException e) { 137 return limitPos; 138 } 139 } 140 141 142 public int find(int bufferStartPos, char buffer[], 143 int offset1, int offset2, int reqPos, int limitPos) { 144 int offset = reqPos - bufferStartPos + 1; while (true) { 146 boolean lfFound = false; 147 int lfOffsetP1 = offset; 148 while (lfOffsetP1 > offset1) { 149 if (buffer[--lfOffsetP1] == '\n') { 150 lfFound = true; 151 lfOffsetP1++; break; 153 } 154 } 155 if (!lfFound) { 156 lfOffsetP1 = offset1; 157 } 158 159 int lineOffset = lineFound(buffer, lfOffsetP1, offset, 160 Math.max(origLimitPos - bufferStartPos, lfOffsetP1), 161 Math.min(origStartPos - bufferStartPos, offset)); 162 if (lineOffset >= 0) { 163 found = true; 164 return bufferStartPos + offset + lineOffset; 165 } 166 167 if (lfFound) { 168 offset = lfOffsetP1 - 1; } else { 170 break; 171 } 172 } 173 return bufferStartPos + offset1 - 1; 174 } 175 176 185 protected abstract int lineFound(char[] buffer, int lineStartOffset, int lineEndOffset, 186 int startOffset, int endOffset); 187 188 } 189 190 194 public static abstract class LineBlocksFinder extends FinderFactory.AbstractBlocksFinder { 195 196 private int origStartPos; 197 198 private int origLimitPos; 199 200 public LineBlocksFinder() { 201 } 202 203 public int adjustStartPos(BaseDocument doc, int startPos) { 204 origStartPos = startPos; 205 try { 206 return Utilities.getRowStart(doc, startPos); 207 } catch (BadLocationException e) { 208 return startPos; 209 } 210 } 211 212 public int adjustLimitPos(BaseDocument doc, int limitPos) { 213 origLimitPos = limitPos; 214 try { 215 return Utilities.getRowEnd(doc, limitPos); 216 } catch (BadLocationException e) { 217 return limitPos; 218 } 219 } 220 221 222 public int find(int bufferStartPos, char buffer[], 223 int offset1, int offset2, int reqPos, int limitPos) { 224 int offset = reqPos - bufferStartPos; while (true) { 226 int lfOffset = Analyzer.findFirstLFOffset(buffer, offset, offset2 - offset); 227 boolean lfFound = (lfOffset >= 0); 228 if (!lfFound) { 229 lfOffset = offset2; 230 } 231 232 int lineOffset = lineFound(buffer, offset, lfOffset, 233 Math.max(origStartPos - bufferStartPos, offset), 234 Math.min(origLimitPos - bufferStartPos, lfOffset)); 235 if (lineOffset >= 0) { 236 found = true; 237 return bufferStartPos + offset + lineOffset; 238 } 239 240 if (lfFound) { 241 offset = lfOffset + 1; } else { 243 break; 244 } 245 } 246 return bufferStartPos + offset2; 247 } 248 249 258 protected abstract int lineFound(char[] buffer, int lineStartOffset, int lineEndOffset, 259 int startOffset, int endOffset); 260 261 } 262 263 264 265 } 266 | Popular Tags |