OLD | NEW |
1 -- In this file, change "/path/to/" to the path where you installed clang-format | 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 | 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 | 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 | 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 | 5 -- can assign the menu item a keyboard shortcut in the BBEdit preferences, under |
6 -- Menus & Shortcuts. | 6 -- Menus & Shortcuts. |
7 on urlToPOSIXPath(theURL) | 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 | 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 | 9 end urlToPOSIXPath |
10 | 10 |
11 tell application "BBEdit" | 11 tell application "BBEdit" |
12 set selectionOffset to characterOffset of selection | 12 set selectionOffset to characterOffset of selection |
13 set selectionLength to length of selection | 13 set selectionLength to length of selection |
14 set fileURL to URL of text document 1 | 14 set fileURL to URL of text document 1 |
15 end tell | 15 end tell |
16 | 16 |
17 set filePath to urlToPOSIXPath(fileURL) | 17 set filePath to urlToPOSIXPath(fileURL) |
18 set newContents to do shell script "/path/to/clang-format -offset=" & selectionO
ffset & " -length=" & selectionLength & " " & quoted form of filePath | 18 set newContents to do shell script "/path/to/clang-format -offset=" & selectionO
ffset & " -length=" & selectionLength & " " & quoted form of filePath |
19 | 19 |
20 tell application "BBEdit" | 20 tell application "BBEdit" |
21 -- "set contents of text document 1 to newContents" scrolls to the botto
m while | 21 -- "set contents of text document 1 to newContents" scrolls to the botto
m while |
22 -- replacing a selection flashes a bit but doesn't affect the scroll pos
ition. | 22 -- replacing a selection flashes a bit but doesn't affect the scroll pos
ition. |
23 set currentLength to length of contents of text document 1 | 23 set currentLength to length of contents of text document 1 |
24 select characters 1 thru currentLength of text document 1 | 24 select characters 1 thru currentLength of text document 1 |
25 set text of selection to newContents | 25 set text of selection to newContents |
26 select characters selectionOffset thru (selectionOffset + selectionLengt
h - 1) of text document 1 | 26 select characters selectionOffset thru (selectionOffset + selectionLengt
h - 1) of text document 1 |
27 end tell | 27 end tell |
OLD | NEW |