thrift-fmt-ts
Version:
A formatter of Thrift, re-implement of python thrift-fmt
112 lines (111 loc) • 5.16 kB
TypeScript
import { ParseTree, TerminalNode } from 'antlr4ts/tree';
import { ThriftData } from 'thrift-parser-ts';
import * as ThriftParserNS from 'thrift-parser-ts/lib/ThriftParser';
declare type IsKindFunc = (node: ParseTree) => boolean;
declare type NodeProcessFunc = (this: PureThriftFormatter, node: ParseTree) => void;
declare type FieldContext = ThriftParserNS.FieldContext | ThriftParserNS.Enum_fieldContext;
export interface Option {
indent: number;
patchRequired: boolean;
patchSeparator: boolean;
keepComment: boolean;
alignByAssign: boolean;
alignByField: boolean;
}
export declare const newOption: (opt?: Partial<Option>) => Option;
export declare const isToken: (node: ParseTree | undefined, text: string) => boolean;
export declare const isEOF: (node: ParseTree) => boolean;
export declare const splitFieldByAssign: (node: FieldContext) => [ParseTree, ParseTree];
export declare const getNodeChildren: (node: ParseTree) => ParseTree[];
export declare const walkNode: (root: ParseTree, callback: (node: ParseTree) => void) => void;
export declare const splitRepeatNodes: (nodes: ParseTree[], kindFn: IsKindFunc) => [ParseTree[], ParseTree[]];
export declare class PureThriftFormatter {
protected _option: Option;
protected currentIndent: string;
protected newlineCounter: number;
private _out;
formatNode(node: ParseTree): string;
option(opt: Option): void;
get out(): string;
private push;
protected append(text: string): void;
protected appendCurrentLine(text: string): void;
protected newline(repeat?: number): void;
protected setCurrentIndent(indent?: string): void;
protected pushCurrentIndent(): void;
protected beforeBlockNode(_: ParseTree): void;
protected afterBlockNode(_: ParseTree): void;
protected beforeSubblocks(_: ParseTree[]): void;
protected afterSubblocks(_: ParseTree[]): void;
protected beforeProcessNode(_: ParseTree): void;
protected afterProcessNode(_: ParseTree): void;
protected processBlockNodes(nodes: ParseTree[], indent?: string): void;
protected processInlineNodes(nodes: ParseTree[], join?: string): void;
protected processNode(node: ParseTree): void;
private _processNode;
protected TerminalNode(node: TerminalNode): void;
protected DocumentContext: NodeProcessFunc;
protected HeaderContext: NodeProcessFunc;
protected DefinitionContext: NodeProcessFunc;
protected Include_Context: NodeProcessFunc;
protected Namespace_Context: NodeProcessFunc;
protected Typedef_Context: NodeProcessFunc;
protected Base_typeContext: NodeProcessFunc;
protected Field_typeContext: NodeProcessFunc;
protected Real_base_typeContext: NodeProcessFunc;
protected Const_ruleContext: NodeProcessFunc;
protected Const_valueContext: NodeProcessFunc;
protected IntegerContext: NodeProcessFunc;
protected Container_typeContext: NodeProcessFunc;
protected Set_typeContext: NodeProcessFunc;
protected List_typeContext: NodeProcessFunc;
protected Cpp_typeContext: NodeProcessFunc;
protected Const_mapContext: NodeProcessFunc;
protected Const_map_entryContext: NodeProcessFunc;
protected List_separatorContext: NodeProcessFunc;
protected Field_idContext: NodeProcessFunc;
protected Field_reqContext: NodeProcessFunc;
protected Map_typeContext: NodeProcessFunc;
protected Const_listContext: NodeProcessFunc;
protected Enum_ruleContext: NodeProcessFunc;
protected Enum_fieldContext: NodeProcessFunc;
protected Struct_Context: NodeProcessFunc;
protected Union_Context: NodeProcessFunc;
protected Exception_Context: NodeProcessFunc;
protected FieldContext: NodeProcessFunc;
protected Function_Context: NodeProcessFunc;
protected OnewayContext: NodeProcessFunc;
protected Function_typeContext: NodeProcessFunc;
protected Throws_listContext: NodeProcessFunc;
protected Type_annotationsContext: NodeProcessFunc;
protected Type_annotationContext: NodeProcessFunc;
protected Annotation_valueContext: NodeProcessFunc;
protected ServiceContext_Default: NodeProcessFunc;
protected ServiceContext_Extends: NodeProcessFunc;
protected ServiceContext: NodeProcessFunc;
protected SenumContext: NodeProcessFunc;
}
export declare const calcFieldAlignByAssignPadding: (fields: ParseTree[]) => [number, number];
export declare class ThriftFormatter extends PureThriftFormatter {
private data;
private document;
private fieldCommentPadding;
private fieldAlignByAssignPadding;
private fieldAlignByFieldPaddingMap;
private lastTokenIndex;
constructor(data: ThriftData);
format(): string;
patch(): void;
protected beforeSubblocks(subblocks: ParseTree[]): void;
protected afterSubblocks(_: ParseTree[]): void;
protected afterBlockNode(_: ParseTree): void;
protected beforeProcessNode(n: ParseTree): void;
protected get currentLine(): string;
private calcAddIndentPadding;
private addAlignPadding;
private padding;
private addInlineComments;
private addTailComment;
protected TerminalNode(n: TerminalNode): void;
}
export {};