clang-format-sublime.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. import sublime
  15. import sublime_plugin
  16. import subprocess
  17. # Change this to the full path if clang-format is not on the path.
  18. binary = 'clang-format'
  19. # Change this to format according to other formatting styles. See the output of
  20. # 'clang-format --help' for a list of supported styles. The default looks for
  21. # a '.clang-format' or '_clang-format' file to indicate the style that should be
  22. # used.
  23. style = 'file'
  24. class ClangFormatCommand(sublime_plugin.TextCommand):
  25. def run(self, edit):
  26. encoding = self.view.encoding()
  27. if encoding == 'Undefined':
  28. encoding = 'utf-8'
  29. regions = []
  30. command = [binary, '-style', style]
  31. for region in self.view.sel():
  32. regions.append(region)
  33. region_offset = min(region.a, region.b)
  34. region_length = abs(region.b - region.a)
  35. command.extend(['-offset', str(region_offset),
  36. '-length', str(region_length),
  37. '-assume-filename', str(self.view.file_name())])
  38. old_viewport_position = self.view.viewport_position()
  39. buf = self.view.substr(sublime.Region(0, self.view.size()))
  40. p = subprocess.Popen(command, stdout=subprocess.PIPE,
  41. stderr=subprocess.PIPE, stdin=subprocess.PIPE)
  42. output, error = p.communicate(buf.encode(encoding))
  43. if error:
  44. print error
  45. self.view.replace(
  46. edit, sublime.Region(0, self.view.size()),
  47. output.decode(encoding))
  48. self.view.sel().clear()
  49. for region in regions:
  50. self.view.sel().add(region)
  51. # FIXME: Without the 10ms delay, the viewport sometimes jumps.
  52. sublime.set_timeout(lambda: self.view.set_viewport_position(
  53. old_viewport_position, False), 10)