HvyIndustries / crane

PHP Intellisense/code-completion for VS Code
https://hvy.io/crane
Other
240 stars 25 forks source link

Fix namespace issues with code completion #216

Open nevadascout opened 7 years ago

nevadascout commented 7 years ago

When getting suggestions for a method, we need to check the namespace that is being used and use the appropriate class.

namespace space
{
    class a
    {
    }
}

class a
{
}

use space\a;

$a = new a(); // <- a is space\a, not \a
ichiriac commented 7 years ago

For information, here some tricks for namespace resolution :

Fully compliant with PHP doc here : http://php.net/manual/en/language.namespaces.rules.php

PHP Script :

<?php
$a = new a(); // <-- this is UnQualified Name
$b = new \a(); // <-- Fully Qualified Name
$c = new namespace\a(); // <-- Relative Name (PHP7)

AST Structure :

{
  "kind": "program",
  "children": [
    {
      "kind": "assign",
      "operator": "=",
      "left": {
        "kind": "variable",
        "name": "a",
        "byref": false
      },
      "right": {
        "kind": "new",
        "what": {
          "kind": "identifier",
          "resolution": "uqn",
          "name": "a"
        },
        "arguments": []
      }
    },
    {
      "kind": "assign",
      "operator": "=",
      "left": {
        "kind": "variable",
        "name": "b",
        "byref": false
      },
      "right": {
        "kind": "new",
        "what": {
          "kind": "identifier",
          "resolution": "fqn",
          "name": "\\a"
        },
        "arguments": []
      }
    },
    {
      "kind": "assign",
      "operator": "=",
      "left": {
        "kind": "variable",
        "name": "c",
        "byref": false
      },
      "right": {
        "kind": "new",
        "what": {
          "kind": "identifier",
          "resolution": "rn",
          "name": "a"
        },
        "arguments": []
      }
    }
  ],
  "errors": []
}