javascript tabbing supportl

--HG--
branch : pmacs2
This commit is contained in:
moculus 2007-07-12 22:14:39 +00:00
parent 91ffeb9a4b
commit f4257a0de7
2 changed files with 30 additions and 6 deletions

View File

@ -1,4 +1,4 @@
import color, mode2 import color, mode2, tab2
from lex2 import Grammar, PatternRule, RegionRule from lex2 import Grammar, PatternRule, RegionRule
from point2 import Point from point2 import Point
from mode_python import StringGrammar from mode_python import StringGrammar
@ -6,7 +6,7 @@ from mode_python import StringGrammar
class JavascriptGrammar(Grammar): class JavascriptGrammar(Grammar):
rules = [ rules = [
PatternRule(name=r'comment', pattern=r'//.*$'), PatternRule(name=r'comment', pattern=r'//.*$'),
RegionRule(name=r'comment', start='/\*', grammar=Grammar(), end='\*/'), RegionRule(name=r'comment', start='/\*', grammar=Grammar, end='\*/'),
PatternRule(name=r'continuation', pattern=r'\\(?= *$)'), PatternRule(name=r'continuation', pattern=r'\\(?= *$)'),
PatternRule(name=r'function', pattern=r"(?<=function )[a-zA-Z_][a-zA-Z0-9_]*"), PatternRule(name=r'function', pattern=r"(?<=function )[a-zA-Z_][a-zA-Z0-9_]*"),
PatternRule(name=r'class', pattern=r"(?<=class )[a-zA-Z_][a-zA-Z0-9_]*"), PatternRule(name=r'class', pattern=r"(?<=class )[a-zA-Z_][a-zA-Z0-9_]*"),
@ -29,13 +29,36 @@ class JavascriptGrammar(Grammar):
PatternRule(name=r'delimiter', pattern=r'%=|&&=|&=|\(|\)|\*=|\+=|,|-=|\.{3}|\.|/=(?= |$)|::|:|;|<<=|>>=|>>>=|\?|\[|\]|^=|^^=|\{|\}|\|=|\|\|='), PatternRule(name=r'delimiter', pattern=r'%=|&&=|&=|\(|\)|\*=|\+=|,|-=|\.{3}|\.|/=(?= |$)|::|:|;|<<=|>>=|>>>=|\?|\[|\]|^=|^^=|\{|\}|\|=|\|\|='),
PatternRule(name=r'operator', pattern=r'!==|!=|!|%|&&|&|\*|\+\+|\+|--|-|/(?= |$)|<<=|<<|<=|<|===|==|=|>>>=|>>>|>>=|>>|>=|>|\\'), PatternRule(name=r'operator', pattern=r'!==|!=|!|%|&&|&|\*|\+\+|\+|--|-|/(?= |$)|<<=|<<|<=|<|===|==|=|>>>=|>>>|>>=|>>|>=|>|\\'),
RegionRule(name='regex', start="/", grammar=StringGrammar(), end="/"), RegionRule(name='regex', start="/", grammar=StringGrammar, end="/"),
RegionRule(name='string', start="'", grammar=StringGrammar(), end="'"), RegionRule(name='string', start="'", grammar=StringGrammar, end="'"),
RegionRule(name='string', start='"', grammar=StringGrammar(), end='"'), RegionRule(name='string', start='"', grammar=StringGrammar, end='"'),
] ]
class JavascriptTabber(tab2.StackTabber):
def is_base(self, y):
if y == 0:
return True
highlighter = self.mode.window.buffer.highlights[self.mode.name()]
if not highlighter.tokens[y]:
return False
t = highlighter.tokens[y][0]
return t.name == 'reserved1' and t.string == 'function'
def _handle_other_token(self, currlvl, y, i):
token = self.get_token(y, i)
fqname = token.fqname()
if token.name == 'operator' and token.string == '=':
self._opt_append("cont", currlvl + 4)
elif token.name == 'delimiter' and token.string == ";":
self._opt_pop("cont")
return currlvl
class Javascript(mode2.Fundamental): class Javascript(mode2.Fundamental):
grammar = JavascriptGrammar() grammar = JavascriptGrammar
tabbercls = JavascriptTabber
opentoken = 'delimiter'
opentags = {'(': ')', '[': ']', '{': '}'}
closetoken = 'delimiter'
closetags = {')': '(', ']': '[', '}': '{'}
def __init__(self, w): def __init__(self, w):
mode2.Fundamental.__init__(self, w) mode2.Fundamental.__init__(self, w)
self.add_bindings('close-paren', (')',)) self.add_bindings('close-paren', (')',))

View File

@ -136,3 +136,4 @@ def _end(w):
w.application.last_search = w.buffer.make_string() w.application.last_search = w.buffer.make_string()
w.buffer.method.old_cursor = None w.buffer.method.old_cursor = None
w.buffer.method.old_window = None w.buffer.method.old_window = None
w.buffer.method.is_literal = None