Skip to content

Commit

Permalink
Code completion for variable on attribute value
Browse files Browse the repository at this point in the history
Signed-off-by: Arun Venmany <[email protected]>
  • Loading branch information
arunvenmany-ibm committed Dec 2, 2024
1 parent 60345bd commit eaa1061
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Properties;
import java.util.Set;
import java.util.stream.Collectors;

Expand All @@ -28,6 +29,7 @@
import org.eclipse.lemminx.services.extensions.completion.ICompletionResponse;
import org.eclipse.lemminx.utils.XMLPositionUtility;
import org.eclipse.lsp4j.CompletionItem;
import org.eclipse.lsp4j.CompletionItemKind;
import org.eclipse.lsp4j.InsertReplaceEdit;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.TextEdit;
Expand All @@ -43,6 +45,30 @@

public class LibertyCompletionParticipant extends CompletionParticipantAdapter {


@Override
public void onAttributeValue(String valuePrefix, ICompletionRequest request, ICompletionResponse response, CancelChecker cancelChecker) throws Exception {
if (!LibertyUtils.isConfigXMLFile(request.getXMLDocument()))
return;
Properties variableProps = SettingsService.getInstance().getVariables();
String variableName = valuePrefix.replace("$", "")
.replace("{", "")
.replace("}", "");
variableProps.entrySet().stream().filter(it -> it.getKey().toString().contains(variableName))
.forEach(variableProp -> {
String varValue = String.format("${%s}", variableProp.getKey());

Either<TextEdit, InsertReplaceEdit> edit = Either.forLeft(new
TextEdit(request.getReplaceRange(), varValue));
CompletionItem completionItem = new CompletionItem();
completionItem.setLabel(variableProp.getKey().toString());
completionItem.setTextEdit(edit);
completionItem.setFilterText(variableProp.getKey().toString());
completionItem.setKind(CompletionItemKind.Value);
response.addCompletionItem(completionItem);
});
}

@Override
public void onXMLContent(ICompletionRequest request, ICompletionResponse response, CancelChecker cancelChecker)
throws IOException, BadLocationException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ public Hover onAttributeName(IHoverRequest request, CancelChecker cancelChecker)
}

@Override
public Hover onAttributeValue(IHoverRequest request, CancelChecker cancelChecker) {
List<String> variables = LibertyUtils.getVariablesFromTextContent(request.getNode().getTextContent());
public Hover onAttributeValue(IHoverRequest request, CancelChecker cancelChecker) {List<String> variables = LibertyUtils.getVariablesFromTextContent(request.getNode().getTextContent());
if(variables.size() == 1){
Properties variableMap = SettingsService.getInstance().getVariables();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,47 @@
package io.openliberty;

import io.openliberty.tools.langserver.lemminx.services.SettingsService;
import org.eclipse.lemminx.XMLAssert;
import org.eclipse.lemminx.commons.BadLocationException;
import org.eclipse.lsp4j.CompletionItem;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import static org.eclipse.lemminx.XMLAssert.*;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
public class LibertyCompletionTest {

@Mock
SettingsService settingsService;

MockedStatic settings;
static String newLine = System.lineSeparator();
static String serverXMLURI = "test/server.xml";

@BeforeEach
public void setup(){
settings= Mockito.mockStatic(SettingsService.class);
settings.when(SettingsService::getInstance).thenReturn(settingsService);
}

@AfterEach
public void cleanup(){
settings.close();
}


// Tests the availability of completion of XML elements provided by the
// server.xsd file
@Test
Expand Down Expand Up @@ -264,4 +294,77 @@ public void testFeatureRepetitionCompletionItem() throws BadLocationException {
XMLAssert.testCompletionFor(serverXML, null, serverXMLURI, 3, sipServletCompletionItem);
}

// Tests the
// availability of feature completion
@Test
public void testVariableCompletionItem() throws BadLocationException {
String serverXML = String.join(newLine, //
"<server description=\"Sample Liberty server\">", //
" <featureManager>", //
" <platform>javaee-6.0</platform>", //
" <feature>acmeCA-2.0</feature>", //
" </featureManager>", //
" <httpEndpoint host=\"*\" httpPort=\"de|\"\n",//
" httpsPort=\"${default.https.port}\" id=\"defaultHttpEndpoint\"/>",//
"</server>" //
);
Map<String,String> propsMap=new HashMap<>();
propsMap.put("default.http.port","9080");
propsMap.put("default.https.port","9443");
propsMap.put("testVar","false");
propsMap.put("testVar2","true");
Properties props = new Properties();
props.putAll(propsMap);

when(settingsService.getVariables()).thenReturn(props);
CompletionItem httpCompletion = c("default.http.port", "${default.http.port}");
CompletionItem httpsCompletion = c("default.https.port", "${default.https.port}");
final int TOTAL_ITEMS = 2; // total number of available completion items

XMLAssert.testCompletionFor(serverXML, null, serverXMLURI, TOTAL_ITEMS, httpCompletion,
httpsCompletion);

serverXML = String.join(newLine, //
"<server description=\"Sample Liberty server\">", //
" <featureManager>", //
" <platform>javaee-6.0</platform>", //
" <feature>acmeCA-2.0</feature>", //
" </featureManager>", //
" <httpEndpoint host=\"*\" httpPort=\"kkpp|\"\n",//
" httpsPort=\"${default.https.port}\" id=\"defaultHttpEndpoint\"/>",//
"</server>" //
);
XMLAssert.testCompletionFor(serverXML, null, serverXMLURI, 0);
}

@Test
public void testVariableCompletionItemWithDefaultXsdValues() throws BadLocationException {
String serverXML = String.join(newLine, //
"<server description=\"Sample Liberty server\">", //
" <featureManager>", //
" <platform>javaee-6.0</platform>", //
" <feature>acmeCA-2.0</feature>", //
" </featureManager>", //
" <applicationManager autoExpand=\"f|\"/>",//
"</server>" //
);
Map<String,String> propsMap=new HashMap<>();
propsMap.put("default.http.port","9080");
propsMap.put("default.https.port","9443");
propsMap.put("testVar","false");
propsMap.put("testVar2","true");
Properties props = new Properties();
props.putAll(propsMap);

when(settingsService.getVariables()).thenReturn(props);
CompletionItem httpCompletion = c("default.http.port", "${default.http.port}");
CompletionItem httpsCompletion = c("default.https.port", "${default.https.port}");
final int TOTAL_ITEMS = 4; // total number of available completion items
// variables values -> default.http.port, default.https.port
// default schema provided value, this is not being filtered -> true, false

XMLAssert.testCompletionFor(serverXML, null, serverXMLURI, TOTAL_ITEMS, httpCompletion,
httpsCompletion);
}

}

0 comments on commit eaa1061

Please sign in to comment.