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