Skip to content

Commit

Permalink
companion pr to prisma/prisma-engines#4934
Browse files Browse the repository at this point in the history
  • Loading branch information
Druue committed Jul 4, 2024
1 parent a4ef9e5 commit 707b7bc
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/language-server/src/lib/MessageHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import {
DocumentSymbol,
SymbolKind,
LocationLink,
ReferenceParams,
Location,
} from 'vscode-languageserver'
import type { TextDocument } from 'vscode-languageserver-textdocument'

Expand Down Expand Up @@ -53,6 +55,7 @@ import {
import { prismaSchemaWasmCompletions, localCompletions } from './completions'
import { PrismaSchema, SchemaDocument } from './Schema'
import { DiagnosticMap } from './DiagnosticMap'
import references from './prisma-schema-wasm/references'

export function handleDiagnosticsRequest(
schema: PrismaSchema,
Expand Down Expand Up @@ -223,6 +226,14 @@ export function handleCompletionRequest(
return prismaSchemaWasmCompletions(schema, params, onError) || localCompletions(schema, document, params, onError)
}

export function handleReferencesRequest(
schema: PrismaSchema,
params: ReferenceParams,
onError?: (errorMessage: string) => void,
): Location[] | undefined {
return references(schema, params, onError)
}

export function handleRenameRequest(
schema: PrismaSchema,
initiatingDocument: TextDocument,
Expand Down
29 changes: 29 additions & 0 deletions packages/language-server/src/lib/prisma-schema-wasm/references.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Location, ReferenceParams } from 'vscode-languageserver'
import { PrismaSchema } from '../Schema'
import { handleFormatPanic, handleWasmError } from './internals'
import { prismaSchemaWasm } from '.'

export default function references(
schema: PrismaSchema,
params: ReferenceParams,
onError?: (errorMessage: string) => void,
): Location[] | undefined {
try {
if (process.env.FORCE_PANIC_PRISMA_SCHEMA) {
handleFormatPanic(() => {
console.debug('Triggering a Rust panic...')
prismaSchemaWasm.debug_panic()
})
}

const response = prismaSchemaWasm.references(JSON.stringify(schema), JSON.stringify(params))

return JSON.parse(response) as Location[]
} catch (e) {
const err = e as Error

handleWasmError(err, 'references', onError)

return undefined
}
}
13 changes: 13 additions & 0 deletions packages/language-server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
DidChangeConfigurationNotification,
Connection,
DocumentSymbolParams,
ReferenceParams,
} from 'vscode-languageserver'
import { createConnection, IPCMessageReader, IPCMessageWriter } from 'vscode-languageserver/node'
import { TextDocument } from 'vscode-languageserver-textdocument'
Expand Down Expand Up @@ -81,6 +82,7 @@ export function startServer(options?: LSOptions): void {
hoverProvider: true,
renameProvider: true,
documentSymbolProvider: true,
referencesProvider: true,
},
}

Expand Down Expand Up @@ -183,6 +185,17 @@ export function startServer(options?: LSOptions): void {
}
})

connection.onReferences(async (params: ReferenceParams) => {
const doc = getDocument(params.textDocument.uri)

if (doc) {
const schema = await PrismaSchema.load(doc, documents)
console.dir(params, { depth: null })

return MessageHandler.handleReferencesRequest(schema, params, showErrorToast)
}
})

// This handler resolves additional information for the item selected in the completion list.
connection.onCompletionResolve((completionItem: CompletionItem) => {
return MessageHandler.handleCompletionResolveRequest(completionItem)
Expand Down

0 comments on commit 707b7bc

Please sign in to comment.