improve javascript indenting; improve fixed_indent mode a bit

--HG--
branch : pmacs2
This commit is contained in:
Erik Osheim 2011-12-02 01:15:38 -05:00
parent 1ea5d18fa5
commit 57ae9a21e0
2 changed files with 17 additions and 0 deletions

View File

@ -67,6 +67,7 @@ class JavascriptGrammar(Grammar):
] ]
class JavascriptTabber2(tab.StackTabber2): class JavascriptTabber2(tab.StackTabber2):
fixed_indent = True
open_tokens = {'delimiter': {'{': '}', '(': ')', '[': ']'}} open_tokens = {'delimiter': {'{': '}', '(': ')', '[': ']'}}
close_tokens = {'delimiter': {'}': '{', ')': '(', ']': '['}} close_tokens = {'delimiter': {'}': '{', ')': '(', ']': '['}}
control_tokens = {'js.keyword': {'if': 1, 'else': 1, 'while': 1, control_tokens = {'js.keyword': {'if': 1, 'else': 1, 'while': 1,
@ -118,6 +119,7 @@ class JavascriptTagManager(TagManager):
class Javascript(Fundamental): class Javascript(Fundamental):
name = 'Javascript' name = 'Javascript'
extensions = ['.js', '.json'] extensions = ['.js', '.json']
tabwidth = 2
grammar = JavascriptGrammar grammar = JavascriptGrammar
tabbercls = JavascriptTabber2 tabbercls = JavascriptTabber2
tagcls = JavascriptTagManager tagcls = JavascriptTagManager

15
tab.py
View File

@ -384,8 +384,23 @@ class StackTabber2(Tabber):
raise Exception, msg % (t.string, y, s) raise Exception, msg % (t.string, y, s)
else: else:
raise Exception, "what? %r (%r)" % (marker, t) raise Exception, "what? %r (%r)" % (marker, t)
# if we start a line with a closing token (e.g. "}") we may want to
# shift the indentation left to "close" the indent. if we're using
# fixed indentation then we may want to do this shift multiple times
# (e.g. "})").
if i == 0: if i == 0:
self._save_curr_level() self._save_curr_level()
elif self.fixed_indent:
all_closed = True
for j in xrange(0, i):
if self._is_ignored(tokens[j]) or self._is_close_token(tokens[j]):
pass
else:
all_closed = False
break
if all_closed:
self._save_curr_level()
# if we need to end at eof and we're at the EOF and we have a control # if we need to end at eof and we're at the EOF and we have a control
# token, then we need to pop it. # token, then we need to pop it.