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

[python] open stdout localhost links in terminal in Viewer #2612

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions extensions/positron-python/positron-dts/positron.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1087,6 +1087,17 @@ declare module 'positron' {
* Returns the current width of the console input, in characters.
*/
export function getConsoleWidth(): Thenable<number>;

/**
* Create and show a new preview panel for a URL. This is a convenience
* method that creates a new webview panel and sets its content to the
* given URL.
*
* @param url The URL to preview
*
* @return New preview panel.
*/
export function previewUrl(url: vscode.Uri): PreviewPanel;
}

namespace runtime {
Expand Down
4 changes: 4 additions & 0 deletions extensions/positron-python/src/client/positron/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

// eslint-disable-next-line import/no-unresolved
import * as positron from 'positron';
import * as vscode from 'vscode';
import { PythonExtension } from '../api/types';
import { IDisposableRegistry } from '../common/types';
import { IInterpreterService } from '../interpreter/contracts';
import { IServiceContainer } from '../ioc/types';
import { traceError, traceInfo } from '../logging';
import { PythonRuntimeManager } from './manager';
import { createPythonRuntimeMetadata } from './runtime';
import { provideTerminalLinks, handleTerminalLink } from './linkProvider';

export async function activatePositron(
activatedPromise: Promise<void>,
Expand All @@ -29,6 +31,8 @@ export async function activatePositron(
traceInfo('activatePositron: awaiting extension activation');
await activatedPromise;

vscode.window.registerTerminalLinkProvider({ provideTerminalLinks, handleTerminalLink });

const registerRuntime = async (interpreterPath: string) => {
if (!manager.registeredPythonRuntimes.has(interpreterPath)) {
// Get the interpreter corresponding to the new runtime.
Expand Down
84 changes: 84 additions & 0 deletions extensions/positron-python/src/client/positron/linkProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*---------------------------------------------------------------------------------------------
* 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';

// Assuming localHosts is an array of localhost URLs
const localHosts: string[] = [
'localhost',
'127.0.0.1',
'[0:0:0:0:0:0:0:1]',
'[::1]',
'0.0.0.0',
'[0:0:0:0:0:0:0:0]',
'[::]',
];

const urlPattern = /(http|https):\/\/([\w_-]+(?:(?:\.[\w_-]+)+)|(localhost))([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])/gi;
const localHostsPattern = new RegExp(
`(?:https?:\/\/)(${localHosts.join('|')})([\\w.,@?^=%&:/~+#-]*[\\w@?^=%&/~/+#-])`,
'gi',
);

interface PositronTerminalLink extends vscode.TerminalLink {
data: string;
openInViewer: boolean;
}

const _links: Map<Thenable<number | undefined>, string> = new Map();

export function provideTerminalLinks(
context: vscode.TerminalLinkContext,
_token: vscode.CancellationToken,
): vscode.ProviderResult<PositronTerminalLink[]> {
const matches = [...context.line.matchAll(urlPattern)];

if (matches.length === 0) {
return [];
}

return matches.map((match) => {
const startIndex = context.line.indexOf(match[0]);

// if localhost, preview through viewer
if (localHostsPattern.test(match[0])) {
const pid = context.terminal.processId;
if (!_links.has(pid) || _links.get(pid) !== match[0]) {
positron.window.previewUrl(vscode.Uri.parse(match[0]));

// set pid to latest localhost address
_links.set(pid, match[0]);

return {
startIndex,
length: match[0].length,
tooltip: 'Open link in Viewer',
data: match[0],
openInViewer: true,
} as PositronTerminalLink;
}
}
// otherwise, treat as external link
return {
startIndex,
length: match[0].length,
tooltip: 'Open link in browser',
data: match[0],
openInViewer: false,
} as PositronTerminalLink;
});
}

export function handleTerminalLink(link: PositronTerminalLink): void {
const config = vscode.workspace.getConfiguration('positron.viewer');

if (link.openInViewer && config.get<boolean>('openLocalhostUrls')) {
const uri = vscode.Uri.parse(link.data);
positron.window.previewUrl(uri);
} else {
vscode.commands.executeCommand('vscode.open', vscode.Uri.parse(link.data));
}
}
Loading