Search code examples
clang-format

cursor position after invoking clang-format


I'm writing a plugin for some editor to use clang-format.

How to get the new position of the cursor after invoking clang-format? (I pass the original position to clang-format by --cursor=<uint>.)


Solution

  • If clang-format gets input from stdin, it will output the new cursor position first. I didn't try it before...

    $ cat a.cpp | clang-format --cursor=5
    { "Cursor": 5, "IncompleteFormat": false } # <= here
    int main() {}
    

    The above approach works only when using just ONE cursor.
    However, sometimes we need to use multi-cursor or there is a selected region, I wonder if there is an efficient method to get any number of new positions.