skip to Main Content

I upgraded to mac os big sur and XCode 12.3. Earlier I was using XCodeClangFormat extension to format my C++ files. But now this extension is not getting recognized. When I open the extension I don’t see it in XCode in the Editor menu. I have already checked that the XCodeClangFormat extension is enabled in the System Preferences -> Extensions option for XCode Editor.

Anyone else facing this issue? Can you please suggest a solution for it. Thanks for your help

3

Answers


  1. I have Xcode 12.4 and the source code formatter appears to be a priced product. If you click Xcode->Xcode Extensions, it takes you to App Store. There you will find Xformat priced at INR 269. Frankly, I didn’t attempt to use this. My favorite is astyle since my Linux days dating back to more than two decades.

    Login or Signup to reply.
  2. Same here. The extension no longer seems to run. But it is not that bad at all. Since a while ago, I started using a script that would reformat my entire project before I commit.

    <Edit>
    I just need to have Homebrew installed, so I can download clang-format by brew install clang-format (Ref: https://formulae.brew.sh/formula/clang-format)
    </Edit>

    It runs on double-click and I also integrated it to my git commit/pull/push script.

    <Better Script>:

    #!/bin/sh
    
    cd `dirname $0`
    
    if [ -z $(which clang-format) ] ; then
      echo "clang-format not found."
      echo "Install Homebrew, then:"
      echo "brew install clang-format"
      echo ""
      echo "Aborting."
      exit
    fi
    
    find . -name *.[h,m] | while read _fmt_f; do
    clang-format -i --style=file $_fmt_f
    done;
    

    <Old Script>:

    #!/bin/sh
    
    cd `dirname $0`
    
    # find files
    find . -name *.h > _fmt_files.txt
    find . -name *.m >> _fmt_files.txt
    
    # run formatter
    while read _fmt_f; do
    clang-format -i --style=file $_fmt_f
    done <_fmt_files.txt
    
    # make clean
    rm _fmt_files.txt
    

    In the folder where you run this script, you must place the style-file with name ‘.clang-format’. Its content should be familiar to you.

    If you use a predefined coding style, have a look at clang-format’s help clang-format --help and there look for ‘–style=’ description.

    Login or Signup to reply.
  3. Another way to use clang-format is by using automator. (Credits to the original author https://www.jianshu.com/p/1385ab726fc1)

    • Step 1: install clang-format (I used Homebrew), as Anticro have pointed out.
    • Step 2: Open automator and search for Run Shell Script
    • Step 3: Enter this script:
    export PATH = WHERE_YOUR_CLANG_FORMAT_IS_INSTALLED
    clang-format
    
    • Step 4: Make sure Output replaces selected text is checked, Workflow receives current is text, and in Xcode
    • Step 5: Save the quick action with the name you want, ex. clang-format
    • Step 6: You can put your own configuration for ".clang-format" inside the folder where you installed clang-format.

    Now you should be able to select the code and right click for Services -> clang-format (or the name you saved for the quick action)

    You can also add shortcuts for the quick action in Keyboard Preferences.

    Hope this helps! 😀 I just love this approach.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search