Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass v:lnum as an argument to GetJsonIndent #102

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions indent/json.vim
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ let b:did_indent = 1
setlocal nosmartindent

" Now, set up our indentation expression and keys that trigger it.
setlocal indentexpr=GetJSONIndent()
setlocal indentexpr=GetJSONIndent(v:lnum)
setlocal indentkeys=0{,0},0),0[,0],!^F,o,O,e

" Only define the function once.
Expand Down Expand Up @@ -86,26 +86,28 @@ endfunction
" 3. GetJSONIndent Function {{{1
" =========================

function GetJSONIndent()
function GetJSONIndent(...)
" 3.1. Setup {{{2
" ----------
" For the current line, use the first argument if given, else v:lnum
let clnum = a:0 ? a:1 : v:lnum

" Set up variables for restoring position in file. Could use v:lnum here.
" Set up variables for restoring position in file. Could use clnum here.
let vcol = col('.')

" 3.2. Work on the current line {{{2
" -----------------------------

" Get the current line.
let line = getline(v:lnum)
let line = getline(clnum)
let ind = -1

" If we got a closing bracket on an empty line, find its match and indent
" according to it.
let col = matchend(line, '^\s*[]}]')

if col > 0 && !s:IsInString(v:lnum, col)
call cursor(v:lnum, col)
if col > 0 && !s:IsInString(clnum, col)
call cursor(clnum, col)
let bs = strpart('{}[]', stridx('}]', line[col - 1]) * 2, 2)

let pairstart = escape(bs[0], '[')
Expand All @@ -122,14 +124,14 @@ function GetJSONIndent()
endif

" If we are in a multi-line string, don't do anything to it.
if s:IsInString(v:lnum, matchend(line, '^\s*') + 1)
if s:IsInString(clnum, matchend(line, '^\s*') + 1)
return indent('.')
endif

" 3.3. Work on the previous line. {{{2
" -------------------------------

let lnum = prevnonblank(v:lnum - 1)
let lnum = prevnonblank(clnum - 1)

if lnum == 0
return 0
Expand Down Expand Up @@ -159,7 +161,7 @@ function GetJSONIndent()
return ind + &sw
endif
else
call cursor(v:lnum, vcol)
call cursor(clnum, vcol)
end
endif

Expand Down