Comments (1)
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)
- difficulty closing a nested bracket if it's deleted HOT 2
- C-style expands not working HOT 5
- Erlang binary matching - question HOT 3
- .gitignore doc/tags for use with Pathogen HOT 1
- Report speech. HOT 5
- Problems when using vim-smartinput and vim-endwise together HOT 1
- Ability to change multiline pair exiting HOT 4
- Please document limitations of redo/undo/repeat HOT 1
- cause infinite loop in vim debug mode HOT 1
- eskk.vimに干渉する HOT 1
- smartinput seems to break cabbrev expansion with <CR> HOT 3
- won't allow } if braces are unbalanced HOT 4
- Feature request: Pair spaces if between brackets HOT 2
- Add support for repeat.vim HOT 1
- didn't delete '}' if delete '{' using <C-u>
- Is there a convenient way to escape from the inserted surroundings? HOT 1
- using <c-w> to delete brackets, only half of brackets deleted
- Can it support unicode character? HOT 1
- [feature] Add support for vim-visual-multi
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vim-smartinput.