djasper | 7b440cb | 2013-05-16 15:08:25 +0000 | [diff] [blame] | 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 |
| 5 | # - Add a key binding: |
| 6 | # { "keys": ["ctrl+shift+c"], "command": "clang_format" }, |
| 7 | # |
| 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. |
| 11 | # |
| 12 | # It operates on the current, potentially unsaved buffer and does not create |
| 13 | # or save any files. To revert a formatting, just undo. |
| 14 | |
serge_sans_paille | 0561334 | 2018-12-18 16:07:37 +0000 | [diff] [blame] | 15 | from __future__ import absolute_import, division, print_function |
djasper | 7b440cb | 2013-05-16 15:08:25 +0000 | [diff] [blame] | 16 | import sublime |
| 17 | import sublime_plugin |
| 18 | import subprocess |
| 19 | |
| 20 | # Change this to the full path if clang-format is not on the path. |
| 21 | binary = 'clang-format' |
| 22 | |
chandlerc | e6992fd | 2013-09-02 07:42:02 +0000 | [diff] [blame] | 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 |
hans | 79a882f | 2013-09-10 15:41:12 +0000 | [diff] [blame] | 25 | # a '.clang-format' or '_clang-format' file to indicate the style that should be |
| 26 | # used. |
sammccall | 2b978b6 | 2019-05-17 07:22:55 +0000 | [diff] [blame^] | 27 | style = None |
djasper | 7b440cb | 2013-05-16 15:08:25 +0000 | [diff] [blame] | 28 | |
| 29 | class ClangFormatCommand(sublime_plugin.TextCommand): |
| 30 | def run(self, edit): |
| 31 | encoding = self.view.encoding() |
| 32 | if encoding == 'Undefined': |
| 33 | encoding = 'utf-8' |
| 34 | regions = [] |
sammccall | 2b978b6 | 2019-05-17 07:22:55 +0000 | [diff] [blame^] | 35 | command = [binary] |
| 36 | if style: |
| 37 | command.extend(['-style', style]) |
djasper | 7b440cb | 2013-05-16 15:08:25 +0000 | [diff] [blame] | 38 | for region in self.view.sel(): |
| 39 | regions.append(region) |
| 40 | region_offset = min(region.a, region.b) |
| 41 | region_length = abs(region.b - region.a) |
| 42 | command.extend(['-offset', str(region_offset), |
djasper | 0584828 | 2013-09-13 13:40:24 +0000 | [diff] [blame] | 43 | '-length', str(region_length), |
| 44 | '-assume-filename', str(self.view.file_name())]) |
djasper | 7b440cb | 2013-05-16 15:08:25 +0000 | [diff] [blame] | 45 | old_viewport_position = self.view.viewport_position() |
| 46 | buf = self.view.substr(sublime.Region(0, self.view.size())) |
| 47 | p = subprocess.Popen(command, stdout=subprocess.PIPE, |
| 48 | stderr=subprocess.PIPE, stdin=subprocess.PIPE) |
| 49 | output, error = p.communicate(buf.encode(encoding)) |
djasper | 0584828 | 2013-09-13 13:40:24 +0000 | [diff] [blame] | 50 | if error: |
silvas | 32bd25d | 2013-11-08 22:46:56 +0000 | [diff] [blame] | 51 | print(error) |
djasper | 0584828 | 2013-09-13 13:40:24 +0000 | [diff] [blame] | 52 | self.view.replace( |
| 53 | edit, sublime.Region(0, self.view.size()), |
| 54 | output.decode(encoding)) |
| 55 | self.view.sel().clear() |
| 56 | for region in regions: |
| 57 | self.view.sel().add(region) |
| 58 | # FIXME: Without the 10ms delay, the viewport sometimes jumps. |
| 59 | sublime.set_timeout(lambda: self.view.set_viewport_position( |
| 60 | old_viewport_position, False), 10) |