From 4247fdf1d0e72c31cba04e361735c20a43fc85fc Mon Sep 17 00:00:00 2001 From: "Roman.Ivanov" Date: Fri, 30 Mar 2018 12:51:08 +0700 Subject: [PATCH] AnnotataionHintsPass: make it extends ElementProcessingHintPass --- .../hints/AnnotationHintsConfigurable.kt | 2 +- .../codeInsight/hints/AnnotationHintsPass.kt | 51 +++++++------------ .../hints/AnnotationHintsPassFactory.kt | 19 ++----- .../hints/MethodChainHintTurningAction.kt | 2 +- .../hints/MethodChainHintsPassFactory.kt | 2 +- 5 files changed, 23 insertions(+), 53 deletions(-) diff --git a/java/java-impl/src/com/intellij/codeInsight/hints/AnnotationHintsConfigurable.kt b/java/java-impl/src/com/intellij/codeInsight/hints/AnnotationHintsConfigurable.kt index b296e5d6ed72..41eaedf4c2ad 100644 --- a/java/java-impl/src/com/intellij/codeInsight/hints/AnnotationHintsConfigurable.kt +++ b/java/java-impl/src/com/intellij/codeInsight/hints/AnnotationHintsConfigurable.kt @@ -23,6 +23,6 @@ class AnnotationHintsConfigurable : BeanConfigurable(CodeIn override fun apply() { super.apply() - AnnotationHintsPassFactory.forceHintsUpdateOnNextPass() + AnnotationHintsPassFactory.modificationStampHolder.forceHintsUpdateOnNextPass() } } diff --git a/java/java-impl/src/com/intellij/codeInsight/hints/AnnotationHintsPass.kt b/java/java-impl/src/com/intellij/codeInsight/hints/AnnotationHintsPass.kt index 821d3c30f558..de568b18117a 100644 --- a/java/java-impl/src/com/intellij/codeInsight/hints/AnnotationHintsPass.kt +++ b/java/java-impl/src/com/intellij/codeInsight/hints/AnnotationHintsPass.kt @@ -1,7 +1,6 @@ // Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.codeInsight.hints -import com.intellij.codeHighlighting.EditorBoundHighlightingPass import com.intellij.codeInsight.CodeInsightSettings import com.intellij.codeInsight.ExternalAnnotationsManager import com.intellij.codeInsight.InferredAnnotationsManager @@ -11,39 +10,27 @@ import com.intellij.openapi.actionSystem.AnActionEvent import com.intellij.openapi.actionSystem.ToggleAction import com.intellij.openapi.editor.Editor import com.intellij.openapi.editor.Inlay -import com.intellij.openapi.editor.ex.util.CaretVisualPositionKeeper -import com.intellij.openapi.progress.ProgressIndicator import com.intellij.openapi.util.Disposer import com.intellij.openapi.util.Key +import com.intellij.openapi.vfs.VirtualFile import com.intellij.psi.PsiAnnotation import com.intellij.psi.PsiElement import com.intellij.psi.PsiModifierListOwner -import com.intellij.psi.SyntaxTraverser import com.intellij.util.DocumentUtil import gnu.trove.TIntObjectHashMap -class AnnotationHintsPass(private val rootElement: PsiElement, editor: Editor) : EditorBoundHighlightingPass(editor, - rootElement.containingFile, - true) { +class AnnotationHintsPass( + rootElement: PsiElement, editor: Editor, + override val modificationStampHolder: ModificationStampHolder +) : ElementProcessingHintPass(rootElement, editor) { private val hints = TIntObjectHashMap>() - private val traverser: SyntaxTraverser = SyntaxTraverser.psiTraverser(rootElement) - override fun doCollectInformation(progress: ProgressIndicator) { - assert(myDocument != null) - hints.clear() + override fun isAvailable(virtualFile: VirtualFile): Boolean = + (CodeInsightSettings.getInstance().SHOW_EXTERNAL_ANNOTATIONS_INLINE + && ExternalAnnotationsManager.getInstance(myProject).hasAnnotationRootsForFile(virtualFile)) + || CodeInsightSettings.getInstance().SHOW_INFERRED_ANNOTATIONS_INLINE - val virtualFile = rootElement.containingFile?.originalFile?.virtualFile - - if ((CodeInsightSettings.getInstance().SHOW_EXTERNAL_ANNOTATIONS_INLINE && - virtualFile != null && - ExternalAnnotationsManager.getInstance(myProject).hasAnnotationRootsForFile(virtualFile)) - || - CodeInsightSettings.getInstance().SHOW_INFERRED_ANNOTATIONS_INLINE) { - traverser.forEach { process(it) } - } - } - - private fun process(element: PsiElement) { + override fun collectElementHints(element: PsiElement) { if (element is PsiModifierListOwner) { var annotations = emptySequence() if (CodeInsightSettings.getInstance().SHOW_EXTERNAL_ANNOTATIONS_INLINE) { @@ -70,9 +57,7 @@ class AnnotationHintsPass(private val rootElement: PsiElement, editor: Editor) : } } - override fun doApplyInformationToEditor() { - val keeper = CaretVisualPositionKeeper(myEditor) - + override fun applyHintsToEditor() { val inlayModel = myEditor.inlayModel val toRemove = inlayModel.getInlineElementsInRange(rootElement.textRange.startOffset + 1, rootElement.textRange.endOffset - 1) .filter { ANNOTATION_INLAY_KEY.isIn(it) } @@ -93,12 +78,6 @@ class AnnotationHintsPass(private val rootElement: PsiElement, editor: Editor) : true } } - - keeper.restoreOriginalLocation(false) - - if (rootElement === myFile) { - AnnotationHintsPassFactory.putCurrentModificationStamp(myEditor, myFile) - } } data class HintData(val presentationText: String) @@ -107,6 +86,10 @@ class AnnotationHintsPass(private val rootElement: PsiElement, editor: Editor) : private val ANNOTATION_INLAY_KEY = Key.create("ANNOTATION_INLAY_KEY") } + override fun clearCollected() { + hints.clear() + } + private class AnnotationHintRenderer(text: String) : HintRenderer(text) { override fun getContextMenuGroupId() = "AnnotationHintsContextMenu" } @@ -116,7 +99,7 @@ class AnnotationHintsPass(private val rootElement: PsiElement, editor: Editor) : override fun setSelected(e: AnActionEvent?, state: Boolean) { CodeInsightSettings.getInstance().SHOW_EXTERNAL_ANNOTATIONS_INLINE = state - AnnotationHintsPassFactory.forceHintsUpdateOnNextPass() + AnnotationHintsPassFactory.modificationStampHolder.forceHintsUpdateOnNextPass() } } @@ -125,7 +108,7 @@ class AnnotationHintsPass(private val rootElement: PsiElement, editor: Editor) : override fun setSelected(e: AnActionEvent?, state: Boolean) { CodeInsightSettings.getInstance().SHOW_INFERRED_ANNOTATIONS_INLINE = state - AnnotationHintsPassFactory.forceHintsUpdateOnNextPass() + AnnotationHintsPassFactory.modificationStampHolder.forceHintsUpdateOnNextPass() } } } \ No newline at end of file diff --git a/java/java-impl/src/com/intellij/codeInsight/hints/AnnotationHintsPassFactory.kt b/java/java-impl/src/com/intellij/codeInsight/hints/AnnotationHintsPassFactory.kt index 6fba56eaf974..a38eedfa1e18 100644 --- a/java/java-impl/src/com/intellij/codeInsight/hints/AnnotationHintsPassFactory.kt +++ b/java/java-impl/src/com/intellij/codeInsight/hints/AnnotationHintsPassFactory.kt @@ -6,7 +6,6 @@ import com.intellij.codeHighlighting.TextEditorHighlightingPassFactory import com.intellij.codeHighlighting.TextEditorHighlightingPassRegistrar import com.intellij.openapi.components.AbstractProjectComponent import com.intellij.openapi.editor.Editor -import com.intellij.openapi.editor.EditorFactory import com.intellij.openapi.project.Project import com.intellij.openapi.util.Key import com.intellij.psi.PsiFile @@ -21,23 +20,11 @@ class AnnotationHintsPassFactory(project: Project, registrar: TextEditorHighligh override fun createHighlightingPass(file: PsiFile, editor: Editor): TextEditorHighlightingPass? { if (editor.isOneLineMode || file !is PsiJavaFile || - LAST_PASS_MODIFICATION_TIMESTAMP.get(editor, 0) == ParameterHintsPassFactory.getCurrentModificationStamp(file)) return null - return AnnotationHintsPass(file, editor) + modificationStampHolder.key.get(editor, 0) == ParameterHintsPassFactory.getCurrentModificationStamp(file)) return null + return AnnotationHintsPass(file, editor, modificationStampHolder) } companion object { - val LAST_PASS_MODIFICATION_TIMESTAMP = Key.create("LAST_PASS_MODIFICATION_TIMESTAMP") - - fun putCurrentModificationStamp(editor: Editor, file: PsiFile) { - editor.putUserData(LAST_PASS_MODIFICATION_TIMESTAMP, ParameterHintsPassFactory.getCurrentModificationStamp(file)) - } - - private fun forceHintsUpdateOnNextPass(editor: Editor) { - editor.putUserData(LAST_PASS_MODIFICATION_TIMESTAMP, null) - } - - fun forceHintsUpdateOnNextPass() { - EditorFactory.getInstance().allEditors.forEach { forceHintsUpdateOnNextPass(it) } - } + val modificationStampHolder = ModificationStampHolder(Key.create("LAST_PASS_MODIFICATION_TIMESTAMP")) } } \ No newline at end of file diff --git a/java/java-impl/src/com/intellij/codeInsight/hints/MethodChainHintTurningAction.kt b/java/java-impl/src/com/intellij/codeInsight/hints/MethodChainHintTurningAction.kt index dc9915ff0c51..6c0be2fb77a7 100644 --- a/java/java-impl/src/com/intellij/codeInsight/hints/MethodChainHintTurningAction.kt +++ b/java/java-impl/src/com/intellij/codeInsight/hints/MethodChainHintTurningAction.kt @@ -12,7 +12,7 @@ class MethodChainHintTurningAction : ToggleAction() { override fun setSelected(e: AnActionEvent?, state: Boolean) { CodeInsightSettings.getInstance().SHOW_METHOD_CHAIN_TYPES_INLINE = state - AnnotationHintsPassFactory.forceHintsUpdateOnNextPass() + AnnotationHintsPassFactory.modificationStampHolder.forceHintsUpdateOnNextPass() } } \ No newline at end of file diff --git a/java/java-impl/src/com/intellij/codeInsight/hints/MethodChainHintsPassFactory.kt b/java/java-impl/src/com/intellij/codeInsight/hints/MethodChainHintsPassFactory.kt index 56a1b9d99ea6..224135e1c5d3 100644 --- a/java/java-impl/src/com/intellij/codeInsight/hints/MethodChainHintsPassFactory.kt +++ b/java/java-impl/src/com/intellij/codeInsight/hints/MethodChainHintsPassFactory.kt @@ -21,7 +21,7 @@ class MethodChainHintsPassFactory(project: Project, registrar: TextEditorHighlig override fun createHighlightingPass(file: PsiFile, editor: Editor): TextEditorHighlightingPass? { if (editor.isOneLineMode || file !is PsiJavaFile - || AnnotationHintsPassFactory.LAST_PASS_MODIFICATION_TIMESTAMP.get(editor, 0) == ParameterHintsPassFactory.getCurrentModificationStamp(file)) return null + || modificationStampHolder.key.get(editor, 0) == ParameterHintsPassFactory.getCurrentModificationStamp(file)) return null return MethodChainHintsPass(modificationStampHolder, file, editor) }