blob: 16ff56e502c66f71c1cbeb06c49cdbd50a8fc6df [file] [log] [blame]
Daniel Jasper299cb282013-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
Sean Silvafbb091a2013-11-08 22:46:56 +000015from __future__ import print_function
Daniel Jasper299cb282013-05-16 15:08:25 +000016import sublime
17import sublime_plugin
18import subprocess
19
20# Change this to the full path if clang-format is not on the path.
21binary = 'clang-format'
22
Chandler Carruth439fc852013-09-02 07:42:02 +000023# 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 Wennborg9a7a50e2013-09-10 15:41:12 +000025# a '.clang-format' or '_clang-format' file to indicate the style that should be
26# used.
Chandler Carruth439fc852013-09-02 07:42:02 +000027style = 'file'
Daniel Jasper299cb282013-05-16 15:08:25 +000028
29class ClangFormatCommand(sublime_plugin.TextCommand):
30 def run(self, edit):
31 encoding = self.view.encoding()
32 if encoding == 'Undefined':
33 encoding = 'utf-8'
34 regions = []
35 command = [binary, '-style', style]
36 for region in self.view.sel():
37 regions.append(region)
38 region_offset = min(region.a, region.b)
39 region_length = abs(region.b - region.a)
40 command.extend(['-offset', str(region_offset),
Daniel Jasper62df7ef2013-09-13 13:40:24 +000041 '-length', str(region_length),
42 '-assume-filename', str(self.view.file_name())])
Daniel Jasper299cb282013-05-16 15:08:25 +000043 old_viewport_position = self.view.viewport_position()
44 buf = self.view.substr(sublime.Region(0, self.view.size()))
45 p = subprocess.Popen(command, stdout=subprocess.PIPE,
46 stderr=subprocess.PIPE, stdin=subprocess.PIPE)
47 output, error = p.communicate(buf.encode(encoding))
Daniel Jasper62df7ef2013-09-13 13:40:24 +000048 if error:
Sean Silvafbb091a2013-11-08 22:46:56 +000049 print(error)
Daniel Jasper62df7ef2013-09-13 13:40:24 +000050 self.view.replace(
51 edit, sublime.Region(0, self.view.size()),
52 output.decode(encoding))
53 self.view.sel().clear()
54 for region in regions:
55 self.view.sel().add(region)
56 # FIXME: Without the 10ms delay, the viewport sometimes jumps.
57 sublime.set_timeout(lambda: self.view.set_viewport_position(
58 old_viewport_position, False), 10)