clutcher / bh

Issue tracker for Better Highlights Intellij IDEA plugin
6 stars 0 forks source link

Automatic Verification of Links #97

Closed mgroth0 closed 4 months ago

mgroth0 commented 5 months ago

This plugin has great potential and I am inspired to create links throughout my code. However, I have started to realize what my main concern is, which is holding me back. And that is my fear of links breaking.

In general, I know this plugin tries to support refactoring. And even if refactoring generally works, the fundamental problem is that if for any reason a link breaks, there is no way to know about it until one day you look at a comment and see // [[SomeClass]] is not clickable. No matter how good this plugin gets at refactoring support, there is no way to prevent this fully. Sometimes, SomeClass might just be deleted, moved, or renamed with some other tool instead of the IDE.

My fear might seem overly-anxious to some. But if one day in 10 years I come upon a link from this plugin that doesn't seem to work or point to anything, I know it will disrupt my workflow. So if I'm trying to write stable, lasting code, I am currently a bit too afraid to incorporate Better Highlights.

What would feel great is if this plugin offered a mechanism to "verify" all links in a project. This would report any links that are not resolving.

Ideally, this verification process will be something that can be triggered programmatically. I know that is tricky since the process will execute inside the IntelliJ Plugin. At least providing a public API so that other IntelliJ plugins that depend on this plugin can use to programmatically trigger this verification and return a list of reported issues (along with file and line numbers) would be great.

And for users who don't need to trigger the verification programmatically, I think it would be a perfect to make it a regular IDE inspection.

mgroth0 commented 5 months ago

Changed name from "Code Links" to "Links" because the verification can be for any type of link: tags, files, code etc

clutcher commented 5 months ago

@mgroth0 Previous week was my first time, when I personally hit that problem so have been thinking about solution since then. And here come you with a good idea)

I'll take a look into inspections API of intellij to understand what it provides and how I can use it to resolve problem with unexiating links.

mgroth0 commented 5 months ago

Awesome. Just to give you a heads up, I will be trying to integrate this programmatically so that I can generate a SARIF report.

I found this thread where people are discussing triggering and getting results from an inspection from another plugin. Hopefully I could be able to do something like this, and hopefully that does not require additional work on your end.

If that doesn't work, I might request from you a small API.

clutcher commented 4 months ago

@mgroth0 Most probably would release next month something based on code inspection api

clutcher commented 4 months ago

@mgroth0 You can try latest verion 2024.1.8. I added inspection.

mgroth0 commented 4 months ago

Nice! It is running right now, and is already catching a bunch of broken links.

It is pretty slow. I do have a huge project, but at this rate it looks like the whole inspection will take 10 minutes.

mgroth0 commented 4 months ago

Actually, it just finished! Took less than 5 minutes.

The progress bar was off. It suddenly finished when the progress bar was at about 25%.

mgroth0 commented 4 months ago

This is awesome. I still need to find a way to programmatically trigger this, but this is more of a general IntelliJ issue and not your responsibility. Feel free to close this

clutcher commented 4 months ago

@mgroth0 superb. Will close this one.

mgroth0 commented 4 months ago

By the way, I figured out my programatic hook:

fun PsiFile.checkWikiLinks(project: Project) {
    InspectionEngine.runInspectionOnFile(
        this,
        InspectionProfileManager
            .getInstance()
            .getProfile("Better Highlights")
            .tools.single {
                "Wikilink" in it.tool.groupDisplayName
            }
            .tool,
        InspectionManager.getInstance(project).createNewGlobalContext()
    )
}

That's missing a couple of small details and not exactly what I have in my application, but hopefully it can serve as a reference to anyone who needs it.