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

Fix opening links in the terminal with column numbers #210898

Merged
merged 3 commits into from
Jun 20, 2024

Conversation

Timmmm
Copy link
Contributor

@Timmmm Timmmm commented Apr 22, 2024

If you open a link from the terminal like src/foo.ext:5.0-4 then the link passed to TerminalSearchLinkOpener.open() will be src/foo.ext:5.0-4. It will try to match it against detected links in the line, but it will fail because the detect link has text src/foo.ext. Then it will pass the full src/foo.ext:5.0-4 to the quick picker which doesn't understand that format.

This fixes the matching against detected links so it strips potential line/column numbers before matching against the detected links' texts.

Fixes #216728

@Timmmm
Copy link
Contributor Author

Timmmm commented Apr 22, 2024

Honestly I'm not sure this is the best approach. The whole link opening code seems to be a total mess (kind of understandable since there are so many heuristics), but still. I'm not sure why we detect links, then throw away the line info, then detect links again and try to match them back up. Weird.

Another thing is that you have the column number of the links in both cases so we should probably check if the passed link actually overlaps the detected link. But I haven't done that.

To reproduce the issue and check the fix, make a directory containing these two files:

❯ cat output.txt 
Warning: No default value src/main.sail:5.5-9:
Warning: No default value src/main.sail:5:

❯ cat sub/src/main.sail 
a00000000000000000000000000
b00000000000000000000000000
c00000000000000000000000000
d00000000000000000000000000
e00000000000000000000000000
f00000000000000000000000000
g00000000000000000000000000
h00000000000000000000000000

Now ctrl-click the two src/main.sail links from cat output.txt. Before this patch only src/main.sail:5 works. The other one just copies the link text to the quick picker but doesn't open the file.

After this patch they both work.

@Timmmm
Copy link
Contributor Author

Timmmm commented Apr 22, 2024

Oh also, to be clear this did work already if the link happens to match the exact path to the file. It's only when it is in a subdirectory somewhere that you see the difference.

If you open a link from the terminal like `src/foo.ext:5.0-4` then the link passed to `TerminalSearchLinkOpener.open()` will be `src/foo.ext:5.0-4`. It will try to match it against detected links in the line, but it will fail because the detect link has text `src/foo.ext`. Then it will pass the full `src/foo.ext:5.0-4` to the quick picker which doesn't understand that format.

This fixes the matching against detected links so it strips potential line/column numbers before matching against the detected links' texts.

This only seems to be necessary when the link path doesn't exactly match a file (e.g. if that file is in a subdirectory).
@Tyriar
Copy link
Member

Tyriar commented Apr 22, 2024

I'm not sure why we detect links, then throw away the line info, then detect links again and try to match them back up. Weird.

You might be referring to the TerminalLocalLinkDetector which provides validated (confirmed on the FS) for high confidence links found? When the link is not validated it'll fall back to a word/search link which makes a link out of every word (on hover) and activating it will do the heavier work (eg. pinging the file system which might be on a remote machine) which may involve re-checking the links.

Allowing word links to read col/line context at all was actually a very recent change: https://code.visualstudio.com/updates/v1_88#_additional-context-for-word-links

@Tyriar Tyriar added this to the April 2024 milestone Apr 22, 2024
@Timmmm
Copy link
Contributor Author

Timmmm commented Apr 22, 2024

Ah that kind of makes sense, though it still feels a little round about. So for my example use case we have:

  1. TerminalLocalLinkDetector finds file links, including line/column numbers. It will find { path: "src/main.sail", line: 5, col: 5 } (roughly). It only returns these if they exactly match a file on disk. In this case it doesn't match because the file is in a subdirectory.
  2. It falls back to treating src/main.sail:5.5-9 as a "word" and tries to open that link.
  3. We run link detection on the line again, and re-find { path: "src/main.sail", line: 5, col: 5 }. After the hacky manipulation in this patch we say "oh it's the same src/main.sail; lets use that instead".
  4. It gets sent to the quick picker which is able to its file database to find the file.

I think in an ideal world if TerminalLocalLinkDetector finds a link it's still preferable to use that link rather than falling back to word-based links even if the file doesn't exist on disk? But... let's not do that now! :-D

@Tyriar
Copy link
Member

Tyriar commented Apr 22, 2024

@Timmmm you should have seen it before I reworked it into the detectors and openers sub-systems 😉. Doing it this way allows loose coupling and easier testing while keeping the flexibility. The cost of potentially reparsing a single link on activate is fairly low so the extra parse doesn't concern me that much. The more important thing is keeping the flexibility of the current system where there are n detectors each with their own priority can create links of any opener type.

@Tyriar Tyriar modified the milestones: April 2024, May 2024 Apr 25, 2024
@Tyriar Tyriar modified the milestones: May 2024, June 2024 May 30, 2024
@Tyriar
Copy link
Member

Tyriar commented Jun 20, 2024

Created an issue for this bug #216728

@Tyriar Tyriar enabled auto-merge June 20, 2024 16:27
@Tyriar Tyriar merged commit 117f76a into microsoft:main Jun 20, 2024
6 checks passed
@Timmmm
Copy link
Contributor Author

Timmmm commented Jun 20, 2024

Thanks!

bricefriha pushed a commit to bricefriha/vscode that referenced this pull request Jun 26, 2024
Fix opening links in the terminal with column numbers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Search terminal links should be aware of more line/column formats
3 participants