Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GLSP-1408: Make live validation asynchronous #247

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
********************************************************************************/
package org.eclipse.glsp.server.features.core.model;

import static java.util.concurrent.TimeUnit.MILLISECONDS;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand All @@ -33,6 +35,8 @@
import org.eclipse.glsp.server.layout.LayoutEngine;
import org.eclipse.glsp.server.layout.ServerLayoutKind;
import org.eclipse.glsp.server.model.GModelState;
import org.eclipse.glsp.server.utils.Debouncer;
import org.eclipse.glsp.server.utils.StatusActionUtil;

import com.google.inject.Inject;
import com.google.inject.Singleton;
Expand Down Expand Up @@ -61,9 +65,17 @@ public class ModelSubmissionHandler {
@Inject
protected GModelState modelState;

@Inject
protected ActionDispatcher actionDispatcher;

@Inject
protected Optional<ModelValidator> validator;

protected Debouncer<ModelValidator> liveValidationDebouncer;

// we use a very slight delay by default to avoid sending very short status messages for very fast validations
protected long liveValidationDelay = 100;

protected final Object modelLock = new Object();
protected Optional<RequestModelAction> requestModelAction = Optional.empty();

Expand Down Expand Up @@ -155,14 +167,38 @@ public List<Action> submitModelDirectly(final String reason) {
result.add(new SetDirtyStateAction(modelState.isDirty(), reason));
}
if (validator.isPresent()) {
List<Marker> markers = validator.get() //
.validate(Arrays.asList(modelState.getRoot()), MarkersReason.LIVE);
result.add(new SetMarkersAction(markers, MarkersReason.LIVE));
result.addAll(validateModel(validator.get()));
}
return result;
}
}

protected List<Action> validateModel(final ModelValidator validator) {
scheduleLiveValidation(validator);
// we are using async live validation so there no actions to return for the model submission
return List.of();
}

protected void scheduleLiveValidation(final ModelValidator validator) {
if (liveValidationDebouncer == null) {
liveValidationDebouncer = new Debouncer<>(this::performLiveValidation, getLiveValidationDelay(), MILLISECONDS);
}
liveValidationDebouncer.accept(validator);
}

public long getLiveValidationDelay() { return this.liveValidationDelay; }

public void setLiveValidationDelay(final long liveValidationDelay) {
this.liveValidationDelay = liveValidationDelay;
}

protected void performLiveValidation(final ModelValidator validator) {
actionDispatcher.dispatch(StatusActionUtil.info("Validate Model..."));
List<Marker> markers = validator.validate(Arrays.asList(modelState.getRoot()), MarkersReason.LIVE);
SetMarkersAction markerAction = new SetMarkersAction(markers, MarkersReason.LIVE);
actionDispatcher.dispatchAll(List.of(markerAction, StatusActionUtil.clear()));
}

public List<Action> submitModelDirectly() {
return submitModelDirectly(null);
}
Expand Down
Loading