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
23 changes: 15 additions & 8 deletions autoload/phpactor/quickfix.vim
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,29 @@ function! phpactor#quickfix#build(entries) abort
endfunction

function! phpactor#quickfix#fzf(entries) abort
" Associate each entry data with a unique key
let entries = {}
" Keep track of the order of the entries by their key
let sortedKeys = []
for entry in a:entries
let key = s:relative_path(entry['filename'])
\ .':'. entry['lnum']
\ .':'. (entry['col'])
\ .':'. entry['text']

let entries[key] = entry
call add(sortedKeys, key)
endfor

let formated = s:align_pairs(keys(entries), '^\(.\{-}:\d\+:\d\+:\)\s*\(.*\)\s*$', 100)
let formatedEntries = s:align_pairs(sortedKeys, '^\(.\{-}:\d\+:\d\+:\)\s*\(.*\)\s*$', 100)

let tmp = copy(entries)
let results = {}
for key in keys(tmp)
let newKey = formated[key]
let results[newKey] = tmp[key]
let entries = {}
let source = [] " Need a list to keep the order (dict does not guarantee it)
for key in sortedKeys
let newKey = formatedEntries[key]
let entries[newKey] = tmp[key]
call add(source, newKey)
endfor
unlet tmp

Expand All @@ -42,18 +48,19 @@ function! phpactor#quickfix#fzf(entries) abort
\ }

call fzf#run(fzf#wrap('find_references', fzf#vim#with_preview({
\ 'source': keys(results),
\ 'source': source,
\ 'down': '60%',
\ '_action': actions,
\ 'sink*': function('<SID>quickfix_sink', [results, actions]),
\ 'sink*': function('<SID>quickfix_sink', [entries, actions]),
\ 'options': [
\ '--exit-0',
\ '--expect='. join(keys(actions), ','),
\ '--multi',
\ '--bind=ctrl-a:select-all,ctrl-d:deselect-all',
\ '--inline-info',
\ '--header', ":: Press \x1b[35mCTRL-Q\x1b[m to open the quickfix with your selection",
\ '--delimiter=:', '--nth=1,4'
\ '--delimiter=:', '--nth=1,4',
\ '--reverse'
\ ]}, 'up', '?'), 1))
endfunction

Expand Down
37 changes: 37 additions & 0 deletions lib/Extension/ClassMover/Rpc/ReferencesHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,22 @@ private function performFindOrReplaceReferences(
string $filesystem,
string $source = null,
string $replacement = null
) {
[$source, $references] = $this->doPerformFindOrReplaceReferences(
$symbolContext,
$filesystem,
$source,
$replacement,
);

return [$source, $this->sortReferences($references)];
}

private function doPerformFindOrReplaceReferences(
SymbolContext $symbolContext,
string $filesystem,
string $source = null,
string $replacement = null
) {
switch ($symbolContext->symbol()->symbolType()) {
case Symbol::CLASS_:
Expand All @@ -283,6 +299,27 @@ private function performFindOrReplaceReferences(
));
}

private function sortReferences(array $fileReferences): array
{
// Sort the references for each file
array_walk($fileReferences, function (array &$fileReference) {
if (empty($fileReference['references'])) {
return; // Do nothing if there is no references
}

usort($fileReference['references'], function (array $first, array $second) {
return $first['start'] - $second['start'];
});
});

// Sort the list by file
usort($fileReferences, function (array $first, array $second) {
return strcmp($first['file'], $second['file']);
});

return $fileReferences;
}

private function echoMessage(string $action, SymbolContext $symbolContext, string $filesystem, array $references): EchoResponse
{
$count = array_reduce($references, function ($count, $result) {
Expand Down
89 changes: 88 additions & 1 deletion tests/Unit/Extension/ClassMover/Rpc/ReferencesHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ public function testFilesystemSelection()
$this->assertEquals('git', $input->default());
}


public function testInvalidSymbolType()
{
$this->expectException(\RuntimeException::class);
Expand Down Expand Up @@ -366,6 +365,94 @@ public function testMemberReferencesWithRisky()
$this->assertContains('risky', $first->message());
}

public function testReferencesAreSorted()
{
$this->classMemberReferences->findOrReplaceReferences(
SourceCodeFilesystemExtension::FILESYSTEM_GIT,
__CLASS__,
'testMemberReferences',
ClassMemberQuery::TYPE_METHOD,
null
)->willReturn([
'references' => [[
'file' => 'foobar',
'references' => [
[
'start' => 10,
'line_no' => 8,
'end' => 20,
'line' => '',
'col_no' => 12,
],
],
], [
'file' => 'barfoo',
'references' => [
[
'start' => 13,
'line_no' => 10,
'end' => 20,
'line' => '',
'col_no' => 15,
], [
'start' => 10,
'line_no' => 10,
'end' => 20,
'line' => '',
'col_no' => 12,
],
],
]],
]);

$action = $this->handle('references', [
'source' => $std = '<?php $foo = new ' . __CLASS__ . '(); $foo->testMemberReferences();',
'offset' => 104,
'path' => self::TEST_PATH,
'filesystem' => 'git',
]);

$this->assertInstanceOf(CollectionResponse::class, $action);

$actions = $action->actions();

$first = array_shift($actions);
$this->assertInstanceOf(EchoResponse::class, $first);

$second = array_shift($actions);
$this->assertEquals([
'file_references' => [[
'file' => 'barfoo',
'references' => [
[
'start' => 10,
'line_no' => 10,
'end' => 20,
'line' => '',
'col_no' => 12,
], [
'start' => 13,
'line_no' => 10,
'end' => 20,
'line' => '',
'col_no' => 15,
],
],
], [
'file' => 'foobar',
'references' => [
[
'start' => 10,
'line_no' => 8,
'end' => 20,
'line' => '',
'col_no' => 12,
],
],
]],
], $second->parameters());
}

private function exampleMemberRiskyResponse()
{
return [
Expand Down