diff --git a/composer.json b/composer.json index 268a989..b29c1ee 100644 --- a/composer.json +++ b/composer.json @@ -32,7 +32,7 @@ "phpdocumentor/reflection-docblock": "^3.0", "sabre/event": "^5.0", "felixfbecker/advanced-json-rpc": "^2.0", - "squizlabs/php_codesniffer" : "^3.0", + "squizlabs/php_codesniffer" : "3.0.0RC3", "netresearch/jsonmapper": "^1.0", "webmozart/path-util": "^2.3", "webmozart/glob": "^4.1", diff --git a/src/DefinitionResolver.php b/src/DefinitionResolver.php index 3a8393a..ec295a5 100644 --- a/src/DefinitionResolver.php +++ b/src/DefinitionResolver.php @@ -12,7 +12,7 @@ use LanguageServer\Index\ReadableIndex; class DefinitionResolver { /** - * @var \LanguageServer\Index + * @var \LanguageServer\Index\ReadableIndex */ private $index; @@ -48,6 +48,7 @@ class DefinitionResolver // Properties and constants can have multiple declarations // Use the parent node (that includes the modifiers), but only render the requested declaration $child = $node; + /** @var Node */ $node = $node->getAttribute('parentNode'); $defLine = clone $node; $defLine->props = [$child]; @@ -363,7 +364,7 @@ class DefinitionResolver * Returns the assignment or parameter node where a variable was defined * * @param Node\Expr\Variable|Node\Expr\ClosureUse $var The variable access - * @return Node\Expr\Assign|Node\Param|Node\Expr\ClosureUse|null + * @return Node\Expr\Assign|Node\Expr\AssignOp|Node\Param|Node\Expr\ClosureUse|null */ public static function resolveVariableToNode(Node\Expr $var) { @@ -415,7 +416,7 @@ class DefinitionResolver * If the type could not be resolved, returns Types\Mixed. * * @param \PhpParser\Node\Expr $expr - * @return \phpDocumentor\Type + * @return \phpDocumentor\Reflection\Type */ public function resolveExpressionNodeToType(Node\Expr $expr): Type { @@ -539,7 +540,7 @@ class DefinitionResolver ]); } if ( - $expr instanceof Node\Expr\InstanceOf_ + $expr instanceof Node\Expr\Instanceof_ || $expr instanceof Node\Expr\Cast\Bool_ || $expr instanceof Node\Expr\BooleanNot || $expr instanceof Node\Expr\Empty_ @@ -559,19 +560,18 @@ class DefinitionResolver return new Types\Boolean; } if ( - $expr instanceof Node\Expr\Concat - || $expr instanceof Node\Expr\Cast\String_ + $expr instanceof Node\Expr\Cast\String_ || $expr instanceof Node\Expr\BinaryOp\Concat || $expr instanceof Node\Expr\AssignOp\Concat - || $expr instanceof Node\Expr\Scalar\String_ - || $expr instanceof Node\Expr\Scalar\Encapsed - || $expr instanceof Node\Expr\Scalar\EncapsedStringPart - || $expr instanceof Node\Expr\Scalar\MagicConst\Class_ - || $expr instanceof Node\Expr\Scalar\MagicConst\Dir - || $expr instanceof Node\Expr\Scalar\MagicConst\Function_ - || $expr instanceof Node\Expr\Scalar\MagicConst\Method - || $expr instanceof Node\Expr\Scalar\MagicConst\Namespace_ - || $expr instanceof Node\Expr\Scalar\MagicConst\Trait_ + || $expr instanceof Node\Scalar\String_ + || $expr instanceof Node\Scalar\Encapsed + || $expr instanceof Node\Scalar\EncapsedStringPart + || $expr instanceof Node\Scalar\MagicConst\Class_ + || $expr instanceof Node\Scalar\MagicConst\Dir + || $expr instanceof Node\Scalar\MagicConst\Function_ + || $expr instanceof Node\Scalar\MagicConst\Method + || $expr instanceof Node\Scalar\MagicConst\Namespace_ + || $expr instanceof Node\Scalar\MagicConst\Trait_ ) { return new Types\String_; } @@ -580,23 +580,35 @@ class DefinitionResolver || $expr instanceof Node\Expr\BinaryOp\Plus || $expr instanceof Node\Expr\BinaryOp\Pow || $expr instanceof Node\Expr\BinaryOp\Mul - || $expr instanceof Node\Expr\AssignOp\Minus - || $expr instanceof Node\Expr\AssignOp\Plus - || $expr instanceof Node\Expr\AssignOp\Pow - || $expr instanceof Node\Expr\AssignOp\Mul ) { if ( - $this->resolveExpressionNodeToType($expr->left) instanceof Types\Integer_ - && $this->resolveExpressionNodeToType($expr->right) instanceof Types\Integer_ + $this->resolveExpressionNodeToType($expr->left) instanceof Types\Integer + && $this->resolveExpressionNodeToType($expr->right) instanceof Types\Integer ) { return new Types\Integer; } return new Types\Float_; } + + if ( + $expr instanceof Node\Expr\AssignOp\Minus + || $expr instanceof Node\Expr\AssignOp\Plus + || $expr instanceof Node\Expr\AssignOp\Pow + || $expr instanceof Node\Expr\AssignOp\Mul + ) { + if ( + $this->resolveExpressionNodeToType($expr->var) instanceof Types\Integer + && $this->resolveExpressionNodeToType($expr->expr) instanceof Types\Integer + ) { + return new Types\Integer; + } + return new Types\Float_; + } + if ( $expr instanceof Node\Scalar\LNumber || $expr instanceof Node\Expr\Cast\Int_ - || $expr instanceof Node\Expr\Scalar\MagicConst\Line + || $expr instanceof Node\Scalar\MagicConst\Line || $expr instanceof Node\Expr\BinaryOp\Spaceship || $expr instanceof Node\Expr\BinaryOp\BitwiseAnd || $expr instanceof Node\Expr\BinaryOp\BitwiseOr @@ -606,7 +618,7 @@ class DefinitionResolver } if ( $expr instanceof Node\Expr\BinaryOp\Div - || $expr instanceof Node\Expr\DNumber + || $expr instanceof Node\Scalar\DNumber || $expr instanceof Node\Expr\Cast\Double ) { return new Types\Float_; @@ -702,7 +714,7 @@ class DefinitionResolver * Returns null if the node does not have a type. * * @param Node $node - * @return \phpDocumentor\Type|null + * @return \phpDocumentor\Reflection\Type|null */ public function getTypeFromNode(Node $node) { @@ -720,6 +732,7 @@ class DefinitionResolver } } } + $type = null; if ($node->type !== null) { // Use PHP7 return type hint if (is_string($node->type)) { @@ -792,7 +805,7 @@ class DefinitionResolver } } else if ($node instanceof Node\Const_) { return $this->resolveExpressionNodeToType($node->value); - } else if ($node instanceof Node\Expr\Assign || $node instanceof Node\Expr\AssignOp) { + } else { return $this->resolveExpressionNodeToType($node); } // TODO: read @property tags of class diff --git a/src/LanguageServer.php b/src/LanguageServer.php index 62b8400..6eb68e4 100644 --- a/src/LanguageServer.php +++ b/src/LanguageServer.php @@ -256,6 +256,7 @@ class LanguageServer extends AdvancedJsonRpc\Dispatcher } if ($this->workspace === null) { $this->workspace = new Server\Workspace( + $this->client, $this->projectIndex, $dependenciesIndex, $sourceIndex, diff --git a/src/Protocol/FileEvent.php b/src/Protocol/FileEvent.php index b4ed833..015044d 100644 --- a/src/Protocol/FileEvent.php +++ b/src/Protocol/FileEvent.php @@ -20,4 +20,14 @@ class FileEvent * @var int */ public $type; + + /** + * @param string $uri + * @param int $type + */ + public function __construct(string $uri, int $type) + { + $this->uri = $uri; + $this->type = $type; + } } diff --git a/src/Server/Workspace.php b/src/Server/Workspace.php index fb3807d..1f35134 100644 --- a/src/Server/Workspace.php +++ b/src/Server/Workspace.php @@ -5,7 +5,15 @@ namespace LanguageServer\Server; use LanguageServer\{LanguageClient, Project, PhpDocumentLoader, Options, Indexer}; use LanguageServer\Index\{ProjectIndex, DependenciesIndex, Index}; -use LanguageServer\Protocol\{SymbolInformation, SymbolDescriptor, ReferenceInformation, DependencyReference, Location}; +use LanguageServer\Protocol\{ + FileChangeType, + FileEvent, + SymbolInformation, + SymbolDescriptor, + ReferenceInformation, + DependencyReference, + Location +}; use Sabre\Event\Promise; use function Sabre\Event\coroutine; use function LanguageServer\{waitForEvent, getPackageName}; @@ -15,6 +23,11 @@ use function LanguageServer\{waitForEvent, getPackageName}; */ class Workspace { + /** + * @var LanguageClient + */ + public $client; + /** * The symbol index for the workspace * @@ -53,6 +66,7 @@ class Workspace public $documentLoader; /** + * @param LanguageClient $client LanguageClient instance used to signal updated results * @param ProjectIndex $index Index that is searched on a workspace/symbol request * @param DependenciesIndex $dependenciesIndex Index that is used on a workspace/xreferences request * @param DependenciesIndex $sourceIndex Index that is used on a workspace/xreferences request @@ -61,8 +75,9 @@ class Workspace * @param Indexer $indexer * @param Options $options */ - public function __construct(ProjectIndex $index, DependenciesIndex $dependenciesIndex, Index $sourceIndex, \stdClass $composerLock = null, PhpDocumentLoader $documentLoader, \stdClass $composerJson = null, Indexer $indexer = null, Options $options = null) + public function __construct(LanguageClient $client, ProjectIndex $index, DependenciesIndex $dependenciesIndex, Index $sourceIndex, \stdClass $composerLock = null, PhpDocumentLoader $documentLoader, \stdClass $composerJson = null, Indexer $indexer = null, Options $options = null) { + $this->client = $client; $this->sourceIndex = $sourceIndex; $this->index = $index; $this->dependenciesIndex = $dependenciesIndex; @@ -96,6 +111,21 @@ class Workspace }); } + /** + * The watched files notification is sent from the client to the server when the client detects changes to files watched by the language client. + * + * @param FileEvent[] $changes + * @return void + */ + public function didChangeWatchedFiles(array $changes) + { + foreach ($changes as $change) { + if ($change->type === FileChangeType::DELETED) { + $this->client->textDocument->publishDiagnostics($change->uri, []); + } + } + } + /** * The workspace references request is sent from the client to the server to locate project-wide references to a symbol given its description / metadata. * diff --git a/tests/Server/ServerTestCase.php b/tests/Server/ServerTestCase.php index 1a45c64..7bee051 100644 --- a/tests/Server/ServerTestCase.php +++ b/tests/Server/ServerTestCase.php @@ -51,9 +51,9 @@ abstract class ServerTestCase extends TestCase public function setUp() { - $sourceIndex = new Index; - $dependenciesIndex = new DependenciesIndex; - $this->projectIndex = new ProjectIndex($sourceIndex, $dependenciesIndex); + $sourceIndex = new Index; + $dependenciesIndex = new DependenciesIndex; + $this->projectIndex = new ProjectIndex($sourceIndex, $dependenciesIndex); $this->projectIndex->setComplete(); $rootPath = realpath(__DIR__ . '/../../fixtures/'); @@ -61,14 +61,15 @@ abstract class ServerTestCase extends TestCase $filesFinder = new FileSystemFilesFinder; $cache = new FileSystemCache; - $this->input = new MockProtocolStream; + $this->input = new MockProtocolStream; $this->output = new MockProtocolStream; + $definitionResolver = new DefinitionResolver($this->projectIndex); $client = new LanguageClient($this->input, $this->output); $this->documentLoader = new PhpDocumentLoader(new FileSystemContentRetriever, $this->projectIndex, $definitionResolver); $this->textDocument = new Server\TextDocument($this->documentLoader, $definitionResolver, $client, $this->projectIndex); $indexer = new Indexer($filesFinder, $rootPath, $client, $cache, $dependenciesIndex, $sourceIndex, $this->documentLoader, null, null, $options); - $this->workspace = new Server\Workspace($this->projectIndex, $dependenciesIndex, $sourceIndex, null, $this->documentLoader, null, $indexer, $options); + $this->workspace = new Server\Workspace($client, $this->projectIndex, $dependenciesIndex, $sourceIndex, null, $this->documentLoader, null, $indexer, $options); $globalSymbolsUri = pathToUri(realpath(__DIR__ . '/../../fixtures/global_symbols.php')); $globalReferencesUri = pathToUri(realpath(__DIR__ . '/../../fixtures/global_references.php')); diff --git a/tests/Server/Workspace/DidChangeWatchedFilesTest.php b/tests/Server/Workspace/DidChangeWatchedFilesTest.php new file mode 100644 index 0000000..1074c58 --- /dev/null +++ b/tests/Server/Workspace/DidChangeWatchedFilesTest.php @@ -0,0 +1,41 @@ +on('message', function (Message $message) use ($fileEvent, &$isDiagnosticsCleared) { + if ($message->body->method === "textDocument/publishDiagnostics") { + $this->assertEquals($message->body->params->uri, $fileEvent->uri); + $this->assertEquals($message->body->params->diagnostics, []); + $isDiagnosticsCleared = true; + } + }); + + $workspace->didChangeWatchedFiles([$fileEvent]); + Loop\tick(true); + + $this->assertTrue($isDiagnosticsCleared, "Deleting file should clear all diagnostics."); + } +}