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