blob: 2099d7a98c6b4a7838b7c6bd5ecdd96321435ecd [file] [log] [blame]
djasper7b440cb2013-05-16 15:08:25 +00001# 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
15import sublime
16import sublime_plugin
17import subprocess
18
19# Change this to the full path if clang-format is not on the path.
20binary = 'clang-format'
21
chandlerce6992fd2013-09-02 07:42:02 +000022# Change this to format according to other formatting styles. See the output of
23# 'clang-format --help' for a list of supported styles. The default looks for
hans79a882f2013-09-10 15:41:12 +000024# a '.clang-format' or '_clang-format' file to indicate the style that should be
25# used.
chandlerce6992fd2013-09-02 07:42:02 +000026style = 'file'
djasper7b440cb2013-05-16 15:08:25 +000027
28class ClangFormatCommand(sublime_plugin.TextCommand):
29 def run(self, edit):
30 encoding = self.view.encoding()
31 if encoding == 'Undefined':
32 encoding = 'utf-8'
33 regions = []
34 command = [binary, '-style', style]
35 for region in self.view.sel():
36 regions.append(region)
37 region_offset = min(region.a, region.b)
38 region_length = abs(region.b - region.a)
39 command.extend(['-offset', str(region_offset),
djasper05848282013-09-13 13:40:24 +000040 '-length', str(region_length),
41 '-assume-filename', str(self.view.file_name())])
djasper7b440cb2013-05-16 15:08:25 +000042 old_viewport_position = self.view.viewport_position()
43 buf = self.view.substr(sublime.Region(0, self.view.size()))
44 p = subprocess.Popen(command, stdout=subprocess.PIPE,
45 stderr=subprocess.PIPE, stdin=subprocess.PIPE)
46 output, error = p.communicate(buf.encode(encoding))
djasper05848282013-09-13 13:40:24 +000047 if error:
djasper7b440cb2013-05-16 15:08:25 +000048 print error
djasper05848282013-09-13 13:40:24 +000049 self.view.replace(
50 edit, sublime.Region(0, self.view.size()),
51 output.decode(encoding))
52 self.view.sel().clear()
53 for region in regions:
54 self.view.sel().add(region)
55 # FIXME: Without the 10ms delay, the viewport sometimes jumps.
56 sublime.set_timeout(lambda: self.view.set_viewport_position(
57 old_viewport_position, False), 10)