diff --git a/method/__init__.py b/method/__init__.py index 52d739f..57eaecd 100644 --- a/method/__init__.py +++ b/method/__init__.py @@ -960,8 +960,9 @@ class SplitWindow(Method): a = w.application a.add_slot() if not w.cursor_is_visible(): - p = w.first - w.goto(p) + #p = w.first + #w.goto(p) + w.center_view(force=True) n = len(a.bufferlist.slots) a.set_error('Window has been split into %d windows!' % n) diff --git a/mode/sql.py b/mode/sql.py index 250433a..c5c2ee5 100644 --- a/mode/sql.py +++ b/mode/sql.py @@ -11,28 +11,30 @@ class HexStringGrammar(Grammar): base_rules = [ PatternRule(r'spaces', r' +'), OverridePatternRule(r'comment', r'--@@:(?P[.a-zA-Z0-9_]+):(?P[.a-zA-Z0-9_]+) *$'), - PatternRule(r'comment', r'--.*\n$'), + PatternRule(r'comment', r'--.*$'), RegionRule(r'comment', '/\*', Grammar, '\*/'), - PatternRule(r'delimiter', r':=|[():;,\.\$\[\]~]'), + PatternRule(r'delimiter', r':=|[():;,\.\[\]~]'), ] end_rules = [ NocasePatternRule(r'sql_bareword', r'[a-z_][a-z0-9_]*'), - PatternRule(r'empty', r'^ *\n$'), PatternRule(r'eol', r'\n'), ] -class PlPgSqlGrammar(Grammar): pass +class PlPgSqlGrammar1(Grammar): pass +class PlPgSqlGrammar2(Grammar): pass sql_type_rule = NocasePatternRule(r'sql_type', r'(?:void|row|serial|varchar|float|integer|int|text|timestamptz|timestamp|datetz|date|timetz|time|boolean|bool)(?![A-Za-z0-9_])') class FunctionGrammar(Grammar): rules = base_rules + [ PatternRule(r'name', r'[a-zA-Z_][a-zA-Z0-9_]*(?=\()'), - NocasePatternRule(r'sql_keyword', r'(?:as|returns|language)'), + NocasePatternRule(r'keyword', r'(?:as|returns|language)'), sql_type_rule, - NocasePatternRule(r'language', r'(?<=language ) *[a-zA-Z_][a-zA-Z0-9_]+'), - RegionRule(r'definition', "'", PlPgSqlGrammar, "'(?!')"), + NocasePatternRule(r'language', r'(?<=language ) *\'[a-zA-Z_][a-zA-Z0-9_]+\''), + NocaseRegionRule(r'body', r"(?<=as )'", PlPgSqlGrammar1, "'(?!')"), + NocaseRegionRule(r'body', r"(?<=as )(?P\$[^$]*\$)", PlPgSqlGrammar2, r"%(delim)s"), + RegionRule(r'string', "'", StringGrammar1, "'"), ] + end_rules function_rules = [ @@ -42,9 +44,10 @@ function_rules = [ sql_rules = [ NocasePatternRule(r'attribute', r'(?:check|exists|unique|not null|default|primary key|minvalue|foreign key|references)(?![A-Za-z0-9_])'), - NocasePatternRule(r'sql_operator', r'(?:case|when|then|else|end|not in|not|and|or|is not|is|in|between|overlaps|like|ilike|similar)(?![A-Za-z0-9_])'), - NocasePatternRule(r'sql_keyword', r'(?:begin|commit|create database|create index|create sequence|create table|create trigger|create view|select|insert|update|delete|drop database|drop index|drop sequence|drop table|drop trigger|drop view|create user|alter user|drop user|drop function|grant|revoke|create function|create or replace function|create or replace view|create language|create operator|create type)(?![A-Za-z0-9_])'), - NocasePatternRule(r'pseudokeyword', r'(?:returns|language|right join|left join|inner join|outer join|join|where|unknown|null|true|false|into|values|as|from|order by|asc|desc|limit|distinct|cascade|using|on)(?![A-Za-z0-9_])'), + NocasePatternRule(r'keyword', r'for update'), + NocasePatternRule(r'sql_keyword', r'(?:while|for|if|loop|end loop|end if|case|when|then|else|not in|not|and|or|is not|is|in|between|overlaps|like|ilike|similar)(?![A-Za-z0-9_])'), + NocasePatternRule(r'keyword', r'(?:alias for|set|return|declare|end|begin|commit|create database|create index|create sequence|create table|create trigger|create view|select into|select|insert into|insert|update|delete|drop database|drop index|drop sequence|drop table|drop trigger|drop view|create user|alter user|drop user|drop function|grant|revoke|create function|create or replace function|create or replace view|create language|create operator|create type)(?![A-Za-z0-9_])'), + NocasePatternRule(r'builtin', r'(?:returns|language|right join|left join|inner join|outer join|join|where|unknown|null|true|false|into|values|as|from|order by|asc|desc|limit|distinct|cascade|using|on)(?![A-Za-z0-9_])'), sql_type_rule, PatternRule(r'sql_builtin', r'(?:nextval|current_timestamp|current_time|current_date)(?![A-Za-z0-9_])'), PatternRule(r'number', r'[0-9]+(?:e[+-]?[0-9]+)?'), @@ -59,39 +62,58 @@ string_rules = [ RegionRule(r'string', "'", StringGrammar1, "'"), ] -PlPgSqlGrammar.rules = base_rules + sql_rules + [ +PlPgSqlGrammar1.rules = [ + PatternRule(r'variable', r'\$[0-9]+'), +] + base_rules + sql_rules + [ RegionRule(r'string', "B''", BitStringGrammar, "''"), RegionRule(r'string', "X''", HexStringGrammar, "''"), RegionRule(r'string', "''", StringGrammar1, "''"), ] + end_rules +PlPgSqlGrammar2.rules = [ + PatternRule(r'variable', r'\$[0-9]+'), +] + base_rules + sql_rules + [ + RegionRule(r'string', "B'", BitStringGrammar, "'"), + RegionRule(r'string', "X'", HexStringGrammar, "'"), + RegionRule(r'string', "'", StringGrammar1, "'"), +] + end_rules + class SqlGrammar(Grammar): rules = base_rules + function_rules + sql_rules + string_rules + end_rules class SqlTabber(tab.StackTabber): - wst = ('null', 'eol',) + wst = ('spaces', 'null', 'eol', 'function.body.spaces', 'function.body.eol') + st = ('spaces', 'null', 'function.body.spaces') def is_base(self, y): - if y == 0: - return True + if y == 0: return True highlighter = self.mode.window.buffer.highlights[self.mode.name] - if not highlighter.tokens[y]: - return False + if not highlighter.tokens[y]: return False t = highlighter.tokens[y][0] return t.name == 'function' def _handle_other_token(self, currlvl, y, i): - w = self.mode.tabwidth - token = self.get_token(y, i) - s = token.string.lower() + w = self.mode.tabwidth + token = self.get_token(y, i) + s = token.string.lower() + fqname = token.fqname() + + unhandled = False if token.name == 'delimiter' and s == ';': self._opt_pop('cont') + elif token.name == 'eol': + pass + elif token.name == 'spaces': + pass + elif token.name == 'comment': + pass + elif fqname in ('function.body.start', 'function.body.end'): + pass + elif fqname in ('function.start', 'function.end'): + pass elif token.name == 'sql_keyword': - if s == 'declare': - self._append('declare', currlvl + w) - elif s == 'begin': - currlvl -= w - elif s == 'end': - self._opt_pop('declare') - currlvl = self.get_curr_level() + if s in ('then', 'loop'): + pass + elif s == 'if': + self._append('if', currlvl + w) elif s == 'end if': self._opt_pop('if') currlvl = self.get_curr_level() @@ -100,16 +122,26 @@ class SqlTabber(tab.StackTabber): currlvl = self.get_curr_level() elif s == 'else': currlvl -= w - elif s == 'if': - self._append('if', currlvl + w) elif s in ('while', 'for'): self._append('loop', currlvl + w) + else: + unhandled = True + elif token.name == 'keyword': + if s == 'declare': + self._append('declare', currlvl + w) + elif s == 'begin': + currlvl -= w + elif s == 'end': + self._opt_pop('declare') + currlvl = self.get_curr_level() + else: + unhandled = True + else: + unhandled = True + + if self.is_rightmost_token(y, i) and unhandled: + self._opt_append('cont', currlvl + w) - if self.is_rightmost_token(y, i): - if not self._empty() and token.name == 'continuation': - self._append('cont', currlvl + w) - elif token.name == 'eol' and not self.markers: - self._opt_pop("cont") return currlvl class Sql(mode.Fundamental): @@ -123,30 +155,28 @@ class Sql(mode.Fundamental): closetokens = ('delimiter',) closetags = {')': '(', ']': '[', '}': '{'} colors = { - 'sql_operator': ('yellow', 'default', 'bold'), - 'attribute': ('magenta', 'default', 'bold'), - 'sql_keyword': ('cyan', 'default', 'bold'), - 'pseudokeyword': ('cyan', 'default', 'bold'), - 'sql_type': ('green', 'default', 'bold'), - 'sql_builtin': ('yellow', 'default', 'bold'), - 'sql_quoted.start': ('yellow', 'default', 'bold'), - 'sql_quoted.data': ('yellow', 'default', 'bold'), - 'sql_quoted.null': ('yellow', 'default', 'bold'), - 'sql_quoted.end': ('yellow', 'default', 'bold'), - 'sql_variable': ('yellow', 'default', 'bold'), - 'sql_bareword': ('default', 'default', 'bold'), - 'function.start': ('cyan', 'default', 'bold'), - 'function.data': ('default', 'default', 'bold'), - 'function.null': ('default', 'default', 'bold'), - 'function.name': ('magenta', 'default', 'bold'), - 'function.language': ('magenta', 'default', 'bold'), - 'function.end': ('default', 'default', 'bold'), + 'sql_keyword': ('magenta', 'default', 'bold'), + 'attribute': ('magenta', 'default', 'bold'), + 'sql_type': ('green', 'default', 'bold'), + 'sql_builtin': ('yellow', 'default', 'bold'), + 'sql_quoted.start': ('yellow', 'default', 'bold'), + 'sql_quoted.data': ('yellow', 'default', 'bold'), + 'sql_quoted.null': ('yellow', 'default', 'bold'), + 'sql_quoted.end': ('yellow', 'default', 'bold'), + 'sql_variable': ('yellow', 'default', 'bold'), + 'sql_bareword': ('default', 'default'), - 'function.definition.start': ('magenta', 'default', 'bold'), - 'function.definition.sql_bareword': ('magenta', 'default', 'bold'), - 'function.definition.data': ('magenta', 'default', 'bold'), - 'function.definition.null': ('magenta', 'default', 'bold'), - 'function.definition.end': ('magenta', 'default', 'bold'), + 'function.start': ('cyan', 'default', 'bold'), + 'function.data': ('default', 'default'), + 'function.null': ('default', 'default'), + 'function.name': ('default', 'default'), + 'function.body.start': ('green', 'default', 'bold'), + 'function.body.sql_bareword': ('default', 'default'), + 'function.body.data': ('default', 'default'), + 'function.body.null': ('default', 'default'), + 'function.body.end': ('green', 'default', 'bold'), + 'function.language': ('green', 'default', 'bold'), + 'function.end': ('default', 'default'), } _bindings = { 'close-paren': (')',),