Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use offsets from .bgv file to select appropriate text #10510

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

JaroslavTulach
Copy link
Contributor

@JaroslavTulach JaroslavTulach commented Jan 18, 2025

Enso is trying to generate IGV graph for our internal IR:

We had issues to provide proper nodeSourceLocation information. I had to debug visualizer code to find out what's wrong. While doing that I made few fixes.

  • we were providing -1 as line location - IGV was then doing nothing - with 75d41ee it at least opens the file
  • I was getting NullPointerExceptions so I am adding checks and assert to catch that earlier
  • Last, but not least: we do provide start and end offsets for Enso nodes. Let's use them!

Location used

With these changes IGV picks up start and end offset from location property and is capable to select appropriate source range. CCing @tkrodriguez, @sdedic, @Akirathan

@oracle-contributor-agreement oracle-contributor-agreement bot added the OCA Verified All contributors have signed the Oracle Contributor Agreement. label Jan 18, 2025

private Line findLine(EditorCookie cake, int line) {
try {
return cake.getLineSet().getOriginal(line - 1);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -63,24 +67,48 @@ private void openOrView(boolean focus) {
return;
}
int line = location.getLine();
int[] offsets = location.getOffsetsOrNull();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Text based languages (like Truffle ones) are likely to fill offsets rather than line numbers. Passing the offsets to the LocationOpener in faba84a and using the offsets rather than line when present. More an experiment for your consideration than bullet proof code right now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have much opinion since I never use this functionality so you should make it work in some rational way. If the two ways of addressing the text disagree then it's unclear what you should do. Naively I might assume that the line number is more to be trusted given that offsets seem a bit squishy given differences in line terminations between windows and unix, plus the lack of actual terminator characters in a UI element that displays text. How are these mismatches expected to be dealt with?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re. different newline terminators: the information about the OS the code was compiled in is lost during the compilation step, and cannot be even dumped to BGV. Maybe a hint for truffle to represent positions using line:column pair rather than offsets.

From the user's standpoint I agree that the whole line is too coarse; it's far better to point at a specific position, if we have such data.

I suggest to make an option for this: if the IGV knows (the user configures it) the line terminator use on the compiling machine, it can then recompute the offset to this machine's newline style and to line:column coordinates.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope we'll find a way that works for current as well as future use-cases without a configuration option. I hope #10510 (comment) is a sound approach to achieve so.

@fniephaus fniephaus requested a review from tkrodriguez January 21, 2025 13:34
@JaroslavTulach JaroslavTulach changed the title More robust visualizer when reading custom .bgv file Use offsets from .bgv file to select appropriate text Jan 22, 2025
task.removeTaskListener(whenShowing[0]);
Mutex.EVENT.postReadRequest(() -> {
if (offsets == null) {
StatusDisplayer.getDefault().setStatusText(Bundle.ERR_LineNotFound());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if I'm understanding the control flow here but it looks to me that if the location had a valid line but no offsets you'd report ERR_LineNotFound. Am I missing some subtlety?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • I think you are right Tom, 8955e69 would address that
  • I have to admit I am not sure if JEditorPane.select is the right way to highlight the selection
  • maybe there is something better like attaching some annotation to a Line.Part
  • I can try to implement whatever you guys think is the best way to handle the highlight

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking a bit more, the editor will have a position that corresponds to the line termination so a unix file and the editor pane should agree about offsets so I think select will do the right thing. I'm unsure what would happen if the underlying file had windows CR+LF terminators so it would be good to test that out and make it work correctly. I don't know anything about best practices in UI code so I can't offer an opinion about which API to use, but select seems reasonable to me.

@@ -63,24 +67,48 @@ private void openOrView(boolean focus) {
return;
}
int line = location.getLine();
int[] offsets = location.getOffsetsOrNull();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re. different newline terminators: the information about the OS the code was compiled in is lost during the compilation step, and cannot be even dumped to BGV. Maybe a hint for truffle to represent positions using line:column pair rather than offsets.

From the user's standpoint I agree that the whole line is too coarse; it's far better to point at a specific position, if we have such data.

I suggest to make an option for this: if the IGV knows (the user configures it) the line terminator use on the compiling machine, it can then recompute the offset to this machine's newline style and to line:column coordinates.

@JaroslavTulach
Copy link
Contributor Author

Thank you for the reviews. Yup, line endings may cause some misalignment. Let's try:

  • if there are no offset information, use line (just like we did so far)
  • if there is no line information, use offset (possibly accepting misalignment of Windows)
  • if there are both information present
    • compute the line from offset
      • if the lines are different - use just line (to keep current behavior)
      • if the lines are the same - select the column region on the line

That way we favor existing behavior and enhance it when additional information is present. I'll write some tests and rewrite the logic now.

@JaroslavTulach
Copy link
Contributor Author

Maybe a hint for truffle to represent positions using line:column pair rather than offsets.

Shouldn't we rather specify that offsets in the IGV treat new line as a single character? That's how Truffle Source does it and also how NetBeans/Swing javax.swing.text.Document does it. Things will be simpler then.

Given Enso is the first system trying to use the offsets, we can make some small adjustments to the specification to make things easier.

@JaroslavTulach
Copy link
Contributor Author

With c4f059d I like what I see, CCing @Akirathan:

Selection of just *

Looks like with this PR the IGV can work for Enso IR graphs well enough. I hope that with #10510 (comment) we have a good compromise to keep the existing behavior unmodified.

Can you do some testing on your side and possibly integrate this enhancement? It would increase our (Enso compiler team) productivity when exploring Enso IR graphs a lot. Btw. We'd be in a need of a new IGV release then...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
OCA Verified All contributors have signed the Oracle Contributor Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants