Skip to content

Commit

Permalink
use formatDocument as fallback for willSaveWaitUntil
Browse files Browse the repository at this point in the history
if server does not support willSaveWaitUntil the code can be formatted
prior to save.

fixes #761
  • Loading branch information
ghentschke committed Aug 29, 2023
1 parent 9c92a32 commit 96a3f92
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 2 deletions.
5 changes: 5 additions & 0 deletions org.eclipse.lsp4e/.project
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.ds.core.builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
Expand Down
1 change: 1 addition & 0 deletions org.eclipse.lsp4e/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,4 @@ Bundle-Vendor: Eclipse.org
Import-Package: com.google.common.base,
com.google.gson;version="2.7.0"
Automatic-Module-Name: org.eclipse.lsp4e
Service-Component: OSGI-INF/org.eclipse.lsp4e.DefaultFormatOnSave.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="org.eclipse.lsp4e.DefaultFormatOnSave">
<property name="service.ranking" type="Integer" value="0"/>
<implementation class="org.eclipse.lsp4e.DefaultFormatOnSave"/>
<service>
<provide interface="org.eclipse.lsp4e.IFormatOnSave"/>
</service>
</scr:component>
3 changes: 2 additions & 1 deletion org.eclipse.lsp4e/build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ bin.includes = META-INF/,\
icons/,\
.options,\
resources/,\
schema/
schema/,\
OSGI-INF/
src.includes = schema/
36 changes: 36 additions & 0 deletions org.eclipse.lsp4e/src/org/eclipse/lsp4e/DefaultFormatOnSave.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*******************************************************************************
* Copyright (c) 2023 Contributors to the Eclipse Foundation.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* See git history
*******************************************************************************/
package org.eclipse.lsp4e;

import org.eclipse.core.filebuffers.ITextFileBuffer;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.Region;
import org.osgi.service.component.annotations.Component;

@Component(property = { "service.ranking:Integer=0" })
public class DefaultFormatOnSave implements IFormatOnSave {

@Override
public boolean isEnabledFor(IDocument document) {
return false;
}

/**
* Formats the full file
*/
@Override
public IRegion[] getFormattingRegions(ITextFileBuffer buffer) {
return new IRegion[] { new Region(0, buffer.getDocument().getLength()) };
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.io.File;
import java.net.URI;
import java.util.Collections;
import java.util.ConcurrentModificationException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
Expand All @@ -31,13 +32,17 @@
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.ServiceCaller;
import org.eclipse.core.runtime.content.IContentType;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentListener;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.MultiTextSelection;
import org.eclipse.lsp4e.operations.format.LSPFormatter;
import org.eclipse.lsp4e.ui.Messages;
import org.eclipse.lsp4j.DidChangeTextDocumentParams;
import org.eclipse.lsp4j.DidCloseTextDocumentParams;
Expand Down Expand Up @@ -65,12 +70,16 @@ final class DocumentContentSynchronizer implements IDocumentListener {
private final @NonNull IDocument document;
private final @NonNull URI fileUri;
private final TextDocumentSyncKind syncKind;
private final LSPFormatter formatter = new LSPFormatter();

private int version = 0;
private DidChangeTextDocumentParams changeParams;
private long openSaveStamp;
private IPreferenceStore store;


private final ServiceCaller<IFormatOnSave> formatOnSave = new ServiceCaller<>(getClass(), IFormatOnSave.class);

public DocumentContentSynchronizer(@NonNull LanguageServerWrapper languageServerWrapper,
@NonNull LanguageServer languageServer,
@NonNull IDocument document, TextDocumentSyncKind syncKind) {
Expand Down Expand Up @@ -221,6 +230,9 @@ private int lsToWillSaveWaitUntilTimeout() {

public void documentAboutToBeSaved() {
if (!serverSupportsWillSaveWaitUntil()) {
if (formatOnSave != null) {
formatOnSave.call(f -> formatDocument(f.isEnabledFor(document), f.getFormattingRegions(LSPEclipseUtils.toBuffer(document))));
}
return;
}

Expand Down Expand Up @@ -257,6 +269,27 @@ public void documentAboutToBeSaved() {
}
}

private void formatDocument(boolean enable, IRegion[] formattingRegions) {
if (enable && document != null && formattingRegions != null) {
try {
var textSelection = new MultiTextSelection(document, formattingRegions);
formatter.requestFormatting(document, textSelection).get(lsToWillSaveWaitUntilTimeout(), TimeUnit.SECONDS)
.ifPresent(edits -> {
try {
edits.apply();
} catch (final ConcurrentModificationException ex) {
ServerMessageHandler.showMessage(Messages.LSPFormatHandler_DiscardedFormat, new MessageParams(MessageType.Error, Messages.LSPFormatHandler_DiscardedFormatResponse));
} catch (BadLocationException e) {
LanguageServerPlugin.logError(e);
}
});
} catch (BadLocationException | InterruptedException | ExecutionException
| TimeoutException e) {
LanguageServerPlugin.logError(e);
}
}
}

public void documentSaved(IFileBuffer buffer) {
if (openSaveStamp >= buffer.getModificationStamp()) {
return;
Expand Down
34 changes: 34 additions & 0 deletions org.eclipse.lsp4e/src/org/eclipse/lsp4e/IFormatOnSave.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*******************************************************************************
* Copyright (c) 2023 Contributors to the Eclipse Foundation.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* See git history
*******************************************************************************/
package org.eclipse.lsp4e;

import org.eclipse.core.filebuffers.ITextFileBuffer;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;

public interface IFormatOnSave {

/**
* Checks whether formatting shall be performed prior to file buffer saving.
* @param document
* @return true if document buffer shall be formatted prior saving
*/
boolean isEnabledFor(IDocument document);

/**
* Get the regions to be formatted (e.g. full file, lines edited this session, lines edited vs. source control baseline).
* @param buffer the buffer to compare contents from
* @return regions to be formatted before saving.
*/
IRegion[] getFormattingRegions(ITextFileBuffer buffer);

}
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ public static CallHierarchyPrepareParams toCallHierarchyPrepareParams(int offset

}

private static ITextFileBuffer toBuffer(IDocument document) {
public static ITextFileBuffer toBuffer(IDocument document) {
ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager();
if (bufferManager == null)
return null;
Expand Down

0 comments on commit 96a3f92

Please sign in to comment.