added experiment to parse java source code using JDTs parser
This commit is contained in:
@@ -14,13 +14,16 @@ package org.springframework.ide.vscode.boot.java.completions;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.eclipse.lsp4j.CompletionItemKind;
|
||||
import org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits;
|
||||
import org.eclipse.jdt.core.JavaCore;
|
||||
import org.eclipse.jdt.core.dom.AST;
|
||||
import org.eclipse.jdt.core.dom.ASTNode;
|
||||
import org.eclipse.jdt.core.dom.ASTParser;
|
||||
import org.eclipse.jdt.core.dom.CompilationUnit;
|
||||
import org.eclipse.jdt.core.dom.NodeFinder;
|
||||
import org.springframework.ide.vscode.commons.languageserver.completion.ICompletionEngine;
|
||||
import org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal;
|
||||
import org.springframework.ide.vscode.commons.util.Renderable;
|
||||
import org.springframework.ide.vscode.commons.util.Renderables;
|
||||
import org.springframework.ide.vscode.commons.util.text.IDocument;
|
||||
|
||||
public class BootJavaCompletionEngine implements ICompletionEngine {
|
||||
@@ -28,45 +31,31 @@ public class BootJavaCompletionEngine implements ICompletionEngine {
|
||||
@Override
|
||||
public Collection<ICompletionProposal> getCompletions(IDocument document, int offset) throws Exception {
|
||||
List<ICompletionProposal> completions = new ArrayList<>();
|
||||
|
||||
ICompletionProposal proposal = new ICompletionProposal() {
|
||||
|
||||
@Override
|
||||
public DocumentEdits getTextEdit() {
|
||||
DocumentEdits edits = new DocumentEdits(document);
|
||||
edits.insert(0, "my noew java code from spring boot completions!!!");
|
||||
|
||||
return edits;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLabel() {
|
||||
return "Do a great Spring Boot Java Code completion";
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletionItemKind getKind() {
|
||||
return CompletionItemKind.Text;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Renderable getDocumentation() {
|
||||
return Renderables.text("this describes the Boot Java completion in more detail");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDetail() {
|
||||
return "and here are the details";
|
||||
}
|
||||
|
||||
@Override
|
||||
public ICompletionProposal deemphasize() {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
completions.add(proposal);
|
||||
|
||||
|
||||
System.out.println("create parser");
|
||||
|
||||
ASTParser parser = ASTParser.newParser(AST.JLS8);
|
||||
Map<String, String> options = JavaCore.getOptions();
|
||||
JavaCore.setComplianceOptions(JavaCore.VERSION_1_8, options);
|
||||
parser.setCompilerOptions(options);
|
||||
parser.setKind(ASTParser.K_COMPILATION_UNIT);
|
||||
parser.setStatementsRecovery(true);
|
||||
parser.setBindingsRecovery(true);
|
||||
|
||||
System.out.println("set source");
|
||||
|
||||
parser.setSource(document.get(0, document.getLength()).toCharArray());
|
||||
|
||||
System.out.println("start parse");
|
||||
|
||||
CompilationUnit cu = (CompilationUnit) parser.createAST(null);
|
||||
|
||||
System.out.println("end parse");
|
||||
|
||||
ASTNode node = NodeFinder.perform(cu, offset, 0);
|
||||
|
||||
System.out.println("AST node found: " + node.getClass().getName());
|
||||
|
||||
return completions;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user