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 enable/disable completion option #786

Merged
merged 1 commit into from
Sep 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -11,6 +11,7 @@
package org.eclipse.jdt.ls.core.internal.handlers;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

Expand All @@ -29,11 +30,14 @@
import org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager;
import org.eclipse.lsp4j.CompletionItem;
import org.eclipse.lsp4j.CompletionList;
import org.eclipse.lsp4j.CompletionOptions;
import org.eclipse.lsp4j.CompletionParams;
import org.eclipse.lsp4j.jsonrpc.messages.Either;

public class CompletionHandler{

public final static CompletionOptions DEFAULT_COMPLETION_OPTIONS = new CompletionOptions(Boolean.TRUE, Arrays.asList(".", "@", "#", "*"));

Either<List<CompletionItem>, CompletionList> completion(CompletionParams position,
IProgressMonitor monitor) {
List<CompletionItem> completionItems = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import org.eclipse.jdt.ls.core.internal.preferences.Preferences;
import org.eclipse.lsp4j.ClientCapabilities;
import org.eclipse.lsp4j.CodeLensOptions;
import org.eclipse.lsp4j.CompletionOptions;
import org.eclipse.lsp4j.DocumentOnTypeFormattingOptions;
import org.eclipse.lsp4j.ExecuteCommandOptions;
import org.eclipse.lsp4j.InitializeParams;
Expand Down Expand Up @@ -134,7 +133,9 @@ InitializeResult initialize(InitializeParams param) {
}
InitializeResult result = new InitializeResult();
ServerCapabilities capabilities = new ServerCapabilities();
capabilities.setCompletionProvider(new CompletionOptions(Boolean.TRUE, Arrays.asList(".", "@", "#", "*")));
if (!preferenceManager.getClientPreferences().isCompletionDynamicRegistered()) {
capabilities.setCompletionProvider(CompletionHandler.DEFAULT_COMPLETION_OPTIONS);
}
if (!preferenceManager.getClientPreferences().isFormattingDynamicRegistrationSupported()) {
capabilities.setDocumentFormattingProvider(Boolean.TRUE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ public CompletableFuture<InitializeResult> initialize(InitializeParams params) {
public void initialized(InitializedParams params) {
logInfo(">> initialized");
JobHelpers.waitForInitializeJobs();
if (preferenceManager.getClientPreferences().isCompletionDynamicRegistered()) {
registerCapability(Preferences.COMPLETION_ID, Preferences.COMPLETION, CompletionHandler.DEFAULT_COMPLETION_OPTIONS);
}
if (preferenceManager.getClientPreferences().isWorkspaceSymbolDynamicRegistered()) {
registerCapability(Preferences.WORKSPACE_SYMBOL_ID, Preferences.WORKSPACE_SYMBOL);
}
Expand Down Expand Up @@ -209,7 +212,7 @@ public void initialized(InitializedParams params) {

workspaceDiagnosticsHandler = new WorkspaceDiagnosticsHandler(this.client, pm);
workspaceDiagnosticsHandler.addResourceChangeListener();

computeAsync((monitor) -> {
try {
workspaceDiagnosticsHandler.publishDiagnostics(monitor);
Expand All @@ -224,6 +227,9 @@ public void initialized(InitializedParams params) {
* Toggles the server capabilities according to user preferences.
*/
private void syncCapabilitiesToSettings() {
if (preferenceManager.getClientPreferences().isCompletionDynamicRegistered()) {
toggleCapability(preferenceManager.getPreferences().isCompletionEnabled(), Preferences.COMPLETION_ID, Preferences.COMPLETION, CompletionHandler.DEFAULT_COMPLETION_OPTIONS);
}
if (preferenceManager.getClientPreferences().isFormattingDynamicRegistrationSupported()) {
toggleCapability(preferenceManager.getPreferences().isJavaFormatEnabled(), Preferences.FORMATTING_ID, Preferences.TEXT_DOCUMENT_FORMATTING, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ public boolean isWorkspaceFoldersSupported() {
return capabilities.getWorkspace() != null && isTrue(capabilities.getWorkspace().getWorkspaceFolders());
}

public boolean isCompletionDynamicRegistered() {
return v3supported && isDynamicRegistrationSupported(capabilities.getTextDocument().getCompletion());
}

public boolean isCompletionSnippetsSupported() {
//@formatter:off
return v3supported && capabilities.getTextDocument().getCompletion() != null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ public class Preferences {
*/
public static final String MAVEN_USER_SETTINGS_KEY = "java.configuration.maven.userSettings";

/**
* Preference key to enable/disable the 'completion'.
*/
public static final String COMPLETION_ENABLED_KEY = "java.completion.enabled";

/**
* A named preference that holds the favorite static members.
* <p>
Expand Down Expand Up @@ -201,6 +206,7 @@ public class Preferences {
public static final String WORKSPACE_SYMBOL = "workspace/symbol";
public static final String WORKSPACE_WATCHED_FILES = "workspace/didChangeWatchedFiles";
public static final String DOCUMENT_SYMBOL = "textDocument/documentSymbol";
public static final String COMPLETION = "textDocument/completion";
public static final String CODE_ACTION = "textDocument/codeAction";
public static final String DEFINITION = "textDocument/definition";
public static final String TYPEDEFINITION = "textDocument/typeDefinition";
Expand All @@ -219,6 +225,7 @@ public class Preferences {
public static final String EXECUTE_COMMAND_ID = UUID.randomUUID().toString();
public static final String WORKSPACE_SYMBOL_ID = UUID.randomUUID().toString();
public static final String DOCUMENT_SYMBOL_ID = UUID.randomUUID().toString();
public static final String COMPLETION_ID = UUID.randomUUID().toString();
public static final String CODE_ACTION_ID = UUID.randomUUID().toString();
public static final String DEFINITION_ID = UUID.randomUUID().toString();
public static final String TYPEDEFINITION_ID = UUID.randomUUID().toString();
Expand All @@ -243,6 +250,7 @@ public class Preferences {
private boolean renameEnabled;
private boolean executeCommandEnabled;
private boolean autobuildEnabled;
private boolean completionEnabled;
private boolean completionOverwrite;
private boolean guessMethodArguments;
private boolean javaFormatComments;
Expand Down Expand Up @@ -336,6 +344,7 @@ public Preferences() {
renameEnabled = true;
executeCommandEnabled = true;
autobuildEnabled = true;
completionEnabled = true;
completionOverwrite = true;
guessMethodArguments = false;
javaFormatComments = true;
Expand Down Expand Up @@ -395,6 +404,8 @@ public static Preferences createFrom(Map<String, Object> configuration) {
boolean autobuildEnable = getBoolean(configuration, AUTOBUILD_ENABLED_KEY, true);
prefs.setAutobuildEnabled(autobuildEnable);

boolean completionEnable = getBoolean(configuration, COMPLETION_ENABLED_KEY, true);
prefs.setCompletionEnabled(completionEnable);
boolean completionOverwrite = getBoolean(configuration, JAVA_COMPLETION_OVERWRITE_KEY, true);
prefs.setCompletionOverwrite(completionOverwrite);

Expand Down Expand Up @@ -520,6 +531,11 @@ public Preferences setAutobuildEnabled(boolean enabled) {
return this;
}

public Preferences setCompletionEnabled(boolean enabled) {
this.completionEnabled = enabled;
return this;
}

public Preferences setCompletionOverwrite(boolean completionOverwrite) {
this.completionOverwrite = completionOverwrite;
return this;
Expand Down Expand Up @@ -633,6 +649,10 @@ public boolean isAutobuildEnabled() {
return autobuildEnabled;
}

public boolean isCompletionEnabled() {
return completionEnabled;
}

public boolean isCompletionOverwrite() {
return completionOverwrite;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,11 @@ public void testRegisterDelayedCapability() throws Exception {
when(mockCapabilies.isHoverDynamicRegistered()).thenReturn(Boolean.TRUE);
when(mockCapabilies.isReferencesDynamicRegistered()).thenReturn(Boolean.TRUE);
when(mockCapabilies.isDocumentHighlightDynamicRegistered()).thenReturn(Boolean.TRUE);
when(mockCapabilies.isCompletionDynamicRegistered()).thenReturn(Boolean.TRUE);
InitializeResult result = initialize(true);
assertNull(result.getCapabilities().getDocumentSymbolProvider());
server.initialized(new InitializedParams());
verify(client, times(7)).registerCapability(any());
verify(client, times(8)).registerCapability(any());
}

@Test
Expand Down