Skip to content

Commit

Permalink
Add indent using dfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
idanarye committed Apr 23, 2015
1 parent 9b590ed commit 2641692
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
10 changes: 10 additions & 0 deletions autoload/dutyl.vim
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,13 @@ function! dutyl#formatExpressionInvoked() abort

call setline(v:lnum, l:formattedLines)
endfunction

function! dutyl#indentExpressionInvoked() abort
try
let l:dutyl=dutyl#core#requireFunctions('calcIndentForLastLineOfCode')
catch
echoerr 'Unable to indent code: '.v:exception
return
endtry
return l:dutyl.calcIndentForLastLineOfCode(getline(1, '.'))
endfunction
49 changes: 49 additions & 0 deletions autoload/dutyl/dfmt.vim
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,52 @@ function! s:functions.formatCode(code) abort
endif
return l:result
endfunction

function! s:functions.calcIndentForLastLineOfCode(code) abort
if empty(a:code)
return -1
endif
if type(a:code) == type('')
let l:code = dutyl#util#splitLines(a:code)
else
let l:code = a:code
endif

let l:markBeforeLastLine = 'dutylmark'.localtime()
call insert(l:code, '//', -1)
call insert(l:code, '// '.l:markBeforeLastLine, -1)
call add(l:code, '//')
call add(l:code, 'foo();')
let l:lastLineLength = len(l:code[-1])

"This will not actually affect the brace style, we are only indenting here
"and can't break lines, but if the bracing style is allman and dfmt is
"configured to use one of the other bracing styles the opening brace will
"be indented weirdly.
let l:dfmtArgs = [
\'--brace_style', 'allman',
\]

let l:formattedCode = dutyl#util#splitLines(dutyl#core#runToolIgnoreStderr('dfmt', l:dfmtArgs, l:code))

"Find the mark we placed:
let l:lineIndex = len(l:formattedCode) - 1
while 0 <= l:lineIndex
let l:line = l:formattedCode[l:lineIndex]
if len(l:markBeforeLastLine) < len(l:line)
if l:line[-len(l:markBeforeLastLine) : -1] == l:markBeforeLastLine
break
endif
endif
let l:lineIndex -= 1
endwhile
if l:lineIndex < 0
return -1
endif
let l:lineIndex += 1
if empty(l:formattedCode[l:lineIndex])
let l:lineIndex += 1
endif

return strwidth(s:getIndentFrom(l:code)) - strwidth(s:getIndentFrom(l:formattedCode)) + strwidth(matchstr(l:formattedCode[l:lineIndex], '\v^\_s*\ze\S'))
endfunction
1 change: 1 addition & 0 deletions ftplugin/d.vim
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ setlocal omnifunc=dutyl#dComplete

if dutyl#register#toolExecutable('dfmt')
setlocal formatexpr=dutyl#formatExpressionInvoked()
setlocal indentexpr=dutyl#indentExpressionInvoked()
endif

0 comments on commit 2641692

Please sign in to comment.