1*67e74705SXin Li# This file is a minimal clang-format sublime-integration. To install: 2*67e74705SXin Li# - Change 'binary' if clang-format is not on the path (see below). 3*67e74705SXin Li# - Put this file into your sublime Packages directory, e.g. on Linux: 4*67e74705SXin Li# ~/.config/sublime-text-2/Packages/User/clang-format-sublime.py 5*67e74705SXin Li# - Add a key binding: 6*67e74705SXin Li# { "keys": ["ctrl+shift+c"], "command": "clang_format" }, 7*67e74705SXin Li# 8*67e74705SXin Li# With this integration you can press the bound key and clang-format will 9*67e74705SXin Li# format the current lines and selections for all cursor positions. The lines 10*67e74705SXin Li# or regions are extended to the next bigger syntactic entities. 11*67e74705SXin Li# 12*67e74705SXin Li# It operates on the current, potentially unsaved buffer and does not create 13*67e74705SXin Li# or save any files. To revert a formatting, just undo. 14*67e74705SXin Li 15*67e74705SXin Lifrom __future__ import print_function 16*67e74705SXin Liimport sublime 17*67e74705SXin Liimport sublime_plugin 18*67e74705SXin Liimport subprocess 19*67e74705SXin Li 20*67e74705SXin Li# Change this to the full path if clang-format is not on the path. 21*67e74705SXin Libinary = 'clang-format' 22*67e74705SXin Li 23*67e74705SXin Li# Change this to format according to other formatting styles. See the output of 24*67e74705SXin Li# 'clang-format --help' for a list of supported styles. The default looks for 25*67e74705SXin Li# a '.clang-format' or '_clang-format' file to indicate the style that should be 26*67e74705SXin Li# used. 27*67e74705SXin Listyle = 'file' 28*67e74705SXin Li 29*67e74705SXin Liclass ClangFormatCommand(sublime_plugin.TextCommand): 30*67e74705SXin Li def run(self, edit): 31*67e74705SXin Li encoding = self.view.encoding() 32*67e74705SXin Li if encoding == 'Undefined': 33*67e74705SXin Li encoding = 'utf-8' 34*67e74705SXin Li regions = [] 35*67e74705SXin Li command = [binary, '-style', style] 36*67e74705SXin Li for region in self.view.sel(): 37*67e74705SXin Li regions.append(region) 38*67e74705SXin Li region_offset = min(region.a, region.b) 39*67e74705SXin Li region_length = abs(region.b - region.a) 40*67e74705SXin Li command.extend(['-offset', str(region_offset), 41*67e74705SXin Li '-length', str(region_length), 42*67e74705SXin Li '-assume-filename', str(self.view.file_name())]) 43*67e74705SXin Li old_viewport_position = self.view.viewport_position() 44*67e74705SXin Li buf = self.view.substr(sublime.Region(0, self.view.size())) 45*67e74705SXin Li p = subprocess.Popen(command, stdout=subprocess.PIPE, 46*67e74705SXin Li stderr=subprocess.PIPE, stdin=subprocess.PIPE) 47*67e74705SXin Li output, error = p.communicate(buf.encode(encoding)) 48*67e74705SXin Li if error: 49*67e74705SXin Li print(error) 50*67e74705SXin Li self.view.replace( 51*67e74705SXin Li edit, sublime.Region(0, self.view.size()), 52*67e74705SXin Li output.decode(encoding)) 53*67e74705SXin Li self.view.sel().clear() 54*67e74705SXin Li for region in regions: 55*67e74705SXin Li self.view.sel().add(region) 56*67e74705SXin Li # FIXME: Without the 10ms delay, the viewport sometimes jumps. 57*67e74705SXin Li sublime.set_timeout(lambda: self.view.set_viewport_position( 58*67e74705SXin Li old_viewport_position, False), 10) 59