-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Preparatory refactor for split schema support (#1713)
* [WIP]: Preparatory refactor for split schema support * Update packages/language-server/src/lib/validations.ts Co-authored-by: Sophie <29753584+Druue@users.noreply.github.com> * Fix completions * Fix renames * Fix jump-to-definition and cleanup * Update packages/language-server/src/lib/completions/types.ts Co-authored-by: Sophie <29753584+Druue@users.noreply.github.com> * Cleanup * Line object * Apply suggestions from code review Co-authored-by: Sophie <29753584+Druue@users.noreply.github.com> * Flip condition in attributes --------- Co-authored-by: Sophie <29753584+Druue@users.noreply.github.com>
- Loading branch information
Showing
14 changed files
with
460 additions
and
336 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { TextDocument } from 'vscode-languageserver-textdocument' | ||
|
||
export type Line = { | ||
readonly document: SchemaDocument | ||
readonly lineIndex: number | ||
readonly text: string | ||
readonly untrimmedText: string | ||
} | ||
export class SchemaDocument { | ||
readonly lines: Line[] | ||
|
||
static fromTextDocument(textDocument: TextDocument): SchemaDocument { | ||
return new SchemaDocument(textDocument.uri, textDocument.getText()) | ||
} | ||
|
||
constructor( | ||
readonly uri: string, | ||
readonly content: string, | ||
) { | ||
this.lines = content.split(/\r?\n/).map((untrimmedText, lineIndex) => ({ | ||
document: this, | ||
lineIndex, | ||
untrimmedText, | ||
text: untrimmedText.trim(), | ||
})) | ||
} | ||
|
||
getLineContent(lineIndex: number): string { | ||
return this.lines[lineIndex].text | ||
} | ||
} | ||
|
||
type FindRegexpResult = { | ||
match: RegExpExecArray | ||
documentUri: string | ||
} | ||
|
||
export class PrismaSchema { | ||
static singleFile(textDocument: TextDocument) { | ||
return new PrismaSchema([SchemaDocument.fromTextDocument(textDocument)]) | ||
} | ||
|
||
constructor(private readonly documents: SchemaDocument[]) {} | ||
|
||
*iterLines(): Generator<Line, void, void> { | ||
for (const doc of this.documents) { | ||
for (const line of doc.lines) { | ||
yield line | ||
} | ||
} | ||
} | ||
|
||
linesAsArray(): Line[] { | ||
return Array.from(this.iterLines()) | ||
} | ||
|
||
findDocByUri(fileUri: string): SchemaDocument | undefined { | ||
return this.documents.find((doc) => doc.uri === fileUri) | ||
} | ||
|
||
findWithRegex(regexp: RegExp): FindRegexpResult | undefined { | ||
for (const doc of this.documents) { | ||
regexp.lastIndex = 0 | ||
const match = regexp.exec(doc.content) | ||
if (match) { | ||
return { match, documentUri: doc.uri } | ||
} | ||
} | ||
return undefined | ||
} | ||
} |
Oops, something went wrong.