in customization-base/src/main/java/com/azure/autorest/customization/ClassCustomization.java [313:364]
public ConstructorCustomization addConstructor(String constructor, List<String> importsToAdd) {
// Get the signature of the constructor.
Matcher constructorSignatureMatcher = CONSTRUCTOR_SIGNATURE_PATTERN.matcher(constructor);
String constructorSignature = null;
if (constructorSignatureMatcher.find()) {
constructorSignature = constructorSignatureMatcher.group(1);
}
// Find all constructor and field symbols.
List<SymbolInformation> constructorLocationFinder = languageClient.listDocumentSymbols(fileUri).stream()
.filter(symbol -> symbol.getKind() == SymbolKind.Field || symbol.getKind() == SymbolKind.Constructor)
.collect(Collectors.toList());
// If no constructors or fields exist in the class place the constructor after the class declaration line.
// Otherwise place the constructor after the last constructor or field.
int constructorStartLine;
if (Utils.isNullOrEmpty(constructorLocationFinder)) {
constructorStartLine = symbol.getLocation().getRange().getStart().getLine();
} else {
SymbolInformation symbol = constructorLocationFinder.get(constructorLocationFinder.size() - 1);
// If the last symbol before the new constructor is a field only a new line needs to be inserted.
// Otherwise if the last symbol is a constructor its closing '}' needs to be found and then a new line
// needs to be inserted.
if (symbol.getKind() == SymbolKind.Field) {
constructorStartLine = symbol.getLocation().getRange().getStart().getLine();
} else {
constructorStartLine = symbol.getLocation().getRange().getStart().getLine();
List<String> fileLines = editor.getFileLines(fileName);
String currentLine = fileLines.get(constructorStartLine);
String constructorIdent = Utils.getIndent(currentLine);
while (!currentLine.endsWith("}") || !currentLine.equals(constructorIdent + "}")) {
currentLine = fileLines.get(++constructorStartLine);
}
}
}
int indentAmount = Utils.getIndent(editor.getFileLine(fileName, constructorStartLine)).length();
editor.insertBlankLine(fileName, ++constructorStartLine, false);
Position constructorPosition = editor.insertBlankLineWithIndent(fileName, ++constructorStartLine, indentAmount);
editor.replaceWithIndentedContent(fileName, constructorPosition, constructorPosition, constructor,
constructorPosition.getCharacter());
final String ctorSignature = (constructorSignature == null)
? editor.getFileLine(fileName, constructorStartLine)
: constructorSignature;
return Utils.addImports(importsToAdd, this, () -> getConstructor(ctorSignature));
}