Skip to content

Commit

Permalink
Split schema support (#1718)
Browse files Browse the repository at this point in the history
Use `@prisma/schema-files-loader` to resolve schema files from
memory or disk.
See also prisma/prisma#24085

Message handler's methods now accept `PrismaSchema` instance from
outside, rather than creating it from a single document. Singatures of
the methods are also adjusted to consistently be in `(schema,
intitianigDocument, params)` form.
  • Loading branch information
SevInf committed May 15, 2024
1 parent 8fe7116 commit 852c5f8
Show file tree
Hide file tree
Showing 24 changed files with 391 additions and 190 deletions.
100 changes: 99 additions & 1 deletion packages/language-server/package-lock.json

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

6 changes: 4 additions & 2 deletions packages/language-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@
"typings": "dist/src/index",
"dependencies": {
"@prisma/prisma-schema-wasm": "5.15.0-3.97f638f5e0f371a1a553cf726f9d597bbe811bff",
"@prisma/schema-files-loader": "5.14.0-dev.77",
"@types/js-levenshtein": "1.1.3",
"js-levenshtein": "1.1.6",
"klona": "2.0.6",
"nyc": "15.1.0",
"vscode-languageserver": "8.1.0",
"vscode-languageserver-textdocument": "1.0.11"
"vscode-languageserver-textdocument": "1.0.11",
"vscode-uri": "^3.0.8"
},
"devDependencies": {
"@types/mocha": "10.0.6",
Expand All @@ -60,4 +62,4 @@
"publishConfig": {
"access": "public"
}
}
}
13 changes: 7 additions & 6 deletions packages/language-server/src/__test__/artificial-panic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { CURSOR_CHARACTER, findCursorPosition, getTextDocument } from './helper'

import * as assert from 'assert'
import { PrismaSchema } from '../lib/Schema'

suite('Artificial Panics', () => {
const OLD_ENV = { ...process.env }
Expand Down Expand Up @@ -49,7 +50,7 @@ suite('Artificial Panics', () => {
}

try {
const _codeActions = handleCodeActions(params, document, onError)
const _codeActions = handleCodeActions(PrismaSchema.singleFile(document), document, params, onError)

assert.fail("This shouldn't happen!")
} catch (e) {
Expand Down Expand Up @@ -83,7 +84,7 @@ suite('Artificial Panics', () => {
}

try {
const _formatResult = handleDocumentFormatting(params, document, onError)
const _formatResult = handleDocumentFormatting(PrismaSchema.singleFile(document), document, params, onError)

assert.fail("This shouldn't happen!")
} catch (e) {
Expand All @@ -109,7 +110,7 @@ suite('Artificial Panics', () => {
}

try {
const _diagnostics = handleDiagnosticsRequest(document, onError)
const _diagnostics = handleDiagnosticsRequest(PrismaSchema.singleFile(document), onError)

assert.fail("This shouldn't happen!")
} catch (e) {
Expand Down Expand Up @@ -151,7 +152,7 @@ suite('Artificial Panics', () => {
}

try {
const _completions = handleCompletionRequest(params, document, onError)
const _completions = handleCompletionRequest(PrismaSchema.singleFile(document), document, params, onError)

assert.fail("This shouldn't happen!")
} catch (e) {
Expand Down Expand Up @@ -193,7 +194,7 @@ suite('Artificial Panics', () => {
}

try {
const _completions = handleCompletionRequest(params, document, onError)
const _completions = handleCompletionRequest(PrismaSchema.singleFile(document), document, params, onError)

assert.fail("This shouldn't happen!")
} catch (e) {
Expand Down Expand Up @@ -224,7 +225,7 @@ suite('Artificial Panics', () => {
}

try {
const _completions = handleCompletionRequest(params, document, onError)
const _completions = handleCompletionRequest(PrismaSchema.singleFile(document), document, params, onError)

assert.fail("This shouldn't happen!")
} catch (e) {
Expand Down
11 changes: 7 additions & 4 deletions packages/language-server/src/__test__/completion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import { CompletionList, CompletionParams, CompletionItemKind, CompletionTrigger
import assert from 'assert'
import dedent from 'ts-dedent'
import { CURSOR_CHARACTER, findCursorPosition } from './helper'

/* eslint-disable @typescript-eslint/restrict-template-expressions, @typescript-eslint/no-misused-promises */
import { PrismaSchema } from '../lib/Schema'

type DatasourceProvider = 'sqlite' | 'postgresql' | 'mysql' | 'mongodb' | 'sqlserver' | 'cockroachdb'

Expand Down Expand Up @@ -57,7 +56,7 @@ function assertCompletion({

const position = findCursorPosition(schema)
const document: TextDocument = TextDocument.create(
'completions/none.prisma',
'file:///completions/none.prisma',
'prisma',
1,
schema.replace(CURSOR_CHARACTER, ''),
Expand All @@ -71,7 +70,11 @@ function assertCompletion({
},
}

const completionResult: CompletionList | undefined = handleCompletionRequest(completionParams, document)
const completionResult: CompletionList | undefined = handleCompletionRequest(
PrismaSchema.singleFile(document),
document,
completionParams,
)

assert.ok(completionResult !== undefined)

Expand Down
3 changes: 2 additions & 1 deletion packages/language-server/src/__test__/format.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { handleDocumentFormatting } from '../lib/MessageHandler'
import { TextEdit, DocumentFormattingParams } from 'vscode-languageserver'
import * as assert from 'assert'
import { getTextDocument } from './helper'
import { PrismaSchema } from '../lib/Schema'

function assertFormat(fixturePath: string): void {
const textDocument = getTextDocument(fixturePath)
Expand All @@ -13,7 +14,7 @@ function assertFormat(fixturePath: string): void {
},
}

const formatResult: TextEdit[] = handleDocumentFormatting(params, textDocument)
const formatResult: TextEdit[] = handleDocumentFormatting(PrismaSchema.singleFile(textDocument), textDocument, params)

assert.ok(formatResult.length !== 0)
}
Expand Down
19 changes: 17 additions & 2 deletions packages/language-server/src/__test__/helper.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
import * as fs from 'fs'
import { Position, TextDocument } from 'vscode-languageserver-textdocument'
import path from 'path'
import { URI } from 'vscode-uri'

export const CURSOR_CHARACTER = '|'

const fixturesDir = path.resolve(__dirname, '../../../test/fixtures')

export function getTextDocument(testFilePath: string): TextDocument {
const content: string = fs.readFileSync(path.join(__dirname, '../../../test/fixtures', testFilePath), 'utf8')
return TextDocument.create(testFilePath, 'prisma', 1, content)
const absPath = path.join(fixturesDir, testFilePath)
const content: string = fs.readFileSync(absPath, 'utf8')

return TextDocument.create(fixturePathToUri(testFilePath), 'prisma', 1, content)
}

export function fixturePathToUri(fixturePath: string) {
const absPath = path.join(fixturesDir, fixturePath)

// that would normalize testFilePath and resolve all of
// the . and ..
const relPath = path.relative(fixturesDir, absPath)
// this will normalize slashes on win/linux
return URI.file(`/${relPath}`).toString()
}

export const findCursorPosition = (input: string): Position => {
Expand Down
3 changes: 2 additions & 1 deletion packages/language-server/src/__test__/hover.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { handleHoverRequest } from '../lib/MessageHandler'
import { Hover } from 'vscode-languageserver'
import * as assert from 'assert'
import { getTextDocument } from './helper'
import { PrismaSchema } from '../lib/Schema'

function assertHover(position: Position, expected: Hover, fixturePath: string): void {
const textDocument = getTextDocument(fixturePath)
Expand All @@ -11,7 +12,7 @@ function assertHover(position: Position, expected: Hover, fixturePath: string):
textDocument,
position: position,
}
const hoverResult: Hover | undefined = handleHoverRequest(textDocument, params)
const hoverResult: Hover | undefined = handleHoverRequest(PrismaSchema.singleFile(textDocument), textDocument, params)

assert.ok(hoverResult !== undefined)
assert.deepStrictEqual(hoverResult.contents, expected.contents)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ import { handleDefinitionRequest } from '../lib/MessageHandler'
import { LocationLink, Range } from 'vscode-languageserver'
import * as assert from 'assert'
import { getTextDocument } from './helper'
import { PrismaSchema } from '../lib/Schema'

function assertJumpToDefinition(position: Position, expectedRange: Range, fixturePath: string): void {
const textDocument = getTextDocument(fixturePath)

const params = { textDocument, position }
const defResult: LocationLink[] | undefined = handleDefinitionRequest(textDocument, params)
const defResult: LocationLink[] | undefined = handleDefinitionRequest(
PrismaSchema.singleFile(textDocument),
textDocument,
params,
)

assert.ok(defResult !== undefined)
assert.deepStrictEqual(defResult[0].targetRange, expectedRange)
Expand Down
3 changes: 2 additions & 1 deletion packages/language-server/src/__test__/linting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import * as assert from 'assert'
import { getTextDocument } from './helper'
import { MAX_SAFE_VALUE_i32 } from '../lib/constants'
import listAllAvailablePreviewFeatures from '../lib/prisma-schema-wasm/listAllAvailablePreviewFeatures'
import { PrismaSchema } from '../lib/Schema'

function assertLinting(expected: Diagnostic[], fixturePath: string): void {
const document = getTextDocument(fixturePath)

const diagnosticsResults: Diagnostic[] = handleDiagnosticsRequest(document)
const diagnosticsResults: Diagnostic[] = handleDiagnosticsRequest(PrismaSchema.singleFile(document)).get(document.uri)

assert.ok(diagnosticsResults.length != 0)
expected.forEach((expectedDiagnostic, i) => {
Expand Down

0 comments on commit 852c5f8

Please sign in to comment.