Skip to content

Commit

Permalink
XMLExtensionRegistry: capture initial settings iff it's absent
Browse files Browse the repository at this point in the history
FileContentGeneratorPlugin: type test the registry

Signed-off-by: Yatao Li <yatli@microsoft.com>
  • Loading branch information
yatli authored and angelozerr committed Oct 12, 2020
1 parent 9f5f757 commit f0503e2
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 5 deletions.
Expand Up @@ -25,9 +25,11 @@ public class FileContentGeneratorPlugin implements IXMLExtension {

@Override
public void start(InitializeParams params, XMLExtensionsRegistry registry) {
IXMLFullFormatter formatter = (IXMLFullFormatter) registry;
FileContentGeneratorManager manager = new FileContentGeneratorManager(formatter);
registry.registerComponent(manager);
if (registry instanceof IXMLFullFormatter) {
IXMLFullFormatter formatter = (IXMLFullFormatter) registry;
FileContentGeneratorManager manager = new FileContentGeneratorManager(formatter);
registry.registerComponent(manager);
}
}

@Override
Expand Down
Expand Up @@ -27,6 +27,7 @@
import org.eclipse.lemminx.services.extensions.diagnostics.IDiagnosticsParticipant;
import org.eclipse.lemminx.services.extensions.format.IFormatterParticipant;
import org.eclipse.lemminx.services.extensions.save.ISaveContext;
import org.eclipse.lemminx.services.extensions.save.ISaveContext.SaveContextType;
import org.eclipse.lemminx.uriresolver.URIResolverExtensionManager;
import org.eclipse.lsp4j.InitializeParams;

Expand Down Expand Up @@ -112,7 +113,10 @@ public void initializeParams(InitializeParams params) {
public void doSave(ISaveContext saveContext) {
if (initialized) {
extensions.stream().forEach(extension -> extension.doSave(saveContext));
} else {
} else if (this.initialSaveContext == null || (saveContext != null && saveContext.getType() == SaveContextType.SETTINGS)) {
// capture initial configuration iff:
// 1. the saveContext is for configuration, not document save
// 2. we haven't captured settings before
this.initialSaveContext = saveContext;
}
}
Expand Down Expand Up @@ -382,4 +386,4 @@ public void setNotificationService(IXMLNotificationService notificationService)
this.notificationService = notificationService;
}

}
}
@@ -0,0 +1,101 @@
/**
* Copyright (c) 2020 Yatao Li.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Yatao Li <yatao.li@live.com> - initial API and implementation
*/
package org.eclipse.lemminx.services.extensions;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.function.Predicate;

import org.eclipse.lemminx.XMLAssert.SettingsSaveContext;
import org.eclipse.lemminx.dom.DOMDocument;
import org.eclipse.lemminx.services.extensions.IXMLExtension;
import org.eclipse.lemminx.services.extensions.XMLExtensionsRegistry;
import org.eclipse.lemminx.services.extensions.save.ISaveContext;
import org.eclipse.lsp4j.InitializeParams;
import org.junit.jupiter.api.Test;

import com.google.gson.JsonObject;

/**
* Ensures that {@code XMLExtensionsRegistry.doSave()} correctly captures the
* initial LS configuration, and the configuration is correctly forwarded to
* {@code IXMLExtension.doSave()} for registered extensions.
*/
public class ExtensionRegistryDoSaveTest {

class RegistryTestExtension implements IXMLExtension {

private ISaveContext context = null;

@Override
public void start(InitializeParams params, XMLExtensionsRegistry registry) {
}

@Override
public void stop(XMLExtensionsRegistry registry) {
}

@Override
public void doSave(ISaveContext context) {
this.context = context;
}

public ISaveContext getContext() {
return context;
}
}

private static class SaveFileContext implements ISaveContext {
@Override
public DOMDocument getDocument(String uri) {
return null;
}

@Override
public void collectDocumentToValidate(Predicate<DOMDocument> validateDocumentPredicate) {
}

@Override
public SaveContextType getType() {
return SaveContextType.DOCUMENT;
}

@Override
public String getUri() {
return null;
}

@Override
public Object getSettings() {
return null;
}
}

@Test
public void initialConfiguration() {
XMLExtensionsRegistry registry = new XMLExtensionsRegistry();
ISaveContext settingsSaveContext = new SettingsSaveContext(new JsonObject());
// initial config passed in during server startup.
registry.doSave(settingsSaveContext);
// then another event comes in from the client
registry.doSave(new SaveFileContext());
// extension registration is triggered
registry.initializeIfNeeded();
RegistryTestExtension extension = new RegistryTestExtension();
registry.registerExtension(extension);
// examine that the extension properly receives the initial config
assertEquals(settingsSaveContext, extension.getContext());
}
}

0 comments on commit f0503e2

Please sign in to comment.