-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpretty_command.py
50 lines (44 loc) · 1.86 KB
/
pretty_command.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import subprocess
import sublime
import sublime_plugin
from .proto_formatter import ProtoSettings, ProtoFormatter
class PrettyProtobufCommand(sublime_plugin.TextCommand):
def run(self, edit):
region = sublime.Region(0, self.view.size())
lines = self.view.substr(region)
# Avoid flashing an ugly cmd prompt on Windows when invoking clang-format
startupinfo = None
if sys.platform.startswith('win32'):
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
startupinfo.wShowWindow = subprocess.SW_HIDE
command = [ProtoSettings().clang_format_path]
if self.view.file_name():
command += ['-assume-filename', self.view.file_name()]
proc = subprocess.Popen(command, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, stdin=subprocess.PIPE, startupinfo=startupinfo)
stdout, stderr = proc.communicate(input=lines.encode())
if stderr:
print(f'{stderr = }')
if not stdout:
print('No output from clang-format, maybe crashed...\n'
'Please report to https://github.com/hanfezh/pretty-protobuf/issues')
return
# Replace with formatted content
self.view.replace(edit, region, stdout.decode())
class PrettyDebugStringCommand(sublime_plugin.TextCommand):
def run(self, edit):
if len(self.view.sel()) < 1:
return
first_reg = self.view.sel()[0]
if first_reg.empty() and ProtoSettings().use_entire_file:
first_reg = sublime.Region(0, self.view.size())
lines = self.view.substr(first_reg)
if not lines:
return
lines = ProtoFormatter(lines).format()
if lines:
self.view.replace(edit, first_reg, lines)