1
0
mirror of https://github.com/chylex/IntelliJ-Inspection-Lens.git synced 2024-10-18 15:42:47 +02:00

Compare commits

..

No commits in common. "443b2b9a2d7237efb2b331aa3c95e1537a7e0d12" and "44f2fa5c1675bfcd2a08342ff6ef4d206b7b5711" have entirely different histories.

16 changed files with 121 additions and 189 deletions

5
.gitignore vendored
View File

@ -1,5 +1,8 @@
/.idea/*
!/.idea/runConfigurations
!/.idea/compiler.xml
!/.idea/encodings.xml
!/.idea/gradle.xml
!/.idea/vcs.xml
/.gradle/
/build/

5
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/

6
.idea/compiler.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<bytecodeTargetLevel target="11" />
</component>
</project>

6
.idea/encodings.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" defaultCharsetForPropertiesFiles="UTF-8">
<file url="PROJECT" charset="UTF-8" />
</component>
</project>

19
.idea/gradle.xml Normal file
View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GradleMigrationSettings" migrationVersion="1" />
<component name="GradleSettings">
<option name="linkedExternalProjectsSettings">
<GradleProjectSettings>
<option name="delegatedBuild" value="true" />
<option name="testRunner" value="GRADLE" />
<option name="distributionType" value="DEFAULT_WRAPPED" />
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="modules">
<set>
<option value="$PROJECT_DIR$" />
</set>
</option>
</GradleProjectSettings>
</option>
</component>
</project>

11
.idea/vcs.xml Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GitSharedSettings">
<option name="FORCE_PUSH_PROHIBITED_PATTERNS">
<list />
</option>
</component>
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View File

@ -8,7 +8,7 @@ plugins {
}
group = "com.chylex.intellij.inspectionlens"
version = "1.2.0"
version = "1.1.2"
repositories {
mavenCentral()
@ -18,15 +18,9 @@ dependencies {
testImplementation("org.junit.jupiter:junit-jupiter:5.9.0")
}
kotlin {
jvmToolchain(17)
}
intellij {
version.set("2023.1")
updateSinceUntilBuild.set(false)
plugins.add("tanvd.grazi")
}
tasks.patchPluginXml {
@ -42,7 +36,8 @@ tasks.test {
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "17"
kotlinOptions.freeCompilerArgs = listOf(
"-Xjvm-default=all"
"-Xjvm-default=enable"
)
}

View File

@ -1,73 +0,0 @@
package com.chylex.intellij.inspectionlens
import com.chylex.intellij.inspectionlens.editor.EditorInlayLensManager
import com.chylex.intellij.inspectionlens.editor.LensMarkupModelListener
import com.intellij.openapi.Disposable
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.fileEditor.TextEditor
import com.intellij.openapi.project.ProjectManager
import com.intellij.openapi.rd.createLifetime
import com.intellij.openapi.rd.createNestedDisposable
import com.jetbrains.rd.util.lifetime.intersect
/**
* Handles installation and uninstallation of plugin features in editors.
*/
internal object InspectionLens {
const val PLUGIN_ID = "com.chylex.intellij.inspectionlens"
/**
* Installs lenses into [editor].
*/
fun install(editor: TextEditor) {
LensMarkupModelListener.register(editor.editor, createEditorDisposable(editor))
}
/**
* Installs lenses into all open editors.
*/
fun install() {
forEachOpenEditor(::install)
}
/**
* Uninstalls lenses from all open editors.
*/
fun uninstall() {
forEachOpenEditor {
EditorInlayLensManager.remove(it.editor)
}
}
/**
* Refreshes lenses in all open editors.
*/
fun refresh() {
forEachOpenEditor {
LensMarkupModelListener.refresh(it.editor)
}
}
/**
* Creates a [Disposable] that will be disposed when either the [TextEditor] is disposed or the plugin is unloaded.
*/
private fun createEditorDisposable(textEditor: TextEditor): Disposable {
val pluginLifetime = ApplicationManager.getApplication().getService(InspectionLensPluginDisposableService::class.java).createLifetime()
val editorLifetime = textEditor.createLifetime()
return pluginLifetime.intersect(editorLifetime).createNestedDisposable()
}
/**
* Executes [action] on all open editors.
*/
private inline fun forEachOpenEditor(action: (TextEditor) -> Unit) {
val projectManager = ProjectManager.getInstanceIfCreated() ?: return
for (project in projectManager.openProjects.filterNot { it.isDisposed }) {
for (editor in FileEditorManager.getInstance(project).allEditors.filterIsInstance<TextEditor>()) {
action(editor)
}
}
}
}

View File

@ -1,5 +1,6 @@
package com.chylex.intellij.inspectionlens
import com.chylex.intellij.inspectionlens.editor.LensMarkupModelListener
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.fileEditor.FileOpenedSyncListener
import com.intellij.openapi.fileEditor.TextEditor
@ -7,14 +8,14 @@ import com.intellij.openapi.fileEditor.ex.FileEditorWithProvider
import com.intellij.openapi.vfs.VirtualFile
/**
* Installs [InspectionLens] in newly opened editors.
* Listens for newly opened editors, and installs a [LensMarkupModelListener] on them.
*/
class InspectionLensFileOpenedListener : FileOpenedSyncListener {
override fun fileOpenedSync(source: FileEditorManager, file: VirtualFile, editorsWithProviders: List<FileEditorWithProvider>) {
for (editorWrapper in editorsWithProviders) {
val fileEditor = editorWrapper.fileEditor
if (fileEditor is TextEditor) {
InspectionLens.install(fileEditor)
LensMarkupModelListener.install(fileEditor)
}
}
}

View File

@ -1,21 +1,45 @@
package com.chylex.intellij.inspectionlens
import com.chylex.intellij.inspectionlens.editor.EditorInlayLensManager
import com.chylex.intellij.inspectionlens.editor.LensMarkupModelListener
import com.intellij.ide.plugins.DynamicPluginListener
import com.intellij.ide.plugins.IdeaPluginDescriptor
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.fileEditor.TextEditor
import com.intellij.openapi.project.ProjectManager
/**
* Installs [InspectionLens] in open editors when the plugin is loaded, and uninstalls it when the plugin is unloaded.
* Handles dynamic plugin loading.
*
* On load, it installs the [LensMarkupModelListener] to all open editors.
* On unload, it removes all lenses from all open editors.
*/
class InspectionLensPluginListener : DynamicPluginListener {
companion object {
private const val PLUGIN_ID = "com.chylex.intellij.inspectionlens"
private inline fun ProjectManager.forEachEditor(action: (TextEditor) -> Unit) {
for (project in this.openProjects.filterNot { it.isDisposed }) {
val fileEditorManager = FileEditorManager.getInstance(project)
for (editor in fileEditorManager.allEditors.filterIsInstance<TextEditor>()) {
action(editor)
}
}
}
}
override fun pluginLoaded(pluginDescriptor: IdeaPluginDescriptor) {
if (pluginDescriptor.pluginId.idString == InspectionLens.PLUGIN_ID) {
InspectionLens.install()
if (pluginDescriptor.pluginId.idString == PLUGIN_ID) {
ProjectManager.getInstanceIfCreated()?.forEachEditor(LensMarkupModelListener.Companion::install)
}
}
override fun beforePluginUnload(pluginDescriptor: IdeaPluginDescriptor, isUpdate: Boolean) {
if (pluginDescriptor.pluginId.idString == InspectionLens.PLUGIN_ID) {
InspectionLens.uninstall()
if (pluginDescriptor.pluginId.idString == PLUGIN_ID) {
ProjectManager.getInstanceIfCreated()?.forEachEditor {
EditorInlayLensManager.remove(it.editor)
}
}
}
}

View File

@ -1,15 +0,0 @@
package com.chylex.intellij.inspectionlens.compatibility
import com.chylex.intellij.inspectionlens.editor.LensSeverity
import com.intellij.grazie.ide.TextProblemSeverities
import com.intellij.openapi.project.Project
import com.intellij.openapi.startup.ProjectActivity
class GrazieSupport : ProjectActivity {
override suspend fun execute(project: Project) {
LensSeverity.registerMapping(TextProblemSeverities.GRAMMAR_ERROR, LensSeverity.ERROR)
LensSeverity.registerMapping(TextProblemSeverities.STYLE_ERROR, LensSeverity.GRAZIE)
LensSeverity.registerMapping(TextProblemSeverities.STYLE_WARNING, LensSeverity.GRAZIE)
LensSeverity.registerMapping(TextProblemSeverities.STYLE_SUGGESTION, LensSeverity.GRAZIE)
}
}

View File

@ -12,7 +12,7 @@ import com.intellij.openapi.util.Key
*/
class EditorInlayLensManager private constructor(private val editor: Editor) {
companion object {
private val EDITOR_KEY = Key<EditorInlayLensManager>(EditorInlayLensManager::class.java.name)
private val KEY = Key<EditorInlayLensManager>(EditorInlayLensManager::class.java.name)
/**
* Highest allowed severity for the purposes of sorting multiple highlights at the same offset.
@ -22,14 +22,14 @@ class EditorInlayLensManager private constructor(private val editor: Editor) {
private const val MAXIMUM_POSITION = ((Int.MAX_VALUE / MAXIMUM_SEVERITY) * 2) - 1
fun getOrCreate(editor: Editor): EditorInlayLensManager {
return editor.getUserData(EDITOR_KEY) ?: EditorInlayLensManager(editor).also { editor.putUserData(EDITOR_KEY, it) }
return editor.getUserData(KEY) ?: EditorInlayLensManager(editor).also { editor.putUserData(KEY, it) }
}
fun remove(editor: Editor) {
val manager = editor.getUserData(EDITOR_KEY)
val manager = editor.getUserData(KEY)
if (manager != null) {
manager.hideAll()
editor.putUserData(EDITOR_KEY, null)
editor.putUserData(KEY, null)
}
}
@ -82,10 +82,8 @@ class EditorInlayLensManager private constructor(private val editor: Editor) {
}
fun hideAll() {
if (inlays.isNotEmpty()) {
executeInInlayBatchMode(inlays.size) { inlays.values.forEach(Inlay<*>::dispose) }
inlays.clear()
}
executeInInlayBatchMode(inlays.size) { inlays.values.forEach(Inlay<*>::dispose) }
inlays.clear()
}
private fun getInlayHintPriority(info: HighlightInfo): Int {

View File

@ -1,22 +1,25 @@
package com.chylex.intellij.inspectionlens.editor
import com.chylex.intellij.inspectionlens.InspectionLensPluginDisposableService
import com.chylex.intellij.inspectionlens.utils.DebouncingInvokeOnDispatchThread
import com.intellij.codeInsight.daemon.impl.HighlightInfo
import com.intellij.lang.annotation.HighlightSeverity
import com.intellij.openapi.Disposable
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.ex.MarkupModelEx
import com.intellij.openapi.editor.ex.RangeHighlighterEx
import com.intellij.openapi.editor.impl.DocumentMarkupModel
import com.intellij.openapi.editor.impl.event.MarkupModelListener
import com.intellij.openapi.editor.markup.RangeHighlighter
import com.intellij.openapi.util.Disposer
import com.intellij.openapi.util.Key
import com.intellij.openapi.fileEditor.TextEditor
import com.intellij.openapi.rd.createLifetime
import com.intellij.openapi.rd.createNestedDisposable
import com.jetbrains.rd.util.lifetime.intersect
/**
* Listens for inspection highlights and reports them to [EditorInlayLensManager].
*/
internal class LensMarkupModelListener private constructor(editor: Editor) : MarkupModelListener {
class LensMarkupModelListener private constructor(editor: Editor) : MarkupModelListener {
private val lens = EditorInlayLensManager.getOrCreate(editor)
private val showOnDispatchThread = DebouncingInvokeOnDispatchThread(lens::showAll)
@ -48,12 +51,7 @@ internal class LensMarkupModelListener private constructor(editor: Editor) : Mar
}
}
private fun showAllValid(highlighters: Array<RangeHighlighter>) {
highlighters.forEach(::showIfValid)
}
companion object {
private val EDITOR_KEY = Key<LensMarkupModelListener>(LensMarkupModelListener::class.java.name)
private val MINIMUM_SEVERITY = HighlightSeverity.TEXT_ATTRIBUTES.myVal + 1
private fun getFilteredHighlightInfo(highlighter: RangeHighlighter): HighlightInfo? {
@ -76,36 +74,25 @@ internal class LensMarkupModelListener private constructor(editor: Editor) : Mar
}
}
private fun getMarkupModel(editor: Editor): MarkupModelEx? {
return DocumentMarkupModel.forDocument(editor.document, editor.project, false) as? MarkupModelEx
}
/**
* Attaches a new [LensMarkupModelListener] to the [Editor], and reports all existing inspection highlights to [EditorInlayLensManager].
* Attaches a new [LensMarkupModelListener] to the document model of the provided [TextEditor], and reports all existing inspection highlights to [EditorInlayLensManager].
*
* The [LensMarkupModelListener] will be disposed when either the [TextEditor] is disposed, or via [InspectionLensPluginDisposableService] when the plugin is unloaded.
*/
fun register(editor: Editor, disposable: Disposable) {
if (editor.getUserData(EDITOR_KEY) != null) {
return
fun install(textEditor: TextEditor) {
val editor = textEditor.editor
val markupModel = DocumentMarkupModel.forDocument(editor.document, editor.project, false)
if (markupModel is MarkupModelEx) {
val pluginLifetime = ApplicationManager.getApplication().getService(InspectionLensPluginDisposableService::class.java).createLifetime()
val editorLifetime = textEditor.createLifetime()
val listener = LensMarkupModelListener(editor)
markupModel.addMarkupModelListener(pluginLifetime.intersect(editorLifetime).createNestedDisposable(), listener)
for (highlighter in markupModel.allHighlighters) {
listener.showIfValid(highlighter)
}
}
val markupModel = getMarkupModel(editor) ?: return
val listener = LensMarkupModelListener(editor)
editor.putUserData(EDITOR_KEY, listener)
Disposer.register(disposable) { editor.putUserData(EDITOR_KEY, null) }
markupModel.addMarkupModelListener(disposable, listener)
listener.showAllValid(markupModel.allHighlighters)
}
/**
* Recreates all inspection highlights in the [Editor].
*/
fun refresh(editor: Editor) {
val listener = editor.getUserData(EDITOR_KEY) ?: return
val markupModel = getMarkupModel(editor) ?: return
listener.showAllValid(markupModel.allHighlighters)
}
}
}

View File

@ -1,14 +1,10 @@
package com.chylex.intellij.inspectionlens.editor
import com.chylex.intellij.inspectionlens.InspectionLens
import com.chylex.intellij.inspectionlens.utils.DebouncingInvokeOnDispatchThread
import com.intellij.lang.annotation.HighlightSeverity
import com.intellij.spellchecker.SpellCheckerSeveritiesProvider
import com.intellij.ui.ColorUtil
import com.intellij.ui.JBColor
import java.awt.Color
import java.awt.Font
import java.util.Collections
/**
* Determines properties of inspection lenses based on severity.
@ -19,8 +15,6 @@ enum class LensSeverity(baseColor: Color, lightThemeDarkening: Int, darkThemeBri
WARNING (Color(190, 145, 23), lightThemeDarkening = 4, darkThemeBrightening = 1),
WEAK_WARNING (Color(117, 109, 86), lightThemeDarkening = 3, darkThemeBrightening = 3),
SERVER_PROBLEM (Color(176, 97, 0), lightThemeDarkening = 4, darkThemeBrightening = 2),
GRAZIE (Color( 53, 146, 196), lightThemeDarkening = 2, darkThemeBrightening = 1),
TYPO (Color( 73, 156, 84), lightThemeDarkening = 3, darkThemeBrightening = 1),
OTHER (Color(128, 128, 128), lightThemeDarkening = 1, darkThemeBrightening = 2);
val colorAttributes: LensSeverityTextAttributes
@ -34,30 +28,12 @@ enum class LensSeverity(baseColor: Color, lightThemeDarkening: Int, darkThemeBri
}
companion object {
private val mapping = Collections.synchronizedMap(mapOf(
HighlightSeverity.ERROR to ERROR,
HighlightSeverity.WARNING to WARNING,
HighlightSeverity.WEAK_WARNING to WEAK_WARNING,
HighlightSeverity.GENERIC_SERVER_ERROR_OR_WARNING to SERVER_PROBLEM,
SpellCheckerSeveritiesProvider.TYPO to TYPO,
))
private val refreshLater = DebouncingInvokeOnDispatchThread<Unit> { InspectionLens.refresh() }
/**
* Registers a mapping from a [HighlightSeverity] to a [LensSeverity], and refreshes all open editors.
*/
internal fun registerMapping(severity: HighlightSeverity, lensSeverity: LensSeverity) {
if (mapping.put(severity, lensSeverity) != lensSeverity) {
refreshLater.enqueue(Unit)
}
}
/**
* Returns the [LensSeverity] associated with the [HighlightSeverity], or [OTHER] if there no explicit mapping is found.
*/
fun from(severity: HighlightSeverity): LensSeverity {
return mapping.getOrDefault(severity, OTHER)
fun from(severity: HighlightSeverity) = when (severity) {
HighlightSeverity.ERROR -> ERROR
HighlightSeverity.WARNING -> WARNING
HighlightSeverity.WEAK_WARNING -> WEAK_WARNING
HighlightSeverity.GENERIC_SERVER_ERROR_OR_WARNING -> SERVER_PROBLEM
else -> OTHER
}
}
}

View File

@ -1,5 +0,0 @@
<idea-plugin>
<extensions defaultExtensionNs="com.intellij">
<postStartupActivity implementation="com.chylex.intellij.inspectionlens.compatibility.GrazieSupport" />
</extensions>
</idea-plugin>

View File

@ -17,11 +17,6 @@
]]></description>
<change-notes><![CDATA[
<b>Version 1.2.0</b>
<ul>
<li>Support for IntelliJ 2023.2 EAP.</li>
<li>Added distinct colors for typos and Grazie inspections.</li>
</ul>
<b>Version 1.1.2</b>
<ul>
<li>Added plugin icon.</li>
@ -44,7 +39,6 @@
]]></change-notes>
<depends>com.intellij.modules.platform</depends>
<depends optional="true" config-file="compatibility/InspectionLens-Grazie.xml">tanvd.grazi</depends>
<extensions defaultExtensionNs="com.intellij">
<applicationService serviceImplementation="com.chylex.intellij.inspectionlens.InspectionLensPluginDisposableService" />