Skip to content

Commit

Permalink
Fix hover unexpectedly showing, Support configDirectory (#232)
Browse files Browse the repository at this point in the history
* Fix hover showing on non-configured config files

* Support configDirectory for custom config support.

* Add tests for configDirectory
  • Loading branch information
evie-lau authored Oct 18, 2023
1 parent 8348d53 commit 8ac6ad2
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ public class LibertyConfigFileManager {
public static final String LIBERTY_PLUGIN_CONFIG_XML = "liberty-plugin-config.xml";
public static final String CUSTOM_SERVER_ENV_XML_TAG = "serverEnv";
public static final String CUSTOM_BOOTSTRAP_PROPERTIES_XML_TAG = "bootstrapPropertiesFile";
public static final String CUSTOM_SERVER_CONFIG_DIR_TAG = "configDirectory";

private static final String DEFAULT_SERVER_ENV = "src/main/liberty/config/server.env".replace("/", File.separator);
private static final String DEFAULT_BOOTSTRAP_PROPERTIES = "src/main/liberty/config/bootstrap.properties".replace("/", File.separator);


private static Set<String> customServerEnvFiles = new HashSet<String>();
private static Set<String> customBootstrapFiles = new HashSet<String>();
private static Set<String> customConfigDirs = new HashSet<String>();

private static final Logger LOGGER = Logger.getLogger(LibertyConfigFileManager.class.getName());

Expand Down Expand Up @@ -81,16 +82,20 @@ public static void processLibertyPluginConfigXml(String uri) {
if (!uri.endsWith(LIBERTY_PLUGIN_CONFIG_XML)) {
return;
}
Map<String, String> customConfigFiles = XmlReader.readTagsFromXml(uri,
Map<String, String> customConfigs = XmlReader.readTagsFromXml(uri,
CUSTOM_SERVER_ENV_XML_TAG,
CUSTOM_BOOTSTRAP_PROPERTIES_XML_TAG);
CUSTOM_BOOTSTRAP_PROPERTIES_XML_TAG,
CUSTOM_SERVER_CONFIG_DIR_TAG);
// TODO: handle deletions
// match uri
if (customConfigFiles.containsKey(CUSTOM_SERVER_ENV_XML_TAG)) {
customServerEnvFiles.add(customConfigFiles.get(CUSTOM_SERVER_ENV_XML_TAG));
if (customConfigs.containsKey(CUSTOM_SERVER_ENV_XML_TAG)) {
customServerEnvFiles.add(customConfigs.get(CUSTOM_SERVER_ENV_XML_TAG));
}
if (customConfigs.containsKey(CUSTOM_BOOTSTRAP_PROPERTIES_XML_TAG)) {
customBootstrapFiles.add(customConfigs.get(CUSTOM_BOOTSTRAP_PROPERTIES_XML_TAG));
}
if (customConfigFiles.containsKey(CUSTOM_BOOTSTRAP_PROPERTIES_XML_TAG)) {
customBootstrapFiles.add(customConfigFiles.get(CUSTOM_BOOTSTRAP_PROPERTIES_XML_TAG));
if (customConfigs.containsKey(CUSTOM_SERVER_CONFIG_DIR_TAG)) {
customConfigDirs.add(customConfigs.get(CUSTOM_SERVER_CONFIG_DIR_TAG));
}
}

Expand All @@ -103,13 +108,22 @@ public static boolean isServerEnvFile(LibertyTextDocument tdi) {
* - is default server.env file in `src/main/liberty/config`
* - is custom env file specified in liberty-plugin-config.xml (generated from
* build file)
* - is default server.env file in custom configDirectory
*
* @param uri - normally comes from LibertyTextDocument.getUri() which is a URI formatted string (file:///path/to/file)
* @return
*/
public static boolean isServerEnvFile(String uri) {
String filePath = normalizeFilePath(uri);
return filePath.endsWith(DEFAULT_SERVER_ENV) || customServerEnvFiles.contains(filePath);
if (filePath.endsWith(DEFAULT_SERVER_ENV) || customServerEnvFiles.contains(filePath)) {
return true;
}

File file = new File(filePath);
if (file.getName().equals("server.env")) {
return customConfigDirs.contains(file.getParent());
}
return false;
}

public static boolean isBootstrapPropertiesFile(LibertyTextDocument tdi) {
Expand All @@ -121,13 +135,31 @@ public static boolean isBootstrapPropertiesFile(LibertyTextDocument tdi) {
* - is default bootstrap.properties file in `src/main/liberty/config`
* - is custom properties file specified in liberty-plugin-config.xml (generated
* from build file)
* - is default bootstrap.properties file in custom configDirectory
*
* @param uri - normally comes from LibertyTextDocument.getUri() which is a URI formatted string (file:///path/to/file)
* @return
*/
public static boolean isBootstrapPropertiesFile(String uri) {
String filePath = normalizeFilePath(uri);
return filePath.endsWith(DEFAULT_BOOTSTRAP_PROPERTIES) || customBootstrapFiles.contains(filePath);
if (filePath.endsWith(DEFAULT_BOOTSTRAP_PROPERTIES) || customBootstrapFiles.contains(filePath)) {
return true;
}

File file = new File(filePath);
if (file.getName().equals("bootstrap.properties")) {
return customConfigDirs.contains(file.getParent());
}
return false;
}

/**
* Returns true if valid server.env or bootstrap.properties file defined by defaults or custom settings
* @param tdi
* @return
*/
public static boolean isConfigFile(LibertyTextDocument tdi) {
return isServerEnvFile(tdi) || isBootstrapPropertiesFile(tdi);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2020, 2022 IBM Corporation and others.
* Copyright (c) 2020, 2023 IBM Corporation and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand Down Expand Up @@ -116,7 +116,6 @@ private void validateAll() {

@Override
public CompletableFuture<Hover> hover(HoverParams hoverParams) {
LOGGER.info("hover: " + hoverParams.toString());
String uri = hoverParams.getTextDocument().getUri();
LibertyTextDocument textDocumentItem = documents.get(uri);
return new LibertyPropertiesHoverProvider(textDocumentItem).getHover(hoverParams.getPosition());
Expand Down Expand Up @@ -145,6 +144,5 @@ private void validate(List<String> uris) {
if (uris.isEmpty()) {
return;
}
LOGGER.info("Running validation for: " + uris.toString());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2022 IBM Corporation and others.
* Copyright (c) 2022, 2023 IBM Corporation and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand All @@ -12,8 +12,10 @@
import java.util.concurrent.CompletableFuture;

import org.eclipse.lsp4j.Hover;
import org.eclipse.lsp4j.MarkupContent;
import org.eclipse.lsp4j.Position;

import io.openliberty.tools.langserver.LibertyConfigFileManager;
import io.openliberty.tools.langserver.ls.LibertyTextDocument;
import io.openliberty.tools.langserver.model.propertiesfile.PropertiesEntryInstance;
import io.openliberty.tools.langserver.utils.ParserFileHelperUtil;
Expand All @@ -26,6 +28,10 @@ public LibertyPropertiesHoverProvider(LibertyTextDocument textDocumentItem) {
}

public CompletableFuture<Hover> getHover(Position position) {
if (!LibertyConfigFileManager.isConfigFile(textDocumentItem)) {
// return empty Hover if not a server config file
return CompletableFuture.completedFuture(new Hover(new MarkupContent("plaintext", "")));
}
String entryLine = new ParserFileHelperUtil().getLine(textDocumentItem, position);
return new PropertiesEntryInstance(entryLine, textDocumentItem).getHover(position);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ public void validateServerProperty() {
String value = entry.getValue();

// check whitespace around equal sign (=)
LOGGER.info("Validating property line: " + entry.getLineContent());
if (LibertyConfigFileManager.isServerEnvFile(textDocumentItem)) {
if (property.endsWith(" ") || value.startsWith(" ")) {
startChar = property.trim().length();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@ public class LibertyConfigFileManagerTest {

public final String CUSTOM_SERVER_ENV = "file:/user/sample-project/src/main/liberty/config/customserverenv.env";
public final String CUSTOM_BOOTSTRAP_PROPERTIES = "file:/user/sample-project/src/main/liberty/config/custombootstrapprops.properties";
public final String CUSTOM_CONFIG_DIR_ENV = "file:/user/sample-project/src/main/liberty/config2/server.env";
public final String CUSTOM_CONFIG_DIR_PROP = "file:/user/sample-project/src/main/liberty/config2/bootstrap.properties";
public final String WINDOWS_CUSTOM_SERVER_ENV = "file:/C:/user/sample-project/src/main/liberty/config/customserverenv.env";
public final String WINDOWS_CUSTOM_BOOTSTRAP_PROPERTIES = "file:/C:/user/sample-project/src/main/liberty/config/custombootstrapprops.properties";
public final String WINDOWS_CUSTOM_CONFIG_DIR_ENV = "file:/C:/user/sample-project/src/main/liberty/config2/server.env";
public final String WINDOWS_CUSTOM_CONFIG_DIR_PROP = "file:/C:/user/sample-project/src/main/liberty/config2/bootstrap.properties";

public final String ANOTHER_BOOTSTRAP = "file:/user/sample-project/src/main/another.properties";

@Test
Expand All @@ -29,6 +34,8 @@ public void processConfigXml() throws IOException {
LibertyConfigFileManager.processLibertyPluginConfigXml(lpcXml.toURI().toString());
assertTrue(LibertyConfigFileManager.isServerEnvFile(CUSTOM_SERVER_ENV));
assertTrue(LibertyConfigFileManager.isBootstrapPropertiesFile(CUSTOM_BOOTSTRAP_PROPERTIES));
assertTrue(LibertyConfigFileManager.isServerEnvFile(CUSTOM_CONFIG_DIR_ENV));
assertTrue(LibertyConfigFileManager.isBootstrapPropertiesFile(CUSTOM_CONFIG_DIR_PROP));
}
}

Expand All @@ -37,9 +44,11 @@ public void processWindowsConfigXml() throws IOException {
if (!File.separator.equals("/")) {
File lpcXml = new File(resourcesDir, "xml/windows/liberty-plugin-config.xml");

LibertyConfigFileManager.processLibertyPluginConfigXml(lpcXml.getCanonicalPath());
LibertyConfigFileManager.processLibertyPluginConfigXml(lpcXml.toURI().toString());
assertTrue(LibertyConfigFileManager.isServerEnvFile(WINDOWS_CUSTOM_SERVER_ENV));
assertTrue(LibertyConfigFileManager.isBootstrapPropertiesFile(WINDOWS_CUSTOM_BOOTSTRAP_PROPERTIES));
assertTrue(LibertyConfigFileManager.isServerEnvFile(WINDOWS_CUSTOM_CONFIG_DIR_ENV));
assertTrue(LibertyConfigFileManager.isBootstrapPropertiesFile(WINDOWS_CUSTOM_CONFIG_DIR_PROP));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<userDirectory>/user/sample-project/target/liberty/wlp/usr</userDirectory>
<serverOutputDirectory>/user/sample-project/target/liberty/wlp/usr/servers/defaultServer</serverOutputDirectory>
<serverName>defaultServer</serverName>
<configDirectory>/user/sample-project/src/main/liberty/config</configDirectory>
<configDirectory>/user/sample-project/src/main/liberty/config2</configDirectory>
<configFile>/user/sample-project/src/main/liberty/config/server.xml</configFile>
<bootstrapPropertiesFile>/user/sample-project/src/main/liberty/config/custombootstrapprops.properties</bootstrapPropertiesFile>
<serverEnv>/user/sample-project/src/main/liberty/config/customserverenv.env</serverEnv>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version='1.0' encoding='UTF-8'?>
<liberty-plugin-config version="2.0">
<installDirectory>C:\Users\user\sample-project\build\wlp</installDirectory>
<configDirectory>C:\user\sample-project\src\main\liberty\config2</configDirectory>
<bootstrapPropertiesFile>C:\user\sample-project\src\main\liberty\config\custombootstrapprops.properties</bootstrapPropertiesFile>
<serverEnv>C:\user\sample-project\src\main\liberty\config\customserverenv.env</serverEnv>
<servers>
Expand Down

0 comments on commit 8ac6ad2

Please sign in to comment.