1 19 package org.netbeans.modules.ruby; 20 21 import java.util.List ; 22 23 import org.jruby.ast.Node; 24 import org.jruby.lexer.yacc.ISourcePosition; 25 import org.netbeans.api.gsf.Element; 26 import org.netbeans.api.gsf.OffsetRange; 27 import org.netbeans.api.gsf.PositionManager; 28 import org.netbeans.modules.ruby.elements.AstElement; 29 30 31 35 public class RubyPositionManager implements PositionManager { 36 39 public RubyPositionManager() { 40 } 41 42 public OffsetRange getOffsetRange(Element file, Element object) { 43 if (object instanceof AstElement) { 44 Node target = ((AstElement)object).getNode(); 45 ISourcePosition pos = target.getPosition(); 46 47 return new OffsetRange(pos.getStartOffset(), pos.getEndOffset()); 48 } else { 49 throw new IllegalArgumentException ((("Foreign element: " + object + " of type " + 50 object) != null) ? object.getClass().getName() : "null"); 51 } 52 } 53 54 58 @SuppressWarnings ("unchecked") 59 public static Node findPathTo(Node node, List <Node> path, int offset) { 60 Node result = find(node, path, offset); 61 path.add(node); 62 63 return result; 64 } 65 66 @SuppressWarnings ("unchecked") 67 private static Node find(Node node, List <Node> path, int offset) { 68 ISourcePosition pos = node.getPosition(); 69 int begin = pos.getStartOffset(); 70 int end = pos.getEndOffset(); 71 72 if ((offset >= begin) && (offset <= end)) { 73 List <Node> children = (List <Node>)node.childNodes(); 74 75 for (Node child : children) { 76 Node found = find(child, path, offset); 77 78 if (found != null) { 79 path.add(child); 80 81 return found; 82 } 83 } 84 85 return node; 86 } else { 87 List <Node> children = (List <Node>)node.childNodes(); 88 89 for (Node child : children) { 90 Node found = find(child, path, offset); 91 92 if (found != null) { 93 path.add(child); 94 95 return found; 96 } 97 } 98 99 return null; 100 } 101 } 102 103 106 @SuppressWarnings ("unchecked") 107 public static boolean find(Node node, List <Node> path, Node target) { 108 if (node == target) { 109 return true; 110 } 111 112 List <Node> children = (List <Node>)node.childNodes(); 113 114 for (Node child : children) { 115 boolean found = find(child, path, target); 116 117 if (found) { 118 path.add(child); 119 120 return found; 121 } 122 } 123 124 return false; 125 } 126 } 127 | Popular Tags |