AnnotataionHintsPass: make it extends ElementProcessingHintPass

This commit is contained in:
Roman.Ivanov
2018-03-30 12:51:08 +07:00
parent 0835528f5a
commit 4247fdf1d0
5 changed files with 23 additions and 53 deletions
@@ -23,6 +23,6 @@ class AnnotationHintsConfigurable : BeanConfigurable<CodeInsightSettings>(CodeIn
override fun apply() {
super.apply()
AnnotationHintsPassFactory.forceHintsUpdateOnNextPass()
AnnotationHintsPassFactory.modificationStampHolder.forceHintsUpdateOnNextPass()
}
}
@@ -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<MutableList<HintData>>()
private val traverser: SyntaxTraverser<PsiElement> = 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<PsiAnnotation>()
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<Boolean>("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()
}
}
}
@@ -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<Long>("LAST_PASS_MODIFICATION_TIMESTAMP")
fun putCurrentModificationStamp(editor: Editor, file: PsiFile) {
editor.putUserData<Long>(LAST_PASS_MODIFICATION_TIMESTAMP, ParameterHintsPassFactory.getCurrentModificationStamp(file))
}
private fun forceHintsUpdateOnNextPass(editor: Editor) {
editor.putUserData<Long>(LAST_PASS_MODIFICATION_TIMESTAMP, null)
}
fun forceHintsUpdateOnNextPass() {
EditorFactory.getInstance().allEditors.forEach { forceHintsUpdateOnNextPass(it) }
}
val modificationStampHolder = ModificationStampHolder(Key.create<Long>("LAST_PASS_MODIFICATION_TIMESTAMP"))
}
}
@@ -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()
}
}
@@ -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)
}