I’ve been trying to figure out how to differentiate between the cursor being positioned at the end of a hanging wrap point vs being at start of the wrapped line. As far as I can tell, there is no clear way to differentiate these two states with CodeMirror, but I’m hoping that I’m missing something.
For instance, if you have the following text, and imagine it’s all one line which is wrapped at the hyphen (most commonly the wrap point would be a space, but seems clearer here to use a hyphen):
Top of wrapped line-
wrapped part of the same line
If I click the empty space after the hyphen, the cursor appears on the top line following the hyphen. If I click in front of the “w” on the wrapped line, the cursor appears there. These are two visually separate places, and I have a use case where I need to know which of these locations the cursor is in.
However, because they’re technically the same character location, CodeMirror seems to treat them completely identically, with no way to determine which point you’re at. I tried looking into the SelectionRange, but even its assoc property does not reliably differentiate these two positions (sometimes it will be correct if you click, with an assoc of 0 when you’re at the top point and 1 when you’re at the bottom point, but if you navigate using the arrow keys then it will have assoc of 0 for both positions).
coordsAtChar does not help either, although that’s somewhat to be expected I suppose since this is more an issue of cursor location than character position.
Does anyone have thoughts on how I could solve this? I am using the native cursor rather than CodeMirror’s mimicked cursor, and would like to keep it that way.
Also fwiw I submitted a bug on GitHub today as well on a similar topic, but this question is not related to that bug. I’m not sure this one is a bug as much as just something I’m missing how to do.
Sorry to drop two things on you at once though, I appreciate all the amazing work you do on CM!
The assoc field of a cursor will tell you whether it is associated with the character after or before it (or neither, which is also possible, when the code creating it didn’t specify).
Hmm, okay so that fixed the issue when the cursor position is set by clicking the mouse to the right of the hanging wrap point. assoc is correctly set to -1 in that case, or set to 1 if I click at the start of the next line. That case is good now, much thanks.
However, if I navigate to the same places using the arrow keys then assoc is always 0. For instance, starting somewhere in the top line and pressing Cmd-ArrowRight moves the cursor to the wrap point after the hyphen, but assoc is 0. If I then arrow down and left to the start of the wrapped line, assoc is also 0. Is there any way to differentiate these positions when navigating to them using arrow keys?
Now that I understand the cases, I think I can probably write an extension to manage this and set assoc correctly to differentiate these positions, but I’m not sure whether this should be in the CodeMirror code rather than being a random extension? (Or maybe there’s another way I could derive the difference between these positions better than manually tracking arrow key presses and trying to set assoc accurately?)