Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Turn off all auto-insertion/leaving when not perfectly balanced. #64

Open
jordwalke opened this Issue · 1 comment

1 participant

@jordwalke

I notice that the times when these types of plugins surprise you, is when the source code was not perfectly balanced, and you try to correct that imbalance, yet the plugin fights your attempts. Example:
* represents the cursor.

`myMethod*).call()`

In this case I want to type ( to complete the function call but smartinput inserts an extra opening paren. I think the best strategy is to disable any actions the plugin would ever take when the code is not already balanced. Or better yet, never insert a matching pair if the users keypress would cause the source to become balanced. That's a good way to tell that your plugin's efforts will fight against the users' attempts to make the source code balanced.

@jordwalke

You can look at one paredit.vim to see how they determine if the source is currently balanced:
Here's a couple of helper utilities to get you started if you choose to implement this feature (mostly lifted from that plugin):

let g:skip_sc = 'synIDattr(synID(line("."), col("."), 0), "name") =~ "[Ss]tring\\|[Cc]omment\\|[Ss]pecial"'
function! InsideString( ... )
    let l = a:0 ? a:1 : line('.')
    let c = a:0 ? a:2 : col('.')
    if &syntax == ''
        " No help from syntax engine,
        " count quote characters up to the cursor position
        let line = strpart( getline(l), 0, c - 1 )
        let line = substitute( line, '\\"', '', 'g' )
        let quotes = substitute( line, '[^"]', '', 'g' )
        return len(quotes) % 2
    endif
    return SynIDMatch( '[Ss]tring', l, c, 0 )
endfunction

function! IsBalanced()
    let l = line( '.' )
    let c =  col( '.' )
    let line = getline( '.' )
    let matchb = max( [l-1000, 1] )   " 1000 is max lookahead
    let matchf = min( [l+1000, line('$')] )
    let p1 = searchpair( '(', '', ')', 'brnmW', g:skip_sc, matchb )
    let p2 = searchpair( '(', '', ')',  'rnmW', g:skip_sc, matchf )
    if !(p1 == p2) && !(p1 == p2 - 1 && line[c-1] == '(') && !(p1 == p2 + 1 && line[c-1] == ')')
        " Number of opening and closing parens differ
        return 0
    endif

    let b1 = searchpair( '\[', '', '\]', 'brnmW', g:skip_sc, matchb )
    let b2 = searchpair( '\[', '', '\]',  'rnmW', g:skip_sc, matchf )
    if !(b1 == b2) && !(b1 == b2 - 1 && line[c-1] == '[') && !(b1 == b2 + 1 && line[c-1] == ']')
        " Number of opening and closing brackets differ
        return 0
    endif
    let b1 = searchpair( '{', '', '}', 'brnmW', g:skip_sc, matchb )
    let b2 = searchpair( '{', '', '}',  'rnmW', g:skip_sc, matchf )
    if !(b1 == b2) && !(b1 == b2 - 1 && line[c-1] == '{') && !(b1 == b2 + 1 && line[c-1] == '}')
        " Number of opening and closing curly braces differ
        return 0
    endif
    return 1
endfunction

" Is the current cursor position inside a comment?
function! InsideComment( ... )
    let l = a:0 ? a:1 : line('.')
    let c = a:0 ? a:2 : col('.')
    if &syntax == ''
        " No help from syntax engine,
        " remove strings and search for ';' up to the cursor position
        let line = strpart( getline(l), 0, c - 1 )
        let line = substitute( line, '\\"', '', 'g' )
        let line = substitute( line, '"[^"]*"', '', 'g' )
        return match( line, ';' ) >= 0
    endif
    return SynIDMatch( '[Cc]omment', l, c, 1 )
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.