Skip to content
Merged
Show file tree
Hide file tree
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
48 changes: 19 additions & 29 deletions plugin/phpactor.vim
Original file line number Diff line number Diff line change
Expand Up @@ -366,47 +366,37 @@ function! phpactor#_applyTextEdits(path, edits)
call phpactor#_switchToBufferOrEdit(a:path)

let postCursorPosition = getpos('.')
let curLine = postCursorPosition[1]
let numberOfLinesToPreviousPosition = 0

for edit in a:edits
let startLine = edit.start.line
let endLine = edit.end.line

let newLines = 0

" start = { line: 1234, character: 0 }
let start = edit['start']

" end = { line: 1234, character: 0 }
let end = edit['end']

" move the cursor into the start position
call setpos('.', [ 0, start['line'] + 1, start['character'] + 1 ])

if start['character'] != 0 || end['character'] != 0
if edit.start.character != 0 || edit.end.character != 0
throw "Non-zero character offsets not supported in text edits, got " . json_encode(edit)
endif

" to delete
let linesToDelete = end['line'] - start['line']
if linesToDelete > 0
call execute('normal ' . linesToDelete . 'dd')
endif
let numberOfDeletedLines = endLine - startLine
if numberOfDeletedLines > 0
silent execute printf('keepjumps %d,%dd _', startLine + 1, endLine)

if edit['text'] == "\n"
" if this is just a new line, add a new line
call append(start['line'], '')
let newLines = 1
else
" insert characters after the current line
let appendLines = split(edit['text'], "\n")
call append(start['line'], appendLines)
let newLines = newLines + len(appendLines)
if startLine < curLine && curLine <= endLine
let numberOfLinesToPreviousPosition += endLine - curLine + 1
elseif endLine < curLine
let curLine -= numberOfDeletedLines
endif
endif

if postCursorPosition[1] > start['line']
let postCursorPosition[1] = postCursorPosition[1] + newLines
endif
let newLines = edit.text == "\n" ? [''] : split(edit.text, "\n")
keepjumps call append(startLine, newLines)

if startLine < curLine
let curLine += len(newLines)
endif
endfor

let postCursorPosition[1] = curLine - numberOfLinesToPreviousPosition
call setpos('.', postCursorPosition)
endfunction

Expand Down
43 changes: 43 additions & 0 deletions plugin/tests/apply_text_edits/replace_a_line.vader
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
Include: utils.vader

Given php (Original source):
<?php

class Foo
{
/**
* @var string
*/
private $foo:

private function foo()
{
return $this->foo();
}
}

Execute (Goes to the method line and changes the visibility):
10
call Apply(
\ Delete(10),
\ Insert(10, ' public function foo()')
\ )

Then (The cursor should not have moved):
call AssertCursorDidntMove()

Expect php (The visibility shoud have changed):
<?php

class Foo
{
/**
* @var string
*/
private $foo:

public function foo()
{
return $this->foo();
}
}
49 changes: 49 additions & 0 deletions plugin/tests/apply_text_edits/replace_line_which_move.vader
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
Include: utils.vader

Given php (Original source):
<?php

class Foo
{
/**
* @var string
*/
private $foo:

private function foo()
{
return $this->foo();
}
}

Execute (Goes to the method line and changes the visibility):
10
call Apply(
\ Delete(10),
\ Insert(10, ' /**'),
\ Insert(11, ' * @var int $i'),
\ Insert(12, ' */'),
\ Insert(13, ' private function foo(int $i)')

@dantleech dantleech Jun 12, 2019 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

might be a stupid question but why don't we just have the above Given and just goto the method line and invoke the change visiblity?

e.g. the insert_use test:

Given php (source with no namespace):
  <?php
  
  class Foobar
  {
      public function foobar(Phpactor $phactor)
      {
      }
  }
Do (put the cursor over an existing class name and add use):
  /Phpactor\<Enter>
  :call phpactor#UseAdd()\<Enter>

Expect php (the use statement to be inserted):
  <?php

  use Phpactor\Phpactor;

  
  class Foobar
  {
      public function foobar(Phpactor $phactor)
      {
      }
  }

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Ah, ok, this isn't testing change visibility :)

\ )

Then (Then cursor should still be on the method signature):
call AssertCursorOnLine(13)

Expect php (The docblock should be added and the signature changed):
<?php

class Foo
{
/**
* @var string
*/
private $foo:

/**
* @var int $i
*/
private function foo(int $i)
{
return $this->foo();
}
}
39 changes: 39 additions & 0 deletions plugin/tests/apply_text_edits/utils.vader
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
Before:
Save g:applyTextEditsPosBefore

set nofoldenable

function! Apply(...)
let g:applyTextEditsPosBefore = getpos('.')
let ApplyTextEdits = function('phpactor#_applyTextEdits', [expand('%:p')])

call ApplyTextEdits(a:000)
endfunction

function! Position(line)
return { 'line': a:line, 'character': 0 }
endfunction

function! Delete(startLine, ...)
let startLine = a:startLine - 1
let endLine = 0 < a:0 ? a:1 : a:startLine

return { 'start': Position(startLine), 'end': Position(endLine), 'text': '' }
endfunction

function! Insert(line, text)
let line = a:line - 1

return { 'start': Position(line), 'end': Position(line), 'text': a:text }
endfunction

function! AssertCursorDidntMove()
AssertEqual g:applyTextEditsPosBefore, getpos('.')
endfunction

function! AssertCursorOnLine(line)
AssertEqual a:line, line('.')
endfunction

After:
Restore