@@ -23,63 +23,83 @@ import { ProvideCompletionItemsSignature, ProvideHoverSignature } from 'vscode-l
23
23
export namespace DocsBrowser {
24
24
'use strict' ;
25
25
26
- // registers the browser in VSCode infrastructure
27
- export function registerDocsBrowser ( ) : Disposable {
28
- return commands . registerCommand (
29
- 'haskell.showDocumentation' ,
30
- async ( { title, localPath, hackageUri } : { title : string ; localPath : string ; hackageUri : string } ) => {
31
- const arr = localPath . match ( / ( [ ^ \/ ] + ) \. [ ^ . ] + $ / ) ;
32
- const ttl = arr !== null && arr . length === 2 ? arr [ 1 ] . replace ( / - / gi, '.' ) : title ;
33
- const documentationDirectory = dirname ( localPath ) ;
34
- let panel ;
35
- try {
36
- // Make sure to use Uri.parse here, as path will already have 'file:///' in it
37
- panel = window . createWebviewPanel ( 'haskell.showDocumentationPanel' , ttl , ViewColumn . Beside , {
38
- localResourceRoots : [ Uri . parse ( documentationDirectory ) ] ,
39
- enableFindWidget : true ,
40
- enableCommandUris : true ,
41
- enableScripts : true ,
42
- } ) ;
26
+ async function showDocumentation ( {
27
+ title,
28
+ localPath,
29
+ hackageUri,
30
+ } : {
31
+ title : string ;
32
+ localPath : string ;
33
+ hackageUri : string ;
34
+ } ) {
35
+ const arr = localPath . match ( / ( [ ^ \/ ] + ) \. [ ^ . ] + $ / ) ;
36
+ const ttl = arr !== null && arr . length === 2 ? arr [ 1 ] . replace ( / - / gi, '.' ) : title ;
37
+ const documentationDirectory = dirname ( localPath ) ;
38
+ let panel ;
39
+ try {
40
+ const docUri = Uri . parse ( documentationDirectory ) ;
43
41
44
- const encoded = encodeURIComponent ( JSON . stringify ( { hackageUri } ) ) ;
45
- const hackageCmd = 'command:haskell.openDocumentationOnHackage?' + encoded ;
42
+ // Make sure to use Uri.parse here, as path will already have 'file:///' in it
43
+ panel = window . createWebviewPanel ( 'haskell.showDocumentationPanel' , ttl , ViewColumn . Beside , {
44
+ localResourceRoots : [ docUri ] ,
45
+ enableFindWidget : true ,
46
+ enableCommandUris : true ,
47
+ enableScripts : true ,
48
+ } ) ;
46
49
47
- const bytes = await workspace . fs . readFile ( Uri . parse ( localPath ) ) ;
50
+ const encoded = encodeURIComponent ( JSON . stringify ( { hackageUri : hackageUri , inWebView : true } ) ) ;
51
+ const hackageCmd = 'command:haskell.openDocumentationOnHackage?' + encoded ;
48
52
49
- const addBase = `
50
- <base href="${ panel . webview . asWebviewUri ( Uri . parse ( documentationDirectory ) ) } /">
51
- ` ;
53
+ const bytes = await workspace . fs . readFile ( Uri . parse ( localPath ) ) ;
52
54
53
- panel . webview . html = `
54
- <html>
55
- ${ addBase }
56
- <body>
57
- <div><a href="${ hackageCmd } ">Open on Hackage</a></div>
58
- ${ bytes . toString ( ) }
59
- </body>
60
- </html>
61
- ` ;
62
- } catch ( e ) {
63
- await window . showErrorMessage ( e ) ;
64
- }
65
- return panel ;
55
+ const addBase = `
56
+ <base href="${ panel . webview . asWebviewUri ( Uri . parse ( documentationDirectory ) ) } /">
57
+ ` ;
58
+
59
+ panel . webview . html = `
60
+ <html>
61
+ ${ addBase }
62
+ <body>
63
+ <div><a href="${ hackageCmd } ">Open on Hackage</a></div>
64
+ ${ bytes . toString ( ) }
65
+ </body>
66
+ </html>
67
+ ` ;
68
+ } catch ( e ) {
69
+ await window . showErrorMessage ( e ) ;
70
+ }
71
+ return panel ;
72
+ }
73
+
74
+ // registers the browser in VSCode infrastructure
75
+ export function registerDocsBrowser ( alwaysOpenInHackage : boolean ) : Disposable {
76
+ if ( alwaysOpenInHackage ) {
77
+ return commands . registerCommand ( 'haskell.showDocumentation' , openDocumentationOnHackage ) ;
78
+ } else {
79
+ return commands . registerCommand ( 'haskell.showDocumentation' , showDocumentation ) ;
80
+ }
81
+ }
82
+
83
+ async function openDocumentationOnHackage ( {
84
+ hackageUri,
85
+ inWebView = false ,
86
+ } : {
87
+ hackageUri : string ;
88
+ inWebView : boolean ;
89
+ } ) {
90
+ try {
91
+ // open on Hackage and close the original webview in VS code
92
+ await env . openExternal ( Uri . parse ( hackageUri ) ) ;
93
+ if ( inWebView ) {
94
+ await commands . executeCommand ( 'workbench.action.closeActiveEditor' ) ;
66
95
}
67
- ) ;
96
+ } catch ( e ) {
97
+ await window . showErrorMessage ( e ) ;
98
+ }
68
99
}
69
100
70
101
export function registerDocsOpenOnHackage ( ) : Disposable {
71
- return commands . registerCommand (
72
- 'haskell.openDocumentationOnHackage' ,
73
- async ( { hackageUri } : { hackageUri : string } ) => {
74
- try {
75
- // open on Hackage and close the original webview in VS code
76
- await env . openExternal ( Uri . parse ( hackageUri ) ) ;
77
- await commands . executeCommand ( 'workbench.action.closeActiveEditor' ) ;
78
- } catch ( e ) {
79
- await window . showErrorMessage ( e ) ;
80
- }
81
- }
82
- ) ;
102
+ return commands . registerCommand ( 'haskell.openDocumentationOnHackage' , openDocumentationOnHackage ) ;
83
103
}
84
104
85
105
export function hoverLinksMiddlewareHook (
0 commit comments