6
6
CompletionItem ,
7
7
CompletionList ,
8
8
Disposable ,
9
+ env ,
9
10
Hover ,
10
11
MarkdownString ,
11
12
MarkedString ,
@@ -23,24 +24,48 @@ export namespace DocsBrowser {
23
24
24
25
// registers the browser in VSCode infrastructure
25
26
export function registerDocsBrowser ( ) : Disposable {
26
- return commands . registerCommand ( 'haskell.showDocumentation' , ( { title, path } : { title : string ; path : string } ) => {
27
- const arr = path . match ( / ( [ ^ \/ ] + ) \. [ ^ . ] + $ / ) ;
28
- const ttl = arr !== null && arr . length === 2 ? arr [ 1 ] . replace ( / - / gi, '.' ) : title ;
29
- const documentationDirectory = dirname ( path ) ;
30
- let panel ;
31
- try {
32
- // Make sure to use Uri.parse here, as path will already have 'file:///' in it
33
- panel = window . createWebviewPanel ( 'haskell.showDocumentationPanel' , ttl , ViewColumn . Beside , {
34
- localResourceRoots : [ Uri . parse ( documentationDirectory ) ] ,
35
- enableFindWidget : true ,
36
- } ) ;
37
- const uri = panel . webview . asWebviewUri ( Uri . parse ( path ) ) ;
38
- panel . webview . html = `<iframe src="${ uri } " frameBorder = "0" style = "background: white; width: 100%; height: 100%; position:absolute; left: 0; right: 0; bottom: 0; top: 0px;" /> ` ;
39
- } catch ( e ) {
40
- window . showErrorMessage ( e ) ;
27
+ return commands . registerCommand (
28
+ 'haskell.showDocumentation' ,
29
+ async ( { title, localPath, hackageUri } : { title : string ; localPath : string ; hackageUri : string } ) => {
30
+ const arr = localPath . match ( / ( [ ^ \/ ] + ) \. [ ^ . ] + $ / ) ;
31
+ const ttl = arr !== null && arr . length === 2 ? arr [ 1 ] . replace ( / - / gi, '.' ) : title ;
32
+ const documentationDirectory = dirname ( localPath ) ;
33
+ let panel ;
34
+ try {
35
+ // Make sure to use Uri.parse here, as path will already have 'file:///' in it
36
+ panel = window . createWebviewPanel ( 'haskell.showDocumentationPanel' , ttl , ViewColumn . Beside , {
37
+ localResourceRoots : [ Uri . parse ( documentationDirectory ) ] ,
38
+ enableFindWidget : true ,
39
+ enableCommandUris : true ,
40
+ } ) ;
41
+ const uri = panel . webview . asWebviewUri ( Uri . parse ( localPath ) ) ;
42
+
43
+ const encoded = encodeURIComponent ( JSON . stringify ( { hackageUri } ) ) ;
44
+ const hackageCmd = 'command:haskell.openDocumentationOnHackage?' + encoded ;
45
+
46
+ panel . webview . html = `<div><a href="${ hackageCmd } ">Reopen on Hackage</a></div>
47
+ <div><iframe src="${ uri } " frameBorder = "0" style = "background: white; width: 100%; height: 100%; position:absolute; left: 0; right: 0; bottom: 0; top: 30px;"/></div>` ;
48
+ } catch ( e ) {
49
+ await window . showErrorMessage ( e ) ;
50
+ }
51
+ return panel ;
41
52
}
42
- return panel ;
43
- } ) ;
53
+ ) ;
54
+ }
55
+
56
+ export function registerDocsOpenOnHackage ( ) : Disposable {
57
+ return commands . registerCommand (
58
+ 'haskell.openDocumentationOnHackage' ,
59
+ async ( { hackageUri } : { hackageUri : string } ) => {
60
+ try {
61
+ // open on Hackage and close the original webview in VS code
62
+ await env . openExternal ( Uri . parse ( hackageUri ) ) ;
63
+ await commands . executeCommand ( 'workbench.action.closeActiveEditor' ) ;
64
+ } catch ( e ) {
65
+ await window . showErrorMessage ( e ) ;
66
+ }
67
+ }
68
+ ) ;
44
69
}
45
70
46
71
export function hoverLinksMiddlewareHook (
@@ -85,11 +110,18 @@ export namespace DocsBrowser {
85
110
86
111
function processLink ( ms : MarkedString ) : string | MarkdownString {
87
112
function transform ( s : string ) : string {
88
- return s . replace ( / \[ ( .+ ) \] \( ( f i l e : .+ \/ d o c \/ .+ \. h t m l # ? .* ) \) / gi, ( all , title , path ) => {
89
- const encoded = encodeURIComponent ( JSON . stringify ( { title, path } ) ) ;
90
- const cmd = 'command:haskell.showDocumentation?' + encoded ;
91
- return `[${ title } ](${ cmd } )` ;
92
- } ) ;
113
+ return s . replace (
114
+ / \[ ( .+ ) \] \( ( f i l e : .+ \/ d o c \/ (?: .* h t m l \/ l i b r a r i e s \/ ) ? ( [ ^ \/ ] + ) \/ ( s r c \/ ) ? ( .+ \. h t m l # ? .* ) ) \) / gi,
115
+ ( all , title , localPath , packageName , maybeSrcDir , fileAndAnchor ) => {
116
+ if ( ! maybeSrcDir ) {
117
+ maybeSrcDir = '' ;
118
+ }
119
+ const hackageUri = `https://hackage.haskell.org/package/${ packageName } /docs/${ maybeSrcDir } ${ fileAndAnchor } ` ;
120
+ const encoded = encodeURIComponent ( JSON . stringify ( { title, localPath, hackageUri } ) ) ;
121
+ const cmd = 'command:haskell.showDocumentation?' + encoded ;
122
+ return `[${ title } ](${ cmd } )` ;
123
+ }
124
+ ) ;
93
125
}
94
126
if ( typeof ms === 'string' ) {
95
127
return transform ( ms as string ) ;
0 commit comments