1 20 21 package org.armedbear.j; 22 23 import java.util.ArrayList ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 27 public final class FindTagTextFieldHandler extends DefaultTextFieldHandler 28 { 29 public FindTagTextFieldHandler(Editor editor, HistoryTextField textField) 30 { 31 super(editor, textField); 32 } 33 34 public boolean wantTab() 35 { 36 return true; 37 } 38 39 public void enter() 40 { 41 String pattern = textField.getText(); 42 if (pattern == null) 43 return; 44 pattern = pattern.trim(); 45 if (pattern.length() == 0) 46 return; 47 History history = textField.getHistory(); 49 if (history != null) { 50 history.append(pattern); 51 history.save(); 52 } 53 editor.ensureActive(); 54 editor.setFocusToDisplay(); 55 editor.updateLocation(); 56 findTag(pattern); 57 editor.getDispatcher().eventHandled(); 58 } 59 60 private void findTag(String pattern) 61 { 62 final Buffer buffer = editor.getBuffer(); 63 List tags = findMatchingTags(buffer, pattern); 64 if (tags != null) { 65 if (tags.size() > 1) { 66 ArrayList shortList = new ArrayList (); 69 for (Iterator it = tags.iterator(); it.hasNext();) { 70 Tag tag = (Tag) it.next(); 71 if (tag instanceof LocalTag) { 72 int type = ((LocalTag)tag).getType(); 73 if (type == TAG_METHOD || type == TAG_EXPLICIT) 75 shortList.add(tag); 76 if (type == TAG_DEFUN || type == TAG_GENERIC_FUNCTION || 78 type == TAG_MACRO) 79 shortList.add(tag); 80 } 81 } 82 if (shortList.size() == 1) 83 tags = shortList; 84 } 85 if (tags.size() > 1) { 86 editor.setDefaultCursor(); 87 ListTagsBuffer buf = 88 new ListTagsBuffer(editor, "findTag", pattern, tags); 89 editor.makeNext(buf); 90 Editor ed = editor.activateInOtherWindow(buf); 91 ed.setDot(buf.getInitialDotPos()); 92 ed.moveCaretToDotCol(); 93 ed.updateDisplay(); 94 } else if (tags.size() == 1) { 95 Tag tag = (Tag) tags.get(0); 97 editor.pushPosition(); 98 if (tag instanceof LocalTag) 99 TagCommands.gotoLocalTag(editor, (LocalTag)tag, false); 100 else if (tag instanceof GlobalTag) 101 TagCommands.gotoGlobalTag(editor, (GlobalTag)tag, false); 102 else 103 Debug.bug(); 104 } 105 } else 106 editor.status("Tag \"".concat(pattern).concat("\" not found")); 107 } 108 109 private static List findMatchingTags(Buffer buffer, String pattern) 110 { 111 boolean ignoreCase = Utilities.isLowerCase(pattern); 112 final Mode mode = buffer.getMode(); 113 List list = findMatchingTagsInBuffer(buffer, pattern, ignoreCase); 116 if (list == null) { 117 list = 120 TagCommands.findMatchingTagsInDirectory(pattern, 121 buffer.getCurrentDirectory(), mode, -1, ignoreCase); 122 if (list == null) { 123 List dirs = TagCommands.getDirectoriesInTagPath(buffer); 125 if (dirs != null) { 126 for (int i = 0; i < dirs.size(); i++) { 127 String dir = (String ) dirs.get(i); 128 File directory = File.getInstance(dir); 129 if (directory.equals(buffer.getCurrentDirectory())) 130 continue; 131 List tagsInDir = 132 TagCommands.findMatchingTagsInDirectory(pattern, 133 directory, mode, -1, ignoreCase); 134 if (tagsInDir != null) { 135 if (list == null) 136 list = new ArrayList (); 137 list.addAll(tagsInDir); 138 } 139 } 140 } 141 } 142 } 143 return (list != null && list.size() > 0) ? list : null; 144 } 145 146 private static List findMatchingTagsInBuffer(Buffer buffer, String pattern, 147 boolean ignoreCase) 148 { 149 if (buffer.getTags() == null) { 150 Tagger tagger = buffer.getMode().getTagger(buffer); 151 if (tagger != null) 152 tagger.run(); 153 } 154 boolean isQualified = buffer.getMode().isQualifiedName(pattern); 155 List list = new ArrayList (); 156 final List localTags = buffer.getTags(); 157 if (localTags != null) { 158 Iterator iter = localTags.iterator(); 160 while (iter.hasNext()) { 161 LocalTag localTag = (LocalTag) iter.next(); 162 if (isQualified) { 163 String tagName = localTag.getName(); 164 if ((ignoreCase && tagName.equalsIgnoreCase(pattern)) || tagName.equals(pattern)) { 165 list.add(localTag); 166 } 167 } else { 168 String methodName = localTag.getMethodName(); 170 if (methodName != null) { 171 if ((ignoreCase && methodName.equalsIgnoreCase(pattern)) || methodName.equals(pattern)) { 172 list.add(localTag); 173 } 174 } 175 } 176 } 177 } 178 return (list != null && list.size() > 0) ? list : null; 179 } 180 181 public List getCompletions(final String prefix) 182 { 183 List list = getCompletionsInCurrentBuffer(prefix); 184 Mode mode = editor.getMode(); 185 List tags = 186 Editor.getTagFileManager().getTags(editor.getCurrentDirectory(), 187 mode); 188 if (tags != null) { 189 boolean prefixIsQualified = mode.isQualifiedName(prefix); 190 boolean ignoreCase = Utilities.isLowerCase(prefix); 191 int prefixLength = prefix.length(); 192 for (int i = 0; i < tags.size(); i++) { 193 GlobalTag tag = (GlobalTag) tags.get(i); 194 if (tag.getName().regionMatches(ignoreCase, 0, prefix, 0, prefixLength)) { 195 String toBeAdded; 196 if (prefixIsQualified) 197 toBeAdded = tag.getName(); 198 else { 199 toBeAdded = tag.getClassName(); 200 if (toBeAdded == null) 201 toBeAdded = tag.getName(); 202 } 203 maybeAdd(list, toBeAdded); 204 continue; 205 } 206 if (!prefixIsQualified && mode.hasQualifiedNames()) { 207 String methodName = tag.getMethodName(); 210 if (methodName != null) { 211 if (methodName.regionMatches(ignoreCase, 0, prefix, 0, prefixLength)) 212 maybeAdd(list, tag.getMethodName()); 213 } 214 } 215 } 216 } 217 return list; 218 } 219 220 private List getCompletionsInCurrentBuffer(String prefix) 221 { 222 List list = new ArrayList (); 223 List tags = editor.getBuffer().getTags(); 224 if (tags != null) { 225 boolean ignoreCase = Utilities.isLowerCase(prefix); 226 int prefixLength = prefix.length(); 227 for (int i = 0; i < tags.size(); i++) { 228 Tag tag = (Tag) tags.get(i); 229 if (tag.getMethodName().regionMatches(ignoreCase, 0, prefix, 0, prefixLength)) 230 maybeAdd(list, tag.getMethodName()); 231 } 232 } 233 return list; 234 } 235 236 private void maybeAdd(List list, String name) 238 { 239 if (name != null) { 240 for (int i = list.size(); i-- > 0;) 241 if (name.equals(list.get(i))) 242 return; list.add(name); 244 } 245 } 246 } 247 | Popular Tags |