forked from denglf/FormatLua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormatLua.py
51 lines (44 loc) · 1.79 KB
/
FormatLua.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
import os
import subprocess
import sublime
import sublime_plugin
import re
import zipfile
from os.path import basename
from os.path import join
version = int(sublime.version())
package = "FormatLua"
def plugin_loaded():
if version >= 3006:
package_location = os.path.join(sublime.installed_packages_path(), package + ".sublime-package")
extract_location = os.path.join(sublime.packages_path(), package)
if os.path.exists(package_location):
if not os.path.exists(extract_location):
with zipfile.ZipFile(package_location) as zip_file:
zip_file.extractall(extract_location)
class FormatLuaCommand(sublime_plugin.TextCommand):
def run(self, edit):
view = self.view
regions = view.sel()
# if there are more than 1 region or region one and it"s not empty
if len(regions) > 1 or not regions[0].empty():
for region in view.sel():
if not region.empty():
s = view.substr(region)
s = self._run(s)
view.replace(edit, region, s)
else: #format all text
alltextreg = sublime.Region(0, view.size())
s = view.substr(alltextreg)
s = self._run(s)
view.replace(edit, alltextreg, s)
def _run(self, s):
s = s.encode("utf-8")
packages_path = join(sublime.packages_path(), package)
os.chdir(packages_path)
settings = sublime.load_settings(package + ".sublime-settings")
perl_path = settings.get("perl_path")
cmd = perl_path + " reindent.pl"
p = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
sourcecode = p.communicate(s)[0]
return sourcecode.decode("utf-8")