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

Vim extract functions handles motions #823

Merged
Merged
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
88 changes: 67 additions & 21 deletions plugin/phpactor.vim
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ function! phpactor#Complete(findstart, base)
endif

return completions
endfunc
endfunction

function! phpactor#_completeTruncateLabel(label, length)
if strlen(a:label) < a:length
Expand Down Expand Up @@ -145,24 +145,50 @@ endfunction
""""""""""""""""""""""""
" Extract method
""""""""""""""""""""""""
function! phpactor#ExtractMethod()
let selectionStart = phpactor#_selectionStart()
let selectionEnd = phpactor#_selectionEnd()

call phpactor#rpc("extract_method", { "path": phpactor#_path(), "offset_start": selectionStart, "offset_end": selectionEnd, "source": phpactor#_source()})
function! phpactor#ExtractMethod(...)
let positions = {}

if 0 == a:0 " Visual mode - backward compatibility
let positions.start = phpactor#_selectionStart()
let positions.end = phpactor#_selectionEnd()
elseif a:1 ==? 'v' " Visual mode
let positions.start = phpactor#_selectionStart()
let positions.end = phpactor#_selectionEnd()
else " Linewise or characterwise motion
let linewise = 'line' == a:1

let positions.start = s:getStartOffsetFromMark("'[", linewise)
let positions.end = s:getEndOffsetFromMark("']", linewise)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this logic is the same as below, can we extract a common method?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the difference is that expression can work with just a single offset, not sure if method works like that - in theory it could extract the expression under the cursor.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes the important is not the two lines but the logic block just above.
And we don't handle expression and method extraction the same way.
Also rule of three: if there is only those 2 occurences I prefer to wait until refactoring.

endif

call phpactor#rpc("extract_method", { "path": phpactor#_path(), "offset_start": positions.start, "offset_end": positions.end, "source": phpactor#_source()})
endfunction

function! phpactor#ExtractExpression(isSelection)
function! phpactor#ExtractExpression(type)
let positions = {}

if a:isSelection
let selectionStart = phpactor#_selectionStart()
let selectionEnd = phpactor#_selectionEnd()
else
let selectionStart = phpactor#_offset()
let selectionEnd = v:null
if v:true == a:type " Invoked from Visual mode - backward compatibility
let positions.start = phpactor#_selectionStart()
let positions.end = phpactor#_selectionEnd()
elseif v:false == a:type " Invoked from an offset - backward compatibility
let positions.start = phpactor#_offset()
let positions.end = v:null
elseif a:type ==? 'v' " Visual mode
let positions.start = phpactor#_selectionStart()
let positions.end = phpactor#_selectionEnd()
else " Linewise or characterwise motion
let linewise = 'line' == a:type

let positions.start = s:getStartOffsetFromMark("'[", linewise)
let positions.end = s:getEndOffsetFromMark("']", linewise)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So v and True are the same? What does linewise / characterwise do? (not familiar with this)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's a vim notion so I will refer you to the doc, since it's been a while I don't remember exactly and it will be more accurate this way:

:h linewise

endif

call phpactor#rpc("extract_expression", { "path": phpactor#_path(), "offset_start": selectionStart, "offset_end": selectionEnd, "source": phpactor#_source()})
call phpactor#rpc("extract_expression", { "path": phpactor#_path(), "offset_start": positions.start, "offset_end": positions.end, "source": phpactor#_source()})
endfunction

function! phpactor#ExtractConstant()
call phpactor#rpc("extract_constant", { "offset": phpactor#_offset(), "source": phpactor#_source(), "path": phpactor#_path()})
endfunction

function! phpactor#ClassExpand()
Expand Down Expand Up @@ -356,20 +382,40 @@ function! phpactor#_path()
return expand('%:p')
endfunction

function! phpactor#_selectionStart()
let [lineStart, columnStart] = getpos("'<")[1:2]
return line2byte(lineStart) + columnStart -2
function! s:getStartOffsetFromMark(mark, linewise)
let [line, column] = getpos(a:mark)[1:2]
let offset = line2byte(line)

if v:true == a:linewise
return offset - 1
endif

return offset + column - 2
endfunction

function! phpactor#_selectionEnd()
let [lineEnd, columnEnd] = getpos("'>")[1:2]
function! s:getEndOffsetFromMark(mark, linewise)
let [line, column] = getpos(a:mark)[1:2]
let offset = line2byte(line)
let lineLenght = strlen(getline(line))

if v:true == a:linewise
return offset + lineLenght - 1
endif

" Note VIM returns 2,147,483,647 on this system when in block select mode
if (columnEnd > 1000000)
let columnEnd = strlen(getline(lineEnd))
if (column > 1000000)
let column = lineLenght
endif

return line2byte(lineEnd) + columnEnd -1
return offset + column - 1
endfunction

function! phpactor#_selectionStart()
return s:getStartOffsetFromMark("'<", v:false)
endfunction

function! phpactor#_selectionEnd()
return s:getEndOffsetFromMark("'>", v:false)
endfunction

function! phpactor#_applyTextEdits(path, edits)
Expand Down