Posted by Andrey Tarantsov Sat 24th Feb 2007 20:22 - Syntax is Diff - 60 views
Download | New Post | Modify | Hide line numbers
Download | New Post | Modify | Hide line numbers
-
### Eclipse Workspace Patch 1.0
-
#P org.eclipse.dltk.ruby.core
-
Index: src/org/eclipse/dltk/ruby/typeinference/RubyModelUtils.java
-
===================================================================
-
RCS file: /cvsroot/technology/org.eclipse.dltk/ruby/plugins/org.eclipse.dltk.ruby.core/src/org/eclipse/dltk/ruby/typeinference/RubyModelUtils.java,v
-
retrieving revision 1.1
-
diff -u -r1.1 RubyModelUtils.java
-
--- src/org/eclipse/dltk/ruby/typeinference/RubyModelUtils.java 24 Feb 2007 13:50:33 -0000 1.1
-
+++ src/org/eclipse/dltk/ruby/typeinference/RubyModelUtils.java 24 Feb 2007 20:21:51 -0000
-
@@ -1,6 +1,15 @@
-
package org.eclipse.dltk.ruby.typeinference;
-
-
+import org.eclipse.core.runtime.Assert;
-
+import org.eclipse.dltk.ast.ASTNode;
-
+import org.eclipse.dltk.ast.ASTVisitor;
-
+import org.eclipse.dltk.ast.declarations.MethodDeclaration;
-
+import org.eclipse.dltk.ast.declarations.ModuleDeclaration;
-
import org.eclipse.dltk.ast.declarations.TypeDeclaration;
-
+import org.eclipse.dltk.ast.expressions.Expression;
-
+import org.eclipse.dltk.ast.statements.Block;
-
+import org.eclipse.dltk.ast.statements.Statement;
-
+import org.eclipse.dltk.core.IMethod;
-
import org.eclipse.dltk.core.ISourceModule;
-
import org.eclipse.dltk.core.ISourceRange;
-
import org.eclipse.dltk.core.IType;
-
@@ -34,5 +43,91 @@
-
}
-
return bestType;
-
}
-
+
-
+ public static MethodDeclaration getNodeByMethod(ModuleDeclaration rootNode, IMethod method) throws ModelException {
-
+
-
+ ISourceRange sourceRange = method.getSourceRange();
-
+ final int modelStart = sourceRange.getOffset();
-
+ final int modelEnd = modelStart + sourceRange.getLength();
-
+ final int modelCutoffStart = modelStart - 100;
-
+ final int modelCutoffEnd = modelEnd + 100;
-
+ final String methodName = method.getElementName();
-
+
-
+ final MethodDeclaration[] bestResult = new MethodDeclaration[1];
-
+
-
+ ASTVisitor visitor = new ASTVisitor() {
-
+
-
+ int bestScore = Integer.MAX_VALUE;
-
+
-
+ private boolean interesting(ASTNode s) {
-
+ if (s.sourceStart() < 0 || s.sourceEnd() < s.sourceStart())
-
+ return true;
-
+ if (s.sourceStart() < modelCutoffStart || s.sourceEnd() > modelCutoffEnd)
-
+ return false;
-
+ return true;
-
+ }
-
+
-
+ public boolean visit(Expression s) throws Exception {
-
+ if (!interesting(s))
-
+ return false;
-
+ return true;
-
+ }
-
+
-
+ public boolean visit(MethodDeclaration s) throws Exception {
-
+ if (!interesting(s))
-
+ return false;
-
+ if (s.getName().equals(methodName)) {
-
+ int astStart = s.sourceStart();
-
+ int astEnd = s.sourceEnd();
-
+ int diff1 = modelStart - astStart;
-
+ int diff2 = modelEnd - astEnd;
-
+ int score = diff1 * diff1 + diff2 * diff2;
-
+ if (score < bestScore) {
-
+ bestScore = score;
-
+ bestResult[0] = s;
-
+ }
-
+
-
+ }
-
+ return true;
-
+ }
-
+
-
+ public boolean visit(ModuleDeclaration s) throws Exception {
-
+ if (!interesting(s))
-
+ return false;
-
+ return true;
-
+ }
-
+
-
+ public boolean visit(Statement s) throws Exception {
-
+ // XXX workaround for a bug in block offset calculation
-
+ if (s instanceof Block)
-
+ return true;
-
+ if (!interesting(s))
-
+ return false;
-
+ return true;
-
+ }
-
+
-
+ public boolean visit(TypeDeclaration s) throws Exception {
-
+ if (!interesting(s))
-
+ return false;
-
+ return true;
-
+ }
-
+
-
+ public boolean endvisit(TypeDeclaration s) throws Exception {
-
+ if (!interesting(s))
-
+ return false;
-
+ return false /* dummy */;
-
+ }
-
+
-
+ public boolean visitGeneral(ASTNode s) throws Exception {
-
+ if (!interesting(s))
-
+ return false;
-
+ return true;
-
+ }
-
+
-
+ };
-
+
-
+ return bestResult[0];
-
+
-
+ }
-
-
}
-
PermaLink to this entry https://pastebin.co.uk/10888
Posted by Andrey Tarantsov Sat 24th Feb 2007 20:22 - Syntax is Diff - 60 views
Download | New Post | Modify | Hide line numbers
Download | New Post | Modify | Hide line numbers
Comments: 0