Skip to content

Commit

Permalink
v.2.6.0, in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikos M committed Oct 27, 2015
1 parent 8f738e4 commit 70db730
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 143 deletions.
1 change: 1 addition & 0 deletions beeld.config
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ tasks =[{}]
../editor-grammar/src/helpers.js
../editor-grammar/src/token.js
../editor-grammar/src/parser.js
../editor-grammar/src/folder.js

./src/main.js

Expand Down
178 changes: 42 additions & 136 deletions build/codemirror_grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -2866,7 +2866,7 @@ function Stream( line, start, pos )
var c, token = '', n;
if ( true === num )
{
re_token = re_token || Stream.$RE_NONSPC$;
re_token = re_token || Stream.$NONSPC$;
while ( self.pos<self.length && re_token.test(c=self[CHAR](self.pos++)) ) token += c;
return token.length ? token : null;
}
Expand All @@ -2893,16 +2893,39 @@ function Stream( line, start, pos )
// eat "space"
self.spc = function( eat, re_space ) {
var m;
if ( m = self.slice(self.pos).match( re_space||Stream.$RE_SPC$ ) )
if ( m = self.slice(self.pos).match( re_space||Stream.$SPC$ ) )
{
if ( false !== eat ) self.mov( m[0].length );
return m[0];
}
};
return self;
}
Stream.$RE_SPC$ = /^[\s\u00a0]+/;
Stream.$RE_NONSPC$ = /[^\s\u00a0]/;
Stream.$SPC$ = /^[\s\u00a0]+/;
Stream.$NONSPC$ = /[^\s\u00a0]/;
Stream.$NOTEMPTY$ = /\S/;
Stream.$SPACE$ = /^\s*/;

// Counts the column offset in a string, taking tabs into account.
// Used mostly to find indentation.
// adapted from codemirror countColumn
function count_column( string, end, tabSize, startIndex, startValue )
{
var i, n, nextTab;
if ( null == end )
{
end = string.search( Stream.$NONSPC$ );
if ( -1 == end ) end = string.length;
}
for (i=startIndex||0,n=startValue||0 ;;)
{
nextTab = string.indexOf( "\t", i );
if ( nextTab < 0 || nextTab >= end ) return n + (end - i);
n += nextTab - i;
n += tabSize - (n % tabSize);
i = nextTab + 1;
}
}


// parser factories
Expand Down Expand Up @@ -3130,36 +3153,16 @@ var Parser = Class({
,fold: function( ) { }
});


function Type( TYPE, positive )
{
if ( T_STR_OR_ARRAY & get_type( TYPE ) )
TYPE = new RegExp( map( make_array( TYPE ).sort( by_length ), esc_re ).join( '|' ) );
TYPE = new_re( map( make_array( TYPE ).sort( by_length ), esc_re ).join( '|' ) );
return false === positive
? function( type ) { return !TYPE.test( type ); }
: function( type ) { return TYPE.test( type ); };
}

// Counts the column offset in a string, taking tabs into account.
// Used mostly to find indentation.
// adapted from codemirror countColumn
function count_column( string, end, tabSize, startIndex, startValue )
{
var i, n, nextTab;
if ( null == end )
{
end = string.search(/[^\s\u00a0]/);
if ( -1 == end ) end = string.length;
}
for (i=startIndex||0,n=startValue||0 ;;)
{
nextTab = string.indexOf("\t", i);
if ( nextTab < 0 || nextTab >= end ) return n + (end - i);
n += nextTab - i;
n += tabSize - (n % tabSize);
i = nextTab + 1;
}
}

function next_tag( iter, T, M, L, R, S )
{
for (;;)
Expand All @@ -3184,33 +3187,7 @@ function next_tag( iter, T, M, L, R, S )
return found;
}
}
/*
function prev_tag( iter, T, M, L, R, S )
{
for (;;)
{
var gt = iter.col ? iter.text.lastIndexOf( R, iter.col-1 ) : -1;
if ( -1 == gt )
{
if ( iter.prev( ) )
{
iter.text = iter.line( iter.row );
continue;
}
else return;
}
if ( !tag_at(iter, gt+1, T) )
{
iter.col = gt;
continue;
}
var lastSlash = iter.text.lastIndexOf( S, gt );
var selfClose = lastSlash > -1 && !/\S/.test(iter.text.slice(lastSlash + 1, gt));
iter.col = gt + 1;
return selfClose ? "autoclosed" : "regular";
}
}
*/

function tag_end( iter, T, M, L, R, S )
{
var gt, lastSlash, selfClose;
Expand All @@ -3232,39 +3209,12 @@ function tag_end( iter, T, M, L, R, S )
continue;
}
lastSlash = iter.text.lastIndexOf( S, gt );
selfClose = lastSlash > -1 && !/\S/.test(iter.text.slice(lastSlash + 1, gt));
selfClose = lastSlash > -1 && !Stream.$NOTEMPTY$.test(iter.text.slice(lastSlash + 1, gt));
iter.col = gt + 1;
return selfClose ? "autoclosed" : "regular";
}
}

function tag_start( iter, T, M, L, R, S )
{
var lt, match;
for (;;)
{
lt = iter.col ? iter.text.lastIndexOf( L, iter.col-1 ) : -1;
if ( -1 == lt )
{
if ( iter.prev( ) )
{
iter.text = iter.line( iter.row );
continue;
}
else return;
}
if ( !tag_at(iter, lt + 1, T) )
{
iter.col = lt;
continue;
}
M.lastIndex = lt;
iter.col = lt;
match = M.exec( iter.text );
if ( match && match.index == lt ) return match;
}
}

function tag_at( iter, ch, T )
{
var type = iter.token(iter.row, ch);
Expand Down Expand Up @@ -3305,72 +3255,28 @@ function find_matching_close( iter, tag, T, M, L, R, S )
}
}
}
/*
function find_matching_open( iter, tag, T, M, L, R, S )
{
var stack = [], prev, endLine, endCh, start, i;
for (;;)
{
prev = prev_tag(iter, T, M, L, R, S);
if ( !prev ) return;
if ( prev == "autoclosed" )
{
tag_start(iter, T, M, L, R, S);
continue;
}
endLine = iter.row, endCh = iter.col;
start = tag_start(iter, T, M, L, R, S);
if ( !start ) return;
if ( start[1] )
{
// closing tag
stack.push( start[2] );
}
else
{
// opening tag
for (i = stack.length-1; i>=0; --i)
{
if ( stack[i] == start[2] )
{
stack.length = i;
break;
}
}
if ( i < 0 && (!tag || tag == start[2]) )
return {
tag: start[2],
pos: [iter.row, iter.col, endLine, endCh]
};
}
}
}
*/

// folder factories
var Folder = {
// adapted from codemirror

_: {
$notempty$: /\S/,
$spc$: /^\s*/,
$block$: /comment/,
$comment$: /comment/
}

,Indented: function( NOTEMPTY ) {
NOTEMPTY = NOTEMPTY || Folder._.$notempty$;
NOTEMPTY = NOTEMPTY || Stream.$NOTEMPTY$;

return function( iter ) {
var first_line, first_indentation, cur_line, cur_indentation,
start_pos, end_pos, last_line_in_fold, i, end,
row = iter.row, col = iter.col;
start_line = iter.row, start_pos, last_line_in_fold, end_pos, i, end;

first_line = iter.line( );
first_line = iter.line( start_line );
if ( !NOTEMPTY.test( first_line ) ) return;
first_indentation = iter.indentation( first_line );
last_line_in_fold = null; start_pos = first_line.length;
for (i=row+1,end=iter.last( ); i<=end; ++i)
for (i=start_line+1,end=iter.last( ); i<=end; ++i)
{
cur_line = iter.line( i ); cur_indentation = iter.indentation( cur_line );
if ( cur_indentation > first_indentation )
Expand All @@ -3391,7 +3297,7 @@ var Folder = {
}
}
// return a range
if ( last_line_in_fold ) return [row, start_pos, last_line_in_fold, end_pos];
if ( last_line_in_fold ) return [start_line, start_pos, last_line_in_fold, end_pos];
};
}

Expand Down Expand Up @@ -3450,10 +3356,10 @@ var Folder = {
return function( ){ };
}

,Markup: function( T, L, R, S, M ) {
,MarkedUp: function( T, L, R, S, M ) {
T = T || Type(/\btag\b/);
L = L || "<"; R = R || ">"; S = S || "/";
M = M || new RegExp(L+"("+S+"?)([a-zA-Z_][a-zA-Z0-9_\\-:]*)","g");
M = M || new_re( L + "(" + S + "?)([a-zA-Z_\\-][a-zA-Z0-9_\\-:]*)", "g" );

return function( iter ) {
iter.col = 0; iter.min = iter.first( ); iter.max = iter.last( );
Expand Down Expand Up @@ -3540,7 +3446,7 @@ var CodeMirrorParser = Class(Parser, {
}
else if ( 'markup' === FOLD || 'html' === FOLD || 'xml' === FOLD )
{
self.$folders.push( CodeMirrorParser.Fold.Markup( ) );
self.$folders.push( CodeMirrorParser.Fold.MarkedUp( ) );
}
}

Expand Down Expand Up @@ -3713,7 +3619,7 @@ function get_mode( grammar, DEFAULT, CodeMirror )
}

,indent: function( state, textAfter, fullLine ) {
return cm_mode.$parser.indent( $CodeMirror$, state, textAfter, fullLine, conf, parserConf, CodeMirror );
return cm_mode.$parser.indent( state, textAfter, fullLine, conf, parserConf, CodeMirror );
}

// support comments toggle functionality
Expand Down Expand Up @@ -3751,7 +3657,7 @@ function get_mode( grammar, DEFAULT, CodeMirror )
? cm_mode.$parser.validate( code, options, CodeMirror )
: [];
};
cm_mode.linter = cm_mode.validator;
cm_mode.linter = cm_mode.validator; // alias
// custom, user-defined, autocompletions generated from grammar
cm_mode.supportAutoCompletion = true;
cm_mode.autocompleter = function( cm, options ) {
Expand All @@ -3762,6 +3668,7 @@ function get_mode( grammar, DEFAULT, CodeMirror )
return cm_mode.$parser.autocomplete( cm, options, CodeMirror );
}
};
cm_mode.autocomplete = cm_mode.autocompleter; // deprecated, for compatibility
cm_mode.autocomplete_renderer = function( elt, data, cmpl ) {
var word = cmpl.text, type = cmpl.meta, p1 = cmpl.start, p2 = cmpl.end,
padding = data.list.maxlen-word.length-type.length+5;
Expand All @@ -3778,7 +3685,6 @@ function get_mode( grammar, DEFAULT, CodeMirror )
elt.style.position = 'relative'; elt.style.boxSizing = 'border-box';
elt.style.width = '100%'; elt.style.maxWidth = '100%';
};
cm_mode.autocomplete = cm_mode.autocompleter; // deprecated, for compatibility
cm_mode.dispose = function( ) {
if ( cm_mode.$parser ) cm_mode.$parser.dispose( );
cm_mode.$parser = null;
Expand Down
4 changes: 2 additions & 2 deletions build/codemirror_grammar.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion editor-grammar
8 changes: 4 additions & 4 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ var CodeMirrorParser = Class(Parser, {
}
else if ( 'markup' === FOLD || 'html' === FOLD || 'xml' === FOLD )
{
self.$folders.push( CodeMirrorParser.Fold.Markup( ) );
self.$folders.push( CodeMirrorParser.Fold.MarkedUp( ) );
}
}

Expand Down Expand Up @@ -234,7 +234,7 @@ function get_mode( grammar, DEFAULT, CodeMirror )
}

,indent: function( state, textAfter, fullLine ) {
return cm_mode.$parser.indent( $CodeMirror$, state, textAfter, fullLine, conf, parserConf, CodeMirror );
return cm_mode.$parser.indent( state, textAfter, fullLine, conf, parserConf, CodeMirror );
}

// support comments toggle functionality
Expand Down Expand Up @@ -272,7 +272,7 @@ function get_mode( grammar, DEFAULT, CodeMirror )
? cm_mode.$parser.validate( code, options, CodeMirror )
: [];
};
cm_mode.linter = cm_mode.validator;
cm_mode.linter = cm_mode.validator; // alias
// custom, user-defined, autocompletions generated from grammar
cm_mode.supportAutoCompletion = true;
cm_mode.autocompleter = function( cm, options ) {
Expand All @@ -283,6 +283,7 @@ function get_mode( grammar, DEFAULT, CodeMirror )
return cm_mode.$parser.autocomplete( cm, options, CodeMirror );
}
};
cm_mode.autocomplete = cm_mode.autocompleter; // deprecated, for compatibility
cm_mode.autocomplete_renderer = function( elt, data, cmpl ) {
var word = cmpl.text, type = cmpl.meta, p1 = cmpl.start, p2 = cmpl.end,
padding = data.list.maxlen-word.length-type.length+5;
Expand All @@ -299,7 +300,6 @@ function get_mode( grammar, DEFAULT, CodeMirror )
elt.style.position = 'relative'; elt.style.boxSizing = 'border-box';
elt.style.width = '100%'; elt.style.maxWidth = '100%';
};
cm_mode.autocomplete = cm_mode.autocompleter; // deprecated, for compatibility
cm_mode.dispose = function( ) {
if ( cm_mode.$parser ) cm_mode.$parser.dispose( );
cm_mode.$parser = null;
Expand Down

0 comments on commit 70db730

Please sign in to comment.