Skip to content

Commit

Permalink
tech(LS): moving hover functionality to engines (#1766)
Browse files Browse the repository at this point in the history
* Moves hover impl to engines, for further details:
prisma/prisma-engines#4923
  • Loading branch information
Druue authored Jul 11, 2024
1 parent be2834d commit 20b25b1
Show file tree
Hide file tree
Showing 14 changed files with 207 additions and 135 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// This is a blog post
model Post {
id String @id @default(uuid())
id String @id @default(uuid()) @map("_id")
title String
content String
authorId String
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
/// This is the user of the platform
model User {
id String @id @default(uuid())
id String @id @default(uuid()) @map("_id")
name String
email String
posts Post[]
address Address
favouriteAnimal FavouriteAnimal
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/// Petrichor V
type Address {
/// ISO 3166-2 standard
country String
POBox Int
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/// My favourite is the red panda, could you tell?
enum FavouriteAnimal {
RedPanda
Cat
Dog
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
datasource db {
provider = "postgresql"
provider = "mongodb"
url = env("DATABASE_URL")
}

Expand Down
135 changes: 135 additions & 0 deletions packages/language-server/src/__test__/hover.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import { test, expect, describe } from 'vitest'
import { handleHoverRequest } from '../lib/MessageHandler'
import { getMultifileHelper } from './MultifileHelper'

describe('hover', () => {
test('model doc from field', async () => {
const helper = await getMultifileHelper('user-posts')
const user = helper.file('User.prisma')

const response = handleHoverRequest(helper.schema, user.textDocument, {
textDocument: {
uri: user.uri,
},
position: user.lineContaining('posts Post[]').characterAfter('Po'),
})

expect(response).toMatchInlineSnapshot(`
{
"contents": {
"kind": "markdown",
"value": "\`\`\`prisma
model Post {
...
author User @relation(name: "PostToUser", fields: [authorId], references: [id])
}
\`\`\`
___
one-to-many
___
This is a blog post",
},
}
`)
})

test('enum doc from field', async () => {
const helper = await getMultifileHelper('user-posts')
const user = helper.file('User.prisma')

const response = handleHoverRequest(helper.schema, user.textDocument, {
textDocument: {
uri: user.uri,
},
position: user.lineContaining('favouriteAnimal FavouriteAnimal').characterAfter('Favo'),
})

expect(response).toMatchInlineSnapshot(`
{
"contents": {
"kind": "markdown",
"value": "\`\`\`prisma
enum FavouriteAnimal {}
\`\`\`
___
My favourite is the red panda, could you tell?",
},
}
`)
})

test('composite doc from field', async () => {
const helper = await getMultifileHelper('user-posts')
const user = helper.file('User.prisma')

const response = handleHoverRequest(helper.schema, user.textDocument, {
textDocument: {
uri: user.uri,
},
position: user.lineContaining('address Address').characterAfter('Addr'),
})

expect(response).toMatchInlineSnapshot(`
{
"contents": {
"kind": "markdown",
"value": "\`\`\`prisma
type Address {}
\`\`\`
___
Petrichor V",
},
}
`)
})

test('doc from block name', async () => {
const helper = await getMultifileHelper('user-posts')
const user = helper.file('animal.prisma')

const response = handleHoverRequest(helper.schema, user.textDocument, {
textDocument: {
uri: user.uri,
},
position: user.lineContaining('enum FavouriteAnimal {').characterAfter('Fav'),
})

expect(response).toMatchInlineSnapshot(`
{
"contents": {
"kind": "markdown",
"value": "\`\`\`prisma
enum FavouriteAnimal {}
\`\`\`
___
My favourite is the red panda, could you tell?",
},
}
`)
})

test('doc from field name', async () => {
const helper = await getMultifileHelper('user-posts')
const user = helper.file('address.prisma')

const response = handleHoverRequest(helper.schema, user.textDocument, {
textDocument: {
uri: user.uri,
},
position: user.lineContaining('country String').characterAfter('cou'),
})

expect(response).toMatchInlineSnapshot(`
{
"contents": {
"kind": "markdown",
"value": "\`\`\`prisma
country
\`\`\`
___
ISO 3166-2 standard",
},
}
`)
})
})
21 changes: 0 additions & 21 deletions packages/language-server/src/__test__/hover/multi-file.test.ts

This file was deleted.

48 changes: 0 additions & 48 deletions packages/language-server/src/__test__/hover/single-file.test.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ test('basic doc', async () => {
"targetRange": {
"end": {
"character": 1,
"line": 6,
"line": 10,
},
"start": {
"character": 0,
Expand Down
33 changes: 14 additions & 19 deletions packages/language-server/src/__test__/rename/multi-file.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ test('rename model', async () => {
"range": {
"end": {
"character": 1,
"line": 6,
"line": 10,
},
"start": {
"character": 0,
"line": 6,
"line": 10,
},
},
},
Expand All @@ -69,7 +69,7 @@ test('rename model', async () => {
{
"file:///user-posts/Post.prisma": "/// This is a blog post
model Post {
id String @id @default(uuid())
id String @id @default(uuid()) @map("_id")
title String
content String
authorId String
Expand All @@ -78,10 +78,14 @@ test('rename model', async () => {
",
"file:///user-posts/User.prisma": "/// This is the user of the platform
model Account {
id String @id @default(uuid())
id String @id @default(uuid()) @map("_id")
name String
email String
posts Post[]
address Address
favouriteAnimal FavouriteAnimal
@@map("User")
}
",
Expand Down Expand Up @@ -133,19 +137,6 @@ test('rename field', async () => {
},
},
},
{
"newText": " @map("id")",
"range": {
"end": {
"character": 36,
"line": 2,
},
"start": {
"character": 36,
"line": 2,
},
},
},
],
},
}
Expand All @@ -155,7 +146,7 @@ test('rename field', async () => {
{
"file:///user-posts/Post.prisma": "/// This is a blog post
model Post {
id String @id @default(uuid())
id String @id @default(uuid()) @map("_id")
title String
content String
authorId String
Expand All @@ -164,10 +155,14 @@ test('rename field', async () => {
",
"file:///user-posts/User.prisma": "/// This is the user of the platform
model User {
primaryKey String @id @default(uuid() @map("id"))
primaryKey String @id @default(uuid()) @map("_id")
name String
email String
posts Post[]
address Address
favouriteAnimal FavouriteAnimal
}
",
}
Expand Down
23 changes: 3 additions & 20 deletions packages/language-server/src/lib/MessageHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import { prismaSchemaWasmCompletions, localCompletions } from './completions'
import { PrismaSchema, SchemaDocument } from './Schema'
import { DiagnosticMap } from './DiagnosticMap'
import references from './prisma-schema-wasm/references'
import hover from './prisma-schema-wasm/hover'

export function handleDiagnosticsRequest(
schema: PrismaSchema,
Expand Down Expand Up @@ -190,27 +191,9 @@ export function handleHoverRequest(
schema: PrismaSchema,
initiatingDocument: TextDocument,
params: HoverParams,
onError?: (errorMessage: string) => void,
): Hover | undefined {
const position = params.position

const word = getWordAtPosition(initiatingDocument, position)

if (word === '') {
return
}

const block = getDatamodelBlock(word, schema)
if (!block) {
return
}

const blockDocumentation = getDocumentationForBlock(block)

if (blockDocumentation.length !== 0) {
return {
contents: blockDocumentation.join('\n\n'),
}
}
return hover(schema, initiatingDocument, params, onError)
}

/**
Expand Down
Loading

0 comments on commit 20b25b1

Please sign in to comment.