-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Formatter infrastructure and BlankLines formatter (#213)
* Formatter styles classes * Added BlankLines formatter * typos * remove unused imports --------- Co-authored-by: Andrii Rodionov <andriih@moderne.io>
- Loading branch information
Showing
26 changed files
with
1,455 additions
and
86 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
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,58 @@ | ||
import {Marker, MarkerSymbol} from "./markers"; | ||
import {UUID} from "./utils"; | ||
|
||
export abstract class Style { | ||
merge(lowerPrecedence: Style): Style { | ||
return this; | ||
} | ||
|
||
applyDefaults(): Style { | ||
return this; | ||
} | ||
} | ||
|
||
export class NamedStyles implements Marker { | ||
[MarkerSymbol] = true; | ||
|
||
private readonly _id: UUID; | ||
name: string; | ||
displayName: string; | ||
description?: string; | ||
tags: Set<string>; | ||
styles: Style[]; | ||
|
||
constructor(id: UUID, name: string, displayName: string, description?: string, tags: Set<string> = new Set(), styles: Style[] = []) { | ||
this._id = id; | ||
this.name = name; | ||
this.displayName = displayName; | ||
this.description = description; | ||
this.tags = tags; | ||
this.styles = styles; | ||
} | ||
|
||
public get id(): UUID { | ||
return this._id; | ||
} | ||
|
||
public withId(id: UUID): NamedStyles { | ||
return id === this._id ? this : new NamedStyles(id, this.name, this.displayName, this.description, this.tags, this.styles); | ||
} | ||
|
||
static merge<S extends Style>(styleClass: new (...args: any[]) => S, namedStyles: NamedStyles[]): S | null { | ||
let merged: S | null = null; | ||
|
||
for (const namedStyle of namedStyles) { | ||
if (namedStyle.styles) { | ||
for (let style of namedStyle.styles) { | ||
if (style instanceof styleClass) { | ||
style = style.applyDefaults(); | ||
merged = merged ? (merged.merge(style) as S) : (style as S); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return merged; | ||
} | ||
|
||
} |
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,139 @@ | ||
import * as J from '../../java'; | ||
import * as JS from '..'; | ||
import {ClassDeclaration, Space} from '../../java'; | ||
import {Cursor, InMemoryExecutionContext} from "../../core"; | ||
import {JavaScriptVisitor} from ".."; | ||
import {BlankLinesStyle} from "../style"; | ||
|
||
export class BlankLinesFormatVisitor extends JavaScriptVisitor<InMemoryExecutionContext> { | ||
private style: BlankLinesStyle; | ||
|
||
constructor(style: BlankLinesStyle) { | ||
super(); | ||
this.style = style; | ||
this.cursor = new Cursor(null, Cursor.ROOT_VALUE); | ||
} | ||
|
||
visitJsCompilationUnit(compilationUnit: JS.CompilationUnit, p: InMemoryExecutionContext): J.J | null { | ||
if (compilationUnit.prefix.comments.length == 0) { | ||
compilationUnit = compilationUnit.withPrefix(Space.EMPTY); | ||
} | ||
return super.visitJsCompilationUnit(compilationUnit, p); | ||
} | ||
|
||
visitStatement(statement: J.Statement, p: InMemoryExecutionContext): J.J { | ||
statement = super.visitStatement(statement, p); | ||
|
||
const parentCursor = this.cursor.parentTreeCursor(); | ||
const topLevel = parentCursor.value() instanceof JS.CompilationUnit; | ||
|
||
let prevBlankLine: number | null | undefined; | ||
const blankLines = this.getBlankLines(statement, parentCursor); | ||
if (blankLines) { | ||
prevBlankLine = parentCursor.getMessage('prev_blank_line', undefined); | ||
parentCursor.putMessage('prev_blank_line', blankLines); | ||
} else { | ||
prevBlankLine = parentCursor.getMessage('prev_blank_line', undefined); | ||
if (prevBlankLine) { | ||
parentCursor.putMessage('prev_blank_line', undefined); | ||
} | ||
} | ||
|
||
if (topLevel) { | ||
const isFirstStatement = p.getMessage<boolean>('is_first_statement', true) ?? true; | ||
if (isFirstStatement) { | ||
p.putMessage('is_first_statement', false); | ||
} else { | ||
const minLines = statement instanceof JS.JsImport ? 0 : max(prevBlankLine, blankLines); | ||
statement = adjustedLinesForTree(statement, minLines, this.style.keepMaximum.inCode); | ||
} | ||
} else { | ||
const inBlock = parentCursor.value() instanceof J.Block; | ||
const inClass = inBlock && parentCursor.parentTreeCursor().value() instanceof J.ClassDeclaration; | ||
let minLines = 0; | ||
|
||
if (inClass) { | ||
const isFirst = (parentCursor.value() as J.Block).statements[0] === statement; | ||
minLines = isFirst ? 0 : max(blankLines, prevBlankLine); | ||
} | ||
|
||
statement = adjustedLinesForTree(statement, minLines, this.style.keepMaximum.inCode); | ||
} | ||
return statement; | ||
} | ||
|
||
getBlankLines(statement: J.Statement, cursor: Cursor): number | undefined { | ||
const inBlock = cursor.value() instanceof J.Block; | ||
let type; | ||
if (inBlock) { | ||
const val = cursor.parentTreeCursor().value(); | ||
if (val instanceof J.ClassDeclaration) { | ||
type = val.padding.kind.type; | ||
} | ||
} | ||
|
||
if (type === ClassDeclaration.Kind.Type.Interface && (statement instanceof J.MethodDeclaration || statement instanceof JS.JSMethodDeclaration)) { | ||
return this.style.minimum.aroundMethodInInterface ?? undefined; | ||
} else if (type === ClassDeclaration.Kind.Type.Interface && (statement instanceof J.VariableDeclarations || statement instanceof JS.JSVariableDeclarations)) { | ||
return this.style.minimum.aroundFieldInInterface ?? undefined; | ||
} else if (type === ClassDeclaration.Kind.Type.Class && (statement instanceof J.VariableDeclarations || statement instanceof JS.JSVariableDeclarations)) { | ||
return this.style.minimum.aroundField; | ||
} else if (statement instanceof JS.JsImport) { | ||
return this.style.minimum.afterImports; | ||
} else if (statement instanceof J.ClassDeclaration) { | ||
return this.style.minimum.aroundClass; | ||
} else if (statement instanceof J.MethodDeclaration || statement instanceof JS.JSMethodDeclaration) { | ||
return this.style.minimum.aroundMethod; | ||
} else if (statement instanceof JS.FunctionDeclaration) { | ||
return this.style.minimum.aroundFunction; | ||
} else { | ||
return undefined; | ||
} | ||
} | ||
|
||
} | ||
|
||
function adjustedLinesForTree(tree: J.J, minLines: number, maxLines: number): J.J { | ||
|
||
if (tree instanceof JS.ScopedVariableDeclarations && tree.padding.scope) { | ||
const prefix = tree.padding.scope.before; | ||
return tree.padding.withScope(tree.padding.scope.withBefore(adjustedLinesForSpace(prefix, minLines, maxLines))); | ||
} else { | ||
const prefix = tree.prefix; | ||
return tree.withPrefix(adjustedLinesForSpace(prefix, minLines, maxLines)); | ||
} | ||
|
||
} | ||
|
||
function adjustedLinesForSpace(prefix: Space, minLines: number, maxLines: number): Space { | ||
if (prefix.comments.length == 0 || prefix.whitespace?.includes('\n')) { | ||
return prefix.withWhitespace(adjustedLinesForString(prefix.whitespace ?? '', minLines, maxLines)); | ||
} | ||
|
||
return prefix; | ||
} | ||
|
||
function adjustedLinesForString(whitespace: string, minLines: number, maxLines: number): string { | ||
const existingBlankLines = Math.max(countLineBreaks(whitespace) - 1, 0); | ||
maxLines = Math.max(minLines, maxLines); | ||
|
||
if (existingBlankLines >= minLines && existingBlankLines <= maxLines) { | ||
return whitespace; | ||
} else if (existingBlankLines < minLines) { | ||
return '\n'.repeat(minLines - existingBlankLines) + whitespace; | ||
} else { | ||
return '\n'.repeat(maxLines) + whitespace.substring(whitespace.lastIndexOf('\n')); | ||
} | ||
} | ||
|
||
function countLineBreaks(whitespace: string): number { | ||
return (whitespace.match(/\n/g) || []).length; | ||
} | ||
|
||
function max(x: number | null | undefined, y: number | null | undefined) { | ||
if (x && y) { | ||
return Math.max(x, y); | ||
} else { | ||
return x ? x : y ? y : 0; | ||
} | ||
} |
Oops, something went wrong.