Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new preference to disable/enable signature description #2051

Merged
merged 2 commits into from
Apr 20, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,21 @@ public final class SignatureHelpRequestor extends CompletionRequestor {
private Map<SignatureInformation, CompletionProposal> infoProposals;
private boolean acceptType = false;
private String methodName;
private boolean isDescriptionEnabled;

public SignatureHelpRequestor(ICompilationUnit aUnit, String methodName, int offset) {
this(aUnit, offset, methodName, false);
public SignatureHelpRequestor(ICompilationUnit aUnit, String methodName, int offset, boolean isDescriptionEnabled) {
this(aUnit, offset, methodName, false, isDescriptionEnabled);
}

public SignatureHelpRequestor(ICompilationUnit aUnit, int offset, String methodName, boolean acceptType) {
public SignatureHelpRequestor(ICompilationUnit aUnit, int offset, String methodName, boolean acceptType, boolean isDescriptionEnabled) {
this.unit = aUnit;
response = new CompletionResponse();
response.setOffset(offset);
setRequireExtendedContext(true);
infoProposals = new HashMap<>();
this.acceptType = acceptType;
this.methodName = methodName;
this.isDescriptionEnabled = isDescriptionEnabled;
}

public SignatureHelp getSignatureHelp(IProgressMonitor monitor) {
Expand Down Expand Up @@ -144,7 +146,9 @@ public SignatureInformation toSignatureInformation(CompletionProposal methodProp
SignatureInformation $ = new SignatureInformation();
StringBuilder desription = descriptionProvider.createMethodProposalDescription(methodProposal);
$.setLabel(desription.toString());
$.setDocumentation(this.computeJavaDoc(methodProposal));
if (isDescriptionEnabled) {
$.setDocumentation(this.computeJavaDoc(methodProposal));
}

char[] signature = SignatureUtil.fix83600(methodProposal.getSignature());
char[][] parameterNames = methodProposal.findParameterNames(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public SignatureHelp signatureHelp(SignatureHelpParams position, IProgressMonito

SignatureHelp help = new SignatureHelp();

if (!preferenceManager.getPreferences(null).isSignatureHelpEnabled()) {
if (!preferenceManager.getPreferences().isSignatureHelpEnabled()) {
return help;
}
try {
Expand All @@ -84,7 +84,8 @@ public SignatureHelp signatureHelp(SignatureHelpParams position, IProgressMonito
}
IMethod method = getMethod(node);
String name = method != null ? method.getElementName() : getMethodName(node, unit, contextInfomation);
SignatureHelpRequestor collector = new SignatureHelpRequestor(unit, name, contextInfomation[0] + 1);
boolean isDescriptionEnabled = preferenceManager.getPreferences().isSignatureHelpDescriptionEnabled();
SignatureHelpRequestor collector = new SignatureHelpRequestor(unit, name, contextInfomation[0] + 1, isDescriptionEnabled);
if (offset > -1 && !monitor.isCanceled()) {
int pos = contextInfomation[0] + 1;
if (method != null) {
Expand All @@ -107,7 +108,7 @@ public SignatureHelp signatureHelp(SignatureHelpParams position, IProgressMonito
SignatureHelp help2 = null;
SignatureHelpRequestor collector2 = null;
if (contextInfomation[0] + 1 != offset) {
collector2 = new SignatureHelpRequestor(unit, offset, name, true);
collector2 = new SignatureHelpRequestor(unit, offset, name, true, isDescriptionEnabled);
unit.codeComplete(offset, collector2, monitor);
help2 = collector2.getSignatureHelp(monitor);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,11 @@ public class Preferences {
*/
public static final String SIGNATURE_HELP_ENABLED_KEY = "java.signatureHelp.enabled";

/**
* Preference key to enable/disable API descriptions in signature help.
*/
public static final String SIGNATURE_HELP_DESCRIPTION_ENABLED_KEY = "java.signatureHelp.description.enabled";

/**
* Preference key to enable/disable rename.
*/
Expand Down Expand Up @@ -490,6 +495,7 @@ public class Preferences {
private boolean javaFormatOnTypeEnabled;
private boolean javaSaveActionsOrganizeImportsEnabled;
private boolean signatureHelpEnabled;
private boolean signatureHelpDescriptionEnabled;
private boolean renameEnabled;
private boolean executeCommandEnabled;
private boolean autobuildEnabled;
Expand Down Expand Up @@ -720,6 +726,7 @@ public Preferences() {
javaFormatOnTypeEnabled = false;
javaSaveActionsOrganizeImportsEnabled = false;
signatureHelpEnabled = false;
signatureHelpDescriptionEnabled = false;
renameEnabled = true;
executeCommandEnabled = true;
autobuildEnabled = true;
Expand Down Expand Up @@ -830,6 +837,9 @@ public static Preferences createFrom(Map<String, Object> configuration) {
boolean signatureHelpEnabled = getBoolean(configuration, SIGNATURE_HELP_ENABLED_KEY, true);
prefs.setSignatureHelpEnabled(signatureHelpEnabled);

boolean signatureDescriptionEnabled = getBoolean(configuration, SIGNATURE_HELP_DESCRIPTION_ENABLED_KEY, true);
prefs.setSignatureHelpDescriptionEnabled(signatureDescriptionEnabled);
jdneo marked this conversation as resolved.
Show resolved Hide resolved
jdneo marked this conversation as resolved.
Show resolved Hide resolved

boolean renameEnabled = getBoolean(configuration, RENAME_ENABLED_KEY, true);
prefs.setRenameEnabled(renameEnabled);

Expand Down Expand Up @@ -1174,6 +1184,10 @@ private Preferences setSignatureHelpEnabled(boolean enabled) {
return this;
}

public void setSignatureHelpDescriptionEnabled(boolean signatureHelpDescriptionEnabled) {
this.signatureHelpDescriptionEnabled = signatureHelpDescriptionEnabled;
}

private Preferences setImplementationCodelensEnabled(boolean enabled) {
this.implementationsCodeLensEnabled = enabled;
return this;
Expand Down Expand Up @@ -1481,6 +1495,10 @@ public boolean isSignatureHelpEnabled() {
return signatureHelpEnabled;
}

public boolean isSignatureHelpDescriptionEnabled() {
return signatureHelpDescriptionEnabled;
}

public boolean isRenameEnabled() {
return renameEnabled;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@
import org.eclipse.jdt.ls.core.internal.WorkspaceHelper;
import org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager;
import org.eclipse.jdt.ls.core.internal.preferences.Preferences;
import org.eclipse.lsp4j.MarkupContent;
import org.eclipse.lsp4j.SignatureHelp;
import org.eclipse.lsp4j.SignatureHelpParams;
import org.eclipse.lsp4j.SignatureInformation;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.junit.Before;
import org.junit.Test;

Expand Down Expand Up @@ -73,13 +75,15 @@ public void setup() throws Exception {
sourceFolder = javaProject.getPackageFragmentRoot(javaProject.getProject().getFolder("src"));
preferenceManager = mock(PreferenceManager.class);
Preferences p = mock(Preferences.class);
when(preferenceManager.getPreferences(null)).thenReturn(p);
when(preferenceManager.getPreferences()).thenReturn(p);
when(p.isSignatureHelpEnabled()).thenReturn(true);
when(p.isSignatureHelpDescriptionEnabled()).thenReturn(false);
handler = new SignatureHelpHandler(preferenceManager);
}

@Test
public void testSignatureHelp_singleMethod() throws JavaModelException {
when(preferenceManager.getPreferences().isSignatureHelpDescriptionEnabled()).thenReturn(true);
IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
StringBuilder buf = new StringBuilder();
buf.append("package test1;\n");
Expand Down Expand Up @@ -303,6 +307,7 @@ public void testSignatureHelp_javadoc() throws JavaModelException {
// See https://github.com/redhat-developer/vscode-java/issues/1258
@Test
public void testSignatureHelp_javadocOriginal() throws JavaModelException {
when(preferenceManager.getPreferences().isSignatureHelpDescriptionEnabled()).thenReturn(true);
IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
StringBuilder buf = new StringBuilder();
buf.append("package test1;\n");
Expand Down Expand Up @@ -502,6 +507,46 @@ public void testSignatureHelpMethod() throws Exception {
assertTrue(help.getSignatures().get(help.getActiveSignature()).getLabel().equals("fail(String msg, Object data) : Boolean"));
}

@Test
public void testSignatureHelpDescriptionDisabled() throws Exception {
IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
StringBuilder buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("public class E {\n");
buf.append(" /**\n");
buf.append(" * This is an API.\n");
buf.append(" */\n");
buf.append(" public void foo(String s) {\n");
buf.append(" foo(null)\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
SignatureHelp help = getSignatureHelp(cu, 6, 7);
assertNotNull(help);
assertNull(help.getSignatures().get(help.getActiveSignature()).getDocumentation());
}

@Test
public void testSignatureHelpDescriptionEnabled() throws Exception {
when(preferenceManager.getPreferences().isSignatureHelpDescriptionEnabled()).thenReturn(true);
IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
StringBuilder buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("public class E {\n");
buf.append(" /**\n");
buf.append(" * This is an API.\n");
buf.append(" */\n");
buf.append(" public void foo(String s) {\n");
buf.append(" foo(null)\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
SignatureHelp help = getSignatureHelp(cu, 6, 7);
assertNotNull(help);
Either<String, MarkupContent> documentation = help.getSignatures().get(help.getActiveSignature()).getDocumentation();
assertEquals("This is an API.", documentation.getLeft().trim());
}

private void testAssertEquals(ICompilationUnit cu, int line, int character) {
SignatureHelp help = getSignatureHelp(cu, line, character);
assertNotNull(help);
Expand Down