UNPKG

@prisma/language-server

Version:
92 lines 3.45 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getMultifileHelper = getMultifileHelper; const vscode_uri_1 = require("vscode-uri"); const Schema_1 = require("../lib/Schema"); const path_1 = __importDefault(require("path")); const helper_1 = require("./helper"); const vscode_languageserver_textdocument_1 = require("vscode-languageserver-textdocument"); const schema_files_loader_1 = require("@prisma/schema-files-loader"); const multifileFixturesDir = path_1.default.join(__dirname, '__fixtures__/multi-file'); async function getMultifileHelper(fixturePath) { const schema = await getMultifileSchema(fixturePath); const helper = new MultfileHelper(`/${fixturePath}`, schema); return helper; } class MultfileHelper { constructor(baseDir, schema) { this.baseDir = baseDir; this.schema = schema; } file(filePath) { const doc = this.schema.findDocByUri(vscode_uri_1.URI.file(path_1.default.join(this.baseDir, filePath)).toString()); if (!doc) { throw new Error(`${filePath} is not found fixture`); } return new File(doc); } applyChanges(edits) { if (!edits) { return {}; } const results = {}; for (const [uri, fileEdits] of Object.entries(edits)) { let doc = this.schema.findDocByUri(uri)?.textDocument; if (!doc) { doc = vscode_languageserver_textdocument_1.TextDocument.create(uri, 'prisma', 1, ''); } const updatedDoc = vscode_languageserver_textdocument_1.TextDocument.applyEdits(doc, fileEdits); results[uri] = updatedDoc; } return results; } } class File { constructor(schemaDocument) { this.schemaDocument = schemaDocument; } get textDocument() { return this.schemaDocument.textDocument; } get uri() { return this.schemaDocument.uri; } lineContaining(match) { for (const line of this.schemaDocument.lines) { if (line.text.includes(match)) { return new Line(line.lineIndex, line.untrimmedText); } } throw new Error(`Can not find line with "${match}"`); } } class Line { constructor(lineNumber, text) { this.lineNumber = lineNumber; this.text = text; } characterAfter(substring) { const index = this.text.indexOf(substring); if (index === -1) { throw new Error(`Can not find ${substring} in ${this.text}`); } return { line: this.lineNumber, character: index + substring.length + 1, }; } } // TODO: this should use `PrismaSchema.load` instead. async function getMultifileSchema(folderPath) { const files = await (0, schema_files_loader_1.loadSchemaFiles)(path_1.default.join(multifileFixturesDir, folderPath)); const schemaDocs = files.map(([filePath, content]) => { const uri = (0, helper_1.fixturePathToUri)(filePath, multifileFixturesDir); const doc = vscode_languageserver_textdocument_1.TextDocument.create(uri, 'prisma', 1, content); return new Schema_1.SchemaDocument(doc); }); return new Schema_1.PrismaSchema(schemaDocs); } //# sourceMappingURL=MultifileHelper.js.map