Enable find all references and rename of auto links and http(s) links in markdown

For #146777, #146277
This commit is contained in:
Matt Bierner
2022-04-05 15:17:48 -07:00
parent c80acef8a1
commit a56c9f10b7
6 changed files with 158 additions and 7 deletions

View File

@@ -170,6 +170,11 @@ const linkPattern = /(\[((!\[[^\]]*?\]\(\s*)([^\s\(\)]+?)\s*\)\]|(?:\\\]|[^\]])*
*/
const referenceLinkPattern = /(?:(\[((?:\\\]|[^\]])+)\]\[\s*?)([^\s\]]*?)\]|\[\s*?([^\s\]]*?)\])(?![\:\(])/g;
/**
* Matches `<http://example.com>`
*/
const autoLinkPattern = /\<(\w+:[^\>\s]+)\>/g;
/**
* Matches `[text]: link`
*/
@@ -256,6 +261,7 @@ export class MdLinkProvider implements vscode.DocumentLinkProvider {
...(await this.getInlineLinks(document)),
...this.getReferenceLinks(document),
...this.getLinkDefinitions(document),
...this.getAutoLinks(document),
]);
}
@@ -277,6 +283,30 @@ export class MdLinkProvider implements vscode.DocumentLinkProvider {
return results;
}
private *getAutoLinks(document: SkinnyTextDocument): Iterable<MdLink> {
const text = document.getText();
for (const match of text.matchAll(autoLinkPattern)) {
const link = match[1];
const linkTarget = parseLink(document, link);
if (linkTarget) {
const offset = (match.index ?? 0) + 1;
const linkStart = document.positionAt(offset);
const linkEnd = document.positionAt(offset + link.length);
yield {
kind: 'link',
href: linkTarget,
source: {
text: link,
resource: document.uri,
hrefRange: new vscode.Range(linkStart, linkEnd),
fragmentRange: getFragmentRange(link, linkStart, linkEnd),
}
};
}
}
}
private *getReferenceLinks(document: SkinnyTextDocument): Iterable<MdLink> {
const text = document.getText();
for (const match of text.matchAll(referenceLinkPattern)) {