public ClassCustomization addStaticBlock()

in customization-base/src/main/java/com/azure/autorest/customization/ClassCustomization.java [109:145]


    public ClassCustomization addStaticBlock(String staticCodeBlock, List<String> importsToAdd) {

        if (Utils.isNullOrEmpty(staticCodeBlock)) {
            return this;
        }

        // the class declaration line
        int lastSymbolLine = symbol.getLocation().getRange().getStart().getLine();

        // Find the last field symbol.
        Optional<SymbolInformation> lastSymbol = languageClient.listDocumentSymbols(fileUri).stream()
                .filter(symbol -> symbol.getKind() == SymbolKind.Field)
                .reduce((first, second) -> second);

        int indentAmount = INDENT_LENGTH;
        if (lastSymbol.isPresent()) {
            // the line number of the last field declaration
            lastSymbolLine = lastSymbol.get().getLocation().getRange().getStart().getLine();
            indentAmount = Utils.getIndent(editor.getFileLine(fileName, lastSymbolLine)).length();
        }
//        System.out.println("indent amount " + indentAmount);

        // start the static block from the next line of the last field or the line after class declaration
        int staticBlockStartLine = lastSymbolLine + 1;
        editor.insertBlankLine(fileName, staticBlockStartLine, false);
        Position staticBlockPosition = editor.insertBlankLineWithIndent(fileName, staticBlockStartLine, indentAmount);
        if(!staticCodeBlock.trim().startsWith("static")) {
            staticCodeBlock = "static { " + System.lineSeparator() + staticCodeBlock + System.lineSeparator() +  "}";
        }

        editor.replaceWithIndentedContent(fileName, staticBlockPosition, staticBlockPosition, staticCodeBlock,
                staticBlockPosition.getCharacter());
        if (importsToAdd != null) {
            return Utils.addImports(importsToAdd, this, this::refreshSymbol);
        }
        return this;
    }