- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要创建一个 Eclipse 插件,当我将鼠标悬停在字符串文字上时显示工具提示。但前提是该字符串文字是特殊方法的第一个参数。
这是我用来测试我的插件的 Test.java 文件:
package test;
public class Test {
public static void main(String[] args) {
String hello = "Hello";
String world = Translator.get("Test.worldLabel");
System.out.println(hello + " " + world);
}
}
我创建了一个实现 IJavaEditorTextHover 的类,我需要编译当前编辑的 Java 文件以计算光标是否悬停在需要翻译的字符串上。
起初我用这个(170在“Test.worldLabel”里面):
ITypeRoot typeRoot = (ITypeRoot)
JavaUI.getEditorInputJavaElement(editorPart.getEditorInput());
JavaElement foundElement = (JavaElement) typeRoot.getElementAt(170);
但是 foundElement 包含整个 main() 方法:它不够细粒度。
那么,我认为正确的做法是:
private static ASTNode parse(ICompilationUnit unit, int position) {
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(unit);
parser.setResolveBindings(true);
parser.setIgnoreMethodBodies(false);
// TODO Future optimisation: parser.setFocalPosition(position);
return parser.createAST((IProgressMonitor) null); // parse
}
在我的 IJavaEditorTextHover.getHoverInfo(...) 实现中:
ICompilationUnit compilationUnit = (ICompilationUnit)
JavaUI.getEditorInputJavaElement(editor.getEditorInput())
int position = 170/*hoverRegion.getOffset()*/;
ASTNode ast = parse(compilationUnit, position);
现在,这是我的问题:
如何从这个 ast 节点获取表示源代码中位置 170 处的 StringLiteral 的 ASTNode(“Test.worldLabel”字符串)?
奖励问题:我选择了正确的解决方案吗?基于性能。
编辑:好吧,这是我找到的解决方案:
private StringLiteral findStringLiteralAtPosition(final ASTNode parent, final int position) {
final List<StringLiteral> stringLiterals = new ArrayList<StringLiteral>();
parent.accept(new ASTVisitor() {
@Override
public boolean visit(StringLiteral stringLiteral) {
int start = stringLiteral.getStartPosition();
int end = start + stringLiteral.getLength();
if (start <= position && position <= end) {
stringLiterals.add(stringLiteral);
}
return super.visit(stringLiteral);
}
});
return (stringLiterals.size() > 0 ? stringLiterals.get(0) : null);
}
缝合好吗?或者它是更简单的方法还是更高效的方法?
最佳答案
一种解决方案是根本不使用偏移逻辑。您可以通过使用节点父检查来概括解决方案。
这是一个示例代码:
public boolean visit(StringLiteral stringLiteral) {
// Check if parent is a method inovacation.
if (stringLiteral.getParent().getNodeType() == ASTNode.METHOD_INVOCATION) {
// get the parent method inovacation.
MethodInvocation miNode = (MethodInvocation) stringLiteral.getParent();
//To do: null and empty check on argument list.
// Check if is the special method and this is the 1st argument
if (miNode.getName().toString().equals("SpecialMethod")
&& miNode.arguments().get(0).toString().equals(stringLiteral.toString())) {
System.out.println("Found it : " + stringLiteral.toString());
}
}
return true;
}
关于eclipse - 如何使用 Eclipse AST 深入了解 StringLiterals?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14955197/
我正在尝试检查 Go 源代码以制作一个工具。为此,我使用 ast.Inspect 函数。 我需要知道函数/方法内部如何使用 channel 。 我将此作为要检查的示例代码: package main
我正在为我自己的语言制作一个解释器作为一个业余爱好项目。目前我的解释器只是执行它看到的代码。我听说你应该让解析器从源代码生成 AST。所以我想知道,正如解析器所见,AST 实际上如何使事情比仅仅线性执
我正在为 JavaScript 实现一个突变测试工具。修改 AST 并针对修改后的代码执行测试用例。运行测试用例后,我想将修改后的 AST 恢复为原始 AST,以便我可以重复变异过程。但是,我不知道如
AST 文档:https://www.dartdocs.org/documentation/analyzer_experimental/0.8.0/analyzer/parseCompilationU
更新2:再次感谢@deepak-azad,我设法解决了我的问题:这里是主代码的链接:https://gist.github.com/1714641 更新:感谢@deepak-azad,我补充了代码,但
我正在编写一些 Go AST 代码,而编译器在这一行上令人窒息: var call ast.Expr = ast.CallExpr{Fun: ast.NewIdent("foo"), Args: []
我正在对 c 程序进行静态分析。我搜索了 antlr 网站,似乎没有合适的语法文件为 c 程序生成 ast。这是否意味着我必须从一开始就自己做。或者是有一个更快的方法。我还需要一个可以遍历解析器创建的
是否可以像这样采用带引号的 Elixir 表达式(AST 树): quote do: 1 + 1 => {:+, [context: Elixir, import: Kernel], [1, 1]}
我遇到了这个异常: unexpected AST node: query 我的查询是: SELECT u.user_id, u.username,u.email,u.phone,u.status,r
我是 Java 编程语言的初学者。我想从 java 源代码中提取 AST 并将 AST 打印到文件或标准输出。 我按照本教程学习了如何使用 AST。 http://www.programcreek.c
NodeVisitor 以深度优先的方式遍历 AST,并且在进入时仅访问每个节点一次。因此,用它做一些严肃的事情是有问题的。是否可以更改其默认行为? 最佳答案 也许有人会对一些草拟的例子感兴趣,如何做
目前,我正在努力用 Java 表示我用 SML 编写的 AST 树,这样我就可以随时用 Java 遍历它。 我想知道是否应该在 Java 中创建一个 Node 类,其中包含我想要表示的数据,以及一个数
我正在尝试修改/重构输入的 C 源代码。我试图在输入代码的每一行之后添加一个 printf 语句。 例如如果我的输入是 - void foo(){ // Sample input code
我目前正在使用 eclipse AST 来生成源代码。除了在大多数示例中,我是在独立应用程序中从头开始生成源代码,而不是在 eclipse 插件中。 当从 ASTParser 读取时,您可以通过调用
我有一个 HQL 查询: query = select item.itemNumber from items item where item.stock>0 and item.price it = q
根据 Om Next's documentation : query->ast (om.next/query->ast '[(:foo {:bar 1})]) Given a query expres
如果能学到一些有用的东西,我会非常感激,至于现在,我一直在盲目地行动。所以问题出在python的ast.NodeTransformer上。我想知道是否可以使用这种方式向现有类添加一个函数,而不是生气。
我们希望为 Elixir 开发一个静态代码分析器,以检测并发问题(主要是死锁和竞争条件)。我们对分析器的结构有了一些基本的了解,但我们的问题是哪种 AST 更适合这项任务。正如我们所了解的,Elixi
我在以下代码段中遇到错误 using (var session = Database.OpenSession()) { var q = from x in session.Query()
我正在使用以下 C# 代码: public IList GetAllByExpression(Expression> expression, int startIndex, int count, Fu
我是一名优秀的程序员,十分优秀!