Replacing the content of a java file editor
Hi,
I'm just beginning with IntelliJ plugin developpment.
I would like to replace *automatically* (on opening) the Java editor content, when the source file is not found (ie I'd like to override the default behaviour that shows comments with "compiled code"). And of course I'd like to keep the same behaviour in order to lookup for sources, notifications, and of course display the original sources if IntelliJ knows them.
To my understanding I should register a component in the aplication scope.
I think the component class should take EditorFactory as an argument, but from there I'm a bit lost.
JavaEditorFileSwapper, AttachSourcesNotificationProvider seem to be good guesses. But I'm not sure how I can plug there.
Can I extend AttachSourcesNotificationProvider and override createNotificationPanel? Or is there a better way?
Besides if it is the best way, how would I register this "extension point" and replace the current AttachSourcesNotificationProvider ?
By the way I'm developping this with IntelliJ 10.5.2.
Guidance would be very much appreciated.
请先登录再写评论。
Hello Brice,
If you want to plug in your own decompiler, the correct way to do so is to
register an extension in the ContentBasedClassFileProcessor extension point.
--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"
Hi Dmitry,
Thx for the answer, it seems it is working. The following implementation seems to do the job (don't mind yet the returned text as I'm experiencing how things work) :
public class JavaDecompilerClassFileProcessor implements ContentBasedClassFileProcessor {
@Override
public boolean isApplicable(Project project, VirtualFile virtualFile) {
return virtualFile.getFileType() == StdFileTypes.CLASS;
}
@NotNull
@Override
public SyntaxHighlighter createHighlighter(Project project, VirtualFile vFile) {
return SyntaxHighlighter.PROVIDER.create(StdFileTypes.JAVA, project, vFile);
}
@NotNull
@Override
public String obtainFileText(Project project, VirtualFile virtualFile) {
return "/* yup */";
}
@Override
public Language obtainLanguageForFile(VirtualFile virtualFile) {
return null;
}
}
However I experience some small issues / questions :
[ 191937] ERROR - i.impl.compiled.ClsElementImpl - getText() == null, element = PsiClass:MethodWriter, parent = PsiFile:MethodWriter.class
java.lang.Throwable
at com.intellij.openapi.diagnostic.Logger.error(Logger.java:52)
at com.intellij.psi.impl.compiled.ClsElementImpl.getTextLength(ClsElementImpl.java:143)
at com.intellij.psi.SingleRootFileViewProvider.findElementAt(SingleRootFileViewProvider.java:390)
at com.intellij.psi.SingleRootFileViewProvider.findElementAt(SingleRootFileViewProvider.java:343)
at com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil.findInjectedElementNoCommitWithOffset(InjectedLanguageUtil.java:246)
at com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil.findInjectedElementNoCommit(InjectedLanguageUtil.java:257)
at com.intellij.codeInsight.highlighting.BraceHighlightingHandler.a(BraceHighlightingHandler.java:131)
at com.intellij.codeInsight.highlighting.BraceHighlightingHandler.access$100(BraceHighlightingHandler.java:69)
at com.intellij.codeInsight.highlighting.BraceHighlightingHandler$1$1.compute(BraceHighlightingHandler.java:104)
at com.intellij.codeInsight.highlighting.BraceHighlightingHandler$1$1.compute(BraceHighlightingHandler.java:100)
at com.intellij.openapi.application.impl.ApplicationImpl$11.run(ApplicationImpl.java:818)
at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:790)
at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:816)
at com.intellij.codeInsight.highlighting.BraceHighlightingHandler$1.run(BraceHighlightingHandler.java:100)
at com.intellij.concurrency.JobUtil$3.call(JobUtil.java:133)
at com.intellij.concurrency.JobUtil$3.call(JobUtil.java:130)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at com.intellij.concurrency.PrioritizedFutureTask.access$101(PrioritizedFutureTask.java:31)
at com.intellij.concurrency.PrioritizedFutureTask$1.run(PrioritizedFutureTask.java:70)
at com.intellij.concurrency.PrioritizedFutureTask.run(PrioritizedFutureTask.java:113)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:680)
[ 191937] ERROR - i.impl.compiled.ClsElementImpl - IntelliJ IDEA 10.5.2 Build #IU-107.587
[ 191937] ERROR - i.impl.compiled.ClsElementImpl - JDK: 1.6.0_26
[ 191937] ERROR - i.impl.compiled.ClsElementImpl - VM: Java HotSpot(TM) 64-Bit Server VM
[ 191937] ERROR - i.impl.compiled.ClsElementImpl - Vendor: Apple Inc.
[ 191937] ERROR - i.impl.compiled.ClsElementImpl - OS: Mac OS X
[ 191937] ERROR - i.impl.compiled.ClsElementImpl - Last Action:
Thanx for your attention, it is greatly appreciated.
Hello brice,
You can replace the icon using the iconProvider extension point.
Returing null from obtainLanguageForFile() is not correct in this case and
shouldn't be needed for "attach sources" to work. This is also likely what
causes the exception.
The refactoring done in the master doesn't affect source compatibility, and
as far as I understand binary compatibility should have also been maintained.
--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"
Hi Dmitry
Thx for the quick reply.
Cool for the binary compatibility.
@Override
public Language obtainLanguageForFile(VirtualFile virtualFile) {
return Language.findLanguageByID("JAVA");
}
If I'm always returning Java language then IntelliJ always use my extension point instead of displaying the attached source.
In this case also all icons displayed are the "binary" one instead of the Java/Interface/Annotation/etc icons.
However not returning null indeed it removed the error.
So the question is : how can I check the class file has an attached source ?
Updated the answer, corrected some typo