If I want a plugin to cache a remotely-fetched file for later quick access, how might I do that?

Is their an existing API for file caching, one that a plugin could use to stash a remotely-acquired resource for quick access?

Something automatically maintained by the IDEA environment, so that a plugin doesn’t have to worry about choosing the directory path for where cached files should be stored?

There’s VirtualFileManager.findFileByUrl(), which eventually delegates to HttpFileSystemBase.findFileByPath().

(Assuming by “remote”, you mean “over the web”.)

I can’t seem to get this to work

  latestJS = VfsUtil.loadText(VirtualFileManager.getInstance()
    .findFileByUrl("https://unpkg.com/@highlightjs/cdn-assets/highlight.min.js")!!)

I needed to add getInstance(), by the way, but that only made the code compile, not work correctly.

A simple:

 URL("https://unpkg.com/@highlightjs/cdn-assets/highlight.min.js").readText()

…does work, but I don’t know if that provides any caching or not.

VirtualFileManager.getInstance()
    .findFileByUrl("https://unpkg.com/@highlightjs/cdn-assets/highlight.min.js")

…by itself returns a non-null result, but the debugger says the local file is null, and VfsUtil.loadText fails with an NPE.

IIRC the results is cached in a directory called httpFileSystem. I’m not sure if it’s part of the contract though. For the record, there are a couple of related bug reports on YouTrack, like this one.

Highlight.js in particular is very stable (11.9.0 was released in October 2023, 11.10.0 in July 2024, 11.11.0 and 11.11.1 in December 2024), and the minified file is just over 120 KB long. You could bundle it with your plugin. PropertiesComponent is also a choice, though probably not recommended.

I’ve actually done both things, bundling highlight.js and trying to pull down the latest version, using the bundled version as a fallback in case loading the latest fails (or is too slow loading).

Working with highlight.js so much lately, trying it out with many languages, has actually lead me to finding several bugs, so I expect to be trying to contribute a few fixes to that myself that I’ll want to have automatically download.

I was easily able to fix one CSS bug (not recognizing unicode-range, and then getting ugly highlighting only the decimal digits in the hex ranges that follow), but some parts of that code base are hard for a newcomer like me to get my head around.

At any rate, I guess I’ll stick with URL...readText() for now.