forked from DisposaBoy/GoSublime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgscommon.py
67 lines (56 loc) · 1.95 KB
/
gscommon.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import sublime
import subprocess
from subprocess import Popen, PIPE
try:
STARTUP_INFO = subprocess.STARTUPINFO()
STARTUP_INFO.dwFlags |= subprocess.STARTF_USESHOWWINDOW
STARTUP_INFO.wShowWindow = subprocess.SW_HIDE
except (AttributeError):
STARTUP_INFO = None
GLOBAL_SNIPPETS = [
(u'\u0282 func: Function ...', 'func ${1:name}($2)$3 {\n\t$0\n}'),
(u'\u0282 func: Function (receiver) ...', 'func (${1:receiver} ${2:type}) ${3:name}($4)$5 {\n\t$0\n}'),
(u'\u0282 var: Variable (...)', 'var (\n\t$1\n)'),
(u'\u0282 const: Constant (...)', 'const (\n\t$1\n)'),
(u'\u0282 import: Import (...)', 'import (\n\t$2"$1"\n)'),
(u'\u0282 package: Package ...', 'package ${1:NAME}')
]
LOCAL_SNIPPETS = [
(u'\u0282 func: Function() ...', 'func($1) {\n\t$0\n}($2)'),
(u'\u0282 var: Variable (...)', 'var ${1:name} ${2:type}'),
]
CLASS_PREFIXES = {
'const': u'\u0196 ',
'func': u'\u0192 ',
'type': u'\u0288 ',
'var': u'\u03BD ',
'package': u'\u03C1 ',
}
NAME_PREFIXES = {
'interface': u'\u00A1 ',
}
def runcmd(args, input=None):
try:
p = Popen(args, stdout=PIPE, stderr=PIPE, stdin=PIPE, startupinfo=STARTUP_INFO)
if isinstance(input, unicode):
input = input.encode('utf-8')
out, err = p.communicate(input=input)
return (out.decode('utf-8') if out else '', err.decode('utf-8') if err else '')
except (OSError, ValueError) as e:
err = u'Error while running %s: %s' % (args[0], e)
return ("", err)
def setting(key, default=None):
s = sublime.load_settings("GoSublime.sublime-settings")
return s.get(key, default)
# stolen from golang's utf8.RuneStart
def is_rune_start(b):
return b&0xC0 != 0x80
def char_to_byte_offset(s, offsetC):
s = s.encode('utf-8')
offsetB = 0
l = len(s)
while offsetC > 0 and offsetB < l:
if is_rune_start(ord(s[offsetB])):
offsetC -= 1
offsetB += 1
return offsetB