Posted by Andrey Tarantsov Sat 24th Feb 2007 20:22 - Syntax is Diff - 60 views
Download | New Post | Modify | Hide line numbers
  1. ### Eclipse Workspace Patch 1.0
  2. #P org.eclipse.dltk.ruby.core
  3. Index: src/org/eclipse/dltk/ruby/typeinference/RubyModelUtils.java
  4. ===================================================================
  5. RCS file: /cvsroot/technology/org.eclipse.dltk/ruby/plugins/org.eclipse.dltk.ruby.core/src/org/eclipse/dltk/ruby/typeinference/RubyModelUtils.java,v
  6. retrieving revision 1.1
  7. diff -u -r1.1 RubyModelUtils.java
  8. --- src/org/eclipse/dltk/ruby/typeinference/RubyModelUtils.java    24 Feb 2007 13:50:33 -0000    1.1
  9. +++ src/org/eclipse/dltk/ruby/typeinference/RubyModelUtils.java    24 Feb 2007 20:21:51 -0000
  10. @@ -1,6 +1,15 @@
  11.  package org.eclipse.dltk.ruby.typeinference;
  12.  
  13. +import org.eclipse.core.runtime.Assert;
  14. +import org.eclipse.dltk.ast.ASTNode;
  15. +import org.eclipse.dltk.ast.ASTVisitor;
  16. +import org.eclipse.dltk.ast.declarations.MethodDeclaration;
  17. +import org.eclipse.dltk.ast.declarations.ModuleDeclaration;
  18.  import org.eclipse.dltk.ast.declarations.TypeDeclaration;
  19. +import org.eclipse.dltk.ast.expressions.Expression;
  20. +import org.eclipse.dltk.ast.statements.Block;
  21. +import org.eclipse.dltk.ast.statements.Statement;
  22. +import org.eclipse.dltk.core.IMethod;
  23.  import org.eclipse.dltk.core.ISourceModule;
  24.  import org.eclipse.dltk.core.ISourceRange;
  25.  import org.eclipse.dltk.core.IType;
  26. @@ -34,5 +43,91 @@
  27.          }
  28.          return bestType;
  29.      }
  30. +   
  31. +    public static MethodDeclaration getNodeByMethod(ModuleDeclaration rootNode, IMethod method) throws ModelException {
  32. +
  33. +        ISourceRange sourceRange = method.getSourceRange();
  34. +        final int modelStart = sourceRange.getOffset();
  35. +        final int modelEnd = modelStart + sourceRange.getLength();
  36. +        final int modelCutoffStart = modelStart - 100;
  37. +        final int modelCutoffEnd = modelEnd + 100;
  38. +        final String methodName = method.getElementName();
  39. +
  40. +        final MethodDeclaration[] bestResult = new MethodDeclaration[1];
  41. +
  42. +        ASTVisitor visitor = new ASTVisitor() {
  43. +           
  44. +            int bestScore = Integer.MAX_VALUE;
  45. +
  46. +            private boolean interesting(ASTNode s) {
  47. +                if (s.sourceStart() < 0 || s.sourceEnd() < s.sourceStart())
  48. +                    return true;
  49. +                if (s.sourceStart() < modelCutoffStart || s.sourceEnd() > modelCutoffEnd)
  50. +                    return false;
  51. +                return true;
  52. +            }
  53. +
  54. +            public boolean visit(Expression s) throws Exception {
  55. +                if (!interesting(s))
  56. +                    return false;
  57. +                return true;
  58. +            }
  59. +
  60. +            public boolean visit(MethodDeclaration s) throws Exception {
  61. +                if (!interesting(s))
  62. +                    return false;
  63. +                if (s.getName().equals(methodName)) {
  64. +                    int astStart = s.sourceStart();
  65. +                    int astEnd = s.sourceEnd();
  66. +                    int diff1 = modelStart - astStart;
  67. +                    int diff2 = modelEnd - astEnd;
  68. +                    int score = diff1 * diff1 + diff2 * diff2;
  69. +                    if (score < bestScore) {
  70. +                        bestScore = score;
  71. +                        bestResult[0] = s;
  72. +                    }
  73. +                   
  74. +                }
  75. +                return true;
  76. +            }
  77. +
  78. +            public boolean visit(ModuleDeclaration s) throws Exception {
  79. +                if (!interesting(s))
  80. +                    return false;
  81. +                return true;
  82. +            }
  83. +
  84. +            public boolean visit(Statement s) throws Exception {
  85. +                // XXX workaround for a bug in block offset calculation
  86. +                if (s instanceof Block)
  87. +                    return true;
  88. +                if (!interesting(s))
  89. +                    return false;
  90. +                return true;
  91. +            }
  92. +
  93. +            public boolean visit(TypeDeclaration s) throws Exception {
  94. +                if (!interesting(s))
  95. +                    return false;
  96. +                return true;
  97. +            }
  98. +
  99. +            public boolean endvisit(TypeDeclaration s) throws Exception {
  100. +                if (!interesting(s))
  101. +                    return false;
  102. +                return false /* dummy */;
  103. +            }
  104. +
  105. +            public boolean visitGeneral(ASTNode s) throws Exception {
  106. +                if (!interesting(s))
  107. +                    return false;
  108. +                return true;
  109. +            }
  110. +           
  111. +        };
  112. +       
  113. +        return bestResult[0];
  114. +       
  115. +    }
  116.  
  117.  }
  118.  

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

 

Comments: 0