public static String format(TokenTree root, int maxWidth) {
        List<TreeCursor> errors = root.zipper().findChildrenByType(TreeType.ERROR);

        if (!errors.isEmpty()) {
            throw new ModelSyntaxException("Cannot format invalid models: " + errors.get(0).getTree().getError(),
                    errors.get(0));
        }

        root = new SortUseStatements().apply(root);
        root = new FixBadDocComments().apply(root);
        root = new RemoveUnusedUseStatements().apply(root);

        // Strip trailing spaces from each line.
        String result = new FormatVisitor(maxWidth).visit(root.zipper()).render(maxWidth).trim();
        StringBuilder builder = new StringBuilder();
        for (String line : result.split(System.lineSeparator())) {
            builder.append(StringUtils.stripEnd(line, " \t")).append(System.lineSeparator());
        }
        return builder.toString();
    }
Formats the given token tree.

@param root     Root {@link TreeType#IDL} tree node to format.
@param maxWidth Maximum line width.
@return Returns the formatted model as a string.
@throws ModelSyntaxException if the model contains errors.
smithy-syntax/src/main/java/software/amazon/smithy/syntax/Formatter.java