How to convert string generated with "Copy Reference" to project file path?
Is there a generic way of converting the reference string generated by "Copy Reference" that will resolve to a file regardless of the language specific IDE?
I would be happy with any of the following:
- Full file path
- File
- VirtualFile
- PsiFile
Please sign in to leave a comment.
Hi Vladimir!
I am afraid I don't understand the question. Do you mean that you want to suppress e.g. com.intellij.codeInsight.editorActions.JavaCopyPasteReferenceProcessor?
Anna
Not quite.
All IDEs (IDEA, PhpStorm, WebStorm, … ) have “Copy Reference” action that copies path relative to some module and if the action is invoked from the editor then line number is included.
The results are as follows:
IDEA (Java):
com/vladsch/idea/multimarkdown/psi/impl/MultiMarkdownPsiReference.java:268PhpStorm:
Http/Controllers/MultiMarkdownController.php:77WebStorm:
src/Router.js:14PyCharm:
SubDirectory/test.py:4RubyMine:
UserIssues/client.rb:23Where the resulting string is a relative path from the project base path, I can easily find the file being referenced.
However in those IDEs/languages where the string results in a language specific reference, such as Java and Php, the reference needs to be resolved to a file.
In my plugin I detect that the string is a reference and convert it to a Markdown link to the file. I have it working for case 1, but not sure how to handle case 2 without creating language specific code and was wondering if there is an API available that can convert the reference to the file.
For example the Java reference resolves to project relative path: src/com/vladsch/idea/multimarkdown/psi/impl/MultiMarkdownPsiReference.java
Php reference resolves to project relative path: app/Http/Controllers/MultiMarkdownController.php
However these will depend on the module roots.
Actually I don't know. Do you convert the string on paste or is it already in the text?
The "Copy Reference" puts a string on the clipboard.
I found a way of doing this that works but seems a bit of a kludge.
I first test to see if a file exists if the reference is treated as a path relative from project base path.
If above fails, I use the file index to get all files matching the file name in the reference. Then see if the file path of one of these files ends with the reference string (less the line reference). First match is assumed to be the file referred to by the reference. It works but it is possible to have a project structure that will result in duplicate matches based on "ends with". In this case the first match will be used.
This is the only way I could think of that is language or IDE independent.
seems you need qualifiedNameProvider ?
qualifiedNameProvider would resolve the issue but it would break clients which rely on default implementation based on relative to content root contract.
@Vladimir, that's the only way to deal with I can think of. It's not perfect but it doesn't look that it's safe to change the default behaviour.
This is exactly what I was looking for. I don't need to change it because what I am looking for is to convert the the fqn string to element (PsiFile). I can use CopyReferenceAction for examples of qualified name provider use.
I will use qualified name providers to convert the reference to PsiFile, failing that I will fallback to my method 2.
Xu Tang, Anna, thank you for your help.
Anna, it seems that PhpStorm’s
PhpQualifiedNameProvidercannot convert reference strings that it generates if they include class methods.For example
\App\Http\Controllers\HomeController::getMissing, where getMissing is a method of the class, thenprovider.qualifiedNameToElement(reference, project)returnsnull.However, if I truncate the FQN string at the first
:then theHomeControllerclass is returned. This behaviour is different from RubyMine which returns the element for which the FQN string was generated.My workaround if the FQN string is not resolved to an element for
PhpQualifiedNameProvider, I truncate it at the first:and try again.Is this a bug for which I should open on YouTrack or the expected behaviour?
Looks like a bug to me.
Anna
Issue opened: https://youtrack.jetbrains.com/issue/WI-37276