Skip to content

Instantly share code, notes, and snippets.

@jaguilar
Created May 7, 2013 15:22
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaguilar/5533466 to your computer and use it in GitHub Desktop.
Save jaguilar/5533466 to your computer and use it in GitHub Desktop.
Clang format on save in sublime text 3
import sublime, sublime_plugin
import subprocess
class ClangFormatCommand(sublime_plugin.TextCommand):
def run(self, edit):
r = sublime.Region(0, self.view.size())
try:
p = subprocess.Popen(
['clang-format', '--style', 'Google'],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
stdoutdata, stderrdata = p.communicate(self.view.substr(r).encode('utf-8'))
p.stdin.close()
self.view.replace(edit, r, stdoutdata.decode('utf-8'))
except subprocess.CalledProcessError as e:
print(str(e))
def description(self):
return "Format: Clang"
class ClangFormatOnSave(sublime_plugin.EventListener):
def on_pre_save(self, view):
if not view.settings().get('clang_format_on_save', False):
return
fname = view.file_name()
if fname is None:
return
suffix = fname[fname.rfind('.'):]
if suffix in ('.cc', '.h'):
view.run_command('clang_format')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment