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

feat: add prisma format functionality #1798

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions packages/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@
"command": "prisma.generate",
"title": "Generate",
"category": "Prisma"
},
{
"command": "prisma.format",
"title": "Format",
"category": "Prisma"
}
]
},
Expand Down
52 changes: 41 additions & 11 deletions packages/vscode/src/CodeLensProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,18 @@ export class CodelensProvider implements vscode.CodeLensProvider {
const generatorRanges = this.getGeneratorRange(document, token)

const lenses = generatorRanges.map(
(range) =>
(range) => [
new vscode.CodeLens(range, {
title: 'Generate',
command: 'prisma.generate',
tooltip: `Run "prisma generate"`,
// ? (@druue) The arguments property does not seem to actually
// ? return an array of arguments. It would consistently
// ? return one singular string element, even when defined as:
// ? [this.scriptRunner, this.schemaPath]
// ?
// ? I've tried to understand why as there are usages in other
// ? codebases that do pass in multiple args so I have to imagine
// ? that it can work, but unsure.
// ? Reference: https://github.com/microsoft/vscode-extension-samples/blob/main/codelens-sample/
// ? arguments: [this.scriptRunner]
}),
new vscode.CodeLens(range, {
title: 'Format',
command: 'prisma.format',
tooltip: `Run "prisma format"`,
})
]
)

return lenses
Expand Down Expand Up @@ -108,3 +104,37 @@ export function generateClient(_args: string) {

cp.exec(cmd, { cwd: rootPath }, (err, stdout, stderr) => handleExec(err, stdout, stderr))
}

export function formatPrismaSchema(_args: string) {
const prismaFormatOutputChannel = vscode.window.createOutputChannel('Prisma Format')
const rootPath = vscode.workspace.workspaceFolders?.[0].uri.fsPath

const scriptRunner = vscode.workspace.getConfiguration('prisma').get('scriptRunner', 'npx')
const schemaPath: string | undefined = vscode.workspace.getConfiguration('prisma').get('schemaPath')

const pathArgFlag = ` --schema=${schemaPath}`
const cmd = `${scriptRunner} prisma format${schemaPath ? pathArgFlag : ''}`

prismaFormatOutputChannel.clear()
prismaFormatOutputChannel.show(true)
prismaFormatOutputChannel.appendLine(['Running prisma format:', rootPath, cmd].join('\n- '))

const handleExec = (err: cp.ExecException | null, stdout: string, stderr: string) => {
try {
if (err) {
prismaFormatOutputChannel.appendLine(err.message)
return
}
if (stdout) {
prismaFormatOutputChannel.append(stdout)
}
if (stderr) {
prismaFormatOutputChannel.append(stderr)
}
} catch (e) {
prismaFormatOutputChannel.append(e as string)
}
}

cp.exec(cmd, { cwd: rootPath }, (err, stdout, stderr) => handleExec(err, stdout, stderr))
}
3 changes: 2 additions & 1 deletion packages/vscode/src/plugins/prisma-language-server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {
import { PrismaVSCodePlugin } from '../types'
import paths from 'env-paths'
import FileWatcher from 'watcher'
import { CodelensProvider, generateClient } from '../../CodeLensProvider'
import { CodelensProvider, generateClient, formatPrismaSchema } from '../../CodeLensProvider'

const packageJson = require('../../../../package.json') // eslint-disable-line

Expand Down Expand Up @@ -219,6 +219,7 @@ const plugin: PrismaVSCodePlugin = {
}),

commands.registerCommand('prisma.generate', (args: string) => generateClient(args)),
commands.registerCommand('prisma.format', (args: string) => formatPrismaSchema(args)),

commands.registerCommand('prisma.restartLanguageServer', async () => {
client = await restartClient(context, client, serverOptions, clientOptions)
Expand Down
Loading