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