-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8fb92a7
commit e0677a1
Showing
2 changed files
with
51 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
extensions/positron-python/src/client/positron/linkProvider.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (C) 2023 Posit Software, PBC. All rights reserved. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
// eslint-disable-next-line import/no-unresolved | ||
import * as positron from 'positron'; | ||
import * as vscode from 'vscode'; | ||
|
||
const UrlRegex = /(http|https):\/\/([\w_-]+(?:(?:\.[\w_-]+)+)|(localhost))([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])/gi; | ||
|
||
interface PositronTerminalLink extends vscode.TerminalLink { | ||
data: string; | ||
} | ||
|
||
const _links: string[] = []; | ||
|
||
export const provider = { | ||
provideTerminalLinks: (context: vscode.TerminalLinkContext, _token: vscode.CancellationToken) => { | ||
const matches = [...context.line.matchAll(UrlRegex)]; | ||
if (matches.length === 0) { | ||
return []; | ||
} | ||
|
||
return matches.map((match) => { | ||
const startIndex = context.line.indexOf(match[0]); | ||
|
||
if (!_links.includes(match[0])) { | ||
const uri = vscode.Uri.parse(match[0]); | ||
positron.window.previewUrl(uri); | ||
} | ||
// fix, people will want to open the same link multiple times | ||
// are we able to get context of the viewer? | ||
// or use some sort of other context from link/application/process lifespan? | ||
_links.push(match[0]); | ||
|
||
return { | ||
startIndex, | ||
length: match[0].length, | ||
tooltip: 'Open in Viewer', | ||
data: match[0], | ||
} as PositronTerminalLink; | ||
}); | ||
}, | ||
handleTerminalLink: (link: PositronTerminalLink) => { | ||
const uri = vscode.Uri.parse(link.data); | ||
positron.window.previewUrl(uri); | ||
}, | ||
}; |