@@ -44,11 +44,17 @@ let canSendNextData: boolean = true;
4444 * If cancellable is set to true, it becomes a notification in the bottom right corner with a button
4545 * allowing the user to stop the task whenever he wants.
4646 *
47- * @param task - The task to launch and wait for while displaying the progress animation.
47+ * @param task - The task to launch and wait for while displaying the progress animation. The
48+ * `token` parameter can be used to check if the user requested cancellation of the task.
49+ * @param message - The message to display alongside the progress animation.
4850 * @param cancellable - Indicates if the progress should be cancellable, which will put it in
4951 * a notification instead of the status bar.
5052 */
51- function withVizProgress ( task : ( ) => void | Promise < void > , message : string , cancellable = false ) {
53+ function withVizProgress (
54+ task : ( token ?: vscode . CancellationToken ) => void | Promise < void > ,
55+ message : string ,
56+ cancellable = false ,
57+ ) {
5258 vscode . window . withProgress (
5359 {
5460 location : cancellable
@@ -71,7 +77,7 @@ function withVizProgress(task: () => void | Promise<void>, message: string, canc
7177 stopProcess = true ;
7278 } ) ;
7379 try {
74- await task ( ) ;
80+ await task ( token ) ;
7581 } catch ( error ) {
7682 logger . error ( error ) ;
7783 return ;
@@ -92,48 +98,85 @@ function withVizProgress(task: () => void | Promise<void>, message: string, canc
9298 * @param hierarchy - The type of hierarchy to visualize.
9399 */
94100export function startVisualize ( context : vscode . ExtensionContext , hierarchy : Hierarchy ) {
95- withVizProgress ( async ( ) => {
96- if ( vscode . window . activeTextEditor ) {
97- const input = new vscode . Location (
98- vscode . window . activeTextEditor . document . uri ,
99- vscode . window . activeTextEditor . selection . active ,
100- ) ;
101+ const direction =
102+ hierarchy === Hierarchy . CALL
103+ ? RelationDirection . SUPER
104+ : hierarchy === Hierarchy . GPR
105+ ? RelationDirection . SUB
106+ : RelationDirection . BOTH ;
101107
102- const languageId = vscode . window . activeTextEditor . document . languageId ;
103- const direction =
104- hierarchy === Hierarchy . CALL
105- ? RelationDirection . SUPER
106- : hierarchy === Hierarchy . GPR
107- ? RelationDirection . SUB
108- : RelationDirection . BOTH ;
109- const middleNode = await createHandler ( languageId ) . provideHierarchy (
110- input ,
111- hierarchy ,
112- languageId ,
113- direction ,
114- ) ;
108+ const progressLabel = getProgressLabel ( hierarchy , true ) ;
115109
116- // Create the webView only if there is something to display.
117- if ( middleNode ) {
118- setupWebView ( context , hierarchy ) ;
119- const panel = panels [ hierarchy ] ;
120- // Make sure the webView was created and initialized.
121- if ( panel ) {
122- // Wait for the webView to notify the end of it's rendering
123- const receive = panel . webview . onDidReceiveMessage ( ( message : Message ) => {
124- if ( message . command === 'rendered' ) {
125- // Remove the listener as it will not be used after sending
126- // the initial request.
127- sendMessage ( middleNode . id , hierarchy ) ;
128- receive . dispose ( ) ;
129- }
130- } ) ;
131- // Check if the client has already been rendered.
132- panel . webview . postMessage ( { command : 'isRendered' } as IsRenderedMessage ) ;
110+ withVizProgress (
111+ async ( token ?: vscode . CancellationToken ) => {
112+ if ( vscode . window . activeTextEditor ) {
113+ const input = new vscode . Location (
114+ vscode . window . activeTextEditor . document . uri ,
115+ vscode . window . activeTextEditor . selection . active ,
116+ ) ;
117+
118+ const languageId = vscode . window . activeTextEditor . document . languageId ;
119+
120+ const middleNode = await createHandler ( languageId ) . provideHierarchy (
121+ input ,
122+ hierarchy ,
123+ languageId ,
124+ direction ,
125+ token ,
126+ ) ;
127+
128+ // Create the webView only if there is something to display and if the
129+ // user did not cancel the operation.
130+ if ( middleNode && ! token ?. isCancellationRequested ) {
131+ setupWebView ( context , hierarchy ) ;
132+ const panel = panels [ hierarchy ] ;
133+ // Make sure the webView was created and initialized.
134+ if ( panel ) {
135+ // Wait for the webView to notify the end of it's rendering
136+ const receive = panel . webview . onDidReceiveMessage ( ( message : Message ) => {
137+ if ( message . command === 'rendered' ) {
138+ // Remove the listener as it will not be used after sending
139+ // the initial request.
140+ sendMessage ( middleNode . id , hierarchy ) ;
141+ receive . dispose ( ) ;
142+ }
143+ } ) ;
144+ // Check if the client has already been rendered.
145+ panel . webview . postMessage ( { command : 'isRendered' } as IsRenderedMessage ) ;
146+ }
133147 }
134148 }
135- }
136- } , 'Visualizing' ) ;
149+ } ,
150+ progressLabel ,
151+ true ,
152+ ) ;
153+ }
154+
155+ /**
156+ * Get the label to display in the progress bar depending on the hierarchy type.
157+ *
158+ * @param hierarchy - The type of hierarchy to visualize.
159+ * @param onCreate - Whether the progress is for creation or not.
160+ * @returns The updated progress label.
161+ */
162+ function getProgressLabel ( hierarchy : Hierarchy , onCreate : boolean = false ) : string {
163+ let hierarchyTypeLabel : string ;
164+ const headerLabel : string = onCreate ? 'Creating' : 'Updating' ;
165+ switch ( hierarchy ) {
166+ case Hierarchy . TYPE :
167+ hierarchyTypeLabel = 'Type Hierarchy' ;
168+ break ;
169+ case Hierarchy . CALL :
170+ hierarchyTypeLabel = 'Call Hierarchy' ;
171+ break ;
172+ case Hierarchy . GPR :
173+ hierarchyTypeLabel = 'GPR Dependencies' ;
174+ break ;
175+ default :
176+ hierarchyTypeLabel = 'File Dependencies' ;
177+ break ;
178+ }
179+ return `${ headerLabel } ${ hierarchyTypeLabel } Graph` ;
137180}
138181
139182/**
@@ -188,7 +231,7 @@ function handleMessage(message: Message) {
188231 withVizProgress ( ( ) => {
189232 const data = message ;
190233 void refreshNodes ( data . nodesId ) ;
191- } , 'Visualizing ' ) ;
234+ } , 'Refreshing Graph ' ) ;
192235 break ;
193236 }
194237 // Stop the current loop of process (mostly for the recursive hierarchy).
@@ -218,6 +261,8 @@ function handleMessage(message: Message) {
218261 * @param data - The data necessary to expand a node.
219262 */
220263function requestHierarchy ( data : HierarchyMessage ) {
264+ const progressLabel = getProgressLabel ( data . hierarchy ) ;
265+
221266 withVizProgress (
222267 async ( ) => {
223268 stopProcess = false ;
@@ -324,7 +369,7 @@ function requestHierarchy(data: HierarchyMessage) {
324369 } , 100 ) ;
325370 } ) ;
326371 } ,
327- data . recursive ? 'Gathering data...' : 'Visualizing' ,
372+ progressLabel ,
328373 data . recursive ,
329374 ) ;
330375}
0 commit comments