Skip to content

Commit

Permalink
Cursor and marks positions are restored after the beautification
Browse files Browse the repository at this point in the history
Zeus's code is used (with small changes) from here:
https://github.com/ZeusTheTrueGod/vim-format-js
Thank you Zeus.

Known bug: the 'last change mark' (.) restore doesn't work.
  • Loading branch information
Peter Foldi committed Dec 26, 2012
1 parent 42fe606 commit 2994b13
Showing 1 changed file with 82 additions and 2 deletions.
84 changes: 82 additions & 2 deletions plugin/beautifier.vim
Expand Up @@ -178,6 +178,83 @@ endfunc



" Helper functions for restoring mark and cursor position
function! s:getNumberOfNonSpaceCharactersFromTheStartOfFile(position)
let cursorRow = a:position.line
let cursorColumn = a:position.column
let lineNumber = 1
let nonBlankCount = 0
while lineNumber <= cursorRow
let lineContent = getline(lineNumber)
if lineNumber == cursorRow
let lineContent = strpart(lineContent,0,cursorColumn)
endif
let charIndex = 0
while charIndex < len(lineContent)
let char = strpart(lineContent,charIndex,1)
if match(char,'\s\|\n\|\r') == -1
let nonBlankCount = nonBlankCount + 1
endif
let charIndex = charIndex + 1
endwhile
"echo nonBlankCount
let lineNumber = lineNumber + 1
endwhile
return nonBlankCount
endfunction



"Converts number of non blank characters to cursor position (line and column)
function! s:getCursorPosition(numberOfNonBlankCharactersFromTheStartOfFile)
"echo a:numberOfNonBlankCharactersFromTheStartOfFile
let lineNumber = 1
let nonBlankCount = 0
while lineNumber <= line('$')
let lineContent = getline(lineNumber)
let charIndex = 0
while charIndex < len(lineContent)
let char = strpart(lineContent,charIndex,1)
if match(char,'\s\|\n\|\r') == -1
let nonBlankCount = nonBlankCount + 1
endif
let charIndex = charIndex + 1
if nonBlankCount == a:numberOfNonBlankCharactersFromTheStartOfFile
"echo 'found position!'
return {'line': lineNumber,'column': charIndex}
end
endwhile
let lineNumber = lineNumber + 1
endwhile
"echo "Oops, nothing found!"
endfunction



"Restoring current position by number of non blank characters
function! s:setNumberOfNonSpaceCharactersBeforeCursor(mark,numberOfNonBlankCharactersFromTheStartOfFile)
let location = s:getCursorPosition(a:numberOfNonBlankCharactersFromTheStartOfFile)
call setpos(a:mark, [0, location.line, location.column, 0])
endfunction



function! s:getCursorAndMarksPositions()
let localMarks = map(range(char2nr('a'), char2nr('z'))," \"'\".nr2char(v:val) ")
let marks = ['.'] + localMarks
let result = {}
for positionType in marks
let cursorPositionAsList = getpos(positionType)
let cursorPosition = {'buffer': cursorPositionAsList[0], 'line': cursorPositionAsList[1], 'column': cursorPositionAsList[2]}
if cursorPosition.buffer == 0 && cursorPosition.line > 0
let result[positionType] = cursorPosition
endif
endfor
return result
endfunction




"% Declaring global variables and functions

Expand Down Expand Up @@ -225,6 +302,8 @@ endfunction
" @param {[String]} line2 The end line on which stop formating,
" by default '$'
func! Beautifier(...)
let cursorPositions = s:getCursorAndMarksPositions()
call map(cursorPositions, " extend (v:val,{'characters': s:getNumberOfNonSpaceCharactersFromTheStartOfFile(v:val)}) ")
if !exists('b:config_Beautifier')
call s:updateConfig(g:config_Beautifier)
endif
Expand Down Expand Up @@ -290,6 +369,9 @@ func! Beautifier(...)
silent exec endline.",$g/.*/d"
endif

for [key,value] in items(cursorPositions)
call s:setNumberOfNonSpaceCharactersBeforeCursor(key,value.characters)
endfor
return result
endfun

Expand Down Expand Up @@ -332,8 +414,6 @@ func! BeautifierEditorconfigHook(config)
let config["indent_char"] = ' '
elseif config["indent_style"] == 'tab'
let config["indent_char"] = '\t'
" When the indent_char is tab, we always want to use 1 tab
let config["indent_size"] = 1
endif
endif

Expand Down

0 comments on commit 2994b13

Please sign in to comment.