1 | -- In this file, change "/path/to/" to the path where you installed clang-format |
2 | -- and save it to ~/Library/Application Support/BBEdit/Scripts. You can then |
3 | -- select the script from the Script menu and clang-format will format the |
4 | -- selection. Note that you can rename the menu item by renaming the script, and |
5 | -- can assign the menu item a keyboard shortcut in the BBEdit preferences, under |
6 | -- Menus & Shortcuts. |
7 | on urlToPOSIXPath(theURL) |
8 | return do shell script "python -c \"import urllib, urlparse, sys; print urllib.unquote(urlparse.urlparse(sys.argv[1])[2])\" " & quoted form of theURL |
9 | end urlToPOSIXPath |
10 | |
11 | tell application "BBEdit" |
12 | set selectionOffset to characterOffset of selection |
13 | set selectionLength to length of selection |
14 | set fileURL to URL of text document 1 |
15 | end tell |
16 | |
17 | set filePath to urlToPOSIXPath(fileURL) |
18 | set newContents to do shell script "/path/to/clang-format -offset=" & selectionOffset & " -length=" & selectionLength & " " & quoted form of filePath |
19 | |
20 | tell application "BBEdit" |
21 | -- "set contents of text document 1 to newContents" scrolls to the bottom while |
22 | -- replacing a selection flashes a bit but doesn't affect the scroll position. |
23 | set currentLength to length of contents of text document 1 |
24 | select characters 1 thru currentLength of text document 1 |
25 | set text of selection to newContents |
26 | select characters selectionOffset thru (selectionOffset + selectionLength - 1) of text document 1 |
27 | end tell |
28 | |