KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ruby > RubyPositionManager


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.ruby;
20
21 import java.util.List JavaDoc;
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 /**
32  *
33  * @author Tor Norbye
34  */

35 public class RubyPositionManager implements PositionManager {
36     /**
37      * Creates a new instance of JRubyPositionManager
38      */

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 JavaDoc((("Foreign element: " + object + " of type " +
50                 object) != null) ? object.getClass().getName() : "null");
51         }
52     }
53
54     /**
55      * Find the position closest to the given offset in the AST. Place the path from the leaf up to the path in the
56      * passed in path list.
57      */

58     @SuppressWarnings JavaDoc("unchecked")
59     public static Node findPathTo(Node node, List JavaDoc<Node> path, int offset) {
60         Node result = find(node, path, offset);
61         path.add(node);
62
63         return result;
64     }
65
66     @SuppressWarnings JavaDoc("unchecked")
67     private static Node find(Node node, List JavaDoc<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 JavaDoc<Node> children = (List JavaDoc<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 JavaDoc<Node> children = (List JavaDoc<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     /**
104      * Find the path to the given node in the AST
105      */

106     @SuppressWarnings JavaDoc("unchecked")
107     public static boolean find(Node node, List JavaDoc<Node> path, Node target) {
108         if (node == target) {
109             return true;
110         }
111
112         List JavaDoc<Node> children = (List JavaDoc<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