Export lintState StateField #7
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "main"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
This PR exports the
lintStateStateField, which enables users to lookup the state of that field view theview.state.field(lintState)mechanism.While building out the ability to provide information via tooltips, I ran into an issue with styling the tooltips that come out of the lint function. This discussion thread describes a change that enabled combining all tooltips into a single container, which ensures they don't overlap. However, there is not currently a mechanism for directly intercepting and controlling the content of a lint hover tooltip at the same time as a normal hover tooltip.
My initial approach was to create a new state field and watch the
setDiagnosticsEffect, and then set that state field to the value of the new diagnostics. However, I found that there was a race condition between the time that a hover tooltip started and the time that the state field was updated–I was seeing the hover tooltip called before the state field update.Here's what that code looked like
After doing some additional research, I found that I could do all of the work I needed to do if I had access to the
lintStatefield, via the.fieldaccessor onstate. Rather than duplicating this into its own locally-defined state field, it would be better to simply access the existing one.Because the properties on
lintStateare read-only, it seemed safe to expose this outside of the library.I added documentation, hopefully it is accurate to what the PR is changing.
Could you go into a bit more details about your use case? Exporting this would also require exporting
LintState, as well as the fact that the code stashes the annotations in the decoration marker, so I'd prefer not to do it like this.Right now we have two types of tooltips–informational and error. Informational describes what the token is/does (e.g. if it's a keyword) and error shows if the token is malformed in some way. By default, codemirror displays two separate tooltips (rendered in the same container) for each of these things. My aim is to get diagnostic information out of the editor state in the place where we call
hoverTooltip, and then construct my own custom tooltip that does some particular manipulation of the "information" portion and "error" portion.That's understandable. I explored a couple of options and was wondering what you thought about them
Option 1:
diagnosticListThe idea behind this option is to export a function similar to
diagnosticCountthat would export the diagnostics.Option 2:
diagnosticStateIn this option, you would internally utilize a function like
diagnosticListbut you would be keeping track of the diagnostics in state:In this example, you would use this like
view.state.field(diagnosticState). It probably is less convenient than adiagnosticListfunction, but it might lead to better performance since it only changes the diagnostics when the effect is triggered.In both of these examples, you don't export the internals of
lintStateand there are no additional types to export (sinceDiagnosticis already exported).What do you think?
I worked on this more and discovered I can do this with a custom state field after all. I modified the state field so it looks like this:
I was previously wrapping the logic in a check against
transaction.docChanged, but after looking through the source of this package, I observed that wasn't what is being done for updating the diagnostics. In an ideal world, it would be nice to be able to get at the diagnostic state from the package instead of creating an additional state field, but this is working for now.If you still have interest in exposing a utility like this out of the package, I'm happy to modify this PR with whatever approach you feel is best. However, if you aren't interested in pursuing that idea, feel free to close the PR.
Let's close until other use cases surface, then.
Pull request closed