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

Fix showDocumentation command. #452

Merged
merged 6 commits into from
Sep 7, 2021
Merged
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
12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,18 @@
"default": "ormolu",
"description": "The formatter to use when formatting a document or range. Ensure the plugin is enabled."
},
"haskell.openDocumentationInHackage": {
"scope": "resource",
"type": "boolean",
"default": true,
"description": "When opening 'Documentation' for external libraries, open in hackage by default. Set to false to instead open in vscode."
},
"haskell.openSourceInHackage": {
"scope": "resource",
"type": "boolean",
"default": true,
"description": "When opening 'Source' for external libraries, open in hackage by default. Set to false to instead open in vscode."
},
"haskell.trace.server": {
"scope": "resource",
"type": "string",
Expand Down
142 changes: 97 additions & 45 deletions src/docsBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,56 +16,86 @@ import {
Uri,
ViewColumn,
window,
workspace,
} from 'vscode';
import { ProvideCompletionItemsSignature, ProvideHoverSignature } from 'vscode-languageclient';

export namespace DocsBrowser {
'use strict';

async function showDocumentation({
title,
localPath,
hackageUri,
}: {
title: string;
localPath: string;
hackageUri: string;
}) {
const arr = localPath.match(/([^\/]+)\.[^.]+$/);
const ttl = arr !== null && arr.length === 2 ? arr[1].replace(/-/gi, '.') : title;
const documentationDirectory = dirname(localPath);
let panel;
try {
const docUri = Uri.parse(documentationDirectory);

// Make sure to use Uri.parse here, as path will already have 'file:///' in it
panel = window.createWebviewPanel('haskell.showDocumentationPanel', ttl, ViewColumn.Beside, {
localResourceRoots: [docUri],
enableFindWidget: true,
enableCommandUris: true,
enableScripts: true,
});

const encoded = encodeURIComponent(JSON.stringify({ hackageUri: hackageUri, inWebView: true }));
const hackageCmd = 'command:haskell.openDocumentationOnHackage?' + encoded;

const bytes = await workspace.fs.readFile(Uri.parse(localPath));

const addBase = `
<base href="${panel.webview.asWebviewUri(Uri.parse(documentationDirectory))}/">
`;

panel.webview.html = `
<html>
${addBase}
<body>
<div><a href="${hackageCmd}">Open on Hackage</a></div>
${bytes.toString()}
</body>
</html>
`;
} catch (e) {
await window.showErrorMessage(e);
}
return panel;
}

// registers the browser in VSCode infrastructure
export function registerDocsBrowser(): Disposable {
return commands.registerCommand(
'haskell.showDocumentation',
async ({ title, localPath, hackageUri }: { title: string; localPath: string; hackageUri: string }) => {
const arr = localPath.match(/([^\/]+)\.[^.]+$/);
const ttl = arr !== null && arr.length === 2 ? arr[1].replace(/-/gi, '.') : title;
const documentationDirectory = dirname(localPath);
let panel;
try {
// Make sure to use Uri.parse here, as path will already have 'file:///' in it
panel = window.createWebviewPanel('haskell.showDocumentationPanel', ttl, ViewColumn.Beside, {
localResourceRoots: [Uri.parse(documentationDirectory)],
enableFindWidget: true,
enableCommandUris: true,
});
const uri = panel.webview.asWebviewUri(Uri.parse(localPath));

const encoded = encodeURIComponent(JSON.stringify({ hackageUri }));
const hackageCmd = 'command:haskell.openDocumentationOnHackage?' + encoded;
return commands.registerCommand('haskell.showDocumentation', showDocumentation);
}

panel.webview.html = `<div><a href="${hackageCmd}">Open on Hackage</a></div>
<div><iframe src="${uri}" frameBorder = "0" style = "background: white; width: 100%; height: 100%; position:absolute; left: 0; right: 0; bottom: 0; top: 30px;"/></div>`;
} catch (e) {
await window.showErrorMessage(e);
}
return panel;
async function openDocumentationOnHackage({
hackageUri,
inWebView = false,
}: {
hackageUri: string;
inWebView: boolean;
}) {
try {
// open on Hackage and close the original webview in VS code
await env.openExternal(Uri.parse(hackageUri));
if (inWebView) {
await commands.executeCommand('workbench.action.closeActiveEditor');
}
);
} catch (e) {
await window.showErrorMessage(e);
}
}

export function registerDocsOpenOnHackage(): Disposable {
return commands.registerCommand(
'haskell.openDocumentationOnHackage',
async ({ hackageUri }: { hackageUri: string }) => {
try {
// open on Hackage and close the original webview in VS code
await env.openExternal(Uri.parse(hackageUri));
await commands.executeCommand('workbench.action.closeActiveEditor');
} catch (e) {
await window.showErrorMessage(e);
}
}
);
return commands.registerCommand('haskell.openDocumentationOnHackage', openDocumentationOnHackage);
}

export function hoverLinksMiddlewareHook(
Expand Down Expand Up @@ -109,17 +139,39 @@ export namespace DocsBrowser {
}

function processLink(ms: MarkdownString | MarkedString): string | MarkdownString {
const openDocsInHackage = workspace.getConfiguration('haskell').get('openDocumentationInHackage');
const openSourceInHackage = workspace.getConfiguration('haskell').get('openSourceInHackage');
function transform(s: string): string {
return s.replace(
/\[(.+)\]\((file:.+\/doc\/(?:.*html\/libraries\/)?([^\/]+)\/(src\/)?(.+\.html#?.*))\)/gi,
(all, title, localPath, packageName, maybeSrcDir, fileAndAnchor) => {
if (!maybeSrcDir) {
maybeSrcDir = '';
/\[(.+)\]\((file:.+\/doc\/(?:.*html\/libraries\/)?([^\/]+)\/(?:.*\/)?(.+\.html#?.*))\)/gi,
(all, title, localPath, packageName, fileAndAnchor) => {
let hackageUri: string;
if (title == 'Documentation') {
hackageUri = `https://hackage.haskell.org/package/${packageName}/docs/${fileAndAnchor}`;
const encoded = encodeURIComponent(JSON.stringify({ title, localPath, hackageUri }));
let cmd: string;
if (openDocsInHackage) {
cmd = 'command:haskell.openDocumentationOnHackage?' + encoded;
} else {
cmd = 'command:haskell.showDocumentation?' + encoded;
}
return `[${title}](${cmd})`;
} else if (title == 'Source') {
hackageUri = `https://hackage.haskell.org/package/${packageName}/docs/src/${fileAndAnchor.replace(
/-/gi,
'.'
)}`;
const encoded = encodeURIComponent(JSON.stringify({ title, localPath, hackageUri }));
let cmd: string;
if (openSourceInHackage) {
cmd = 'command:haskell.openDocumentationOnHackage?' + encoded;
} else {
cmd = 'command:haskell.showDocumentation?' + encoded;
}
return `[${title}](${cmd})`;
} else {
return s;
}
const hackageUri = `https://hackage.haskell.org/package/${packageName}/docs/${maybeSrcDir}${fileAndAnchor}`;
const encoded = encodeURIComponent(JSON.stringify({ title, localPath, hackageUri }));
const cmd = 'command:haskell.showDocumentation?' + encoded;
return `[${title}](${cmd})`;
}
);
}
Expand Down