public static T replaceBody()

in customization-base/src/main/java/com/azure/autorest/customization/implementation/Utils.java [378:410]


    public static <T extends CodeCustomization> T replaceBody(String newBody, CodeCustomization customization,
        Supplier<T> refreshedCustomizationSupplier) {
        SymbolInformation symbol = customization.getSymbol();
        Editor editor = customization.getEditor();
        String fileName = customization.getFileName();

        int line = symbol.getLocation().getRange().getStart().getLine();
        String methodBlockIndent = getIndent(editor.getFileLine(fileName, line));

        // Loop until the line containing the body start is found.
        Pattern startPattern = Pattern.compile(".*\\{\\s*");
        int startLine = walkDownFileUntilLineMatches(editor, fileName, line, lineContent ->
            startPattern.matcher(lineContent).matches()) + 1; // Plus one since the start is after the opening '{'

        // Then determine the base indentation level for the body.
        String methodContentIndent = getIndent(editor.getFileLine(fileName, startLine));
        Position oldBodyStart = new Position(startLine, methodContentIndent.length());

        // Then continue iterating over lines until the body close line is found.
        Pattern closePattern = Pattern.compile(methodBlockIndent + "}\\s*");
        int lastLine = walkDownFileUntilLineMatches(editor, fileName, startLine, lineContent ->
            closePattern.matcher(lineContent).matches()) - 1; // Minus one since the end is before the closing '}'
        Position oldBodyEnd = new Position(lastLine, editor.getFileLine(fileName, lastLine).length());

        editor.replaceWithIndentedContent(fileName, oldBodyStart, oldBodyEnd, newBody, methodContentIndent.length());
        FileEvent fileEvent = new FileEvent();
        fileEvent.setUri(customization.getFileUri());
        fileEvent.setType(FileChangeType.Changed);
        customization.getLanguageClient().notifyWatchedFilesChanged(Collections.singletonList(fileEvent));

        // Return the refreshed customization.
        return refreshedCustomizationSupplier.get();
    }