Skip to content

Commit

Permalink
Merge pull request #453 from eclipse/javaContext
Browse files Browse the repository at this point in the history
Make snippets context-aware
  • Loading branch information
turkeylurkey authored May 25, 2023
2 parents d5ebc8a + 5d5e238 commit 232c75a
Show file tree
Hide file tree
Showing 31 changed files with 1,730 additions and 162 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
import org.eclipse.lsp4jakarta.commons.JakartaClasspathParams;
import org.eclipse.lsp4jakarta.commons.JakartaDiagnosticsParams;
import org.eclipse.lsp4jakarta.commons.JakartaJavaCodeActionParams;
import org.eclipse.lsp4jakarta.commons.JakartaJavaCompletionParams;
import org.eclipse.lsp4jakarta.commons.JavaCursorContextKind;
import org.eclipse.lsp4jakarta.commons.JavaCursorContextResult;
import org.eclipse.lsp4jakarta.jdt.core.JDTServicesManager;
import org.eclipse.lsp4jakarta.jdt.core.JDTUtils;

Expand Down Expand Up @@ -69,6 +72,19 @@ public CompletableFuture<List<String>> getContextBasedFilter(JakartaClasspathPar
});
}

@Override
public CompletableFuture<JavaCursorContextResult> getJavaCursorContext(JakartaJavaCompletionParams params) {
JDTUtils utils = new JDTUtils();
return CompletableFutures.computeAsync((cancelChecker) -> {
IProgressMonitor monitor = getProgressMonitor(cancelChecker);
try {
return JDTServicesManager.getInstance().javaCursorContext(params, utils, monitor);
} catch (JavaModelException e) {
return new JavaCursorContextResult(JavaCursorContextKind.IN_EMPTY_FILE, "");
}
});
}

public CompletableFuture<List<CodeAction>> getCodeAction(JakartaJavaCodeActionParams params) {
JDTUtils utils = new JDTUtils();

Expand Down
1 change: 1 addition & 0 deletions jakarta.jdt/org.eclipse.lsp4jakarta.jdt.core/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<command id="jakarta/java/classpath"/>
<command id="jakarta/java/diagnostics"/>
<command id="jakarta/java/codeaction"/>
<command id="jakarta/java/cursorcontext"/>
</delegateCommandHandler>
</extension>
</plugin>
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*******************************************************************************
* Copyright (c) 2021, 2023 Red Hat Inc. and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*
* Contributors:
* Red Hat Inc. - initial API and implementation
* IBM Corporation - convert for Jakarta
*******************************************************************************/
package org.eclipse.lsp4jakarta.commons;


import org.eclipse.lsp4j.Position;

/**
* Parameters that are passed on completion trigger for MicroProfile projects in Java files
*
* @author datho7561
*/
public class JakartaJavaCompletionParams {

private String uri;
private Position position;

public JakartaJavaCompletionParams() {

}

public JakartaJavaCompletionParams(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 hover position
*
* @return the hover position
*/
public Position getPosition() {
return position;
}

/**
* Sets the hover position
*
* @param position the hover position
*/
public void setPosition(Position position) {
this.position = position;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*******************************************************************************
* Copyright (c) 2023 Red Hat Inc. and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*
* Contributors:
* Red Hat Inc. - initial API and implementation
* IBM Corporation - convert to Jakarta
*******************************************************************************/
package org.eclipse.lsp4jakarta.commons;

import org.eclipse.lsp4j.CompletionList;

/**
* Represents completion information and context calculated by the Java language server component.
*/
public class JakartaJavaCompletionResult {
private CompletionList completionList;
private JavaCursorContextResult cursorContext;

public JakartaJavaCompletionResult(CompletionList completionList,
JavaCursorContextResult cursorContext) {
this.completionList = completionList;
this.cursorContext = cursorContext;
}

public JakartaJavaCompletionResult() {
this(null, null);
}

/**
* Returns the list of completion items contributed by the Java language server
* component.
*
* @return the list of completion items contributed by the Java language server
* component
*/
public CompletionList getCompletionList() {
return completionList;
}

/**
* Returns information on the context of the cursor in the Java file, calculated
* by the Java language server component.
*
* @return information on the context of the cursor in the Java file, calculated
* by the Java language server component
*/
public JavaCursorContextResult getCursorContext() {
return cursorContext;
}

}
Loading

0 comments on commit 232c75a

Please sign in to comment.