|
| 1 | +import * as J from '../../java'; |
| 2 | +import * as JS from '..'; |
| 3 | +import {ClassDeclaration, Space} from '../../java'; |
| 4 | +import {Cursor, InMemoryExecutionContext} from "../../core"; |
| 5 | +import {JavaScriptVisitor} from ".."; |
| 6 | +import {BlankLinesStyle} from "../style"; |
| 7 | + |
| 8 | +export class BlankLinesFormatVisitor extends JavaScriptVisitor<InMemoryExecutionContext> { |
| 9 | + private style: BlankLinesStyle; |
| 10 | + |
| 11 | + constructor(style: BlankLinesStyle) { |
| 12 | + super(); |
| 13 | + this.style = style; |
| 14 | + this.cursor = new Cursor(null, Cursor.ROOT_VALUE); |
| 15 | + } |
| 16 | + |
| 17 | + visitJsCompilationUnit(compilationUnit: JS.CompilationUnit, p: InMemoryExecutionContext): J.J | null { |
| 18 | + if (compilationUnit.prefix.comments.length == 0) { |
| 19 | + compilationUnit = compilationUnit.withPrefix(Space.EMPTY); |
| 20 | + } |
| 21 | + return super.visitJsCompilationUnit(compilationUnit, p); |
| 22 | + } |
| 23 | + |
| 24 | + visitStatement(statement: J.Statement, p: InMemoryExecutionContext): J.J { |
| 25 | + statement = super.visitStatement(statement, p); |
| 26 | + |
| 27 | + const parentCursor = this.cursor.parentTreeCursor(); |
| 28 | + const topLevel = parentCursor.value() instanceof JS.CompilationUnit; |
| 29 | + |
| 30 | + let prevBlankLine: number | null | undefined; |
| 31 | + const blankLines = this.getBlankLines(statement, parentCursor); |
| 32 | + if (blankLines) { |
| 33 | + prevBlankLine = parentCursor.getMessage('prev_blank_line', undefined); |
| 34 | + parentCursor.putMessage('prev_blank_line', blankLines); |
| 35 | + } else { |
| 36 | + prevBlankLine = parentCursor.getMessage('prev_blank_line', undefined); |
| 37 | + if (prevBlankLine) { |
| 38 | + parentCursor.putMessage('prev_blank_line', undefined); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + if (topLevel) { |
| 43 | + const isFirstStatement = p.getMessage<boolean>('is_first_statement', true) ?? true; |
| 44 | + if (isFirstStatement) { |
| 45 | + p.putMessage('is_first_statement', false); |
| 46 | + } else { |
| 47 | + const minLines = statement instanceof JS.JsImport ? 0 : max(prevBlankLine, blankLines); |
| 48 | + statement = adjustedLinesForTree(statement, minLines, this.style.keepMaximum.inCode); |
| 49 | + } |
| 50 | + } else { |
| 51 | + const inBlock = parentCursor.value() instanceof J.Block; |
| 52 | + const inClass = inBlock && parentCursor.parentTreeCursor().value() instanceof J.ClassDeclaration; |
| 53 | + let minLines = 0; |
| 54 | + |
| 55 | + if (inClass) { |
| 56 | + const isFirst = (parentCursor.value() as J.Block).statements[0] === statement; |
| 57 | + minLines = isFirst ? 0 : max(blankLines, prevBlankLine); |
| 58 | + } |
| 59 | + |
| 60 | + statement = adjustedLinesForTree(statement, minLines, this.style.keepMaximum.inCode); |
| 61 | + } |
| 62 | + return statement; |
| 63 | + } |
| 64 | + |
| 65 | + getBlankLines(statement: J.Statement, cursor: Cursor): number | undefined { |
| 66 | + const inBlock = cursor.value() instanceof J.Block; |
| 67 | + let type; |
| 68 | + if (inBlock) { |
| 69 | + const val = cursor.parentTreeCursor().value(); |
| 70 | + if (val instanceof J.ClassDeclaration) { |
| 71 | + type = val.padding.kind.type; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + if (type === ClassDeclaration.Kind.Type.Interface && (statement instanceof J.MethodDeclaration || statement instanceof JS.JSMethodDeclaration)) { |
| 76 | + return this.style.minimum.aroundMethodInInterface ?? undefined; |
| 77 | + } else if (type === ClassDeclaration.Kind.Type.Interface && (statement instanceof J.VariableDeclarations || statement instanceof JS.JSVariableDeclarations)) { |
| 78 | + return this.style.minimum.aroundFieldInInterface ?? undefined; |
| 79 | + } else if (type === ClassDeclaration.Kind.Type.Class && (statement instanceof J.VariableDeclarations || statement instanceof JS.JSVariableDeclarations)) { |
| 80 | + return this.style.minimum.aroundField; |
| 81 | + } else if (statement instanceof JS.JsImport) { |
| 82 | + return this.style.minimum.afterImports; |
| 83 | + } else if (statement instanceof J.ClassDeclaration) { |
| 84 | + return this.style.minimum.aroundClass; |
| 85 | + } else if (statement instanceof J.MethodDeclaration || statement instanceof JS.JSMethodDeclaration) { |
| 86 | + return this.style.minimum.aroundMethod; |
| 87 | + } else if (statement instanceof JS.FunctionDeclaration) { |
| 88 | + return this.style.minimum.aroundFunction; |
| 89 | + } else { |
| 90 | + return undefined; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | +} |
| 95 | + |
| 96 | +function adjustedLinesForTree(tree: J.J, minLines: number, maxLines: number): J.J { |
| 97 | + |
| 98 | + if (tree instanceof JS.ScopedVariableDeclarations && tree.padding.scope) { |
| 99 | + const prefix = tree.padding.scope.before; |
| 100 | + return tree.padding.withScope(tree.padding.scope.withBefore(adjustedLinesForSpace(prefix, minLines, maxLines))); |
| 101 | + } else { |
| 102 | + const prefix = tree.prefix; |
| 103 | + return tree.withPrefix(adjustedLinesForSpace(prefix, minLines, maxLines)); |
| 104 | + } |
| 105 | + |
| 106 | +} |
| 107 | + |
| 108 | +function adjustedLinesForSpace(prefix: Space, minLines: number, maxLines: number): Space { |
| 109 | + if (prefix.comments.length == 0 || prefix.whitespace?.includes('\n')) { |
| 110 | + return prefix.withWhitespace(adjustedLinesForString(prefix.whitespace ?? '', minLines, maxLines)); |
| 111 | + } |
| 112 | + |
| 113 | + return prefix; |
| 114 | +} |
| 115 | + |
| 116 | +function adjustedLinesForString(whitespace: string, minLines: number, maxLines: number): string { |
| 117 | + const existingBlankLines = Math.max(countLineBreaks(whitespace) - 1, 0); |
| 118 | + maxLines = Math.max(minLines, maxLines); |
| 119 | + |
| 120 | + if (existingBlankLines >= minLines && existingBlankLines <= maxLines) { |
| 121 | + return whitespace; |
| 122 | + } else if (existingBlankLines < minLines) { |
| 123 | + return '\n'.repeat(minLines - existingBlankLines) + whitespace; |
| 124 | + } else { |
| 125 | + return '\n'.repeat(maxLines) + whitespace.substring(whitespace.lastIndexOf('\n')); |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +function countLineBreaks(whitespace: string): number { |
| 130 | + return (whitespace.match(/\n/g) || []).length; |
| 131 | +} |
| 132 | + |
| 133 | +function max(x: number | null | undefined, y: number | null | undefined) { |
| 134 | + if (x && y) { |
| 135 | + return Math.max(x, y); |
| 136 | + } else { |
| 137 | + return x ? x : y ? y : 0; |
| 138 | + } |
| 139 | +} |
0 commit comments