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