Skip to content

Commit

Permalink
Tested and Enabled incremental document syncing.
Browse files Browse the repository at this point in the history
Fixes eclipse-lemminx#133

Incremental sync is off by default preference to turn on is:

"xml.experimental.incrementalSupport.enabled": true|false

Signed-off-by: Nikolas <nikolaskomonen@gmail.com>
  • Loading branch information
NikolasKomonen committed Jun 14, 2019
1 parent 7d8eccf commit 4604224
Show file tree
Hide file tree
Showing 11 changed files with 335 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@
import org.eclipse.lsp4xml.settings.LogsSettings;
import org.eclipse.lsp4xml.settings.ServerSettings;
import org.eclipse.lsp4xml.settings.SharedSettings;
import org.eclipse.lsp4xml.settings.XMLExperimentalCapabilities;
import org.eclipse.lsp4xml.settings.XMLExperimentalSettings;
import org.eclipse.lsp4xml.settings.XMLFormattingOptions;
import org.eclipse.lsp4xml.settings.XMLGeneralClientSettings;
import org.eclipse.lsp4xml.settings.XMLIncrementalSupportSettings;
import org.eclipse.lsp4xml.settings.XMLSymbolSettings;
import org.eclipse.lsp4xml.settings.capabilities.ServerCapabilitiesInitializer;
import org.eclipse.lsp4xml.settings.capabilities.XMLCapabilityManager;
Expand Down Expand Up @@ -116,7 +117,7 @@ public void initialized(InitializedParams params) {
*
* @param initializationOptionsSettings the XML settings
*/
public void updateSettings(Object initializationOptionsSettings) {
public synchronized void updateSettings(Object initializationOptionsSettings) {
if (initializationOptionsSettings == null) {
return;
}
Expand Down Expand Up @@ -152,13 +153,12 @@ public void updateSettings(Object initializationOptionsSettings) {
FilesUtils.setCachePathSetting(workDir);
}

// Experimental capabilities
XMLExperimentalCapabilities experimental = xmlClientSettings.getExperimental();
if (experimental != null) {
boolean incrementalSupport = experimental.getIncrementalSupport() != null
&& experimental.getIncrementalSupport().getEnabled() != null
&& experimental.getIncrementalSupport().getEnabled().booleanValue();
xmlTextDocumentService.setIncrementalSupport(incrementalSupport);
XMLExperimentalSettings experimentalSettings = xmlClientSettings.getExperimental();
if(experimentalSettings != null) {
XMLIncrementalSupportSettings incrementalSettings = experimentalSettings.getIncrementalSupport();
if(incrementalSettings != null) {
xmlTextDocumentService.updateIncrementalSettings(incrementalSettings);
}
}
}
ContentModelSettings cmSettings = ContentModelSettings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
import org.eclipse.lsp4xml.services.extensions.save.AbstractSaveContext;
import org.eclipse.lsp4xml.settings.SharedSettings;
import org.eclipse.lsp4xml.settings.XMLFormattingOptions;
import org.eclipse.lsp4xml.settings.XMLIncrementalSupportSettings;
import org.eclipse.lsp4xml.settings.XMLSymbolSettings;

/**
Expand Down Expand Up @@ -412,7 +413,7 @@ private XMLLanguageService getXMLLanguageService() {
}

public void updateCompletionSettings(CompletionSettings newCompletion) {
sharedSettings.completionSettings.setAutoCloseTags(newCompletion.isAutoCloseTags());
sharedSettings.setCompletionSettings(newCompletion);
}

public void updateSymbolSettings(XMLSymbolSettings newSettings) {
Expand All @@ -436,10 +437,6 @@ public XMLFormattingOptions getSharedFormattingSettings() {
return sharedSettings.formattingSettings;
}

public void setIncrementalSupport(boolean incrementalSupport) {
this.documents.setIncremental(incrementalSupport);
}

public XMLValidationSettings getValidationSettings() {

return sharedSettings.validationSettings;
Expand All @@ -449,4 +446,9 @@ public SharedSettings getSharedSettings() {
return this.sharedSettings;
}

public void updateIncrementalSettings(XMLIncrementalSupportSettings settings) {
sharedSettings.experimentalSettings.getIncrementalSupport().setEnabled(settings.getEnabled());
this.documents.setIncremental(sharedSettings.experimentalSettings.getIncrementalSupport().getEnabled());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@
*/
public class TextDocument extends TextDocumentItem {

private final Object lock = new Object();

private static String DEFAULT_DELIMTER = System.lineSeparator();

private ListLineTracker lineTracker;
private final ListLineTracker lineTracker;

// Buffer of the text document used only in incremental mode.
private StringBuilder buffer;
private boolean incremental;

public TextDocument(TextDocumentItem document) {
this(document.getText(), document.getUri());
Expand All @@ -40,22 +41,23 @@ public TextDocument(TextDocumentItem document) {
}

public TextDocument(String text, String uri) {
this.lineTracker = new ListLineTracker();
super.setUri(uri);
super.setText(text);
this.setText(text);
}

public void setIncremental(boolean incremental) {
if (incremental) {
buffer = new StringBuilder(getText());
} else {
buffer = null;
}
this.incremental = incremental;
}

public boolean isIncremental() {
return incremental;
}

@Override
public void setText(String text) {
super.setText(text);
lineTracker = null;
lineTracker.set(text);
}

public Position positionAt(int position) throws BadLocationException {
Expand Down Expand Up @@ -117,10 +119,6 @@ public Range getWordRangeAt(int textOffset, Pattern wordDefinition) {
}

private ListLineTracker getLineTracker() {
if (lineTracker == null) {
lineTracker = new ListLineTracker();
lineTracker.set(super.getText());
}
return lineTracker;
}

Expand All @@ -137,8 +135,17 @@ public void update(List<TextDocumentContentChangeEvent> changes) {
}
if (isIncremental()) {
try {
synchronized (buffer) {
for (TextDocumentContentChangeEvent changeEvent : changes) {
// Initialize buffer and line tracker from the current text document
String initialText = getText();
StringBuilder buffer = new StringBuilder(getText());
ListLineTracker lt = new ListLineTracker();
lt.set(initialText);
synchronized (lock) {
for (int i = 0; i < changes.size(); i++) {
if (i > 0) {
lt.set(buffer.toString());
}
TextDocumentContentChangeEvent changeEvent = changes.get(i);

Range range = changeEvent.getRange();
int length = 0;
Expand All @@ -147,17 +154,18 @@ public void update(List<TextDocumentContentChangeEvent> changes) {
length = changeEvent.getRangeLength().intValue();
} else {
// range is optional and if not given, the whole file content is replaced
length = getText().length();
range = new Range(positionAt(0), positionAt(length));
length = buffer.length();
range = new Range(lt.getPositionAt(0), lt.getPositionAt(length));
}
String text = changeEvent.getText();
int startOffset = offsetAt(range.getStart());
int startOffset = lt.getOffsetAt(range.getStart());
buffer.replace(startOffset, startOffset + length, text);
}
// Update the new text content from the updated buffer
setText(buffer.toString());
}
} catch (BadLocationException e) {
// Should never occurs.
// Should never occur.
}
} else {
// like vscode does, get the last changes
Expand All @@ -170,8 +178,4 @@ public void update(List<TextDocumentContentChangeEvent> changes) {
}
}

public boolean isIncremental() {
return buffer != null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
*/
public class TextDocuments implements ITextDocumentFactory {

private boolean incremental;

private final Map<String, TextDocument> documents;

private boolean incremental = true; //default on

public TextDocuments() {
documents = new HashMap<>();
}
Expand Down Expand Up @@ -72,7 +72,7 @@ public TextDocument get(String uri) {
@Override
public TextDocument createDocument(TextDocumentItem document) {
TextDocument doc = new TextDocument(document);
doc.setIncremental(incremental);
doc.setIncremental(isIncremental());
return doc;
}

Expand Down Expand Up @@ -121,4 +121,5 @@ public Collection<TextDocument> all() {
return documents.values();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ public class SharedSettings {
public XMLFormattingOptions formattingSettings;
public final XMLValidationSettings validationSettings;
public final XMLSymbolSettings symbolSettings;
public final XMLExperimentalSettings experimentalSettings;

public SharedSettings() {
this.completionSettings = new CompletionSettings();
this.foldingSettings = new FoldingRangeCapabilities();
this.formattingSettings = new XMLFormattingOptions(true);
this.validationSettings = new XMLValidationSettings();
this.symbolSettings = new XMLSymbolSettings();
this.experimentalSettings = new XMLExperimentalSettings();
}

public void setFormattingSettings(XMLFormattingOptions formattingOptions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@
* XML experimental capabilities.
*
*/
public class XMLExperimentalCapabilities {
public class XMLExperimentalSettings {

private XMLIncrementalSupportCapabilities incrementalSupport;
private XMLIncrementalSupportSettings incrementalSupport;

public void setIncrementalSupport(XMLIncrementalSupportCapabilities incrementalSupport) {
public void setIncrementalSupport(XMLIncrementalSupportSettings incrementalSupport) {
this.incrementalSupport = incrementalSupport;
}

public XMLIncrementalSupportCapabilities getIncrementalSupport() {
public XMLIncrementalSupportSettings getIncrementalSupport() {
if(incrementalSupport == null) {
incrementalSupport = new XMLIncrementalSupportSettings();
}
return incrementalSupport;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,34 @@ public class XMLGeneralClientSettings {
private LogsSettings logs;

private XMLFormattingOptions format;

private XMLExperimentalCapabilities experimental;

private CompletionSettings completion;

private ServerSettings server;

private XMLSymbolSettings symbols;

private XMLExperimentalSettings experimental;



public XMLExperimentalSettings getExperimental() {
return experimental;
}

public void setExperimental(XMLExperimentalSettings experimental) {
this.experimental = experimental;
}

public void setLogs(LogsSettings logs) {
this.logs = logs;
}

public LogsSettings getLogs() {
return logs;
}


public XMLSymbolSettings getSymbols() {
return symbols;
}
Expand All @@ -58,21 +73,14 @@ public void setSymbols(XMLSymbolSettings symbols) {
this.symbols = symbols;
}

public LogsSettings getLogs() {
return logs;
}


public void setFormat(XMLFormattingOptions format) {
this.format = format;
}

public XMLFormattingOptions getFormat() {
return format;
}

public XMLExperimentalCapabilities getExperimental() {
return experimental;
}

/**
* Set completion settings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@
* XML experimental incremental support capabilities.
*
*/
public class XMLIncrementalSupportCapabilities {
public class XMLIncrementalSupportSettings {

private Boolean enabled;

public Boolean getEnabled() {
if(enabled == null) {
enabled = true; // default on
}
return enabled;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

import static org.eclipse.lsp4xml.settings.capabilities.ServerCapabilitiesConstants.DEFAULT_COMPLETION_OPTIONS;
import static org.eclipse.lsp4xml.settings.capabilities.ServerCapabilitiesConstants.DEFAULT_LINK_OPTIONS;
import static org.eclipse.lsp4xml.settings.capabilities.ServerCapabilitiesConstants.DEFAULT_SYNC_OPTION;

import org.eclipse.lsp4j.ServerCapabilities;
import org.eclipse.lsp4j.TextDocumentSyncKind;
Expand All @@ -37,9 +36,9 @@ private ServerCapabilitiesInitializer() {
public static ServerCapabilities getNonDynamicServerCapabilities(ClientCapabilitiesWrapper clientCapabilities,
boolean isIncremental) {
ServerCapabilities serverCapabilities = new ServerCapabilities();
// @formatter:off
serverCapabilities.setTextDocumentSync(DEFAULT_SYNC_OPTION);

serverCapabilities.setTextDocumentSync(isIncremental ? TextDocumentSyncKind.Incremental : TextDocumentSyncKind.Full);

serverCapabilities.setDocumentSymbolProvider(!clientCapabilities.isDocumentSymbolDynamicRegistrationSupported());
serverCapabilities.setDocumentHighlightProvider(!clientCapabilities.isDocumentHighlightDynamicRegistered());
serverCapabilities.setCodeActionProvider(!clientCapabilities.isCodeActionDynamicRegistered());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,10 @@ public void initializeCapabilities() {
}

/**
* Registers all capabilities that this server can support client side
* preferences to turn on/off
* Registers(indicates the servers ability to support the service) all capabilities that have the ability to be turned
* on/off on the client side through preferences.
*
* In the case the preference is set to off/false this server will tell the cliet it does not support this capability.
*
* If a capability is not dynamic, it's handled by
* {@link ServerCapabilitiesInitializer}
Expand Down
Loading

0 comments on commit 4604224

Please sign in to comment.