Search code examples
regexvisual-studio-codeurl

How does VS Code recognize URLs in text (strings)?


I find that VS Code does really good URL recognition / extraction- better than any methods found in the internet. Since VS Code is opensource, where can I find the code? Probably regex rules.

shortcut for nice url extraction result

I searched the source code with keyword url or extract, but can not find any clues.


Solution

  • For the builtin link detection in editors

    Not 100% percent sure that this is it, but from my search through the source code, I think you might be looking for src/vs/editor/common/languages/linkComputer.ts, which uses a state-machine-based approach.

    You can see in src/vs/editor/browser/services/editorWorkerService.ts, in EditorWorkerService's constructor, there's a comment saying "register default link-provider and default completions-provider", and a call to client.computeLinks, which is defined in src/vs/editor/common/services/editorSimpleWorker.ts in the EditorSimpleWorker class' computeLinks function, which in turn calls computeLinks from linkComputer.ts. That comment lines up nicely with the description of DocumentLinkProvider<T>'s provideDocumentLinks function, which states:

    Note that the editor ships with a default provider that detects http(s) and file links.

    Fun fact: you can find where it's used in https://github.com/search?q=repo%3Amicrosoft%2Fvscode%20computeLinks&type=code, including src/vs/workbench/contrib/output/common/outputLinkComputer.ts and src/vs/workbench/contrib/terminalContrib/links/browser/terminalUriLinkDetector.ts.

    For the builtin link detection in terminal

    The integrated terminal in VS Code has various other capabilities for links (including URLs, file links, folder links, and "word links"). The integrated uses xtermjs, which supports OSC 8 hyperlinks (see also How to create links in xterm.js). VS Code also has several builtin link detector patterns specifically designed to recognize things like file links (that don't use file://) in command outputs of popular commandline tools. See src/vs/workbench/contrib/terminalContrib/links/browser/terminalLinkParsing.ts. Also, the word links feature has some extra bits to search following text in a line for info about line numbers and such (see 1.88 release notes).

    For the builtin link detection in Output Panel

    The Output Panel also has some special link detection patterns, which you can find in src/vs/workbench/contrib/output/common/outputLinkComputer.ts.

    For extension-contributed link "detection"

    As mentioned above, there's the DocumentLinkProvider extension API that extension can use to contribute links to documents. If you want to find out how a specific extension is doing that, you'll need to post a question specifically about that extension.