Skip to content

Commit 63df569

Browse files
committed
Ran PEP8 and use os.path.splitext to detect language
Ran PEP8 to make everything look pretty. Changed from explicitly replacing the .tmLanguage extension to using os.path.splitext to get the filename without the extension so that it works with the new sublime-syntax files. Replaced `from xml.dom.minidom import *` with `from xml.dom.minidom import parseString`, since that's the only function used from that module.
1 parent 45fef95 commit 63df569

File tree

1 file changed

+31
-26
lines changed

1 file changed

+31
-26
lines changed

indentxml.py

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,32 @@
22
import sublime_plugin
33
import re
44
import json
5-
from xml.dom.minidom import *
6-
from os.path import basename
5+
from xml.dom.minidom import parseString
6+
from os.path import basename, splitext
77

88

99
class BaseIndentCommand(sublime_plugin.TextCommand):
10+
1011
def __init__(self, view):
1112
self.view = view
1213
self.language = self.get_language()
1314

14-
def get_language(self):
15+
def get_language(self):
1516
syntax = self.view.settings().get('syntax')
16-
language = basename(syntax).replace('.tmLanguage', '').lower() if syntax != None else "plain text"
17+
language = splitext(basename(syntax))[0].lower() if syntax is not None else "plain text"
1718
return language
1819

1920
def check_enabled(self, lang):
2021
return True
2122

2223
def is_enabled(self):
2324
"""
24-
Enables or disables the 'indent' command.
25-
Command will be disabled if there are currently no text selections and current file is not 'XML' or 'Plain Text'.
26-
This helps clarify to the user about when the command can be executed, especially useful for UI controls.
25+
Enables or disables the 'indent' command. Command will be disabled if
26+
there are currently no text selections and current file is not 'XML' or
27+
'Plain Text'. This helps clarify to the user about when the command can
28+
be executed, especially useful for UI controls.
2729
"""
28-
if self.view == None:
30+
if self.view is None:
2931
return False
3032

3133
return self.check_enabled(self.get_language())
@@ -38,21 +40,22 @@ def run(self, edit):
3840
regions = view.sel()
3941
# if there are more than 1 region or region one and it's not empty
4042
if len(regions) > 1 or not regions[0].empty():
41-
for region in view.sel():
42-
if not region.empty():
43-
s = view.substr(region).strip()
44-
s = self.indent(s)
45-
view.replace(edit, region, s)
46-
else: #format all text
47-
alltextreg = sublime.Region(0, view.size())
48-
s = view.substr(alltextreg).strip()
49-
s = self.indent(s)
50-
view.replace(edit, alltextreg, s)
43+
for region in view.sel():
44+
if not region.empty():
45+
s = view.substr(region).strip()
46+
s = self.indent(s)
47+
view.replace(edit, region, s)
48+
else: # format all text
49+
alltextreg = sublime.Region(0, view.size())
50+
s = view.substr(alltextreg).strip()
51+
s = self.indent(s)
52+
view.replace(edit, alltextreg, s)
5153

5254

5355
class AutoIndentCommand(BaseIndentCommand):
56+
5457
def get_text_type(self, s):
55-
language = self.language
58+
language = self.language
5659
if language == 'xml':
5760
return 'xml'
5861
if language == 'json':
@@ -73,22 +76,23 @@ def indent(self, s):
7376
command = IndentJsonCommand(self.view)
7477
if text_type == 'notsupported':
7578
return s
76-
79+
7780
return command.indent(s)
7881

7982
def check_enabled(self, lang):
8083
return True
8184

8285

8386
class IndentXmlCommand(BaseIndentCommand):
84-
def indent(self, s):
87+
88+
def indent(self, s):
8589
# convert to utf
86-
s = s.encode("utf-8")
90+
s = s.encode("utf-8")
8791
xmlheader = re.compile(b"<\?.*\?>").match(s)
8892
# convert to plain string without indents and spaces
8993
s = re.compile(b'>\s+([^\s])', re.DOTALL).sub(b'>\g<1>', s)
9094
# replace tags to convince minidom process cdata as text
91-
s = s.replace(b'<![CDATA[', b'%CDATAESTART%').replace(b']]>', b'%CDATAEEND%')
95+
s = s.replace(b'<![CDATA[', b'%CDATAESTART%').replace(b']]>', b'%CDATAEEND%')
9296
try:
9397
s = parseString(s).toprettyxml()
9498
except Exception as e:
@@ -100,18 +104,19 @@ def indent(self, s):
100104
s = s.replace('%CDATAESTART%', '<![CDATA[').replace('%CDATAEEND%', ']]>')
101105
# remove xml header
102106
s = s.replace("<?xml version=\"1.0\" ?>", "").strip()
103-
if xmlheader:
104-
s = xmlheader.group().decode("utf-8") + "\n" + s
107+
if xmlheader:
108+
s = xmlheader.group().decode("utf-8") + "\n" + s
105109
return s
106110

107111
def check_enabled(self, language):
108112
return ((language == "xml") or (language == "plain text"))
109113

110114

111115
class IndentJsonCommand(BaseIndentCommand):
116+
112117
def check_enabled(self, language):
113118
return ((language == "json") or (language == "plain text"))
114119

115120
def indent(self, s):
116121
parsed = json.loads(s)
117-
return json.dumps(parsed, sort_keys=True, indent=4, separators=(',', ': '))
122+
return json.dumps(parsed, sort_keys=True, indent=4, separators=(',', ': '))

0 commit comments

Comments
 (0)