@@ -9,20 +9,20 @@ import {
99 ServerStoppedReason ,
1010 TextInsertKind ,
1111} from '@leanprover/infoview-api'
12- import { join } from 'path'
1312import {
1413 commands ,
1514 Diagnostic ,
1615 Disposable ,
1716 env ,
17+ Event ,
1818 ExtensionContext ,
1919 Position ,
2020 Range ,
2121 Selection ,
2222 TextEditor ,
2323 TextEditorRevealType ,
2424 Uri ,
25- WebviewPanel ,
25+ ViewColumn ,
2626 window ,
2727 workspace ,
2828} from 'vscode'
@@ -40,8 +40,6 @@ import {
4040 getInfoViewShowGoalNames ,
4141 getInfoViewShowTooltipOnHover ,
4242 getInfoViewStyle ,
43- minIfProd ,
44- prodOrDev ,
4543} from './config'
4644import { LeanClient } from './leanclient'
4745import { Rpc } from './rpc'
@@ -53,6 +51,19 @@ import { logger } from './utils/logger'
5351import { displayNotification } from './utils/notifs'
5452import { viewColumnOfActiveTextEditor , viewColumnOfInfoView } from './utils/viewColumn'
5553
54+ export interface InfoWebview {
55+ readonly api : InfoviewApi
56+ readonly rpc : Rpc
57+ readonly visible : boolean
58+ dispose ( ) : any
59+ reveal ( viewColumn ?: ViewColumn , preserveFocus ?: boolean ) : void
60+ onDidDispose : Event < void >
61+ }
62+
63+ export interface InfoWebviewFactory {
64+ make ( editorApi : EditorApi , stylesheet : string , column : number ) : InfoWebview
65+ }
66+
5667const keepAlivePeriodMs = 10000
5768
5869async function rpcConnect ( client : LeanClient , uri : ls . DocumentUri ) : Promise < string > {
@@ -90,7 +101,7 @@ class RpcSessionAtPos implements Disposable {
90101
91102export class InfoProvider implements Disposable {
92103 /** Instance of the panel, if it is open. Otherwise `undefined`. */
93- private webviewPanel ?: WebviewPanel & { rpc : Rpc ; api : InfoviewApi }
104+ private webviewPanel ?: InfoWebview
94105 private subscriptions : Disposable [ ] = [ ]
95106 private clientSubscriptions : Disposable [ ] = [ ]
96107
@@ -320,6 +331,7 @@ export class InfoProvider implements Disposable {
320331 constructor (
321332 private clientProvider : LeanClientProvider ,
322333 private context : ExtensionContext ,
334+ private infoWebviewFactory : InfoWebviewFactory ,
323335 ) {
324336 this . updateStylesheet ( )
325337
@@ -582,48 +594,13 @@ export class InfoProvider implements Disposable {
582594 if ( this . webviewPanel ) {
583595 this . webviewPanel . reveal ( undefined , true )
584596 } else {
585- const webviewPanel = window . createWebviewPanel (
586- 'lean4_infoview' ,
587- 'Lean Infoview' ,
588- { viewColumn : viewColumnOfInfoView ( ) , preserveFocus : true } ,
589- {
590- enableFindWidget : true ,
591- retainContextWhenHidden : true ,
592- enableScripts : true ,
593- enableCommandUris : true ,
594- } ,
595- ) as WebviewPanel & { rpc : Rpc ; api : InfoviewApi }
596-
597- // Note that an extension can send data to its webviews using webview.postMessage().
598- // This method sends any JSON serializable data to the webview. The message is received
599- // inside the webview through the standard message event.
600- // The receiving of these messages is done inside webview\index.ts where it
601- // calls window.addEventListener('message',...
602- webviewPanel . rpc = new Rpc ( m => {
603- try {
604- void webviewPanel . webview . postMessage ( m )
605- } catch ( e ) {
606- // ignore any disposed object exceptions
607- }
608- } )
609- webviewPanel . rpc . register ( this . editorApi )
610-
611- // Similarly, we can received data from the webview by listening to onDidReceiveMessage.
612- webviewPanel . webview . onDidReceiveMessage ( m => {
613- try {
614- webviewPanel . rpc . messageReceived ( m )
615- } catch {
616- // ignore any disposed object exceptions
617- }
618- } )
619- webviewPanel . api = webviewPanel . rpc . getApi ( )
597+ const webviewPanel = this . infoWebviewFactory . make ( this . editorApi , this . stylesheet )
620598 webviewPanel . onDidDispose ( ( ) => {
621599 this . webviewPanel = undefined
622600 this . clearNotificationHandlers ( )
623601 this . clearRpcSessions ( null ) // should be after `webviewPanel = undefined`
624602 } )
625603 this . webviewPanel = webviewPanel
626- webviewPanel . webview . html = this . initialHtml ( )
627604
628605 const client = this . clientProvider . findClient ( leanEditor . documentExtUri )
629606 await this . initInfoView ( leanEditor , client )
@@ -819,35 +796,4 @@ export class InfoProvider implements Disposable {
819796 // ensure the text document has the keyboard focus.
820797 await window . showTextDocument ( editor . document , { viewColumn : editor . viewColumn , preserveFocus : false } )
821798 }
822-
823- private getLocalPath ( path : string ) : string | undefined {
824- if ( this . webviewPanel ) {
825- return this . webviewPanel . webview . asWebviewUri ( Uri . file ( join ( this . context . extensionPath , path ) ) ) . toString ( )
826- }
827- return undefined
828- }
829-
830- private initialHtml ( ) {
831- const libPostfix = `.${ prodOrDev } ${ minIfProd } .js`
832- return `
833- <!DOCTYPE html>
834- <html>
835- <head>
836- <meta charset="UTF-8" />
837- <meta http-equiv="Content-type" content="text/html;charset=utf-8">
838- <title>Infoview</title>
839- <style>${ this . stylesheet } </style>
840- <link rel="stylesheet" href="${ this . getLocalPath ( 'dist/lean4-infoview/index.css' ) } ">
841- </head>
842- <body>
843- <div id="react_root"></div>
844- <script
845- data-importmap-leanprover-infoview="${ this . getLocalPath ( `dist/lean4-infoview/index${ libPostfix } ` ) } "
846- data-importmap-react="${ this . getLocalPath ( `dist/lean4-infoview/react${ libPostfix } ` ) } "
847- data-importmap-react-jsx-runtime="${ this . getLocalPath ( `dist/lean4-infoview/react-jsx-runtime${ libPostfix } ` ) } "
848- data-importmap-react-dom="${ this . getLocalPath ( `dist/lean4-infoview/react-dom${ libPostfix } ` ) } "
849- src="${ this . getLocalPath ( 'dist/webview.js' ) } "></script>
850- </body>
851- </html>`
852- }
853799}
0 commit comments