Skip to content

Commit

Permalink
Add ability to Go to Definition on Use statements (#3805)
Browse files Browse the repository at this point in the history
This adds the ability to use the LSP's "Go to Definition" on `use MyClass` statements.

Co-authored-by: Matthew Brown <github@muglug.com>
  • Loading branch information
joehoyle and muglug committed Jul 11, 2020
1 parent 0b6d682 commit b8c4abf
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Psalm/Internal/Analyzer/CanAlias.php
Expand Up @@ -63,6 +63,12 @@ public function visitUse(PhpParser\Node\Stmt\Use_ $stmt)
break;

case PhpParser\Node\Stmt\Use_::TYPE_NORMAL:
$codebase->analyzer->addOffsetReference(
$this->getFilePath(),
(int) $use->getAttribute('startFilePos'),
(int) $use->getAttribute('endFilePos'),
$use_path
);
if ($codebase->collect_locations) {
// register the path
$codebase->use_referencing_locations[strtolower($use_path)][] =
Expand Down
27 changes: 27 additions & 0 deletions tests/LanguageServer/SymbolLookupTest.php
Expand Up @@ -336,6 +336,33 @@ protected function get_command() : AClass {

$this->assertSame('B\AClass', $symbol_at_position[0]);
}

/**
* @return void
*/
public function testGetSymbolPositionUseStatement()
{
$codebase = $this->project_analyzer->getCodebase();
$config = $codebase->config;
$config->throw_exception = false;

$this->addFile(
'somefile.php',
'<?php
namespace B;
use StreamWrapper;
'
);

$codebase->file_provider->openFile('somefile.php');
$codebase->scanFiles();
$this->analyzeFile('somefile.php', new Context());

$symbol_at_position = $codebase->getReferenceAtPosition('somefile.php', new Position(2, 25));
$this->assertNotNull($symbol_at_position);

$this->assertSame('StreamWrapper', $symbol_at_position[0]);
}

/**
* @return void
Expand Down

0 comments on commit b8c4abf

Please sign in to comment.