Search in sources :

Example 1 with TextDocumentEdit

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());
}
Also used : LinkedTreeMap(com.google.gson.internal.LinkedTreeMap) BLangService(org.wso2.ballerinalang.compiler.tree.BLangService) ArrayList(java.util.ArrayList) TextDocumentEdit(org.eclipse.lsp4j.TextDocumentEdit) VersionedTextDocumentIdentifier(org.eclipse.lsp4j.VersionedTextDocumentIdentifier) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) TextEdit(org.eclipse.lsp4j.TextEdit) LSCustomErrorStrategy(org.ballerinalang.langserver.common.LSCustomErrorStrategy)

Example 2 with TextDocumentEdit

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;
}
Also used : CommonUtil(org.ballerinalang.langserver.common.utils.CommonUtil) Arrays(java.util.Arrays) Range(org.eclipse.lsp4j.Range) HashMap(java.util.HashMap) Lists(org.wso2.ballerinalang.util.Lists) ArrayList(java.util.ArrayList) LSDocument(org.ballerinalang.langserver.common.LSDocument) List(java.util.List) TextEdit(org.eclipse.lsp4j.TextEdit) TextDocumentEdit(org.eclipse.lsp4j.TextDocumentEdit) Map(java.util.Map) Location(org.eclipse.lsp4j.Location) Position(org.eclipse.lsp4j.Position) VersionedTextDocumentIdentifier(org.eclipse.lsp4j.VersionedTextDocumentIdentifier) WorkspaceDocumentManager(org.ballerinalang.langserver.workspace.WorkspaceDocumentManager) RenameParams(org.eclipse.lsp4j.RenameParams) Comparator(java.util.Comparator) Collections(java.util.Collections) HashMap(java.util.HashMap) Position(org.eclipse.lsp4j.Position) ArrayList(java.util.ArrayList) TextDocumentEdit(org.eclipse.lsp4j.TextDocumentEdit) Range(org.eclipse.lsp4j.Range) VersionedTextDocumentIdentifier(org.eclipse.lsp4j.VersionedTextDocumentIdentifier) LSDocument(org.ballerinalang.langserver.common.LSDocument) TextEdit(org.eclipse.lsp4j.TextEdit) Location(org.eclipse.lsp4j.Location)

Example 3 with TextDocumentEdit

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);
}
Also used : ApplyWorkspaceEditParams(org.eclipse.lsp4j.ApplyWorkspaceEditParams) TextEdit(org.eclipse.lsp4j.TextEdit) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) TextDocumentEdit(org.eclipse.lsp4j.TextDocumentEdit)

Aggregations

TextDocumentEdit (org.eclipse.lsp4j.TextDocumentEdit)3 TextEdit (org.eclipse.lsp4j.TextEdit)3 ArrayList (java.util.ArrayList)2 VersionedTextDocumentIdentifier (org.eclipse.lsp4j.VersionedTextDocumentIdentifier)2 LinkedTreeMap (com.google.gson.internal.LinkedTreeMap)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 LSCustomErrorStrategy (org.ballerinalang.langserver.common.LSCustomErrorStrategy)1 LSDocument (org.ballerinalang.langserver.common.LSDocument)1 CommonUtil (org.ballerinalang.langserver.common.utils.CommonUtil)1 WorkspaceDocumentManager (org.ballerinalang.langserver.workspace.WorkspaceDocumentManager)1 ApplyWorkspaceEditParams (org.eclipse.lsp4j.ApplyWorkspaceEditParams)1 Location (org.eclipse.lsp4j.Location)1 Position (org.eclipse.lsp4j.Position)1 Range (org.eclipse.lsp4j.Range)1 RenameParams (org.eclipse.lsp4j.RenameParams)1