Skip to content

Commit

Permalink
Disable the keybinding of formatting document
Browse files Browse the repository at this point in the history
Signed-off-by: Snjezana Peco <snjezana.peco@redhat.com>
  • Loading branch information
snjeza committed Jul 3, 2017
1 parent 4bc6398 commit 9473d61
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import org.eclipse.lsp4j.MessageParams;
import org.eclipse.lsp4j.MessageType;
import org.eclipse.lsp4j.PublishDiagnosticsParams;
import org.eclipse.lsp4j.RegistrationParams;
import org.eclipse.lsp4j.ShowMessageRequestParams;
import org.eclipse.lsp4j.UnregistrationParams;
import org.eclipse.lsp4j.WorkspaceEdit;
import org.eclipse.lsp4j.jsonrpc.services.JsonNotification;
import org.eclipse.lsp4j.services.LanguageClient;
Expand All @@ -43,6 +45,7 @@ public interface JavaLanguageClient extends LanguageClient{
*/
@JsonNotification("language/actionableNotification")
void sendActionableNotification(ActionableNotification notification);

}

private final LogHandler logHandler;
Expand Down Expand Up @@ -140,6 +143,20 @@ public boolean applyWorkspaceEdit(WorkspaceEdit edit){
return response.getApplied().booleanValue();
}

/**
* @see {@link org.eclipse.lsp4j.services.LanguageClient#unregisterCapability(RegistrationParams)}
*/
public void unregisterCapability(UnregistrationParams params) {
client.unregisterCapability(params);
}

/**
* @see {@link org.eclipse.lsp4j.services.LanguageClient#registerCapability(RegistrationParams)}
*/
public void registerCapability(RegistrationParams params) {
client.registerCapability(params);
}

public void disconnect() {
if (logHandler != null) {
logHandler.uninstall();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.eclipse.jdt.internal.corext.refactoring.util.TextEditUtil;
import org.eclipse.jdt.ls.core.internal.JDTUtils;
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
import org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
Expand All @@ -45,6 +46,12 @@
*/
public class FormatterHandler {

private PreferenceManager preferenceManager;

public FormatterHandler(PreferenceManager preferenceManager) {
this.preferenceManager = preferenceManager;
}

List<? extends org.eclipse.lsp4j.TextEdit> formatting(DocumentFormattingParams params, IProgressMonitor monitor) {
return format(params.getTextDocument().getUri(), params.getOptions(), null, monitor);
}
Expand All @@ -56,6 +63,9 @@ List<? extends org.eclipse.lsp4j.TextEdit> rangeFormatting(DocumentRangeFormatti

private List<org.eclipse.lsp4j.TextEdit> format(String uri, FormattingOptions options, Range range,
IProgressMonitor monitor) {
if (!preferenceManager.getPreferences().isJavaFormatEnabled()) {
return Collections.emptyList();
}
ICompilationUnit cu = JDTUtils.resolveCompilationUnit(uri);
if(cu == null ) {
return Collections.emptyList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,12 @@ InitializeResult initialize(InitializeParams param){
capabilities.setWorkspaceSymbolProvider(Boolean.TRUE);
capabilities.setReferencesProvider(Boolean.TRUE);
capabilities.setDocumentHighlightProvider(Boolean.TRUE);
capabilities.setDocumentFormattingProvider(Boolean.TRUE);
capabilities.setDocumentRangeFormattingProvider(Boolean.TRUE);
if (!preferenceManager.getClientPreferences().isFormattingDynamicRegistrationSupported()) {
capabilities.setDocumentFormattingProvider(Boolean.TRUE);
}
if (!preferenceManager.getClientPreferences().isRangeFormattingDynamicRegistrationSupported()) {
capabilities.setDocumentRangeFormattingProvider(Boolean.TRUE);
}
capabilities.setCodeLensProvider(new CodeLensOptions(Boolean.TRUE));
capabilities.setCodeActionProvider(Boolean.TRUE);
result.setCapabilities(capabilities);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import static org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin.logInfo;
import static org.eclipse.lsp4j.jsonrpc.CompletableFutures.computeAsync;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -53,12 +54,16 @@
import org.eclipse.lsp4j.InitializeResult;
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.ReferenceParams;
import org.eclipse.lsp4j.Registration;
import org.eclipse.lsp4j.RegistrationParams;
import org.eclipse.lsp4j.RenameParams;
import org.eclipse.lsp4j.SignatureHelp;
import org.eclipse.lsp4j.SymbolInformation;
import org.eclipse.lsp4j.TextDocumentIdentifier;
import org.eclipse.lsp4j.TextDocumentPositionParams;
import org.eclipse.lsp4j.TextEdit;
import org.eclipse.lsp4j.Unregistration;
import org.eclipse.lsp4j.UnregistrationParams;
import org.eclipse.lsp4j.WorkspaceEdit;
import org.eclipse.lsp4j.WorkspaceSymbolParams;
import org.eclipse.lsp4j.jsonrpc.CancelChecker;
Expand Down Expand Up @@ -176,6 +181,36 @@ public void didChangeConfiguration(DidChangeConfigurationParams params) {
Preferences prefs = Preferences.createFrom(javaConfig);
preferenceManager.update(prefs);
}
if (preferenceManager.getClientPreferences().isFormattingDynamicRegistrationSupported()) {
if (!preferenceManager.getPreferences().isJavaFormatEnabled()) {
Unregistration unregistration = new Unregistration(Preferences.FORMATTING_ID, Preferences.TEXT_DOCUMENT_FORMATTING);
List<Unregistration> unregistrations = new ArrayList<>();
unregistrations.add(unregistration);
UnregistrationParams unregistrationParams = new UnregistrationParams(unregistrations);
client.unregisterCapability(unregistrationParams);
} else {
Registration registration = new Registration(Preferences.FORMATTING_ID, Preferences.TEXT_DOCUMENT_FORMATTING);
List<Registration> registrations = new ArrayList<>();
registrations.add(registration);
RegistrationParams registrationParams = new RegistrationParams(registrations);
client.registerCapability(registrationParams);
}
}
if (preferenceManager.getClientPreferences().isRangeFormattingDynamicRegistrationSupported()) {
if (!preferenceManager.getPreferences().isJavaFormatEnabled()) {
List<Unregistration> unregistrations = new ArrayList<>();
Unregistration unregistration = new Unregistration(Preferences.FORMATTING_RANGE_ID, Preferences.TEXT_DOCUMENT_RANGE_FORMATTING);
unregistrations.add(unregistration);
UnregistrationParams unregistrationParams = new UnregistrationParams(unregistrations);
client.unregisterCapability(unregistrationParams);
} else {
List<Registration> registrations = new ArrayList<>();
Registration registration = new Registration(Preferences.FORMATTING_RANGE_ID, Preferences.TEXT_DOCUMENT_RANGE_FORMATTING);
registrations.add(registration);
RegistrationParams registrationParams = new RegistrationParams(registrations);
client.registerCapability(registrationParams);
}
}
logInfo(">>New configuration: "+settings);
}

Expand Down Expand Up @@ -305,7 +340,7 @@ public CompletableFuture<CodeLens> resolveCodeLens(CodeLens unresolved) {
@Override
public CompletableFuture<List<? extends TextEdit>> formatting(DocumentFormattingParams params) {
logInfo(">> document/formatting");
FormatterHandler handler = new FormatterHandler();
FormatterHandler handler = new FormatterHandler(preferenceManager);
return computeAsync((cc) -> handler.formatting(params, toMonitor(cc)));
}

Expand All @@ -315,7 +350,7 @@ public CompletableFuture<List<? extends TextEdit>> formatting(DocumentFormatting
@Override
public CompletableFuture<List<? extends TextEdit>> rangeFormatting(DocumentRangeFormattingParams params) {
logInfo(">> document/rangeFormatting");
FormatterHandler handler = new FormatterHandler();
FormatterHandler handler = new FormatterHandler(preferenceManager);
return computeAsync((cc) -> handler.rangeFormatting(params, toMonitor(cc)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,16 @@ public boolean isCompletionSnippetsSupported() {
return v3supported && capabilities.getTextDocument().getCompletion().getCompletionItem().getSnippetSupport().booleanValue();
}

public boolean isV3Supported() {
return v3supported;
}

public boolean isFormattingDynamicRegistrationSupported() {
return v3supported && capabilities.getTextDocument().getFormatting().getDynamicRegistration().booleanValue();
}

public boolean isRangeFormattingDynamicRegistrationSupported() {
return v3supported && capabilities.getTextDocument().getRangeFormatting().getDynamicRegistration().booleanValue();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package org.eclipse.jdt.ls.core.internal.preferences;

import java.util.Map;
import java.util.UUID;

import org.eclipse.lsp4j.MessageType;

Expand All @@ -27,6 +28,11 @@ public class Preferences {
*/
public static final String REFERENCES_CODE_LENS_ENABLED_KEY = "java.referencesCodeLens.enabled";

/**
* Preference key to enable/disable formatter.
*/
public static final String JAVA_FORMAT_ENABLED_KEY = "java.format.enabled";

/**
* Preference key for project build/configuration update settings.
*/
Expand Down Expand Up @@ -72,9 +78,15 @@ public class Preferences {
*/
public static final String MEMBER_SORT_ORDER = "java.memberSortOrder"; //$NON-NLS-1$

public static final String TEXT_DOCUMENT_FORMATTING = "textDocument/formatting";
public static final String TEXT_DOCUMENT_RANGE_FORMATTING = "textDocument/rangeFormatting";
public static final String FORMATTING_ID = UUID.randomUUID().toString();
public static final String FORMATTING_RANGE_ID = UUID.randomUUID().toString();

private Severity incompleteClasspathSeverity;
private FeatureStatus updateBuildConfigurationStatus;
private boolean referencesCodeLensEnabled;
private boolean javaFormatEnabled;
private MemberSortOrder memberOrders;

private String mavenUserSettings;
Expand Down Expand Up @@ -127,6 +139,7 @@ public Preferences() {
incompleteClasspathSeverity = Severity.warning;
updateBuildConfigurationStatus = FeatureStatus.interactive;
referencesCodeLensEnabled = true;
javaFormatEnabled = true;
memberOrders = new MemberSortOrder(null);
favoriteStaticMembers = "";
}
Expand Down Expand Up @@ -176,6 +189,9 @@ public static Preferences createFrom(Map<String, Object> configuration) {
boolean referenceCodelensEnabled = getBooleanValue(configuration, REFERENCES_CODE_LENS_ENABLED_KEY, true);
prefs.setReferencesCodelensEnabled(referenceCodelensEnabled);

boolean javaFormatEnabled = getBooleanValue(configuration, JAVA_FORMAT_ENABLED_KEY, true);
prefs.setJavaFormatEnabled(javaFormatEnabled);

String mavenUserSettings = getStringValue(configuration, MAVEN_USER_SETTINGS_KEY, null);
prefs.setMavenUserSettings(mavenUserSettings);

Expand All @@ -199,6 +215,11 @@ private Preferences setReferencesCodelensEnabled(boolean enabled) {
return this;
}

public Preferences setJavaFormatEnabled(boolean enabled) {
this.javaFormatEnabled = enabled;
return this;
}

private Preferences setUpdateBuildConfigurationStatus(FeatureStatus status) {
this.updateBuildConfigurationStatus = status;
return this;
Expand Down Expand Up @@ -230,6 +251,10 @@ public boolean isReferencesCodeLensEnabled() {
return referencesCodeLensEnabled;
}

public boolean isJavaFormatEnabled() {
return javaFormatEnabled;
}

public Preferences setMavenUserSettings(String mavenUserSettings) {
this.mavenUserSettings = mavenUserSettings;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,25 @@ public void testDocumentFormatting() throws Exception {
assertEquals(expectedText, newText);
}

@Test
public void testJavaFormatEnable() throws Exception {
String text =
//@formatter:off
"package org.sample ;\n\n" +
" public class Baz { String name;}\n";
//@formatter:on"
ICompilationUnit unit = getWorkingCopy("src/org/sample/Baz.java", text);
preferenceManager.getPreferences().setJavaFormatEnabled(false);
String uri = JDTUtils.getFileURI(unit);
TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uri);
FormattingOptions options = new FormattingOptions(4, true, Collections.emptyMap());// ident == 4 spaces
DocumentFormattingParams params = new DocumentFormattingParams(textDocument, options);
List<? extends TextEdit> edits = server.formatting(params).get();
assertNotNull(edits);
String newText = TextEditUtil.apply(unit, edits);
assertEquals(text, newText);
}

@Test
public void testDocumentFormattingWithTabs() throws Exception {
ICompilationUnit unit = getWorkingCopy("src/org/sample/Baz.java",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;

import java.io.File;
import java.io.IOException;
Expand All @@ -36,6 +37,7 @@
import org.eclipse.jdt.ls.core.internal.ResourceUtils;
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.junit.After;
import org.junit.Before;
import org.mockito.Mock;
Expand All @@ -52,9 +54,13 @@ public abstract class AbstractProjectsManagerBasedTest {
protected ProjectsManager projectsManager;
@Mock
protected PreferenceManager preferenceManager;
protected Preferences preferences = new Preferences();

@Before
public void initProjectManager() {
if (preferenceManager != null) {
when(preferenceManager.getPreferences()).thenReturn(preferences);
}
projectsManager = new ProjectsManager(preferenceManager);
WorkingCopyOwner.setPrimaryBufferProvider(new WorkingCopyOwner() {
@Override
Expand Down

0 comments on commit 9473d61

Please sign in to comment.