Autocompleting relative paths

Hi, 

I have the following situation:

 

$classList = array(

'className' => 'path/to/file/a-class-file.inc.php',

//.....

);

is there any way to give a hint to the ide (via phpdoc comment or something) what is the location  of this relative file? Let's say the location is:

/lib/classes

and full path, relative to project root is:

/lib/classes/path/to/file/a-class-file.inc.php

How can I enable autocompletion in this case?

6 comments
Comment actions Permalink

Hi there,

Place caret in such place and press Alt+Enter -- see if you will have "Inject language or reference" option in appeared menu. If it's present -- use it and choose "File Reference" there.

  • NOTE 1: this option may not be available if string is empty or has less than 2 characters in it
  • NOTE 2: this is temp injection -- AFAIK it will be lost after IDE restart (and you will need to do it again if needed)

Otherwise: select desired file in Project View panel and use "Copy Reference" -- it will copy a file path relative to the project root -- you may only need to add leading "/"...

0
Comment actions Permalink

Hi,

The temp reference worked, because the path I want autocompleted is relative to the directory I'm working in which is fine in this case, but I don't want to insert temp reference every time I edit these paths.

I don't like duplicate code. There is no point to write lib/classes for every class if it'll never change.

What also works is following: 

$classList = array(

'className' => __DIR__.'path/to/file/a-class-file.inc.php',

);

But I don't want to have all this duplicate content. What I was hoping for is a way to tell what the key and the value of an array is - like:

/**

 * @array_key string

 * @array_value fileref('lib/classes') 

*/

$classList = array(

 'AClass' => 'path/to/file.php'

);

and it would be completed like it was:

define('PROJECT_ROOT', __DIR__'xxxxxx');

$classList = array(

 'AClass' => PROJECT_ROOT.'/lib/classes/path/to/file.php'

);

 

But that's looks like too good to be true :). Well if it can be achieved somehow, it would be very nice. For now I think the best solution is to use __DIR__ because the IDE knows what it is (although it's not as clean and readable)  

 

0
Comment actions Permalink

I do not know how exactly I will sound to you .. and what your project is (is that your own code or some existing/3rd party which you cannot modify; is it a modern (written now) code or some few-years old legacy code etc) .. but it's 2016 outside:

  • Composer can do class autoloading for you (supports many styles -- PSR-0, PSR-4, PEAR_STYLE .. even no style at all (it will scan all your files and detect all classes in them));
  • if Composer is unwanted (for whatever reason) you can always write your own autoloader;

Obviously, the above written applies if you want that "file referencing" for class/file loading (which seems to be the case based on the provided code sample).

1
Comment actions Permalink

>@array_value fileref('lib/classes') 

Not supported, and unlikely will be in nearest future (main reason: very limited usage case + code can be written in another way)

>The temp reference worked, because the path I want autocompleted is relative to the directory I'm working in

If you mark some folder as "Resource Root" then all references can be resolved against it which will make injected File Reference in constructions like PROJECT_ROOT . '/lib/classes/path/to/file.php' working properly.

1
Comment actions Permalink

> I do not know how exactly I will sound to you

You sound like you are nagging. The year never had anything to do with code.

Let's assume that I can't/don't want to use composer. Let's say that classes are not named to be easily find what class in what directory is because they are not named alll in a specific convention (and other reasons). So the only thing left is to put them in array and use the array in the autoloader.

> If you mark some folder as "Resource Root" then all references can be resolved against it which will make injected File Reference in constructions like PROJECT_ROOT . '/lib/classes/path/to/file.php' working properly.

Well thank you very much that you decided to write something on the subject (file reference, not autoloading). I need to know that not If you mark some folder as "Resource Root" then all references can be resolved against it which will make injected File Reference in constructions like PROJECT_ROOT . '/lib/classes/path/to/file.php' working properly because of autoloading.

0
Comment actions Permalink

Hmm. I'm not sure if I missed something. Setting resource root doesn't autocomplete file path (for relative or absolute paths). On the other hand having a constant other than __DIR__ also doesn't work (regardless if dir is resource root):

define(PROJECT_ROOT, __DIR__); // or any function of __DIR__ like dirname(__DIR__) or __DIR__.'/..'

readfile(PROJECT_ROOT.'/lib/classes');

but having the constant __DIR__ itself works:

readfile(__DIR__.'/lib/classes');

Also let's say that lib/classes is resource root - this doesn't work:

myFunction('/path-in-resource-root/file-that-should-autocomplete.php');

 

P.S. I just want to remind that I'm interested in file references/autocomplete in general, not only for current case - autoloading php classes. I also want that for resources.

0

Please sign in to leave a comment.