Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Heptazhou committed Sep 2, 2023
1 parent 09ced44 commit 507f116
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"displayName": "Julia Formatter",
"publisher": "0h7z",
"icon": "icon.ico",
"version": "1.2.0",
"version": "1.2.1",
"license": "AGPL-3.0-or-later",
"description": "A formatter for the Julia language",
"repository": {
Expand Down Expand Up @@ -56,7 +56,7 @@
},
"scripts": {
"npm": "npm i && npm ci && npm up",
"all": "npm ci && pnpm compile && pnpm build && pnpm package",
"all": "npm ci && pnpm compile && pnpm build && pnpm package && npm ci",
"upd": "pnpm up --latest && pnpm add -D @types/[email protected] && pnpm npm",
"package": "julia pre-package.jl && vsce package && julia post-package.jl",
"build": "pnpm esbuild --minify",
Expand Down
38 changes: 27 additions & 11 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import untildify from "untildify"
export const promiseExec = util.promisify(cp.exec)
export let registration: vscode.Disposable | undefined

let installingJlFmt = false
const vscodeOutput = vscode.window.createOutputChannel("Julia Formatter")
const progressBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, -1)
progressBar.text = "Formatting..."
Expand Down Expand Up @@ -83,25 +84,40 @@ export async function buildFormatArgs(path: string): Promise<string[]> {
export async function installFormatter(): Promise<void> {
const julia = await getJulia()
try {
await promiseExec(`${julia} -e "using Pkg; Pkg.Registry.update(); Pkg.add(\\"JuliaFormatter\\")"`)
await promiseExec(`${julia} -e "using Pkg; Pkg.Registry.update(); Pkg.add(\\"JuliaFormatter\\"); Pkg.update(\\"JuliaFormatter\\")"`)
} catch (err) {
vscode.window.showErrorMessage(`Could not install JuliaFormatter automatically. Try manually installing with \` julia -e "using Pkg; Pkg.add(string(:JuliaFormatter))" \`.\n\nFull error: ${err}.`)
vscode.window.showErrorMessage(`Could not install JuliaFormatter automatically. Try manually installing with \` julia -e "using Pkg; Pkg.update(); Pkg.add(string(:JuliaFormatter))" \`.\n\nFull error: ${err}.`)
throw err
}
}

// From https://github.com/iansan5653/vscode-format-python-docstrings/blob/0135de8/src/extension.ts#L101-L132
export async function alertFormattingError(err: FormatException): Promise<void> {
vscodeOutput.appendLine(err.message)

if (err.message.includes("Package JuliaFormatter not found")) {
export async function alertFormattingError(error: FormatException): Promise<void> {
const err = error.message
vscodeOutput.appendLine(err)

if (
err.includes("ERROR: ArgumentError: Package JuliaFormatter not found") ||
err.includes("ERROR: MethodError: no method matching JuliaFormatter.Options") ||
err.includes("ERROR: UndefVarError: `valid_for_in_op` not defined") //
) {
const installButton = "Install Module"
const response = await vscode.window.showErrorMessage(`The Julia package "JuliaFormatter" must be installed to format files.`, installButton)
if (response === installButton) installFormatter()
const response = await vscode.window.showErrorMessage(`The Julia package "JuliaFormatter" must be installed to format code. Installation will take some time.`, installButton)
if (response !== installButton) return
let progress = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, -1)
try {
installingJlFmt = true
progress.text = "Installing JuliaFormatter..."
progress.show()
await installFormatter()
} finally {
installingJlFmt = false
progress.dispose()
}
} else {
const bugReportButton = "Submit Bug Report"
const err_header_match = err.message.match(/^(ERROR:.*)/m)
const err_body = err_header_match !== null ? err_header_match[1] : `Unknown Error: Could not format file. Full error:\n\n${err.message}`
const err_header_match = err.match(/^(ERROR:.*)/m)
const err_body = err_header_match !== null ? err_header_match[1] : `Unknown Error: Could not format code. Full error:\n\n${err}`

const response = await vscode.window.showErrorMessage(err_body, bugReportButton)
if (response === bugReportButton) vscode.commands.executeCommand("vscode.open", vscode.Uri.parse("https://github.com/0h7z/vscode-julia-format/issues/new"))
Expand Down Expand Up @@ -144,7 +160,7 @@ export async function format(path: string, content: string): Promise<Hunk[]> {
return parsed[0].hunks
} catch (e) {
const err = <FormatException>e
alertFormattingError(err)
if (!installingJlFmt) alertFormattingError(err)
throw err
} finally {
progressBar.hide()
Expand Down

0 comments on commit 507f116

Please sign in to comment.