Skip to content

Commit

Permalink
Initial support for coffeescript multiline strings
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewRadev committed Apr 7, 2012
1 parent eedac86 commit dd86b4b
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 2 deletions.
28 changes: 28 additions & 0 deletions autoload/sj.vim
Expand Up @@ -98,6 +98,20 @@ function! sj#ReplaceCols(start, end, text)
return sj#ReplaceMotion(motion, a:text)
endfunction

" function! sj#ReplaceByPosition(start, end, text) {{{2
"
" Replace the area defined by the 'start' and 'end' positions with 'text'. The
" positions should be compatible with the results of getpos():
"
" [bufnum, lnum, col, off]
"
function! sj#ReplaceByPosition(start, end, text)
call setpos('.', a:start)
call setpos("'z", a:end)

return sj#ReplaceMotion('v`z', a:text)
endfunction

" Text retrieval {{{1
"
" These functions are similar to the text replacement functions, only retrieve
Expand Down Expand Up @@ -139,6 +153,20 @@ function! sj#GetCols(start, end)
return strpart(getline('.'), a:start - 1, a:end - a:start + 1)
endfunction

" function! sj#GetByPosition(start, end) {{{2
"
" Fetch the area defined by the 'start' and 'end' positions. The positions
" should be compatible with the results of getpos():
"
" [bufnum, lnum, col, off]
"
function! sj#GetByPosition(start, end)
call setpos('.', a:start)
call setpos("'z", a:end)

return sj#GetMotion('v`z')
endfunction

" String functions {{{1
" Various string manipulation utility functions
function! sj#BlankString(s)
Expand Down
36 changes: 36 additions & 0 deletions autoload/sj/coffee.vim
Expand Up @@ -107,6 +107,42 @@ function! sj#coffee#JoinObjectLiteral()
return 1
endfunction

function! sj#coffee#SplitString()
if search('"', 'Wb', line('.')) <= 0
return 0
endif

let body = sj#GetMotion('vi"')
let new_body = "\"\"\n".body."\n\"\"" " Note: only two double quotes

call sj#ReplaceMotion('vi"', new_body)
normal! j>>

return 1
endfunction

function! sj#coffee#JoinString()
if search('"""', 'Wbc') <= 0
return 0
endif
let start = getpos('.')
normal! j

if search('"""', 'Wce') <= 0
return 0
endif
let end = getpos('.')

let body = sj#GetByPosition(start, end)
let new_body = substitute(body, '^"""\_s*\(.*\)\_s*"""', '\1', 'g')
let new_body = sj#Trim(new_body)
let new_body = '"'.new_body.'"'

call sj#ReplaceByPosition(start, end, new_body)

return 1
endfunction

function! s:ParseHash(from, to)
let parser = sj#argparser#js#Construct(a:from, a:to, getline('.'))
call parser.Process()
Expand Down
7 changes: 7 additions & 0 deletions examples/test.coffee
Expand Up @@ -7,3 +7,10 @@ do ->
if foo? then console.log bar

foo = { one: two, three: 'four' }

foo = "example"
foo = "example with #{interpolation}"
foo = "example with \"nested\" quotes"

# TODO (2012-04-07) Implement
foo = 'example with single quotes'
6 changes: 4 additions & 2 deletions ftplugin/coffee/splitjoin.vim
@@ -1,15 +1,17 @@
if !exists('b:splitjoin_split_callbacks')
let b:splitjoin_split_callbacks = [
\ 'sj#coffee#SplitString',
\ 'sj#coffee#SplitFunction',
\ 'sj#coffee#SplitIfClause',
\ 'sj#coffee#SplitObjectLiteral'
\ 'sj#coffee#SplitObjectLiteral',
\ ]
endif

if !exists('b:splitjoin_join_callbacks')
let b:splitjoin_join_callbacks = [
\ 'sj#coffee#JoinString',
\ 'sj#coffee#JoinFunction',
\ 'sj#coffee#JoinIfClause',
\ 'sj#coffee#JoinObjectLiteral'
\ 'sj#coffee#JoinObjectLiteral',
\ ]
endif

0 comments on commit dd86b4b

Please sign in to comment.