Skip to content

Commit

Permalink
feat: [lean4web] customizable project precondition checks
Browse files Browse the repository at this point in the history
  • Loading branch information
abentkamp authored and joneugster committed Aug 16, 2024
1 parent 93095f9 commit 403c539
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 27 deletions.
17 changes: 17 additions & 0 deletions vscode-lean4/src/diagnostics/setupDiagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { SemVer } from 'semver'
import { OutputChannel, commands } from 'vscode'
import { ExtUri, FileUri, extUriToCwdUri } from '../utils/exturi'
import { LeanInstaller } from '../utils/leanInstaller'
import { willUseLakeServer } from '../utils/projectInfo'
import { diagnose } from './setupDiagnoser'
import {
PreconditionCheckResult,
Expand Down Expand Up @@ -235,3 +236,19 @@ export async function checkIsVSCodeUpToDate(): Promise<PreconditionCheckResult>
return 'Fulfilled'
}
}

export async function checkLean4ProjectPreconditions(
channel: OutputChannel,
folderUri: ExtUri,
): Promise<PreconditionCheckResult> {
return await checkAll(
() => checkIsValidProjectFolder(channel, folderUri),
() => checkIsLeanVersionUpToDate(channel, folderUri, { modal: false }),
async () => {
if (!(await willUseLakeServer(folderUri))) {
return 'Fulfilled'
}
return await checkIsLakeInstalledCorrectly(channel, folderUri, {})
},
)
}
7 changes: 6 additions & 1 deletion vscode-lean4/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
checkIsElanUpToDate,
checkIsLean4Installed,
checkIsVSCodeUpToDate,
checkLean4ProjectPreconditions,
} from './diagnostics/setupDiagnostics'
import { PreconditionCheckResult } from './diagnostics/setupNotifs'
import { AlwaysEnabledFeatures, Exports, Lean4EnabledFeatures } from './exports'
Expand Down Expand Up @@ -170,7 +171,11 @@ async function activateLean4Features(
return undefined
}

const clientProvider = new LeanClientProvider(installer, installer.getOutputChannel())
const clientProvider = new LeanClientProvider(
installer,
installer.getOutputChannel(),
checkLean4ProjectPreconditions,
)
context.subscriptions.push(clientProvider)

const watchService = new LeanConfigWatchService()
Expand Down
40 changes: 14 additions & 26 deletions vscode-lean4/src/utils/clientProvider.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,12 @@
import { LeanFileProgressProcessingInfo, ServerStoppedReason } from '@leanprover/infoview-api'
import { Disposable, EventEmitter, OutputChannel, TextDocument, commands, window, workspace } from 'vscode'
import {
checkAll,
checkIsLakeInstalledCorrectly,
checkIsLeanVersionUpToDate,
checkIsValidProjectFolder,
} from '../diagnostics/setupDiagnostics'
import { PreconditionCheckResult } from '../diagnostics/setupNotifs'
import { LeanClient } from '../leanclient'
import { ExtUri, FileUri, UntitledUri, getWorkspaceFolderUri, toExtUri } from './exturi'
import { LeanInstaller } from './leanInstaller'
import { logger } from './logger'
import { displayError } from './notifs'
import { findLeanProjectRoot, willUseLakeServer } from './projectInfo'

async function checkLean4ProjectPreconditions(
channel: OutputChannel,
folderUri: ExtUri,
): Promise<PreconditionCheckResult> {
return await checkAll(
() => checkIsValidProjectFolder(channel, folderUri),
() => checkIsLeanVersionUpToDate(channel, folderUri, { modal: false }),
async () => {
if (!(await willUseLakeServer(folderUri))) {
return 'Fulfilled'
}
return await checkIsLakeInstalledCorrectly(channel, folderUri, {})
},
)
}
import { findLeanProjectRoot } from './projectInfo'

// This class ensures we have one LeanClient per folder.
export class LeanClientProvider implements Disposable {
Expand All @@ -53,7 +31,14 @@ export class LeanClientProvider implements Disposable {
private clientStoppedEmitter = new EventEmitter<[LeanClient, boolean, ServerStoppedReason]>()
clientStopped = this.clientStoppedEmitter.event

constructor(installer: LeanInstaller, outputChannel: OutputChannel) {
constructor(
installer: LeanInstaller,
outputChannel: OutputChannel,
private checkLean4ProjectPreconditions: (
channel: OutputChannel,
folderUri: ExtUri,
) => Promise<PreconditionCheckResult>,
) {
this.outputChannel = outputChannel
this.installer = installer

Expand Down Expand Up @@ -122,7 +107,10 @@ export class LeanClientProvider implements Disposable {
continue
}

const preconditionCheckResult = await checkLean4ProjectPreconditions(this.outputChannel, projectUri)
const preconditionCheckResult = await this.checkLean4ProjectPreconditions(
this.outputChannel,
projectUri,
)
if (preconditionCheckResult !== 'Fatal') {
logger.log('[ClientProvider] got lean version 4')
const [cached, client] = await this.ensureClient(uri)
Expand Down Expand Up @@ -234,7 +222,7 @@ export class LeanClientProvider implements Disposable {
}
this.pending.set(key, true)

const preconditionCheckResult = await checkLean4ProjectPreconditions(this.outputChannel, folderUri)
const preconditionCheckResult = await this.checkLean4ProjectPreconditions(this.outputChannel, folderUri)
if (preconditionCheckResult === 'Fatal') {
this.pending.delete(key)
return [false, undefined]
Expand Down

0 comments on commit 403c539

Please sign in to comment.