1 21 22 package org.armedbear.j; 23 24 import java.util.ArrayList ; 25 import java.util.List ; 26 import javax.swing.SwingUtilities ; 27 28 public class FollowContextTask extends IdleThreadTask implements Constants 29 { 30 private Expression lastExpression; 31 private Position lastPos; 32 33 public FollowContextTask() 34 { 35 setIdle(500); setRunnable(runnable); 37 } 38 39 private final Runnable runnable = new Runnable () { 40 public void run() 41 { 42 final Editor editor = Editor.currentEditor(); 43 if (editor == null) 44 return; 45 if (editor.getBuffer() == null) 46 return; 47 if (editor.getMark() != null) 48 return; 49 if (editor.getDot() == null || editor.getDot().equals(lastPos)) 50 return; 51 lastPos = new Position(editor.getDot()); 52 Expression expression = 53 editor.getMode().getExpressionAtDot(editor, false); 54 if (expression == null) 55 return; 56 if (!expression.equals(lastExpression)) { 57 final Tag tag = findMatchingTag(editor, expression); 58 if (tag != null) { 59 Runnable r = new Runnable () { 60 public void run() 61 { 62 if (tag instanceof LocalTag) 63 TagCommands.gotoLocalTag(editor, (LocalTag)tag, true); 64 else if (tag instanceof GlobalTag) 65 TagCommands.gotoGlobalTag(editor, (GlobalTag)tag, true); 66 editor.updateDisplay(); 67 } 68 }; 69 SwingUtilities.invokeLater(r); 70 } 71 lastExpression = expression; 72 } 73 } 74 }; 75 76 private static Tag findMatchingTag(Editor editor, Expression expression) 77 { 78 List list = 79 TagCommands.findMatchingTags(editor.getBuffer(), expression); 80 if (list != null && list.size() == 1) { 81 return (Tag) list.get(0); 83 } 84 return null; 85 } 86 87 public static void followContext() 88 { 89 if (!Editor.checkExperimental()) 90 return; 91 IdleThread.runFollowContextTask(new FollowContextTask()); 92 } 93 } 94 | Popular Tags |