gpt4 book ai didi

java - 无法在插件外部合并 Eclispe JDT codeAssist 设施

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:19:32 25 4
gpt4 key购买 nike

使用Eclipse jdt工具,你可以遍历java代码片段的AST,如下所示:

ASTParser ASTparser = ASTParser.newParser(AST.JLS3);
ASTparser.setSource("package x;class X{}".toCharArray());
ASTparser.createAST(null).accept(...);

但是当尝试执行代码完成和代码选择时,似乎我必须在插件应用程序中执行此操作,因为我必须编写如下代码

IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(somePath));
ICodeAssist i = JavaCore.createCompilationUnitFrom(f);
i.codeComplete/codeSelect(...)

我是否最终可以获得一个包含 jdt 代码完成/选择功能的独立 Java 应用程序?

非常感谢!石奎


我注意到使用 org.eclipse.jdt.internal.codeassist.complete.CompletionParser我也可以解析代码片段。

CompletionParser parser =new CompletionParser(new ProblemReporter(
DefaultErrorHandlingPolicies.proceedWithAllProblems(),
new CompilerOptions(null),
new DefaultProblemFactory(Locale.getDefault())),
false);
org.eclipse.jdt.internal.compiler.batch.CompilationUnit sourceUnit =
new org.eclipse.jdt.internal.compiler.batch.CompilationUnit(
"class T{f(){new T().=1;} \nint j;}".toCharArray(), "testName", null);
CompilationResult compilationResult = new CompilationResult(sourceUnit, 0, 0, 0);
CompilationUnitDeclaration unit = parser.dietParse(sourceUnit, compilationResult, 25);

但是我有两个问题: 1、如何找回辅助信息? 2. 如何指定类路径或源路径让编译器查找类型/方法/字段信息?

最佳答案

我不这么认为,除非您提供自己的 ICodeAssist 实现.

作为Performing code assist on Java code提到,允许这种操作的元素应该实现 ICodeAssist。

There are two kinds of manipulation:

  • Code completion - compute the completion of a Java token.
  • Code selection - answer the Java element indicated by the selected text of a given offset and length.

In the Java model there are two elements that implement this interface: IClassFile and ICompilationUnit.
Code completion and code selection only answer results for a class file if it has attached source.

您可以尝试打开文件 outside of any workspace (如 this FAQ ),但结果不会实现 ICodeAssist

所以 IFile most of the timefrom a workspace location .

关于java - 无法在插件外部合并 Eclispe JDT codeAssist 设施,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2711340/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com