Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notification for imports-out-of-date error #335

Merged
merged 1 commit into from
Oct 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion vscode-lean4/src/leanclient.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { TextDocument, EventEmitter, Diagnostic,
DocumentHighlight, Range, DocumentHighlightKind, workspace,
Disposable, Uri, ConfigurationChangeEvent, OutputChannel, DiagnosticCollection,
WorkspaceFolder, window } from 'vscode'
WorkspaceFolder, window, commands } from 'vscode'
import {
DiagnosticSeverity,
DidChangeTextDocumentParams,
DidCloseTextDocumentParams,
DidOpenTextDocumentNotification,
Expand Down Expand Up @@ -30,6 +31,7 @@ import { logger } from './utils/logger'
import { SemVer } from 'semver';
import { fileExists, isFileInFolder } from './utils/fsHelper';
import { c2pConverter, p2cConverter, patchConverters } from './utils/converters'
import path = require('path')

const escapeRegExp = (s: string) => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');

Expand Down Expand Up @@ -98,6 +100,39 @@ export class LeanClient implements Disposable {
this.elanDefaultToolchain = elanDefaultToolchain;
if (!this.toolchainPath) this.toolchainPath = toolchainPath();
this.subscriptions.push(workspace.onDidChangeConfiguration((e) => this.configChanged(e)));

this.subscriptions.push(this.diagnostics(params => this.checkForImportsOutdatedError(params)))
}

private async checkForImportsOutdatedError(params: PublishDiagnosticsParams) {
const fileUri = Uri.parse(params.uri)
const fileName = path.basename(fileUri.fsPath)
const isImportsOutdatedError = params.diagnostics.some(d =>
d.severity === DiagnosticSeverity.Error
&& d.message.includes('Imports are out of date and must be rebuilt')
&& d.range.start.line === 0
&& d.range.start.character === 0
&& d.range.end.line === 0
&& d.range.end.character === 0)
if (!isImportsOutdatedError) {
return
}

const message = `Imports of '${fileName}' are out of date and must be rebuilt.`
const input = 'Rebuild Imports'
const choice = await window.showInformationMessage(message, input)
if (choice !== input) {
return
}

const fileUriString = fileUri.toString()
const document = workspace.textDocuments.find(doc => doc.uri.toString() === fileUriString)
if (!document || document.isClosed) {
void window.showErrorMessage(`'${fileName}' was closed in the meantime. Imports will not be rebuilt.`)
return
}

await this.restartFile(document)
}

dispose(): void {
Expand Down