Skip to content

Commit

Permalink
feat: prettier cancellation
Browse files Browse the repository at this point in the history
  • Loading branch information
goerlibe authored and lukasrothenberger committed Nov 14, 2023
1 parent ba51ce2 commit 73e82d2
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 14 deletions.
5 changes: 4 additions & 1 deletion src/DiscoPoP/DiscoPoPRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { getDefaultErrorHandler } from '../Utils/ErrorHandler'
import { LineMapping } from '../LineMapping/LineMapping'
import { Suggestion } from './classes/Suggestion/Suggestion'
import { DiscoPoPAppliedSuggestionsWatcher } from './DiscoPoPAppliedSuggestionsWatcher'
import { UserCancellationError } from '../Utils/CancellationError'

export interface DiscoPoPRunnerRunArguments {
fullConfiguration: DefaultConfiguration // TODO replace with only the necessary fields
Expand Down Expand Up @@ -68,7 +69,9 @@ export abstract class DiscoPoPRunner {
)
return runningFinishedSuccessfully
? DiscoPoPRunner.parse(dpRunnerArgs)
: Promise.reject(new Error('DiscoPoP run was cancelled.'))
: Promise.reject(
new UserCancellationError('DiscoPoP run was cancelled.')
)
}

public static async run(
Expand Down
52 changes: 40 additions & 12 deletions src/DiscoPoPExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
HotspotDetectionRunnerResults,
} from './HotspotDetection/HotspotDetectionRunner'
import { getDefaultErrorHandler } from './Utils/ErrorHandler'
import { UserCancellationError } from './Utils/CancellationError'

function _removeDecorations(
editor: vscode.TextEditor,
Expand Down Expand Up @@ -116,14 +117,23 @@ export class DiscoPoPExtension {
async (configuration: Configuration) => {
const fullConfig = configuration.getFullConfiguration()

// DiscoPoP
this.dpResults.finalize()
this.dpResults = await fullConfig.runDiscoPoP()
await this.showDiscoPoPResults(fullConfig)
try {
// DiscoPoP
this.dpResults?.finalize()
this.dpResults = await fullConfig.runDiscoPoP()
await this.showDiscoPoPResults(fullConfig)

// HotspotDetection
this.hsResults = await configuration.runHotspotDetection()
await this.showHotspotDetectionResults(this.hsResults)
// HotspotDetection
this.hsResults =
await configuration.runHotspotDetection()
await this.showHotspotDetectionResults(this.hsResults)
} catch (error) {
if (error instanceof UserCancellationError) {
error.showErrorMessageNotification()
} else {
throw error
}
}
}
)
)
Expand All @@ -132,9 +142,18 @@ export class DiscoPoPExtension {
vscode.commands.registerCommand(
Commands.runDiscoPoP,
async (configuration: Configuration) => {
const fullConfig = configuration.getFullConfiguration()
this.dpResults = await fullConfig.runDiscoPoP()
await this.showDiscoPoPResults(fullConfig)
try {
const fullConfig = configuration.getFullConfiguration()
this.dpResults?.finalize()
this.dpResults = await fullConfig.runDiscoPoP()
await this.showDiscoPoPResults(fullConfig)
} catch (error) {
if (error instanceof UserCancellationError) {
error.showErrorMessageNotification()
} else {
throw error
}
}
}
)
)
Expand All @@ -143,8 +162,17 @@ export class DiscoPoPExtension {
vscode.commands.registerCommand(
Commands.runHotspotDetection,
async (configuration: Configuration) => {
const results = await configuration.runHotspotDetection()
this.showHotspotDetectionResults(results)
try {
const results =
await configuration.runHotspotDetection()
this.showHotspotDetectionResults(results)
} catch (error) {
if (error instanceof UserCancellationError) {
error.showErrorMessageNotification()
} else {
throw error
}
}
}
)
)
Expand Down
5 changes: 4 additions & 1 deletion src/HotspotDetection/HotspotDetectionRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { FileMapping } from '../FileMapping/FileMapping'
import { HotspotDetectionResults } from './HotspotDetectionResults'
import { HotspotDetectionParser } from './HotspotDetectionParser'
import { HotspotTree } from './HotspotTree'
import { UserCancellationError } from '../Utils/CancellationError'

export interface HotspotDetectionRunnerRunArguments {
configuration: Configuration // TODO only the relevent properties
Expand Down Expand Up @@ -42,7 +43,9 @@ export abstract class HotspotDetectionRunner {
)
return runningFinishedSuccessfully
? HotspotDetectionRunner.parse(args)
: Promise.reject(new Error('Hotspot Detection was cancelled.'))
: Promise.reject(
new UserCancellationError('Hotspot Detection was cancelled.')
)
}

public static async run(
Expand Down
21 changes: 21 additions & 0 deletions src/Utils/CancellationError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as vscode from 'vscode'

export class UserCancellationError extends Error {
constructor(message?: string) {
super(message)
}

public showErrorMessageNotification(timeout = 3000) {
vscode.window.withProgress(
{
location: vscode.ProgressLocation.Notification,
title: this.message,
cancellable: false,
},
async (progress, token) => {
progress.report({ increment: 100 })
await new Promise((resolve) => setTimeout(resolve, timeout))
}
)
}
}

0 comments on commit 73e82d2

Please sign in to comment.