Skip to content

Commit

Permalink
Filter for Java (server) snippet
Browse files Browse the repository at this point in the history
Fixes redhat-developer#265

Signed-off-by: azerr <azerr@redhat.com>
  • Loading branch information
angelozerr committed Apr 7, 2020
1 parent ad9c9cf commit adba5df
Show file tree
Hide file tree
Showing 20 changed files with 304 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
*******************************************************************************/
package com.redhat.microprofile.commons;

import java.util.List;

/**
* MicroProfile Java Project labels
*
Expand All @@ -21,6 +23,8 @@ public class MicroProfileJavaProjectLabelsParams {

private String uri;

private List<String> types;

/**
* Returns the Java file uri.
*
Expand All @@ -38,4 +42,12 @@ public String getUri() {
public void setUri(String uri) {
this.uri = uri;
}

public List<String> getTypes() {
return types;
}

public void setTypes(List<String> types) {
this.types = types;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.redhat.microprofile.commons.ProjectLabelInfoEntry;
import com.redhat.microprofile.jdt.core.utils.IJDTUtils;
import com.redhat.microprofile.jdt.core.utils.JDTMicroProfileUtils;
import com.redhat.microprofile.jdt.core.utils.JDTTypeUtils;
import com.redhat.microprofile.jdt.internal.core.ProjectLabelRegistry;

/**
Expand Down Expand Up @@ -54,7 +55,7 @@ public List<ProjectLabelInfoEntry> getProjectLabelInfo() {
IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();

for (IProject project : projects) {
ProjectLabelInfoEntry info = getProjectLabelInfo(project);
ProjectLabelInfoEntry info = getProjectLabelInfo(project, null);
if (info != null) {
results.add(info);
}
Expand All @@ -66,12 +67,13 @@ public List<ProjectLabelInfoEntry> getProjectLabelInfo() {
* Returns project label results for the given Eclipse project.
*
* @param project Eclipse project.
* @param types
* @return project label results for the given Eclipse project.
*/
private ProjectLabelInfoEntry getProjectLabelInfo(IProject project) {
private ProjectLabelInfoEntry getProjectLabelInfo(IProject project, List<String> types) {
String uri = JDTMicroProfileUtils.getProjectURI(project);
if (uri != null) {
return new ProjectLabelInfoEntry(uri, getProjectLabels(project));
return new ProjectLabelInfoEntry(uri, getProjectLabels(project, types));
}
return null;
}
Expand All @@ -91,10 +93,10 @@ public ProjectLabelInfoEntry getProjectLabelInfo(MicroProfileJavaProjectLabelsPa
// The uri doesn't belong to an Eclipse project
return ProjectLabelInfoEntry.EMPTY_PROJECT_INFO;
}
return getProjectLabelInfo(file.getProject());
return getProjectLabelInfo(file.getProject(), params.getTypes());
}

private List<String> getProjectLabels(IProject project) {
private List<String> getProjectLabels(IProject project, List<String> types) {
IJavaProject javaProject = JavaCore.create(project);

if (javaProject == null) {
Expand All @@ -106,6 +108,13 @@ private List<String> getProjectLabels(IProject project) {
for (ProjectLabelDefinition definition : definitions) {
projectLabels.addAll(definition.getProjectLabels(javaProject));
}
if (types != null) {
for (String type : types) {
if (JDTTypeUtils.findType(javaProject, type) != null) {
projectLabels.add(type);
}
}
}

return projectLabels;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import static com.redhat.microprofile.jdt.internal.core.ls.ArgumentUtils.getFirst;
import static com.redhat.microprofile.jdt.internal.core.ls.ArgumentUtils.getString;
import static com.redhat.microprofile.jdt.internal.core.ls.ArgumentUtils.getStringList;

import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -57,8 +58,10 @@ private static Object getProjectLabelInfo(List<Object> arguments, String command
"Command '%s' must be called with required MicroProfileJavaProjectLabelsParams.uri (java file URI)!",
commandId));
}
List<String> types = getStringList(obj, "types");
MicroProfileJavaProjectLabelsParams params = new MicroProfileJavaProjectLabelsParams();
params.setUri(javaFileUri);
params.setTypes(types);
return ProjectLabelManager.getInstance().getProjectLabelInfo(params, JDTUtilsLSImpl.getInstance(), monitor);
}
}
Empty file modified microprofile.ls/com.redhat.microprofile.ls/mvnw
100755 → 100644
Empty file.
Empty file modified microprofile.ls/com.redhat.microprofile.ls/mvnw.cmd
100755 → 100644
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
*******************************************************************************/
package com.redhat.microprofile.commons;

import java.util.List;

/**
* MicroProfile Java Project labels
*
Expand All @@ -21,6 +23,8 @@ public class MicroProfileJavaProjectLabelsParams {

private String uri;

private List<String> types;

/**
* Returns the Java file uri.
*
Expand All @@ -38,4 +42,12 @@ public String getUri() {
public void setUri(String uri) {
this.uri = uri;
}

public List<String> getTypes() {
return types;
}

public void setTypes(List<String> types) {
this.types = types;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
import com.redhat.microprofile.ls.commons.snippets.TextDocumentSnippetRegistry;
import com.redhat.microprofile.settings.MicroProfileCodeLensSettings;
import com.redhat.microprofile.settings.SharedSettings;
import com.redhat.microprofile.snippets.LanguageId;
import com.redhat.microprofile.snippets.SnippetContextForJava;

/**
* LSP text document service for Java file.
Expand All @@ -80,7 +80,8 @@ public JavaTextDocumentService(MicroProfileLanguageServer microprofileLanguageSe
this.documents = new JavaTextDocuments(microprofileLanguageServer);
}

// ------------------------------ did* for Java file ------------------------------
// ------------------------------ did* for Java file
// ------------------------------

@Override
public void didOpen(DidOpenTextDocumentParams params) {
Expand Down Expand Up @@ -110,32 +111,31 @@ public void didSave(DidSaveTextDocumentParams params) {

@Override
public CompletableFuture<Either<List<CompletionItem>, CompletionList>> completion(CompletionParams params) {
return CompletableFutures.computeAsync(cancel -> {
try {
// Returns java snippets
TextDocument document = documents.get(params.getTextDocument().getUri());
int completionOffset = document.offsetAt(params.getPosition());
boolean canSupportMarkdown = true;
CompletionList list = new CompletionList();
list.setItems(new ArrayList<>());
getSnippetRegistry().getCompletionItems(document, completionOffset, canSupportMarkdown, context -> {
return true;
}).forEach(item -> {
list.getItems().add(item);
});
return Either.forRight(list);
} catch (BadLocationException e) {
LOGGER.log(Level.SEVERE, "Error while getting java completions", e);
return Either.forRight(null);
}
});
}

private TextDocumentSnippetRegistry getSnippetRegistry() {
if (snippetRegistry == null) {
snippetRegistry = new TextDocumentSnippetRegistry(LanguageId.java.name());
}
return snippetRegistry;
JavaTextDocument document = documents.get(params.getTextDocument().getUri());
return document.executeIfInMicroProfileProject((projectInfo) -> {
return CompletableFutures.computeAsync(cancel -> {
try {
// Returns java snippets
int completionOffset = document.offsetAt(params.getPosition());
boolean canSupportMarkdown = true;
CompletionList list = new CompletionList();
list.setItems(new ArrayList<>());
documents.getSnippetRegistry()
.getCompletionItems(document, completionOffset, canSupportMarkdown, context -> {
if (context != null && context instanceof SnippetContextForJava) {
return ((SnippetContextForJava) context).isMatch(projectInfo);
}
return true;
}).forEach(item -> {
list.getItems().add(item);
});
return null;// Either.forRight(list);
} catch (BadLocationException e) {
LOGGER.log(Level.SEVERE, "Error while getting java completions", e);
return Either.forRight(null);
}
});
}, Either.forRight(null));
}

// ------------------------------ Code Lens ------------------------------
Expand All @@ -152,7 +152,7 @@ public CompletableFuture<List<? extends CodeLens>> codeLens(CodeLensParams param
return CompletableFuture.completedFuture(Collections.emptyList());
}
JavaTextDocument document = documents.get(params.getTextDocument().getUri());
return document.executeIfInMicroProfileProject(() -> {
return document.executeIfInMicroProfileProject((projectInfo) -> {
MicroProfileJavaCodeLensParams javaParams = new MicroProfileJavaCodeLensParams(
params.getTextDocument().getUri());
if (sharedSettings.getCommandCapabilities().isCommandSupported(CommandKind.COMMAND_OPEN_URI)) {
Expand All @@ -171,7 +171,7 @@ public CompletableFuture<List<? extends CodeLens>> codeLens(CodeLensParams param
@Override
public CompletableFuture<List<Either<Command, CodeAction>>> codeAction(CodeActionParams params) {
JavaTextDocument document = documents.get(params.getTextDocument().getUri());
return document.executeIfInMicroProfileProject(() -> {
return document.executeIfInMicroProfileProject((projectInfo) -> {
MicroProfileJavaCodeActionParams javaParams = new MicroProfileJavaCodeActionParams();
javaParams.setTextDocument(params.getTextDocument());
javaParams.setRange(params.getRange());
Expand All @@ -195,7 +195,7 @@ public CompletableFuture<List<Either<Command, CodeAction>>> codeAction(CodeActio
@Override
public CompletableFuture<Hover> hover(HoverParams params) {
JavaTextDocument document = documents.get(params.getTextDocument().getUri());
return document.executeIfInMicroProfileProject(() -> {
return document.executeIfInMicroProfileProject((projectinfo) -> {
boolean markdownSupported = sharedSettings.getHoverSettings().isContentFormatSupported(MarkupKind.MARKDOWN);
DocumentFormat documentFormat = markdownSupported ? DocumentFormat.Markdown : DocumentFormat.PlainText;
MicroProfileJavaHoverParams javaParams = new MicroProfileJavaHoverParams(params.getTextDocument().getUri(),
Expand All @@ -212,7 +212,7 @@ public CompletableFuture<Hover> hover(HoverParams params) {
* @param document the opened Java file.
*/
private void triggerValidationFor(JavaTextDocument document) {
document.executeIfInMicroProfileProject(() -> {
document.executeIfInMicroProfileProject((projectinfo) -> {
String uri = document.getUri();
triggerValidationFor(Arrays.asList(uri));
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.redhat.microprofile.ls;

import java.util.ArrayList;
import java.util.List;

import com.redhat.microprofile.ls.commons.snippets.Snippet;
import com.redhat.microprofile.ls.commons.snippets.TextDocumentSnippetRegistry;
import com.redhat.microprofile.snippets.LanguageId;
import com.redhat.microprofile.snippets.SnippetContextForJava;

public class JavaTextDocumentSnippetRegistry extends TextDocumentSnippetRegistry {

private List<String> types;

public JavaTextDocumentSnippetRegistry() {
super(LanguageId.java.name());
}

public List<String> getTypes() {
if (types != null) {
return types;
}
types = collectTypes();
return types;
}

private synchronized List<String> collectTypes() {
if (types != null) {
return types;
}
List<String> types = new ArrayList<>();
for (Snippet snippet : getSnippets()) {
if (snippet.getContext() != null && snippet.getContext() instanceof SnippetContextForJava) {
List<String> t = ((SnippetContextForJava) snippet.getContext()).getTypes();
if (t != null) {
types.addAll(t);
}
}
}
return types;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;
import java.util.function.Function;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -49,6 +49,8 @@ class JavaTextDocuments extends TextDocuments<JavaTextDocument> {

private final MicroProfileJavaProjectLabelsProvider provider;

private JavaTextDocumentSnippetRegistry snippetRegistry;

/**
* Opened Java file.
*
Expand Down Expand Up @@ -80,13 +82,13 @@ public void setProjectURI(String projectURI) {
* @return the given code only if the Java file belongs to a MicroProfile
* project.
*/
public <T> CompletableFuture<T> executeIfInMicroProfileProject(Supplier<CompletableFuture<T>> code,
T defaultValue) {
public <T> CompletableFuture<T> executeIfInMicroProfileProject(
Function<ProjectLabelInfoEntry, CompletableFuture<T>> code, T defaultValue) {
return getProjectInfo(this).thenComposeAsync(projectInfo -> {
if (!isMicroProfileProject(projectInfo)) {
return CompletableFuture.completedFuture(defaultValue);
}
return code.get();
return code.apply(projectInfo);
});
}

Expand Down Expand Up @@ -150,6 +152,7 @@ CompletableFuture<ProjectLabelInfoEntry> getProjectInfoFromCache(JavaTextDocumen
// not found in the cache, load the project info from the JDT LS Extension
MicroProfileJavaProjectLabelsParams params = new MicroProfileJavaProjectLabelsParams();
params.setUri(documentURI);
params.setTypes(getSnippetRegistry().getTypes());
final CompletableFuture<ProjectLabelInfoEntry> future = provider.getJavaProjectlabels(params);
future.thenApply(entry -> {
if (entry != null) {
Expand Down Expand Up @@ -200,4 +203,11 @@ private void classpathChanged(Set<String> projectURIs) {
private static boolean isMicroProfileProject(ProjectLabelInfoEntry projectInfo) {
return projectInfo != null && projectInfo.hasLabel(MICROPROFILE_PROJECT_LABEL);
}

public JavaTextDocumentSnippetRegistry getSnippetRegistry() {
if (snippetRegistry == null) {
snippetRegistry = new JavaTextDocumentSnippetRegistry();
}
return snippetRegistry;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.TypeAdapter;

/**
* GSON deserializer to build Snippet from vscode JSON snippet.
Expand All @@ -36,9 +37,9 @@ class SnippetDeserializer implements JsonDeserializer<Snippet> {
private static final String BODY_ELT = "body";
private static final String CONTEXT_ELT = "context";

private final JsonDeserializer<? extends ISnippetContext<?>> contextDeserializer;
private final TypeAdapter<? extends ISnippetContext<?>> contextDeserializer;

public SnippetDeserializer(JsonDeserializer<? extends ISnippetContext<?>> contextDeserializer) {
public SnippetDeserializer(TypeAdapter<? extends ISnippetContext<?>> contextDeserializer) {
this.contextDeserializer = contextDeserializer;
}

Expand Down Expand Up @@ -96,12 +97,12 @@ public Snippet deserialize(JsonElement json, Type typeOfT, JsonDeserializationCo
if (contextDeserializer != null) {
JsonElement contextElt = snippetObj.get(CONTEXT_ELT);
if (contextElt != null) {
ISnippetContext<?> snippetContext = contextDeserializer.deserialize(contextElt, typeOfT, context);
ISnippetContext<?> snippetContext = contextDeserializer.fromJsonTree(contextElt);
snippet.setContext(snippetContext);
}
}

return snippet;
}

}
}
Loading

0 comments on commit adba5df

Please sign in to comment.