Created
May 7, 2013 15:22
-
-
Save jaguilar/5533466 to your computer and use it in GitHub Desktop.
Clang format on save in sublime text 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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