use of org.eclipse.lsp4j.TextDocumentEdit in project ballerina by ballerina-lang.
the class CommandExecutor method executeAddAllDocumentation.
/**
* Generate workspace edit for generating doc comments for all top level nodes and resources.
* @param context Workspace Service Context
*/
private static void executeAddAllDocumentation(WorkspaceServiceContext context) {
String documentUri = "";
VersionedTextDocumentIdentifier textDocumentIdentifier = new VersionedTextDocumentIdentifier();
for (Object arg : context.get(ExecuteCommandKeys.COMMAND_ARGUMENTS_KEY)) {
if (((LinkedTreeMap) arg).get(ARG_KEY).equals(CommandConstants.ARG_KEY_DOC_URI)) {
documentUri = (String) ((LinkedTreeMap) arg).get(ARG_VALUE);
textDocumentIdentifier.setUri(documentUri);
context.put(DocumentServiceKeys.FILE_URI_KEY, documentUri);
}
}
BLangPackage bLangPackage = TextDocumentServiceUtil.getBLangPackage(context, context.get(ExecuteCommandKeys.DOCUMENT_MANAGER_KEY), false, LSCustomErrorStrategy.class, false).get(0);
String fileContent = context.get(ExecuteCommandKeys.DOCUMENT_MANAGER_KEY).getFileContent(Paths.get(URI.create(documentUri)));
String[] contentComponents = fileContent.split(System.lineSeparator());
List<TextEdit> textEdits = new ArrayList<>();
bLangPackage.topLevelNodes.forEach(topLevelNode -> {
CommandUtil.DocAttachmentInfo docAttachmentInfo = getDocumentEditForNode(topLevelNode);
if (docAttachmentInfo != null) {
textEdits.add(getTextEdit(docAttachmentInfo, contentComponents));
}
if (topLevelNode instanceof BLangService) {
((BLangService) topLevelNode).getResources().forEach(bLangResource -> {
CommandUtil.DocAttachmentInfo resourceInfo = getDocumentEditForNode(bLangResource);
if (resourceInfo != null) {
textEdits.add(getTextEdit(resourceInfo, contentComponents));
}
});
}
});
TextDocumentEdit textDocumentEdit = new TextDocumentEdit(textDocumentIdentifier, textEdits);
applyWorkspaceEdit(Collections.singletonList(textDocumentEdit), context.get(ExecuteCommandKeys.LANGUAGE_SERVER_KEY).getClient());
}
use of org.eclipse.lsp4j.TextDocumentEdit in project ballerina by ballerina-lang.
the class RenameUtil method getRenameTextEdits.
/**
* Get the list of rename related TextEdits.
*
* @param locationList List of locations of occurrences
* @param documentManager {@link WorkspaceDocumentManager} instance
* @param newName New name to be replaced with
* @param replaceSymbolName Symbol name being replaced
* @return {@link List} List of TextEdits
*/
public static List<TextDocumentEdit> getRenameTextEdits(List<Location> locationList, WorkspaceDocumentManager documentManager, String newName, String replaceSymbolName) {
Map<String, ArrayList<Location>> documentLocationMap = new HashMap<>();
List<TextDocumentEdit> documentEdits = new ArrayList<>();
Comparator<Location> locationComparator = (location1, location2) -> location1.getRange().getStart().getCharacter() - location2.getRange().getStart().getCharacter();
locationList.forEach(location -> {
if (documentLocationMap.containsKey(location.getUri())) {
documentLocationMap.get(location.getUri()).add(location);
} else {
documentLocationMap.put(location.getUri(), (ArrayList<Location>) Lists.of(location));
}
});
documentLocationMap.forEach((uri, locations) -> {
Collections.sort(locations, locationComparator);
String fileContent = documentManager.getFileContent(CommonUtil.getPath(new LSDocument(uri)));
String[] contentComponents = fileContent.split("\\n|\\r\\n|\\r");
int lastNewLineCharIndex = Math.max(fileContent.lastIndexOf("\n"), fileContent.lastIndexOf("\r"));
int lastCharCol = fileContent.substring(lastNewLineCharIndex + 1).length();
for (Location location : locations) {
int line = location.getRange().getStart().getLine();
StringBuilder lineComponent = new StringBuilder(contentComponents[line]);
int index = lineComponent.indexOf(replaceSymbolName);
while (index >= 0) {
char previousChar = lineComponent.charAt(index - 1);
if (Character.isLetterOrDigit(previousChar) || String.valueOf(previousChar).equals("_")) {
index = lineComponent.indexOf(replaceSymbolName, index + replaceSymbolName.length());
} else {
lineComponent.replace(index, index + replaceSymbolName.length(), newName);
index = lineComponent.indexOf(replaceSymbolName, index + newName.length());
}
}
contentComponents[line] = lineComponent.toString();
}
Range range = new Range(new Position(0, 0), new Position(contentComponents.length, lastCharCol));
TextEdit textEdit = new TextEdit(range, String.join("\r\n", Arrays.asList(contentComponents)));
VersionedTextDocumentIdentifier textDocumentIdentifier = new VersionedTextDocumentIdentifier();
textDocumentIdentifier.setUri(uri);
TextDocumentEdit textDocumentEdit = new TextDocumentEdit(textDocumentIdentifier, Collections.singletonList(textEdit));
documentEdits.add(textDocumentEdit);
});
return documentEdits;
}
use of org.eclipse.lsp4j.TextDocumentEdit in project ballerina by ballerina-lang.
the class CommandExecutor method applySingleTextEdit.
private static void applySingleTextEdit(String editText, Range range, VersionedTextDocumentIdentifier identifier, LanguageClient client) {
WorkspaceEdit workspaceEdit = new WorkspaceEdit();
ApplyWorkspaceEditParams applyWorkspaceEditParams = new ApplyWorkspaceEditParams();
TextEdit textEdit = new TextEdit(range, editText);
TextDocumentEdit textDocumentEdit = new TextDocumentEdit(identifier, Collections.singletonList(textEdit));
workspaceEdit.setDocumentChanges(Collections.singletonList(textDocumentEdit));
applyWorkspaceEditParams.setEdit(workspaceEdit);
client.applyEdit(applyWorkspaceEditParams);
}
Aggregations