Skip to content

Commit

Permalink
Validate non-saved docs on close
Browse files Browse the repository at this point in the history
  • Loading branch information
BoykoAlex committed Dec 4, 2024
1 parent 380dcbb commit f7de356
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2016, 2023 VMware Inc.
* Copyright (c) 2016, 2024 VMware Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -111,6 +111,7 @@
import org.springframework.ide.vscode.commons.util.AsyncRunner;
import org.springframework.ide.vscode.commons.util.BadLocationException;
import org.springframework.ide.vscode.commons.util.CollectionUtil;
import org.springframework.ide.vscode.commons.util.text.LazyTextDocument;
import org.springframework.ide.vscode.commons.util.text.TextDocument;

import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -760,8 +761,15 @@ public void validateWith(TextDocumentIdentifier docId, IReconcileEngine engine)
TextDocument doc = documents.getLatestSnapshot(docId.getUri());

if (doc == null) {
//Do not bother reconciling if document doesn't exist anymore (got closed in the meantime)
return;
// If document doesn't exist anymore it likely got closed in the meantime. Still needs validation.
LanguageComputer languageDetector = appContext.getBean(LanguageComputer.class);
if (languageDetector != null) {
doc = new LazyTextDocument(uri.toASCIIString(), languageDetector.computeLanguage(uri));
} else {
// Cannot determine the language? Give up.
log.warn("Cannot determine the language for document: " + uri);
return;
}
}

if (testListener != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,7 @@ public void didSave(DidSaveTextDocumentParams params) {
if (url != null) {
TextDocument doc = getLatestSnapshot(url);
if (doc != null) {
doc.saved();
for (Consumer<TextDocumentSaveChange> l : documentSaveListeners) {
l.accept(new TextDocumentSaveChange(doc));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public LazyTextDocument(String uri, LanguageId languageId, Supplier<String> load
}

public LazyTextDocument(String uri, LanguageId languageId) {
this(uri, LanguageId.JAVA, () -> {
this(uri, languageId, () -> {
try {
InputStream stream = URI.create(uri).toURL().openStream();
return IOUtil.toString(stream);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2016, 2021 Pivotal, Inc.
* Copyright (c) 2016, 2024 Pivotal, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -37,6 +37,7 @@ public class TextDocument implements IDocument {
private final String uri;
private Text text = new Text("");
private int version;
private boolean changedSinceLastSave = false;

public TextDocument(String uri, LanguageId languageId) {
this(uri, languageId, 0, "");
Expand All @@ -48,6 +49,7 @@ private TextDocument(TextDocument other) {
this.text = other.text;
this.lineTracker.set(text.toString());
this.version = other.version;
this.changedSinceLastSave = other.changedSinceLastSave;
}

public TextDocument(String uri, LanguageId languageId, int version, String text) {
Expand Down Expand Up @@ -95,6 +97,7 @@ public synchronized void apply(DidChangeTextDocumentParams params) throws BadLoc
apply(change);
}
this.version = newVersion;
this.changedSinceLastSave = true;
} else {
log.warn("Change event with bad version ignored");
}
Expand Down Expand Up @@ -298,4 +301,12 @@ public TextDocumentIdentifier getId() {
}
return null;
}

public void saved() {
this.changedSinceLastSave = false;
}

public boolean hasChangedSinceLastSave() {
return this.changedSinceLastSave;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,16 @@ private void startListening() {
TextDocument doc = params.getDocument();
server.validateWith(doc.getId(), reconcileEngine);
});

server.getTextDocumentService().onDidClose(doc -> {
if (doc.hasChangedSinceLastSave()) {
/*
* If doc is changed since last save closing it would ignore the latest changes.
* Therefore the file requires to be validated again.
*/
server.validateWith(doc.getId(), reconcileEngine);
}

});
// ServerUtils.listenToClassFileChanges(server.getWorkspaceService().getFileObserver(), projectFinder, project -> validateAll(components, server, project));
});
config.addListener(evt -> {
Expand Down

0 comments on commit f7de356

Please sign in to comment.