Using codeStyleManager.shortenClassReferences for single field only
I'm adding a field to a class, and calling "codeStyleManager.shortenClassReferences(psiClass)"
afterwards to move the FQN classname (of the field just added) to the imports
list.
I'd like to perform this operation for the field only, but it seems to have
no effect. (Only psiClass and psiMethod seem to be valid args to shortenClassReferences()).
I assume that the workaround is simply to add the necessary imports myself,
correct?
And another question: what does "importList.findOnDemandImportStatement()"
actually mean? Does IDEA remember how an import statement was created (manually
or "on demand"?)? What's the distinction, and what's the use for this?
请先登录再写评论。
In Inspection Gadgets shortenClassReferences is used all the time for PsiStatements, PsiExpressions and other PsiElements, so it should work for PsiFields too.
importList.findOnDemandInportStatement() finds "" imports (e.g. import java.awt.;)
Bas
Hello Bas,
The code I'm looking at is:
-
PsiField newField = factory.createFieldFromText("private static final java.util.List
myList = new java.util.LinkedList();", parentClass);
parentClass.add(newField);
CodeStyleManager codeStyleManager = manager.getCodeStyleManager();
codeStyleManager.shortenClassReferences(newField);
-
The result I observe is certainly not shorteded.
This is in IDEA 5.1.2, and the sandbox instance has "Optimize imports on
the fly" disabled.
Ok. Seems a bit of strange naming? (I'd expect findWholePackageImportStatements()
or something similar).
Is there some code that computes whether an importlist does include include
(explicitly or implicitly) a specific class?
P.S. Did you manage to solve your SVN problem?
Found out my mistake: you have to operate on the physical element returned
from add(), not on the nonphysical elements passed to add().
Hi Taras,
Try this:
I guess it is, it never bothered me though.
No that I know of, but it should not be hard to make.
Not yet, but I did not invest any time in it either. I have another machine I can still work on.
Bas
Whoops, you discovered the problem yourself already.
Thank you sir, you saved me much grief :-). I was curious though -- there is a PsiElement returned from shortenClassReferences, but that does not seem to be needed. For example:
So is the PsiElement returned from shortenClassReferences the same as the element passed to it? It seems to work so I'd guess so, but just curious.