@@ -5,12 +5,31 @@ import { FormattingOptions, Range, TextEdit } from 'vscode-languageserver';
5
5
6
6
export class ZModelFormatter extends AbstractFormatter {
7
7
private formatOptions ?: FormattingOptions ;
8
+ private isPrismaStyle = true ;
8
9
protected format ( node : AstNode ) : void {
9
10
const formatter = this . getNodeFormatter ( node ) ;
11
+
10
12
if ( ast . isDataModelField ( node ) ) {
11
- formatter . property ( 'type' ) . prepend ( Formatting . oneSpace ( ) ) ;
12
- if ( node . attributes . length > 0 ) {
13
- formatter . properties ( 'attributes' ) . prepend ( Formatting . oneSpace ( ) ) ;
13
+ if ( this . isPrismaStyle && ast . isDataModel ( node . $container ) ) {
14
+ const dataModel = node . $container ;
15
+
16
+ const compareFn = ( a : number , b : number ) => b - a ;
17
+ const maxNameLength = dataModel . fields . map ( ( x ) => x . name . length ) . sort ( compareFn ) [ 0 ] ;
18
+ const maxTypeLength = dataModel . fields . map ( this . getFieldTypeLength ) . sort ( compareFn ) [ 0 ] ;
19
+
20
+ formatter . property ( 'type' ) . prepend ( Formatting . spaces ( maxNameLength - node . name . length + 1 ) ) ;
21
+ if ( node . attributes . length > 0 ) {
22
+ formatter
23
+ . node ( node . attributes [ 0 ] )
24
+ . prepend ( Formatting . spaces ( maxTypeLength - this . getFieldTypeLength ( node ) + 1 ) ) ;
25
+
26
+ formatter . nodes ( ...node . attributes . slice ( 1 ) ) . prepend ( Formatting . oneSpace ( ) ) ;
27
+ }
28
+ } else {
29
+ formatter . property ( 'type' ) . prepend ( Formatting . oneSpace ( ) ) ;
30
+ if ( node . attributes . length > 0 ) {
31
+ formatter . properties ( 'attributes' ) . prepend ( Formatting . oneSpace ( ) ) ;
32
+ }
14
33
}
15
34
} else if ( ast . isDataModelFieldAttribute ( node ) ) {
16
35
formatter . keyword ( '(' ) . surround ( Formatting . noSpace ( ) ) ;
@@ -52,4 +71,22 @@ export class ZModelFormatter extends AbstractFormatter {
52
71
public getIndent ( ) {
53
72
return 1 ;
54
73
}
74
+
75
+ public setPrismaStyle ( isPrismaStyle : boolean ) {
76
+ this . isPrismaStyle = isPrismaStyle ;
77
+ }
78
+
79
+ private getFieldTypeLength ( field : ast . DataModelField ) {
80
+ let length = ( field . type . type || field . type . reference ?. $refText ) ! . length ;
81
+
82
+ if ( field . type . optional ) {
83
+ length += 1 ;
84
+ }
85
+
86
+ if ( field . type . array ) {
87
+ length += 2 ;
88
+ }
89
+
90
+ return length ;
91
+ }
55
92
}
0 commit comments