1 21 22 package org.armedbear.j; 23 24 public final class ListOccurrencesInFiles extends ListOccurrences 25 { 26 private Position lastDotPos; 27 28 public ListOccurrencesInFiles(Search search) 30 { 31 super(search); 32 if (search instanceof FindInFiles) { 33 appendLine("Files: " + ((FindInFiles)search).getFiles()); 34 if (search.wholeWordsOnly()) 35 appendLine("Mode: " + ((FindInFiles)search).getMode().getDisplayName()); 36 renumber(); 37 } 38 title = "\"" + search.getPattern() + "\""; 39 setLoaded(true); 40 } 41 42 private final void setLastDotPos(Position pos) 43 { 44 lastDotPos = pos.copy(); 45 } 46 47 public final Position getLastDotPos() 48 { 49 return lastDotPos != null ? lastDotPos : getInitialDotPos(); 50 } 51 52 public final File getCurrentDirectory() 53 { 54 return Directories.getUserHomeDirectory(); 55 } 56 57 public void findOccurrenceAtDot(Editor editor, boolean killList) 58 { 59 Position pos = editor.getDotCopy(); 60 if (pos == null) 61 return; 62 Line sourceLine = null; 63 int sourceLineNumber = 0; 64 Buffer buf = null; 65 String canonicalPath = null; 66 setLastDotPos(pos); 67 final Line line = pos.getLine(); 68 if ((line instanceof OccurrenceLine)) { 69 sourceLine = ((OccurrenceLine)line).getSourceLine(); 70 Line ln = line; 71 if (!ln.getText().startsWith("File: ")) 72 sourceLineNumber = Utilities.parseInt(ln.getText()) - 1; 73 while (ln != null) { 74 if (ln.getText().startsWith("File: ")) { 75 canonicalPath = ln.getText().substring(6); 76 break; 77 } 78 ln = ln.previous(); 79 } 80 } else if (line instanceof FileLine) 81 canonicalPath = ((FileLine)line).getCanonicalPath(); 82 if (buf == null && canonicalPath != null) 83 buf = Editor.getBuffer(File.getInstance(canonicalPath)); 84 if (buf != null) { 85 if (!buf.isLoaded()) { 86 if (!buf.initialized()) 87 buf.initialize(); 88 buf.load(); 89 if (!buf.isLoaded()) { 90 editor.status("Unable to load buffer"); 91 return; 92 } 93 } 94 if (line instanceof FileLine) { 95 ((FileLine)line).markVisited(); 97 Position dot = editor.getDot(); 99 if (dot.equals(pos) && dot.getNextLine() != null) { 100 dot.moveTo(dot.getNextLine(), 0); 101 editor.moveCaretToDotCol(); 102 } 103 } 104 Line target; 105 if (sourceLine != null) { 106 if (buf.contains(sourceLine)) 107 target = sourceLine; 108 else 109 target = buf.getLine(sourceLine.lineNumber()); 110 } else 111 target = buf.getLine(sourceLineNumber); 112 gotoSource(editor, buf, target, killList); 113 } 114 } 115 116 public void findNextOccurrence(Editor editor) 117 { 118 Line line; 119 for (line = editor.getDotLine().next(); line != null; line = line.next()) { 120 if (line instanceof OccurrenceLine) 121 break; 122 } 123 if (line instanceof OccurrenceLine) 124 findOccurrence(editor, line); 125 } 126 127 public void findPreviousOccurrence(Editor editor) 128 { 129 Line line; 130 for (line = editor.getDotLine().previous(); line != null; line = line.previous()) { 131 if (line instanceof OccurrenceLine) 132 break; 133 } 134 if (line instanceof OccurrenceLine) 135 findOccurrence(editor, line); 136 } 137 138 private void findOccurrence(Editor editor, Line line) 140 { 141 if (line instanceof OccurrenceLine) { 142 for (EditorIterator it = new EditorIterator(); it.hasNext();) { 143 Editor ed = it.nextEditor(); 144 if (ed.getBuffer() == this) { 145 ed.moveDotTo(line, 0); 146 ed.updateDisplay(); 147 } 148 } 149 findOccurrenceAtDot(editor, false); 150 } 151 } 152 153 public final void appendStatusLine(String s) 154 { 155 appendLine(new ListOccurrencesStatusLine(s)); 156 } 157 158 public final void appendFileLine(File file, boolean listEachOccurrence) 159 { 160 appendLine(new FileLine(file, listEachOccurrence)); 161 } 162 163 protected String getOptions() 164 { 165 String s = super.getOptions(); 166 if (search instanceof FindInFiles) 167 if (((FindInFiles)search).getIncludeSubdirs()) 168 s = s.concat(", include subdirectories"); 169 return s; 170 } 171 172 public Position getInitialDotPos() 173 { 174 final boolean listEachOccurrence; 175 if (search instanceof FindInFiles) 176 listEachOccurrence = ((FindInFiles)search).getListEachOccurrence(); 177 else 178 listEachOccurrence = true; 179 for (Line line = getFirstLine(); line != null; line = line.next()) { 180 if (line instanceof OccurrenceLine) 181 return new Position(line, 0); 182 if (!listEachOccurrence && line instanceof FileLine) 183 return new Position(line, 0); 184 } 185 return new Position(getFirstLine(), 0); 186 } 187 188 public void follow(File sourceFile, Line sourceLine) 189 { 190 Line line = findLineForOccurrence(sourceFile, sourceLine); 191 if (line == null) 192 return; 193 for (EditorIterator it = new EditorIterator(); it.hasNext();) { 194 Editor ed = it.nextEditor(); 195 if (ed.getBuffer() == this) { 196 ed.moveDotTo(line, 0); 197 ed.updateDisplay(); 198 } 199 } 200 } 201 202 private Line findLineForOccurrence(File sourceFile, Line sourceLine) 203 { 204 if (sourceFile == null) 205 return null; 206 if (sourceLine == null) 207 return null; 208 final String cp = sourceFile.canonicalPath(); 209 Line line; 210 for (line = getFirstLine(); line != null; line = line.next()) { 211 if (line instanceof FileLine) { 212 if (((FileLine)line).getCanonicalPath().equals(cp)) 213 break; 214 } 215 } 216 if (line == null) 217 return null; 218 Debug.assertTrue(line instanceof FileLine); 219 Debug.assertTrue(((FileLine)line).getCanonicalPath().equals(cp)); 220 int target = sourceLine.lineNumber() + 1; 221 Line best = null; 222 for (line = line.next(); line instanceof OccurrenceLine; line = line.next()) { 223 int candidate = ((OccurrenceLine)line).getSourceLineNumber(); 224 if (candidate == target) 225 return line; 226 if (best != null && candidate > target) 227 return best; 228 best = line; 229 } 230 return best; 231 } 232 233 public String getFileNameForDisplay() 234 { 235 return ""; 236 } 237 } 238 | Popular Tags |