Minified code pasted from a chat window, an editorial, or your own submission history is unreadable, and pasting it into a full IDE just to indent it is a heavy answer to a small problem. The Code Formatter reindents C++, Java and Python in the page, with a choice of 2 or 4 spaces.
It is a real tokenizer rather than a regular expression. The source is broken into tokens, comments are lifted out into placeholders before anything is rearranged, and braces and operators drive the indentation. That is why a brace inside a string literal does not shift the whole rest of the file.
Each language has its own pass, because their block structures do not agree. C++ and Java indent on braces, while Python has none and its blocks are inferred from colons and the existing layout. Nothing is compiled, and no code is transmitted.
What Each Language Pass Handles
- The C++ pass knows keywords, template types and label keywords, so
public: and case dedent by one level rather than opening a block. - Operators get spacing from a token table that distinguishes binary from unary, which is what stops
i++ becoming i + + and -1 becoming - 1. - Angle brackets are ambiguous between templates and comparisons. The C++ pass uses the template type list to decide, so
vector<pair<int,int>> survives intact. - The Java pass follows the same brace logic with its own keyword set, which is why a class body and a method body nest correctly rather than flattening.
- The Python pass cannot use braces at all. It detects colon-terminated block openers, tracks an indent stack, and dedents on keywords such as
else and except, resetting to column 0 at each top-level definition.
Tricky Inputs and Where It Gives Up
- Comments are pulled out before formatting and always land on their own output line, so a trailing comment after a statement moves above it rather than being wrapped into the code.
- Preprocessor directives must begin a line. A macro spanning lines with trailing backslashes will not survive reindentation intact.
- Raw string literals and multi-line strings are not fully modelled. Code containing one is the case most likely to come back wrong.
- Python is the weakest of the three by nature. Already-correct indentation is preserved, but genuinely flattened Python cannot always be recovered, because the blocks were the information that was lost.
- This is a formatter, not a linter or a compiler. It will happily reindent code that does not build, which is often exactly what you want when reading somebody else's submission.
- Both panes update as you type, so pasting is the whole workflow and there is no format button to press.