1 # This file is a minimal clang-format sublime-integration. To install:
2 # - Change 'binary' if clang-format is not on the path (see below).
3 # - Put this file into your sublime Packages directory, e.g. on Linux:
4 # ~/.config/sublime-text-2/Packages/User/clang-format-sublime.py
6 # { "keys": ["ctrl+shift+c"], "command": "clang_format" },
8 # With this integration you can press the bound key and clang-format will
9 # format the current lines and selections for all cursor positions. The lines
10 # or regions are extended to the next bigger syntactic entities.
12 # It operates on the current, potentially unsaved buffer and does not create
13 # or save any files. To revert a formatting, just undo.
15 from __future__
import absolute_import
, division
, print_function
20 # Change this to the full path if clang-format is not on the path.
21 binary
= "clang-format"
23 # Change this to format according to other formatting styles. See the output of
24 # 'clang-format --help' for a list of supported styles. The default looks for
25 # a '.clang-format' or '_clang-format' file to indicate the style that should be
30 class ClangFormatCommand(sublime_plugin
.TextCommand
):
32 encoding
= self
.view
.encoding()
33 if encoding
== "Undefined":
38 command
.extend(["-style", style
])
39 for region
in self
.view
.sel():
40 regions
.append(region
)
41 region_offset
= min(region
.a
, region
.b
)
42 region_length
= abs(region
.b
- region
.a
)
50 str(self
.view
.file_name()),
53 old_viewport_position
= self
.view
.viewport_position()
54 buf
= self
.view
.substr(sublime
.Region(0, self
.view
.size()))
57 stdout
=subprocess
.PIPE
,
58 stderr
=subprocess
.PIPE
,
59 stdin
=subprocess
.PIPE
,
61 output
, error
= p
.communicate(buf
.encode(encoding
))
65 edit
, sublime
.Region(0, self
.view
.size()), output
.decode(encoding
)
67 self
.view
.sel().clear()
68 for region
in regions
:
69 self
.view
.sel().add(region
)
70 # FIXME: Without the 10ms delay, the viewport sometimes jumps.
72 lambda: self
.view
.set_viewport_position(old_viewport_position
, False), 10