Skip to content

Commit

Permalink
some refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: Arun Venmany <[email protected]>
  • Loading branch information
arunvenmany-ibm committed Feb 11, 2025
1 parent 35c7595 commit fdc0f9d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2024 IBM Corporation and others.
* Copyright (c) 2024, 2025 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 @@ -15,20 +15,17 @@

import com.google.gson.JsonPrimitive;
import io.openliberty.tools.langserver.lemminx.services.SettingsService;
import org.apache.commons.lang3.StringUtils;
import io.openliberty.tools.langserver.lemminx.util.LibertyUtils;
import org.eclipse.lemminx.commons.CodeActionFactory;
import org.eclipse.lemminx.dom.DOMDocument;
import org.eclipse.lemminx.services.extensions.codeaction.ICodeActionParticipant;
import org.eclipse.lemminx.services.extensions.codeaction.ICodeActionRequest;
import org.eclipse.lemminx.utils.XMLPositionUtility;
import org.eclipse.lsp4j.CodeAction;
import org.eclipse.lsp4j.Diagnostic;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.jsonrpc.CancelChecker;

import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
Expand Down Expand Up @@ -60,19 +57,22 @@ public void doCodeAction(ICodeActionRequest request, List<CodeAction> codeAction
// filter with entered word -> may not be required
String finalInvalidVariable = invalidVariable;
Set<Map.Entry<Object, Object>> filteredVariables = existingVariables
.entrySet().stream().filter(entry ->
entry.getKey().toString().toLowerCase()
.contains(finalInvalidVariable.toLowerCase())
|| finalInvalidVariable.toLowerCase().contains(entry.getKey().toString().toLowerCase())
).collect(Collectors.toSet());
.entrySet().stream()
.filter(entry -> LibertyUtils.containsEachOther(entry.toString(), finalInvalidVariable, false))
.collect(Collectors.toSet());
for (Map.Entry<Object, Object> nextVariable : filteredVariables) {
String title = "Replace attribute value with " + nextVariable.getKey();
String variableInDoc = String.format("${%s}", nextVariable.getKey().toString());
codeActions.add(CodeActionFactory.replace(title, diagnostic.getRange(), variableInDoc, document.getTextDocument(), diagnostic));
}
Position insertPos=new Position();
// code action for add variable
Position insertPos = new Position();
// variable will be added in the current line where diagnostic is showing
// line separator in end of text insert will move current diagnostic line to 1 line
insertPos.setLine(diagnostic.getRange().getEnd().getLine());
codeActions.add(CodeActionFactory.insert("Add variable "+ invalidVariable, insertPos, String.format(" <variable name=\"%s\" value=\"\"/> %s",invalidVariable,System.lineSeparator()), document.getTextDocument(), diagnostic));
codeActions.add(CodeActionFactory.insert("Add variable " + invalidVariable, insertPos,
String.format(" <variable name=\"%s\" value=\"\"/> %s", invalidVariable, System.lineSeparator()),
document.getTextDocument(), diagnostic));
}
} catch (Exception e) {
// BadLocationException not expected
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -625,4 +625,20 @@ public static List<VariableLoc> getVariablesFromTextContent(String docContent) {
}
return variables;
}

/**
* check whether two strings contains each other
*
* @param string string 1
* @param otherString string 2
* @param caseSensitive is comparison case-sensitive or not
* @return boolean result
*/
public static boolean containsEachOther(String string, String otherString, boolean caseSensitive) {
if (!caseSensitive) {
string = string.toLowerCase();
otherString = otherString.toLowerCase();
}
return string.contains(otherString) || otherString.contains(string);
}
}

0 comments on commit fdc0f9d

Please sign in to comment.