Compare commits

...

5 Commits

16 changed files with 189 additions and 121 deletions

5
.gitignore vendored
View File

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

5
.idea/.gitignore vendored
View File

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

View File

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

View File

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

View File

@ -1,19 +0,0 @@
<?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>

View File

@ -1,11 +0,0 @@
<?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.1.2"
version = "1.2.0"
repositories {
mavenCentral()
@ -18,9 +18,15 @@ 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 {
@ -36,8 +42,7 @@ tasks.test {
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "17"
kotlinOptions.freeCompilerArgs = listOf(
"-Xjvm-default=enable"
"-Xjvm-default=all"
)
}

View File

@ -0,0 +1,73 @@
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,6 +1,5 @@
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
@ -8,14 +7,14 @@ import com.intellij.openapi.fileEditor.ex.FileEditorWithProvider
import com.intellij.openapi.vfs.VirtualFile
/**
* Listens for newly opened editors, and installs a [LensMarkupModelListener] on them.
* Installs [InspectionLens] in newly opened editors.
*/
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) {
LensMarkupModelListener.install(fileEditor)
InspectionLens.install(fileEditor)
}
}
}

View File

@ -1,45 +1,21 @@
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
/**
* Handles dynamic plugin loading.
*
* On load, it installs the [LensMarkupModelListener] to all open editors.
* On unload, it removes all lenses from all open editors.
* Installs [InspectionLens] in open editors when the plugin is loaded, and uninstalls it when the plugin is unloaded.
*/
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 == PLUGIN_ID) {
ProjectManager.getInstanceIfCreated()?.forEachEditor(LensMarkupModelListener.Companion::install)
if (pluginDescriptor.pluginId.idString == InspectionLens.PLUGIN_ID) {
InspectionLens.install()
}
}
override fun beforePluginUnload(pluginDescriptor: IdeaPluginDescriptor, isUpdate: Boolean) {
if (pluginDescriptor.pluginId.idString == PLUGIN_ID) {
ProjectManager.getInstanceIfCreated()?.forEachEditor {
EditorInlayLensManager.remove(it.editor)
}
if (pluginDescriptor.pluginId.idString == InspectionLens.PLUGIN_ID) {
InspectionLens.uninstall()
}
}
}

View File

@ -0,0 +1,15 @@
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 KEY = Key<EditorInlayLensManager>(EditorInlayLensManager::class.java.name)
private val EDITOR_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(KEY) ?: EditorInlayLensManager(editor).also { editor.putUserData(KEY, it) }
return editor.getUserData(EDITOR_KEY) ?: EditorInlayLensManager(editor).also { editor.putUserData(EDITOR_KEY, it) }
}
fun remove(editor: Editor) {
val manager = editor.getUserData(KEY)
val manager = editor.getUserData(EDITOR_KEY)
if (manager != null) {
manager.hideAll()
editor.putUserData(KEY, null)
editor.putUserData(EDITOR_KEY, null)
}
}
@ -82,8 +82,10 @@ class EditorInlayLensManager private constructor(private val editor: Editor) {
}
fun hideAll() {
executeInInlayBatchMode(inlays.size) { inlays.values.forEach(Inlay<*>::dispose) }
inlays.clear()
if (inlays.isNotEmpty()) {
executeInInlayBatchMode(inlays.size) { inlays.values.forEach(Inlay<*>::dispose) }
inlays.clear()
}
}
private fun getInlayHintPriority(info: HighlightInfo): Int {

View File

@ -1,25 +1,22 @@
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.application.ApplicationManager
import com.intellij.openapi.Disposable
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.fileEditor.TextEditor
import com.intellij.openapi.rd.createLifetime
import com.intellij.openapi.rd.createNestedDisposable
import com.jetbrains.rd.util.lifetime.intersect
import com.intellij.openapi.util.Disposer
import com.intellij.openapi.util.Key
/**
* Listens for inspection highlights and reports them to [EditorInlayLensManager].
*/
class LensMarkupModelListener private constructor(editor: Editor) : MarkupModelListener {
internal class LensMarkupModelListener private constructor(editor: Editor) : MarkupModelListener {
private val lens = EditorInlayLensManager.getOrCreate(editor)
private val showOnDispatchThread = DebouncingInvokeOnDispatchThread(lens::showAll)
@ -51,7 +48,12 @@ class LensMarkupModelListener private constructor(editor: Editor) : MarkupModelL
}
}
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? {
@ -74,25 +76,36 @@ class LensMarkupModelListener private constructor(editor: Editor) : MarkupModelL
}
}
private fun getMarkupModel(editor: Editor): MarkupModelEx? {
return DocumentMarkupModel.forDocument(editor.document, editor.project, false) as? MarkupModelEx
}
/**
* 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.
* Attaches a new [LensMarkupModelListener] to the [Editor], and reports all existing inspection highlights to [EditorInlayLensManager].
*/
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)
}
fun register(editor: Editor, disposable: Disposable) {
if (editor.getUserData(EDITOR_KEY) != null) {
return
}
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,10 +1,14 @@
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.
@ -15,6 +19,8 @@ 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
@ -28,12 +34,30 @@ enum class LensSeverity(baseColor: Color, lightThemeDarkening: Int, darkThemeBri
}
companion object {
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
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)
}
}
}

View File

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

View File

@ -17,6 +17,11 @@
]]></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>
@ -39,6 +44,7 @@
]]></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" />