Skip to content

Commit

Permalink
Merge pull request apache#6904 from dbalek/dbalek/lsp-completion-labe…
Browse files Browse the repository at this point in the history
…l-details

LSP: Using labelDetails in CompletionItems
  • Loading branch information
dbalek authored Jan 3, 2024
2 parents df47821 + b081810 commit 46477fc
Show file tree
Hide file tree
Showing 13 changed files with 197 additions and 86 deletions.
2 changes: 1 addition & 1 deletion enterprise/micronaut/nbproject/project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
<compile-dependency/>
<run-dependency>
<release-version>1</release-version>
<specification-version>1.23</specification-version>
<specification-version>1.24</specification-version>
</run-dependency>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ public Completion createControllerMethodItem(CompilationInfo info, VariableEleme
ExecutableType type = (ExecutableType) info.getTypes().asMemberOf((DeclaredType) delegateRepositoryType, delegateMethod);
Iterator<? extends VariableElement> it = delegateMethod.getParameters().iterator();
Iterator<? extends TypeMirror> tIt = type.getParameterTypes().iterator();
StringBuilder label = new StringBuilder();
StringBuilder labelDetail = new StringBuilder();
StringBuilder sortParams = new StringBuilder();
label.append(methodName).append("(");
labelDetail.append("(");
sortParams.append('(');
int cnt = 0;
while(it.hasNext() && tIt.hasNext()) {
Expand All @@ -87,15 +87,15 @@ public Completion createControllerMethodItem(CompilationInfo info, VariableEleme
cnt++;
String paramTypeName = MicronautDataCompletionTask.getTypeName(info, tm, false, delegateMethod.isVarArgs() && !tIt.hasNext()).toString();
String paramName = it.next().getSimpleName().toString();
label.append(paramTypeName).append(' ').append(paramName);
labelDetail.append(paramTypeName).append(' ').append(paramName);
sortParams.append(paramTypeName);
if (tIt.hasNext()) {
label.append(", ");
labelDetail.append(", ");
sortParams.append(',');
}
}
sortParams.append(')');
label.append(')');
labelDetail.append(')');
TypeMirror returnType = type.getReturnType();
if ("findAll".contentEquals(delegateMethod.getSimpleName()) && !delegateMethod.getParameters().isEmpty() && returnType.getKind() == TypeKind.DECLARED) {
TypeElement te = (TypeElement) ((DeclaredType) returnType).asElement();
Expand All @@ -104,12 +104,13 @@ public Completion createControllerMethodItem(CompilationInfo info, VariableEleme
returnType = (ExecutableType) info.getTypes().asMemberOf((DeclaredType) returnType, getContentMethod.get());
}
}
label.append(" : ").append(MicronautDataCompletionTask.getTypeName(info, returnType, false, false));
FileObject fo = info.getFileObject();
ElementHandle<VariableElement> repositoryHandle = ElementHandle.create(delegateRepository);
ElementHandle<ExecutableElement> methodHandle = ElementHandle.create(delegateMethod);
return CompletionCollector.newBuilder(String.format("%s - generate", label.toString()))
return CompletionCollector.newBuilder(methodName)
.kind(Completion.Kind.Method)
.labelDetail(String.format("%s - generate", labelDetail.toString()))
.labelDescription(MicronautDataCompletionTask.getTypeName(info, returnType, false, false).toString())
.sortText(String.format("%04d%s#%02d%s", 1500, methodName, cnt, sortParams.toString()))
.insertTextFormat(Completion.TextFormat.PlainText)
.textEdit(new TextEdit(offset, offset, ""))
Expand Down Expand Up @@ -203,11 +204,11 @@ public Completion createJavaElementItem(CompilationInfo info, Element element, i
if (element.getKind() == ElementKind.METHOD) {
Iterator<? extends VariableElement> it = ((ExecutableElement)element).getParameters().iterator();
Iterator<? extends TypeMirror> tIt = ((ExecutableType) element.asType()).getParameterTypes().iterator();
StringBuilder label = new StringBuilder();
StringBuilder labelDetail = new StringBuilder();
StringBuilder insertText = new StringBuilder();
StringBuilder sortParams = new StringBuilder();
label.append(simpleName).append("(");
insertText.append(simpleName).append("(");
labelDetail.append('(');
insertText.append(simpleName).append('(');
sortParams.append('(');
int cnt = 0;
boolean asTemplate = false;
Expand All @@ -219,21 +220,23 @@ public Completion createJavaElementItem(CompilationInfo info, Element element, i
cnt++;
String paramTypeName = MicronautDataCompletionTask.getTypeName(info, tm, false, ((ExecutableElement)element).isVarArgs() && !tIt.hasNext()).toString();
String paramName = it.next().getSimpleName().toString();
label.append(paramTypeName).append(' ').append(paramName);
labelDetail.append(paramTypeName).append(' ').append(paramName);
sortParams.append(paramTypeName);
insertText.append("${").append(cnt).append(":").append(paramName).append("}");
insertText.append("${").append(cnt).append(':').append(paramName).append('}');
asTemplate = true;
if (tIt.hasNext()) {
label.append(", ");
labelDetail.append(", ");
sortParams.append(',');
insertText.append(", ");
}
}
label.append(") : ").append(MicronautDataCompletionTask.getTypeName(info, ((ExecutableElement)element).getReturnType(), false, false).toString());
labelDetail.append(')');
insertText.append(')');
sortParams.append(')');
return CompletionCollector.newBuilder(label.toString())
return CompletionCollector.newBuilder(simpleName)
.kind(Completion.Kind.Method)
.labelDetail(labelDetail.toString())
.labelDescription(MicronautDataCompletionTask.getTypeName(info, ((ExecutableElement)element).getReturnType(), false, false).toString())
.sortText(String.format("%04d%s#%02d%s", 100, simpleName, cnt, sortParams.toString()))
.insertText(insertText.toString())
.insertTextFormat(asTemplate ? Completion.TextFormat.Snippet : Completion.TextFormat.PlainText)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ protected void performTest(String text, int offset, String goldenFileName) throw
|| item.getLabel().equals("nbjavac") //ignoring 'nbjavac' package from nbjavac
|| item.getLabel().equals("oracle"))) { //ignoring 'oracle' package present in some JDK builds
out.write(item.getLabel());
if (item.getLabelDetail() != null) {
out.write(item.getLabelDetail());
}
if (item.getLabelDescription() != null) {
out.write(" : " + item.getLabelDescription());
}
out.write("\n");
}
}
Expand Down
20 changes: 18 additions & 2 deletions ide/api.lsp/apichanges.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,22 @@
<!-- ACTUAL CHANGES BEGIN HERE: -->

<changes>
<change id="Completion_getLabelDetail">
<api name="LSP_API"/>
<summary>Added Completion.getLabelDetail() and Completion.getLabelDescription() methods.</summary>
<version major="1" minor="24"/>
<date day="2" month="1" year="2024"/>
<author login="dbalek"/>
<compatibility binary="compatible" source="compatible" addition="yes" deletion="no"/>
<description>
<a href="@TOP@/org/netbeans/api/lsp/Completion.html#getLabelDetail--">Completion.getLabelDetail()</a>
to get an optional string describing function signatures or type annotations.
<a href="@TOP@/org/netbeans/api/lsp/Completion.html#getLabelDescription--">Completion.getLabelDescription()</a>
to get an optional string describing fully qualified names or file path.
</description>
<class package="org.netbeans.api.lsp" name="Completion"/>
<class package="org.netbeans.spi.lsp" name="CompletionCollector"/>
</change>
<change id="CodeActionProvider">
<api name="LSP_API"/>
<summary>Added CodeActionProvider interface</summary>
Expand Down Expand Up @@ -108,13 +124,13 @@
</change>
<change id="Completion_getCommand">
<api name="LSP_API"/>
<summary>Added URL to diagnostic code description</summary>
<summary>Added Completion.getCommand() to get an optional command</summary>
<version major="1" minor="17"/>
<date day="23" month="5" year="2023"/>
<author login="dbalek"/>
<compatibility binary="compatible" source="compatible" addition="yes" deletion="no"/>
<description>
<a href="@TOP@/org/netbeans/api/lsp/Completion.html#getCommand--">Completion.getCommand</a> to get an optional command
<a href="@TOP@/org/netbeans/api/lsp/Completion.html#getCommand--">Completion.getCommand()</a> to get an optional command
that is executed after inserting the completion.
</description>
<class package="org.netbeans.api.lsp" name="Completion"/>
Expand Down
2 changes: 1 addition & 1 deletion ide/api.lsp/manifest.mf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Manifest-Version: 1.0
OpenIDE-Module: org.netbeans.api.lsp/1
OpenIDE-Module-Localizing-Bundle: org/netbeans/api/lsp/Bundle.properties
OpenIDE-Module-Specification-Version: 1.23
OpenIDE-Module-Specification-Version: 1.24
AutoUpdate-Show-In-Client: false
34 changes: 31 additions & 3 deletions ide/api.lsp/src/org/netbeans/api/lsp/Completion.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,17 @@ public final class Completion {
static {
CompletionAccessor.setDefault(new CompletionAccessor() {
@Override
public Completion createCompletion(String label, Kind kind, List<Tag> tags, CompletableFuture<String> detail, CompletableFuture<String> documentation,
public Completion createCompletion(String label, String labelDetail, String description, Kind kind, List<Tag> tags, CompletableFuture<String> detail, CompletableFuture<String> documentation,
boolean preselect, String sortText, String filterText, String insertText, TextFormat insertTextFormat, TextEdit textEdit, Command command,
CompletableFuture<List<TextEdit>> additionalTextEdits, List<Character> commitCharacters) {
return new Completion(label, kind, tags, detail, documentation, preselect, sortText, filterText, insertText, insertTextFormat, textEdit, command, additionalTextEdits, commitCharacters);
return new Completion(label, labelDetail, description, kind, tags, detail, documentation, preselect, sortText, filterText, insertText, insertTextFormat, textEdit, command, additionalTextEdits, commitCharacters);
}
});
}

private final String label;
private final String labelDetail;
private final String labelDescription;
private final Kind kind;
private final List<Tag> tags;
private final CompletableFuture<String> detail;
Expand All @@ -66,10 +68,12 @@ public Completion createCompletion(String label, Kind kind, List<Tag> tags, Comp
private final CompletableFuture<List<TextEdit>> additionalTextEdits;
private final List<Character> commitCharacters;

private Completion(String label, Kind kind, List<Tag> tags, CompletableFuture<String> detail, CompletableFuture<String> documentation,
private Completion(String label, String labelDetail, String labelDescription, Kind kind, List<Tag> tags, CompletableFuture<String> detail, CompletableFuture<String> documentation,
boolean preselect, String sortText, String filterText, String insertText, TextFormat insertTextFormat,
TextEdit textEdit, Command command, CompletableFuture<List<TextEdit>> additionalTextEdits, List<Character> commitCharacters) {
this.label = label;
this.labelDetail = labelDetail;
this.labelDescription = labelDescription;
this.kind = kind;
this.tags = tags;
this.detail = detail;
Expand All @@ -96,6 +100,30 @@ public String getLabel() {
return label;
}

/**
* An optional string which is rendered less prominently directly after
* {@link Completion#getLabel() label}, without any spacing. Should be
* used for function signatures or type annotations.
*
* @since 1.24
*/
@CheckForNull
public String getLabelDetail() {
return labelDetail;
}

/**
* An optional string which is rendered less prominently after
* {@link Completion#getLabelDetail() label detail}. Should be used for fully qualified
* names or file path.
*
* @since 1.24
*/
@CheckForNull
public String getLabelDescription() {
return labelDescription;
}

/**
* The kind of this completion.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static void setDefault(@NonNull final CompletionAccessor accessor) {
DEFAULT = accessor;
}

public abstract Completion createCompletion(String label, Completion.Kind kind, List<Completion.Tag> tags, CompletableFuture<String> detail, CompletableFuture<String> documentation,
public abstract Completion createCompletion(String label, String labelDetail, String labelDescription, Completion.Kind kind, List<Completion.Tag> tags, CompletableFuture<String> detail, CompletableFuture<String> documentation,
boolean preselect, String sortText, String filterText, String insertText, Completion.TextFormat insertTextFormat,
TextEdit textEdit, Command command, CompletableFuture<List<TextEdit>> additionalTextEdits, List<Character> commitCharacters);
}
36 changes: 32 additions & 4 deletions ide/api.lsp/src/org/netbeans/spi/lsp/CompletionCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ public static Builder newBuilder(@NonNull String label) {
public static final class Builder {

private String label;
private String labelDetail;
private String labelDescription;
private Completion.Kind kind;
private List<Completion.Tag> tags;
private CompletableFuture<String> detail;
Expand Down Expand Up @@ -120,6 +122,32 @@ public Builder label(@NonNull String label) {
return this;
}

/**
* An optional string which is rendered less prominently directly after
* {@link Completion#getLabel() label}, without any spacing. Should be
* used for function signatures or type annotations.
*
* @since 1.24
*/
@NonNull
public Builder labelDetail(@NonNull String labelDetail) {
this.labelDetail = labelDetail;
return this;
}

/**
* An optional string which is rendered less prominently after
* {@link Completion#getLabelDetail() label detail}. Should be used for fully qualified
* names or file path.
*
* @since 1.24
*/
@NonNull
public Builder labelDescription(@NonNull String labelDescription) {
this.labelDescription = labelDescription;
return this;
}

/**
* The kind of this completion.
*
Expand Down Expand Up @@ -344,10 +372,10 @@ public Builder addCommitCharacter(char commitCharacter) {
*/
@NonNull
public Completion build() {
return CompletionAccessor.getDefault().createCompletion(label, kind,
tags, detail, documentation, preselect, sortText, filterText,
insertText, insertTextFormat, textEdit, command, additionalTextEdits,
commitCharacters);
return CompletionAccessor.getDefault().createCompletion(label, labelDetail,
labelDescription, kind, tags, detail, documentation, preselect, sortText,
filterText, insertText, insertTextFormat, textEdit, command,
additionalTextEdits, commitCharacters);
}

private static class LazyCompletableFuture<T> extends CompletableFuture<T> {
Expand Down
2 changes: 1 addition & 1 deletion java/java.editor/nbproject/project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
<compile-dependency/>
<run-dependency>
<release-version>1</release-version>
<specification-version>1.20</specification-version>
<specification-version>1.24</specification-version>
</run-dependency>
</dependency>
<dependency>
Expand Down
Loading

0 comments on commit 46477fc

Please sign in to comment.