forked from redhat-developer/quarkus-ls
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Filter and fill some value for Java (server) snippet
Fixes redhat-developer#265 Signed-off-by: azerr <azerr@redhat.com>
- Loading branch information
1 parent
e3a5dcd
commit 1f2b249
Showing
29 changed files
with
839 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...redhat.microprofile.jdt.core/src/main/java/com/redhat/microprofile/commons/JavaScope.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2020 Red Hat Inc. and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
package com.redhat.microprofile.commons; | ||
|
||
/** | ||
* The java element scope. | ||
* | ||
* @author Angelo ZERR | ||
* | ||
*/ | ||
public enum JavaScope { | ||
|
||
UNKNOWN(0), ANY(1), TYPE(2), METHOD(3), FIELD(4), PARAMETER(5), ANNOTATION_TYPE(6); | ||
|
||
private final int value; | ||
|
||
JavaScope(int value) { | ||
this.value = value; | ||
} | ||
|
||
public int getValue() { | ||
return value; | ||
} | ||
|
||
public static JavaScope forValue(int value) { | ||
JavaScope[] allValues = JavaScope.values(); | ||
if (value < 1 || value > allValues.length) | ||
throw new IllegalArgumentException("Illegal enum value: " + value); | ||
return allValues[value - 1]; | ||
} | ||
|
||
public static JavaScope getScope(String scope) { | ||
if (scope != null) { | ||
scope = scope.toUpperCase(); | ||
try { | ||
return JavaScope.valueOf(scope); | ||
} catch (Exception e) { | ||
// Do nothing | ||
} | ||
} | ||
return UNKNOWN; | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
....core/src/main/java/com/redhat/microprofile/commons/MicroProfileJavaCompletionParams.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2020 Red Hat Inc. and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
package com.redhat.microprofile.commons; | ||
|
||
import org.eclipse.lsp4j.Position; | ||
|
||
/** | ||
* MicroProfile Java completion parameters. | ||
* | ||
* @author Angelo ZERR | ||
* | ||
*/ | ||
public class MicroProfileJavaCompletionParams { | ||
|
||
private String uri; | ||
private Position position; | ||
|
||
public MicroProfileJavaCompletionParams() { | ||
|
||
} | ||
|
||
public MicroProfileJavaCompletionParams(String uri, Position position) { | ||
this(); | ||
setUri(uri); | ||
setPosition(position); | ||
} | ||
|
||
/** | ||
* Returns the java file uri. | ||
* | ||
* @return the java file uri. | ||
*/ | ||
public String getUri() { | ||
return uri; | ||
} | ||
|
||
/** | ||
* Set the java file uri. | ||
* | ||
* @param uri the java file uri. | ||
*/ | ||
public void setUri(String uri) { | ||
this.uri = uri; | ||
} | ||
|
||
/** | ||
* Returns the completion position | ||
* | ||
* @return the completion position | ||
*/ | ||
public Position getPosition() { | ||
return position; | ||
} | ||
|
||
/** | ||
* Sets the completion position | ||
* | ||
* @param position the completion position | ||
*/ | ||
public void setPosition(Position position) { | ||
this.position = position; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
....core/src/main/java/com/redhat/microprofile/commons/MicroProfileJavaCompletionResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2020 Red Hat Inc. and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
package com.redhat.microprofile.commons; | ||
|
||
/** | ||
* MicroProfile Java completion result. | ||
* | ||
* @author Angelo ZERR | ||
* | ||
*/ | ||
public class MicroProfileJavaCompletionResult { | ||
|
||
private JavaScope scope; | ||
|
||
public JavaScope getScope() { | ||
return scope; | ||
} | ||
|
||
public void setScope(JavaScope scope) { | ||
this.scope = scope; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
...ain/java/com/redhat/microprofile/jdt/internal/core/java/completion/CompletionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2020 Red Hat Inc. and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
package com.redhat.microprofile.jdt.internal.core.java.completion; | ||
|
||
import org.eclipse.core.runtime.IProgressMonitor; | ||
import org.eclipse.jdt.core.CompletionContext; | ||
import org.eclipse.jdt.core.ICompilationUnit; | ||
import org.eclipse.jdt.core.IJavaElement; | ||
import org.eclipse.jdt.core.JavaModelException; | ||
import org.eclipse.jdt.core.dom.CompilationUnit; | ||
import org.eclipse.jdt.core.dom.NodeFinder; | ||
import org.eclipse.jdt.core.manipulation.CoreASTProvider; | ||
import org.eclipse.jdt.internal.codeassist.InternalCompletionContext; | ||
import org.eclipse.jdt.internal.codeassist.complete.CompletionOnMarkerAnnotationName; | ||
import org.eclipse.jdt.internal.compiler.ast.ASTNode; | ||
import org.eclipse.jdt.internal.compiler.ast.TypeReference; | ||
import org.eclipse.jdt.internal.compiler.lookup.Binding; | ||
|
||
import com.redhat.microprofile.commons.JavaScope; | ||
import com.redhat.microprofile.commons.MicroProfileJavaCompletionParams; | ||
import com.redhat.microprofile.commons.MicroProfileJavaCompletionResult; | ||
import com.redhat.microprofile.jdt.core.utils.IJDTUtils; | ||
|
||
/** | ||
* Java completion handler. | ||
* | ||
* @author Angelo ZERR | ||
* | ||
*/ | ||
public class CompletionHandler { | ||
|
||
/** | ||
* Returns completion result for the given uri and position. | ||
* | ||
* @param params the completion parameters | ||
* @param utils the utilities class | ||
* @param monitor the monitor | ||
* @return completion result for the given uri and position. | ||
* @throws JavaModelException | ||
*/ | ||
public MicroProfileJavaCompletionResult completion(MicroProfileJavaCompletionParams params, IJDTUtils utils, | ||
IProgressMonitor monitor) throws JavaModelException { | ||
MicroProfileJavaCompletionResult result = new MicroProfileJavaCompletionResult(); | ||
result.setScope(JavaScope.UNKNOWN); | ||
|
||
String uri = params.getUri(); | ||
ICompilationUnit unit = utils.resolveCompilationUnit(uri); | ||
if (unit == null) { | ||
return result; | ||
} | ||
int offset = utils.toOffset(unit.getBuffer(), params.getPosition().getLine(), | ||
params.getPosition().getCharacter()); | ||
|
||
|
||
CompilationUnit ast = CoreASTProvider.getInstance().getAST(unit, CoreASTProvider.WAIT_YES, monitor); | ||
NodeFinder fNodeFinder = new NodeFinder(ast, offset, 1); | ||
org.eclipse.jdt.core.dom.ASTNode coveringNode = fNodeFinder.getCoveringNode(); | ||
org.eclipse.jdt.core.dom.ASTNode coveredNode = fNodeFinder.getCoveredNode(); | ||
|
||
CompletionProposalRequestor collector = new CompletionProposalRequestor(unit, offset); | ||
unit.codeComplete(offset, collector); | ||
|
||
CompletionContext completionContext = collector.getContext(); | ||
if (completionContext instanceof InternalCompletionContext) { | ||
ASTNode node = ((InternalCompletionContext) completionContext).getCompletionNode(); | ||
// ((InternalCompletionContext) completionContext). | ||
((InternalCompletionContext) completionContext).getEnclosingElement(); | ||
IJavaElement element = unit.getElementAt(offset); | ||
System.err.println(element); | ||
result.setScope(getJavaScope(node)); | ||
} | ||
return result; | ||
} | ||
|
||
private static JavaScope getJavaScope(ASTNode node) { | ||
if (node instanceof CompletionOnMarkerAnnotationName) { | ||
CompletionOnMarkerAnnotationName marker = (CompletionOnMarkerAnnotationName) node; | ||
if ((node.bits & ASTNode.T_JavaLangAnnotationTarget) != 0) { | ||
|
||
} | ||
|
||
TypeReference typeReference = marker.type; | ||
Binding recipent = marker.resolvedType; | ||
if (recipent != null) { | ||
int kind = recipent.kind(); | ||
switch (kind) { | ||
case Binding.FIELD: | ||
return JavaScope.FIELD; | ||
case Binding.METHOD: | ||
return JavaScope.METHOD; | ||
case Binding.TYPE_PARAMETER: | ||
return JavaScope.PARAMETER; | ||
case Binding.TYPE: | ||
return JavaScope.TYPE; | ||
default: | ||
break; | ||
} | ||
} | ||
} | ||
return JavaScope.UNKNOWN; | ||
} | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
...om/redhat/microprofile/jdt/internal/core/java/completion/CompletionProposalRequestor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2020 Red Hat Inc. and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
package com.redhat.microprofile.jdt.internal.core.java.completion; | ||
|
||
import org.eclipse.jdt.core.CompletionContext; | ||
import org.eclipse.jdt.core.CompletionProposal; | ||
import org.eclipse.jdt.core.CompletionRequestor; | ||
import org.eclipse.jdt.core.ICompilationUnit; | ||
|
||
/** | ||
* JDT completion request to get the JDT completion context. | ||
* | ||
* @author Angelo ZERR | ||
* | ||
*/ | ||
public class CompletionProposalRequestor extends CompletionRequestor { | ||
|
||
private CompletionContext context; | ||
|
||
public void acceptContext(CompletionContext context) { | ||
super.acceptContext(context); | ||
this.context = context; | ||
} | ||
|
||
public CompletionProposalRequestor(ICompilationUnit unit, int offset) { | ||
super.setRequireExtendedContext(true); | ||
} | ||
|
||
@Override | ||
public void accept(CompletionProposal proposal) { | ||
|
||
} | ||
|
||
public CompletionContext getContext() { | ||
return context; | ||
} | ||
} |
Oops, something went wrong.