xref: /llvm-project/clang/tools/clang-format/clang-format-sublime.py (revision 625841c3be4dbaab089c01217726a2906f3a8103)
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
15from __future__ import absolute_import, division, print_function
16import 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
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
25# a '.clang-format' or '_clang-format' file to indicate the style that should be
26# used.
27style = None
28
29
30class ClangFormatCommand(sublime_plugin.TextCommand):
31    def run(self, edit):
32        encoding = self.view.encoding()
33        if encoding == "Undefined":
34            encoding = "utf-8"
35        regions = []
36        command = [binary]
37        if style:
38            command.extend(["--style", style])
39        for region in self.view.sel():
40            regions.append(region)
41            region_offset = min(region.a, region.b)
42            region_length = abs(region.b - region.a)
43            command.extend(
44                [
45                    "--offset",
46                    str(region_offset),
47                    "--length",
48                    str(region_length),
49                    "--assume-filename",
50                    str(self.view.file_name()),
51                ]
52            )
53        old_viewport_position = self.view.viewport_position()
54        buf = self.view.substr(sublime.Region(0, self.view.size()))
55        p = subprocess.Popen(
56            command,
57            stdout=subprocess.PIPE,
58            stderr=subprocess.PIPE,
59            stdin=subprocess.PIPE,
60        )
61        output, error = p.communicate(buf.encode(encoding))
62        if error:
63            print(error)
64        self.view.replace(
65            edit, sublime.Region(0, self.view.size()), output.decode(encoding)
66        )
67        self.view.sel().clear()
68        for region in regions:
69            self.view.sel().add(region)
70        # FIXME: Without the 10ms delay, the viewport sometimes jumps.
71        sublime.set_timeout(
72            lambda: self.view.set_viewport_position(old_viewport_position, False), 10
73        )
74