From 94a84cdf579c095c3cbf519d58429d49f57a6d1a Mon Sep 17 00:00:00 2001 From: Matt Bierner Date: Fri, 25 Oct 2024 08:48:59 -0700 Subject: [PATCH] Fix bad multiline matches for link definitions Fixes #192 --- src/languageFeatures/documentLinks.ts | 9 +++++++-- src/test/documentLinks.test.ts | 10 ++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/languageFeatures/documentLinks.ts b/src/languageFeatures/documentLinks.ts index 44df1e4..04f63ff 100644 --- a/src/languageFeatures/documentLinks.ts +++ b/src/languageFeatures/documentLinks.ts @@ -33,7 +33,12 @@ function createHref( ): ExternalHref | InternalHref | undefined { if (/^[a-z\-][a-z\-]+:/i.test(link)) { // Looks like a uri - return { kind: HrefKind.External, uri: URI.parse(tryDecodeUri(link)) }; + try { + return { kind: HrefKind.External, uri: URI.parse(tryDecodeUri(link)) }; + } catch (e) { + console.warn(r`Failed to parse link ${link} in ${sourceDocUri.toString(true)}`); + return undefined; + } } const resolved = resolveInternalDocumentLink(sourceDocUri, link, workspace); @@ -185,7 +190,7 @@ const autoLinkPattern = /(?\s]+)\>/g; /** * Matches `[text]: link` */ -const definitionPattern = /^([\t ]*(?]|[^<>])+>)/gm; +const definitionPattern = /^([\t ]*(?]|[^<>])+>)/gm; class InlineRanges { diff --git a/src/test/documentLinks.test.ts b/src/test/documentLinks.test.ts index c7704a4..395852c 100644 --- a/src/test/documentLinks.test.ts +++ b/src/test/documentLinks.test.ts @@ -782,6 +782,16 @@ suite('Link computer', () => { assertLinksEqual(links, []); }); + + test('Should not crash on definition that spans multiple lines (#192)', async () => { + const links = await getLinksForText(joinLines( + `Options`, + `[positional parameters]:`, + `-R: recursively list subdirectories.`, + )); + + assertLinksEqual(links, []); + }); });