Code Monkey home page Code Monkey logo

Comments (1)

jordwalke avatar jordwalke commented on June 11, 2024

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

from vim-smartinput.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.