Skip to content

Commit

Permalink
Add trace to understand the probability of document out-of-sync (#2954)
Browse files Browse the repository at this point in the history
* Log an error message for the out-of-sync case
  • Loading branch information
testforstephen authored Nov 15, 2023
1 parent fbd482b commit a802ab5
Showing 1 changed file with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public abstract class BaseDocumentLifeCycleHandler {
private Set<ICompilationUnit> toReconcile = new HashSet<>();
private Set<ICompilationUnit> toValidate = ConcurrentHashMap.newKeySet();
private Map<String, Integer> documentVersions = new HashMap<>();
private Map<String, Integer> lastSyncedDocumentLengths = new ConcurrentHashMap<>();
private MovingAverage movingAverageForValidation = new MovingAverage(DOCUMENT_LIFECYCLE_MAX_DEBOUNCE);
private MovingAverage movingAverageForDiagnostics = new MovingAverage(PUBLISH_DIAGNOSTICS_MIN_DEBOUNCE);
protected final PreferenceManager preferenceManager;
Expand Down Expand Up @@ -328,12 +329,14 @@ public IProblemRequestor getProblemRequestor(ICompilationUnit workingCopy) {

public void didClose(DidCloseTextDocumentParams params) {
documentVersions.remove(params.getTextDocument().getUri());
lastSyncedDocumentLengths.remove(params.getTextDocument().getUri());
handleClosed(params);
}

public void didOpen(DidOpenTextDocumentParams params) {
String uri = params.getTextDocument().getUri();
documentVersions.put(uri, params.getTextDocument().getVersion());
lastSyncedDocumentLengths.remove(params.getTextDocument().getUri());
IFile resource = JDTUtils.findFile(uri);
if (resource != null) { // Open a managed file from the existing projects.
handleOpen(params);
Expand All @@ -352,6 +355,7 @@ public void didChange(DidChangeTextDocumentParams params) {
}

public void didSave(DidSaveTextDocumentParams params) {
lastSyncedDocumentLengths.remove(params.getTextDocument().getUri());
IFile file = JDTUtils.findFile(params.getTextDocument().getUri());
if (file != null && !Objects.equals(ProjectsManager.getDefaultProject(), file.getProject())) {
// no need for a workspace runnable, change is trivial
Expand Down Expand Up @@ -432,6 +436,18 @@ public ICompilationUnit handleChanged(DidChangeTextDocumentParams params) {
}

if (!preferenceManager.getClientPreferences().skipTextEventPropagation()) {
int currentBufferLength = unit.getBuffer().getLength();
if (lastSyncedDocumentLengths.containsKey(uri) && lastSyncedDocumentLengths.get(uri) != currentBufferLength) {
/**
* The didChange handler is the only owner that has the responsibility
* of synchronizing the client changes with the buffer. If the last
* synchronized document length in the didChange handler does not
* match the current buffer length, this indicates that the document
* buffer has been modified by an unexpected program and has become
* inconsistent with the client document.
*/
JavaLanguageServerPlugin.logError("Document on language server is out-of-sync: " + unit.getElementName());
}
List<TextDocumentContentChangeEvent> contentChanges = params.getContentChanges();
for (TextDocumentContentChangeEvent changeEvent : contentChanges) {

Expand Down Expand Up @@ -460,6 +476,7 @@ public ICompilationUnit handleChanged(DidChangeTextDocumentParams params) {
}
edit.apply(document, TextEdit.NONE);
}
lastSyncedDocumentLengths.put(uri, unit.getBuffer().getLength());
}
triggerValidation(unit);
} catch (JavaModelException | MalformedTreeException | BadLocationException e) {
Expand Down

0 comments on commit a802ab5

Please sign in to comment.