Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / tools / clang-format / clang-format-sublime.py
blobdcd72e68e94faa10c5fc8e40ddaf501cf43bf661
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" },
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.
12 # It operates on the current, potentially unsaved buffer and does not create
13 # or save any files. To revert a formatting, just undo.
15 from __future__ import absolute_import, division, print_function
16 import sublime
17 import sublime_plugin
18 import subprocess
20 # Change this to the full path if clang-format is not on the path.
21 binary = "clang-format"
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.
27 style = None
30 class 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(
45 "-offset",
46 str(region_offset),
47 "-length",
48 str(region_length),
49 "-assume-filename",
50 str(self.view.file_name()),
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,
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)
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