@aurahelper/languages
Version:
Language Libraries to work with XML, Aura, Apex... files. tokenizers, parsers, system classes and much more
803 lines • 45.6 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ApexFormatter = void 0;
var core_1 = require("@aurahelper/core");
var utils_1 = require("../utils");
var tokenizer_1 = require("./tokenizer");
var StrUtils = core_1.CoreUtils.StrUtils;
/**
* Class to format any Apex Class with a selected configuration to format code as you want
*/
var ApexFormatter = /** @class */ (function () {
function ApexFormatter() {
}
/**
* Method to get a default formatter config object
*
* @returns {ApexFormatterConfig} Returns a default formatter config object
*/
ApexFormatter.config = function () {
return new core_1.ApexFormatterConfig();
};
/**
* Method to format an Apex Class with the selected options
* @param {string | Token[]} tokensOrContent Class file path or string file content or Apex Class Tokens (Use ApexTokenizer)
* @param {ApexFormatterConfig | { [key: string]: any }} [config] Apex formatter config object or VSCode Config JSON object
* @param {ParserData} [systemData] System data like System Apex Classes or Namespaces to tokenize apex class if pathContentOrTokens is a class content or file path. Can get it with System Class from System Module
* @param {number} [tabSize] Tab size to format
* @param {boolean} [insertSpaces] True to insert spaces instead tabs
*
* @returns {string} Returns the Apex Class content formatted
*
* @throws {WrongDatatypeException} If pathContentOrTokens datatype is not an string, path or file tokens
* @throws {WrongFilePathException} If the file Path is not a string or can't convert to absolute path
* @throws {FileNotFoundException} If the file not exists or not have access to it
* @throws {InvalidFilePathException} If the path is not a file
*/
ApexFormatter.format = function (tokensOrContent, config, systemData, tabSize, insertSpaces) {
var tokens = [];
if (typeof tokensOrContent !== 'string') {
tokens = tokensOrContent;
}
else if (typeof tokensOrContent === 'string') {
var content = tokensOrContent;
tokens = tokenizer_1.ApexTokenizer.tokenize(content, systemData, tabSize);
}
else {
throw new core_1.WrongDatatypeException('You must to select a file path, file content or file tokens');
}
var conf = new core_1.ApexFormatterConfig(config);
return formatApex(tokens, conf, tabSize, insertSpaces);
};
return ApexFormatter;
}());
exports.ApexFormatter = ApexFormatter;
function formatApex(tokens, config, tabSize, insertSpaces) {
var indent = 0;
var indentOffset = 0;
var strIndex;
var lines = '';
var line = '';
var beforeWhitespaces = 0;
var afterWhitespaces = 0;
var guardOpen = false;
var mainBodyIndent = 0;
var queryOpenIndex = [];
var querySelectIndex = [];
var complexString = false;
var onProperty = false;
var emptyProperty = false;
if (!tabSize) {
tabSize = 4;
}
var projectionFieldsOnLine = [];
var objectFieldsOnLine = [];
var conditionsOnLine = [];
var conditionOpenIndex = [];
var priorityIndex = [];
for (var len = tokens.length, index = 0; index < len; index++) {
var token = tokens[index];
var lastToken = utils_1.LanguageUtils.getLastToken(tokens, index);
var twoLastToken = utils_1.LanguageUtils.getTwoLastToken(tokens, index);
var nextToken = utils_1.LanguageUtils.getNextToken(tokens, index);
var newLines = 0;
var originalNewLines = (lastToken && !token.isAux && !lastToken.isAux) ? token.range.start.line - lastToken.range.start.line : 0;
var parentToken = (token.parentToken !== undefined && token.parentToken !== -1) ? new core_1.Token(tokens[token.parentToken]) : undefined;
var lastParentToken = (lastToken && lastToken.parentToken !== undefined && lastToken.parentToken !== -1) ? new core_1.Token(tokens[lastToken.parentToken]) : undefined;
if (token.type === core_1.ApexTokenTypes.BRACKET.CURLY_OPEN || token.type === core_1.ApexTokenTypes.BRACKET.INITIALIZER_OPEN) {
indent++;
}
else if (token.type === core_1.ApexTokenTypes.BRACKET.CURLY_CLOSE || token.type === core_1.ApexTokenTypes.BRACKET.INITIALIZER_CLOSE) {
if (mainBodyIndent === indent) {
mainBodyIndent--;
}
indent--;
}
else if (token.type === core_1.ApexTokenTypes.BRACKET.PARENTHESIS_GUARD_OPEN) {
guardOpen = true;
}
else if (token.type === core_1.ApexTokenTypes.BRACKET.PARENTHESIS_GUARD_CLOSE) {
guardOpen = false;
}
if (token.type === core_1.ApexTokenTypes.OPERATOR.PRIORITY.PARENTHESIS_OPEN) {
priorityIndex.push(StrUtils.replace(line, '\t', StrUtils.getWhitespaces(tabSize)).length - (indent * tabSize));
}
else if (token.type === core_1.ApexTokenTypes.OPERATOR.PRIORITY.PARENTHESIS_CLOSE) {
priorityIndex.pop();
}
if (lastToken && (lastToken.type === core_1.ApexTokenTypes.BRACKET.QUERY_START || lastToken && lastToken.type === core_1.ApexTokenTypes.BRACKET.INNER_QUERY_START)) {
queryOpenIndex.push(StrUtils.replace(line, '\t', StrUtils.getWhitespaces(tabSize)).length - (indent * tabSize));
projectionFieldsOnLine.push(0);
}
if (lastToken && lastToken.type === core_1.ApexTokenTypes.BRACKET.PARENTHESIS_SOBJECT_OPEN) {
objectFieldsOnLine.push(0);
if (config && config.punctuation.SObjectFieldsPerLine > 0) {
indent++;
}
}
if (lastToken && lastToken.type === core_1.ApexTokenTypes.BRACKET.PARENTHESIS_GUARD_OPEN) {
conditionsOnLine.push(1);
conditionOpenIndex.push(StrUtils.replace(line, '\t', StrUtils.getWhitespaces(tabSize)).length - (indent * tabSize));
/*if (config && config.punctuation.maxConditionsPerLine > 0) {
indent++;
}*/
}
if (token.type === core_1.ApexTokenTypes.BRACKET.INNER_QUERY_START) {
if (config && config.query.maxProjectionFieldPerLine > 0) {
if (projectionFieldsOnLine[projectionFieldsOnLine.length - 1] === config.query.maxProjectionFieldPerLine) {
newLines = 1;
beforeWhitespaces = querySelectIndex[0];
projectionFieldsOnLine[projectionFieldsOnLine.length - 1] = 1;
}
else {
projectionFieldsOnLine[projectionFieldsOnLine.length - 1] = projectionFieldsOnLine[projectionFieldsOnLine.length - 1] + 1;
}
}
}
if (lastToken && lastToken.type === core_1.ApexTokenTypes.QUERY.CLAUSE.SELECT) {
querySelectIndex.push(StrUtils.replace(line, '\t', StrUtils.getWhitespaces(tabSize)).length - (indent * tabSize));
}
if (token.type === core_1.ApexTokenTypes.KEYWORD.DECLARATION.CLASS || token.type === core_1.ApexTokenTypes.KEYWORD.DECLARATION.ENUM || token.type === core_1.ApexTokenTypes.KEYWORD.DECLARATION.INTERFACE) {
mainBodyIndent = indent + 1;
}
if (lastToken && isAnnotationToken(lastToken) && !isAnnotationToken(token) && (isCommentToken(token) && !isOnSameLine(token, lastToken) || !isCommentToken(token))) {
newLines = 1;
}
else if (lastToken && isAnnotationToken(lastToken) && !isAnnotationToken(token) && isCommentToken(token) && isOnSameLine(token, lastToken)) {
beforeWhitespaces = 1;
}
if (lastToken && lastToken.type === core_1.ApexTokenTypes.BRACKET.ANNOTATION_PARAM_CLOSE && (isCommentToken(token) && !isOnSameLine(token, lastToken) || !isCommentToken(token))) {
newLines = 1;
}
else if (lastToken && lastToken.type === core_1.ApexTokenTypes.BRACKET.ANNOTATION_PARAM_CLOSE && isCommentToken(token) && isOnSameLine(token, lastToken)) {
beforeWhitespaces = 1;
}
if (isCommentToken(token) && nextToken && isCommentToken(nextToken) && isOnSameLine(token, nextToken)) {
afterWhitespaces = nextToken.range.start.character - token.range.end.character;
}
if (isCommentToken(token) && lastToken && isCommentToken(lastToken) && !isOnSameLine(token, lastToken)) {
newLines = (lastToken) ? token.range.start.line - lastToken.range.start.line : 0;
}
if (lastToken && isCommentToken(lastToken) && !isCommentToken(token) && !isOnSameLine(token, lastToken)) {
newLines = 1;
}
if (lastToken && !isCommentToken(lastToken) && isCommentToken(token) && isOnSameLine(token, lastToken) && config && config.comment.holdBeforeWhitespacesOnLineComment) {
beforeWhitespaces = token.range.start.character - lastToken.range.end.character;
}
if (lastToken && isCommentToken(lastToken) && !isCommentToken(token) && isOnSameLine(token, lastToken) && config && config.comment.holdAfterWhitespacesOnLineComment) {
afterWhitespaces = token.range.start.character - lastToken.range.end.character;
}
if (lastToken && isCommentToken(lastToken) && (token.type === core_1.ApexTokenTypes.COMMENT.BLOCK_START || token.type === core_1.ApexTokenTypes.COMMENT.LINE || token.type === core_1.ApexTokenTypes.COMMENT.LINE_DOC) && !isOnSameLine(token, lastToken)) {
newLines = (config) ? config.comment.newLinesBewteenComments + 1 : 1;
}
if (isStringToken(token) && nextToken && isStringToken(nextToken) && isOnSameLine(token, nextToken)) {
afterWhitespaces = nextToken.range.start.character - token.range.end.character;
if (!strIndex) {
strIndex = token.range.start.character;
}
}
if (lastToken && (lastToken.type === core_1.ApexTokenTypes.BRACKET.CURLY_OPEN || lastToken.type === core_1.ApexTokenTypes.BRACKET.INITIALIZER_OPEN)) {
newLines = 1;
}
if (lastToken && (lastToken.type === core_1.ApexTokenTypes.BRACKET.CURLY_CLOSE || lastToken.type === core_1.ApexTokenTypes.BRACKET.INITIALIZER_CLOSE)) {
newLines = 1;
}
if (lastToken && lastToken.type === core_1.ApexTokenTypes.BRACKET.CURLY_CLOSE && lastToken.isAux && token.type === core_1.ApexTokenTypes.BRACKET.CURLY_CLOSE && token.isAux) {
newLines = 0;
}
else if (token.type === core_1.ApexTokenTypes.BRACKET.CURLY_CLOSE && !token.isAux) {
newLines = 1;
}
if (!token.isAux && token.type === core_1.ApexTokenTypes.BRACKET.CURLY_OPEN && config && config.punctuation.addWhitespaceBeforeOpenCurlyBracket && !config.punctuation.openCurlyBracketOnNewLine) {
beforeWhitespaces = 1;
}
if (!token.isAux && token.type === core_1.ApexTokenTypes.BRACKET.CURLY_OPEN && config && config.punctuation.openCurlyBracketOnNewLine) {
newLines = 1;
indentOffset = -1;
}
if (lastToken && lastToken.type === core_1.ApexTokenTypes.PUNCTUATION.SEMICOLON && !guardOpen && token.type !== core_1.ApexTokenTypes.BRACKET.CURLY_CLOSE) {
strIndex = undefined;
if (isCommentToken(token) && isOnSameLine(token, lastToken)) {
if (config && config.comment.holdAfterWhitespacesOnLineComment) {
beforeWhitespaces = token.range.start.character - lastToken.range.end.character;
}
else {
beforeWhitespaces = 1;
}
}
else {
newLines = 1;
}
}
else if (lastToken && lastToken.type === core_1.ApexTokenTypes.PUNCTUATION.SEMICOLON && guardOpen) {
beforeWhitespaces = 1;
}
if (lastToken && lastToken.type === core_1.ApexTokenTypes.BRACKET.CURLY_CLOSE && lastToken.parentToken && config && config.punctuation.addNewLineAfterCloseCurlyBracket) {
newLines = 1;
}
else if (lastToken && lastToken.type === core_1.ApexTokenTypes.BRACKET.CURLY_CLOSE && !lastToken.isAux && lastToken.parentToken && config && !config.punctuation.addNewLineAfterCloseCurlyBracket && isDependentFlowStructure(token)) {
newLines = 0;
if (config && config.punctuation.addWhitespaceAfterCloseCurlyBracket) {
beforeWhitespaces = 1;
}
}
if (token.type === core_1.ApexTokenTypes.BRACKET.TRIGGER_GUARD_OPEN && config && config.punctuation.addWhitespaceBeforeOpenTriggerEvents) {
beforeWhitespaces = 1;
}
if (token.type === core_1.ApexTokenTypes.PUNCTUATION.COMMA && config && config.punctuation.addWhiteSpaceAfterComma) {
afterWhitespaces = 1;
}
if (lastToken && lastToken.type === core_1.ApexTokenTypes.BRACKET.CURLY_CLOSE && !lastToken.isAux && config.classMembers.newLinesBetweenCodeBlockMembers > 0) {
if (lastParentToken) {
}
if (lastParentToken && (lastParentToken.type === core_1.ApexTokenTypes.DECLARATION.ENTITY.FUNCTION || lastParentToken.type === core_1.ApexTokenTypes.DECLARATION.ENTITY.CLASS || lastParentToken.type === core_1.ApexTokenTypes.DECLARATION.ENTITY.PROPERTY || lastParentToken.type === core_1.ApexTokenTypes.DECLARATION.ENTITY.CONSTRUCTOR || lastParentToken.type === core_1.ApexTokenTypes.DECLARATION.ENTITY.ENUM || lastParentToken.type === core_1.ApexTokenTypes.DECLARATION.ENTITY.INTERFACE)) {
newLines = (config) ? config.classMembers.newLinesBetweenCodeBlockMembers + 1 : 1;
if (isFieldInstructionDeclaration(tokens, index)) {
if (isNextInstructionFieldDeclaration(tokens, index)) {
newLines = (config) ? config.classMembers.newLinesBetweenClassFields + 1 : 1;
}
else {
newLines = (config) ? config.classMembers.newLinesBetweenCodeBlockMembers + 1 : 1;
}
}
}
}
if (lastToken && (lastToken.type === core_1.ApexTokenTypes.BRACKET.CURLY_CLOSE || lastToken.type === core_1.ApexTokenTypes.PUNCTUATION.SEMICOLON) && !lastToken.isAux && (token.type === core_1.ApexTokenTypes.KEYWORD.DECLARATION.PROPERTY_GETTER || token.type === core_1.ApexTokenTypes.KEYWORD.DECLARATION.PROPERTY_SETTER) && config && config.classMembers.newLinesBetweenGetterAndSetterAccessor > 0) {
newLines = (config) ? config.classMembers.newLinesBetweenGetterAndSetterAccessor + 1 : 1;
}
if (isKeyword(token) && nextToken && nextToken.type !== core_1.ApexTokenTypes.BRACKET.CURLY_OPEN && nextToken.type !== core_1.ApexTokenTypes.PUNCTUATION.COMMA && nextToken.type !== core_1.ApexTokenTypes.BRACKET.TRIGGER_GUARD_CLOSE && nextToken.type !== core_1.ApexTokenTypes.PUNCTUATION.SEMICOLON) {
afterWhitespaces = 1;
if (lastToken && lastToken.type === core_1.ApexTokenTypes.BRACKET.PARAMETRIZED_TYPE_CLOSE) {
beforeWhitespaces = 1;
}
}
if (isMemberDeclaration(token) && lastToken && lastToken.type !== core_1.ApexTokenTypes.PUNCTUATION.COMMA) {
beforeWhitespaces = 1;
}
if (twoLastToken && isFieldDeclaration(twoLastToken) && lastToken && lastToken.type === core_1.ApexTokenTypes.PUNCTUATION.SEMICOLON && isNextInstructionFieldDeclaration(tokens, index) && config && config.classMembers.newLinesBetweenClassFields > 0 && mainBodyIndent === indent) {
newLines = (config) ? config.classMembers.newLinesBetweenClassFields + 1 : 1;
}
else if (twoLastToken && isFieldDeclaration(twoLastToken) && lastToken && lastToken.type === core_1.ApexTokenTypes.PUNCTUATION.SEMICOLON && !isNextInstructionFieldDeclaration(tokens, index) && mainBodyIndent === indent) {
newLines = (config) ? config.classMembers.newLinesBetweenCodeBlockMembers + 1 : 1;
}
if (lastToken && lastToken.type === core_1.ApexTokenTypes.PUNCTUATION.COMMA && twoLastToken && twoLastToken.type === core_1.ApexTokenTypes.ENTITY.ENUM_VALUE && token.type !== core_1.ApexTokenTypes.COMMENT.LINE && token.type !== core_1.ApexTokenTypes.COMMENT.LINE_DOC) {
newLines = 1;
}
if (isKeyword(token) && lastToken && lastToken.type === core_1.ApexTokenTypes.DECLARATION.ENTITY.VARIABLE) {
beforeWhitespaces = 1;
}
if (isQueryClause(token) && config && config.query.oneClausePerLine && lastToken && lastToken.type !== core_1.ApexTokenTypes.BRACKET.QUERY_START && lastToken.type !== core_1.ApexTokenTypes.BRACKET.INNER_QUERY_START) {
newLines = 1;
beforeWhitespaces = queryOpenIndex[queryOpenIndex.length - 1];
}
else if (isQueryClause(token) && lastToken && lastToken.type !== core_1.ApexTokenTypes.BRACKET.QUERY_START && lastToken.type !== core_1.ApexTokenTypes.BRACKET.INNER_QUERY_START) {
beforeWhitespaces = 1;
}
if (isQueryClause(token)) {
afterWhitespaces = 1;
}
if (token.type === core_1.ApexTokenTypes.QUERY.OPERATOR && lastToken && lastToken.type !== core_1.ApexTokenTypes.QUERY.OPERATOR) {
beforeWhitespaces = 1;
}
if (token.type === core_1.ApexTokenTypes.QUERY.OPERATOR && nextToken && nextToken.type !== core_1.ApexTokenTypes.QUERY.VALUE_BIND) {
afterWhitespaces = 1;
}
if (lastToken && isQueryClause(lastToken) && token.type === core_1.ApexTokenTypes.DATATYPE.SOBJECT) {
afterWhitespaces = 1;
}
if (lastToken && isQueryFunction(lastToken) && token.type === core_1.ApexTokenTypes.DATATYPE.SOBJECT) {
beforeWhitespaces = 1;
}
if (lastToken && (isLiteral(lastToken) || lastToken.type === core_1.ApexTokenTypes.ENTITY.SOBJECT_FIELD || lastToken.type === core_1.ApexTokenTypes.ENTITY.SOBJECT_PROJECTION_FIELD || lastToken.type === core_1.ApexTokenTypes.PUNCTUATION.QUOTTES_END || lastToken.type === core_1.ApexTokenTypes.OPERATOR.PRIORITY.PARENTHESIS_CLOSE || lastToken.type === core_1.ApexTokenTypes.ENTITY.VARIABLE) && isQueryClause(token) && newLines === 0) {
beforeWhitespaces = 1;
}
if (lastToken && lastToken.type === core_1.ApexTokenTypes.QUERY.CLAUSE.SELECT && config && config.query.maxProjectionFieldPerLine > 0) {
if (config && projectionFieldsOnLine[projectionFieldsOnLine.length - 1] === config.query.maxProjectionFieldPerLine) {
newLines = 1;
beforeWhitespaces = querySelectIndex[querySelectIndex.length - 1];
projectionFieldsOnLine[projectionFieldsOnLine.length - 1] = 1;
}
else {
projectionFieldsOnLine[projectionFieldsOnLine.length - 1] = projectionFieldsOnLine[projectionFieldsOnLine.length - 1] + 1;
}
}
if (twoLastToken && twoLastToken.type === core_1.ApexTokenTypes.ENTITY.SOBJECT_PROJECTION_FIELD && lastToken && (lastToken.type === core_1.ApexTokenTypes.PUNCTUATION.COMMA) && config && config.query.maxProjectionFieldPerLine > 0) {
if (config && projectionFieldsOnLine[projectionFieldsOnLine.length - 1] === config.query.maxProjectionFieldPerLine) {
newLines = 1;
beforeWhitespaces = querySelectIndex[querySelectIndex.length - 1];
projectionFieldsOnLine[projectionFieldsOnLine.length - 1] = 1;
}
else {
projectionFieldsOnLine[projectionFieldsOnLine.length - 1] = projectionFieldsOnLine[projectionFieldsOnLine.length - 1] + 1;
}
}
if (token.type === core_1.ApexTokenTypes.QUERY.VALUE_BIND && lastToken && lastToken.type !== core_1.ApexTokenTypes.QUERY.OPERATOR && !isOperator(lastToken) && !isKeyword(lastToken) && !isQueryClause(lastToken)) {
beforeWhitespaces = 1;
}
if ((isOperator(token) || (token.type === core_1.ApexTokenTypes.ANNOTATION.CONTENT && token.text === '=')) && config && config.operator.addWhitespaceBeforeOperator) {
beforeWhitespaces = 1;
}
if ((isOperator(token) || (token.type === core_1.ApexTokenTypes.ANNOTATION.CONTENT && token.text === '=')) && config && config.operator.addWhitespaceAfterOperator) {
afterWhitespaces = 1;
}
if (token.type === core_1.ApexTokenTypes.OPERATOR.ARITHMETIC.INCREMENT && nextToken && (nextToken.type === core_1.ApexTokenTypes.ENTITY.VARIABLE)) {
afterWhitespaces = 0;
}
if (token.type === core_1.ApexTokenTypes.OPERATOR.ARITHMETIC.INCREMENT && lastToken && (lastToken.type === core_1.ApexTokenTypes.ENTITY.VARIABLE)) {
beforeWhitespaces = 0;
if (nextToken && (nextToken.type === core_1.ApexTokenTypes.OPERATOR.PRIORITY.PARENTHESIS_OPEN || nextToken.type === core_1.ApexTokenTypes.OPERATOR.PRIORITY.PARENTHESIS_CLOSE || nextToken.type === core_1.ApexTokenTypes.PUNCTUATION.SEMICOLON || nextToken.type === core_1.ApexTokenTypes.BRACKET.PARENTHESIS_GUARD_CLOSE || nextToken.type === core_1.ApexTokenTypes.BRACKET.PARENTHESIS_GUARD_OPEN)) {
afterWhitespaces = 0;
}
}
if (token.type === core_1.ApexTokenTypes.OPERATOR.ARITHMETIC.DECREMENT && nextToken && (nextToken.type === core_1.ApexTokenTypes.ENTITY.VARIABLE)) {
afterWhitespaces = 0;
}
if (token.type === core_1.ApexTokenTypes.OPERATOR.ARITHMETIC.DECREMENT && lastToken && (lastToken.type === core_1.ApexTokenTypes.ENTITY.VARIABLE)) {
beforeWhitespaces = 0;
}
if (token.type === core_1.ApexTokenTypes.OPERATOR.PRIORITY.PARENTHESIS_OPEN && config && config.operator.addWhitespaceAfterOpenParenthesisOperator) {
afterWhitespaces = 1;
}
if (token.type === core_1.ApexTokenTypes.OPERATOR.PRIORITY.PARENTHESIS_OPEN && config && config.operator.addWhitespaceBeforeOpenParenthesisOperator) {
beforeWhitespaces = 1;
}
if (token.type === core_1.ApexTokenTypes.OPERATOR.PRIORITY.PARENTHESIS_CLOSE && config && config.operator.addWhitespaceAfterCloseParenthesisOperator) {
afterWhitespaces = 1;
}
if (token.type === core_1.ApexTokenTypes.OPERATOR.PRIORITY.PARENTHESIS_CLOSE && config && config.operator.addWhitespaceBeforeCloseParenthesisOperator) {
beforeWhitespaces = 1;
}
if (token.type === core_1.ApexTokenTypes.BRACKET.PARENTHESIS_GUARD_OPEN && config && config.punctuation.addWhitespaceBeforeOpenGuardParenthesis) {
beforeWhitespaces = 1;
}
if (token.type === core_1.ApexTokenTypes.BRACKET.PARENTHESIS_GUARD_CLOSE && config && config.punctuation.addWhitespaceBeforeCloseGuardParenthesis) {
beforeWhitespaces = 1;
}
if (token.type === core_1.ApexTokenTypes.BRACKET.PARENTHESIS_GUARD_OPEN && config && config.punctuation.addWhitespaceAfterOpenGuardParenthesis) {
afterWhitespaces = 1;
}
if (token.type === core_1.ApexTokenTypes.BRACKET.PARENTHESIS_GUARD_CLOSE && config && config.punctuation.addWhitespaceBeforeCloseGuardParenthesis) {
beforeWhitespaces = 1;
}
if (token.type === core_1.ApexTokenTypes.ENTITY.SOBJECT_PROJECTION_FIELD && nextToken && nextToken.type === core_1.ApexTokenTypes.ENTITY.SOBJECT_PROJECTION_FIELD) {
afterWhitespaces = 1;
}
if (token.type === core_1.ApexTokenTypes.ENTITY.SOBJECT_PROJECTION_FIELD && nextToken && nextToken.type === core_1.ApexTokenTypes.ENTITY.ALIAS_FIELD) {
afterWhitespaces = 1;
}
if (token.type === core_1.ApexTokenTypes.QUERY.SCOPE_VALUE || token.type === core_1.ApexTokenTypes.QUERY.NULLS_VALUE || token.type === core_1.ApexTokenTypes.QUERY.ORDER) {
beforeWhitespaces = 1;
}
if (token.type === core_1.ApexTokenTypes.PUNCTUATION.OBJECT_ACCESSOR) {
afterWhitespaces = 0;
beforeWhitespaces = 0;
}
if (lastToken && lastToken.type === core_1.ApexTokenTypes.PUNCTUATION.OBJECT_ACCESSOR) {
beforeWhitespaces = 0;
}
if (nextToken && nextToken.type === core_1.ApexTokenTypes.PUNCTUATION.OBJECT_ACCESSOR) {
afterWhitespaces = 0;
}
if (token.type === core_1.ApexTokenTypes.OPERATOR.LOGICAL.INSTANCE_OF) {
afterWhitespaces = 1;
beforeWhitespaces = 1;
}
if (token && token.type === core_1.ApexTokenTypes.BRACKET.INIT_VALUES_OPEN) {
afterWhitespaces = 1;
}
if (token && token.type === core_1.ApexTokenTypes.BRACKET.INIT_VALUES_CLOSE) {
beforeWhitespaces = 1;
}
if (token.type === core_1.ApexTokenTypes.BRACKET.CASTING_OPEN) {
afterWhitespaces = 0;
}
else if (isDatatype(token) && lastToken && lastToken.type === core_1.ApexTokenTypes.BRACKET.CASTING_OPEN && nextToken && nextToken.type === core_1.ApexTokenTypes.BRACKET.CASTING_CLOSE) {
afterWhitespaces = 0;
beforeWhitespaces = 0;
}
else if (token.type === core_1.ApexTokenTypes.BRACKET.CASTING_CLOSE) {
beforeWhitespaces = 0;
afterWhitespaces = 1;
}
if (lastToken && lastToken.type === core_1.ApexTokenTypes.PUNCTUATION.COMMA && isCommentToken(token) && originalNewLines > 0) {
newLines = 1;
}
if (token.type === core_1.ApexTokenTypes.ENTITY.SOBJECT_FIELD && objectFieldsOnLine.length > 0 && config && config.punctuation.SObjectFieldsPerLine > 0) {
if (lastToken && lastToken.type === core_1.ApexTokenTypes.BRACKET.PARENTHESIS_SOBJECT_OPEN) {
newLines = 1;
}
if (objectFieldsOnLine[objectFieldsOnLine.length - 1] === config.punctuation.SObjectFieldsPerLine) {
newLines = 1;
objectFieldsOnLine[objectFieldsOnLine.length - 1] = 1;
}
else {
objectFieldsOnLine[objectFieldsOnLine.length - 1] = objectFieldsOnLine[objectFieldsOnLine.length - 1] + 1;
}
}
if (isLogicalOperator(token) && conditionsOnLine.length > 0 && priorityIndex.length === 0 && config && config.punctuation.maxConditionsPerLine > 0 && config.punctuation.conditionLogicOperatorOnNewLine) {
if (conditionsOnLine[conditionsOnLine.length - 1] === config.punctuation.maxConditionsPerLine) {
newLines = 1;
beforeWhitespaces = conditionOpenIndex[conditionOpenIndex.length - 1];
conditionsOnLine[conditionsOnLine.length - 1] = 1;
}
else {
conditionsOnLine[conditionsOnLine.length - 1] = conditionsOnLine[conditionsOnLine.length - 1] + 1;
}
}
if (lastToken && isLogicalOperator(lastToken) && conditionsOnLine.length > 0 && (priorityIndex.length === 0 || (priorityIndex.length === 1 && core_1.ApexTokenTypes.OPERATOR.PRIORITY.PARENTHESIS_OPEN)) && config && config.punctuation.maxConditionsPerLine > 0 && !config.punctuation.conditionLogicOperatorOnNewLine) {
if (conditionsOnLine[conditionsOnLine.length - 1] === config.punctuation.maxConditionsPerLine) {
newLines = 1;
beforeWhitespaces = conditionOpenIndex[conditionOpenIndex.length - 1];
conditionsOnLine[conditionsOnLine.length - 1] = 1;
}
else {
conditionsOnLine[conditionsOnLine.length - 1] = conditionsOnLine[conditionsOnLine.length - 1] + 1;
}
}
if ((lastToken && isOperator(lastToken) && isStringToken(token) && originalNewLines > 0) || complexString) {
complexString = false;
if (strIndex) {
var rest = strIndex % 4;
indentOffset = (strIndex - (indent * tabSize)) / tabSize;
if (rest > 0) {
indentOffset -= 1;
beforeWhitespaces = rest - 1;
}
}
newLines = 1;
}
if (isKeyword(token) && token.textToLower === 'on') {
if (beforeWhitespaces === 0) {
beforeWhitespaces = 1;
}
}
if (lastToken && isStringToken(lastToken) && isOperator(token) && originalNewLines > 0) {
newLines = 0;
complexString = true;
}
if (token.type === core_1.ApexTokenTypes.BRACKET.QUERY_END || token.type === core_1.ApexTokenTypes.BRACKET.INNER_QUERY_END) {
querySelectIndex.pop();
queryOpenIndex.pop();
projectionFieldsOnLine.pop();
}
if (token.type === core_1.ApexTokenTypes.BRACKET.PARENTHESIS_GUARD_CLOSE) {
conditionOpenIndex.pop();
conditionsOnLine.pop();
}
if (token.type === core_1.ApexTokenTypes.BRACKET.PARENTHESIS_SOBJECT_CLOSE) {
objectFieldsOnLine.pop();
if (config.punctuation.SObjectFieldsPerLine > 0) {
newLines = lastToken && lastToken.type !== core_1.ApexTokenTypes.BRACKET.PARENTHESIS_SOBJECT_OPEN ? 1 : 0;
indent--;
}
}
if (indent > 0 && indent !== mainBodyIndent && token.type !== core_1.ApexTokenTypes.KEYWORD.DECLARATION.PROPERTY_GETTER && token.type !== core_1.ApexTokenTypes.KEYWORD.DECLARATION.PROPERTY_SETTER) {
if (newLines > 0 && originalNewLines > 1) {
if (config && config.punctuation.maxBlankLines === -1) {
newLines = originalNewLines;
}
else if (config && config.punctuation.maxBlankLines === 0) {
newLines = 1;
}
else if (config && config.punctuation.maxBlankLines > 0 && originalNewLines > config.punctuation.maxBlankLines) {
newLines = config.punctuation.maxBlankLines + 1;
}
else if (config && config.punctuation.maxBlankLines > 0 && originalNewLines <= config.punctuation.maxBlankLines) {
newLines = originalNewLines;
}
}
}
if (lastToken && lastToken.type === core_1.ApexTokenTypes.DECLARATION.ENTITY.PROPERTY) {
onProperty = true;
emptyProperty = isEmptyProperty(tokens, index);
}
if (token.type === core_1.ApexTokenTypes.BRACKET.CURLY_CLOSE && onProperty && emptyProperty && config && config.classMembers.singleLineProperties) {
onProperty = false;
emptyProperty = false;
newLines = 0;
beforeWhitespaces = 1;
}
if (onProperty && emptyProperty && config && config.classMembers.singleLineProperties) {
newLines = 0;
if (token.type === core_1.ApexTokenTypes.KEYWORD.DECLARATION.PROPERTY_GETTER || token.type === core_1.ApexTokenTypes.KEYWORD.DECLARATION.PROPERTY_SETTER) {
beforeWhitespaces = 1;
}
}
if (isInitializer(token)) {
indentOffset = -1;
beforeWhitespaces = 0;
newLines = 1;
}
if (isUnaryOperator(token)) {
afterWhitespaces = 0;
}
if (newLines > 0) {
if (newLines > 1) {
line += StrUtils.getNewLines(newLines - 1);
}
lines += line + '\n';
line = '';
if (!insertSpaces) {
line += StrUtils.getTabs(indent + indentOffset);
}
else {
line += StrUtils.getWhitespaces((indent + indentOffset) * tabSize);
}
if (token.type === core_1.ApexTokenTypes.COMMENT.CONTENT || token.type === core_1.ApexTokenTypes.COMMENT.BLOCK_END) {
beforeWhitespaces = 1;
}
if (!token.isAux) {
if (beforeWhitespaces > 0) {
line += StrUtils.getWhitespaces(beforeWhitespaces);
}
line += token.text;
if (afterWhitespaces > 0) {
line += StrUtils.getWhitespaces(afterWhitespaces);
}
}
beforeWhitespaces = 0;
afterWhitespaces = 0;
newLines = 0;
indentOffset = 0;
}
else {
if (!token.isAux) {
if (beforeWhitespaces > 0) {
line += StrUtils.getWhitespaces(beforeWhitespaces);
}
line += token.text;
if (afterWhitespaces > 0) {
line += StrUtils.getWhitespaces(afterWhitespaces);
}
}
beforeWhitespaces = 0;
afterWhitespaces = 0;
indentOffset = 0;
}
}
lines += line;
return lines;
}
function isLiteral(token) {
return token.type === core_1.ApexTokenTypes.LITERAL.BOOLEAN || token.type === core_1.ApexTokenTypes.LITERAL.DATE || token.type === core_1.ApexTokenTypes.LITERAL.DATETIME || token.type === core_1.ApexTokenTypes.LITERAL.DATE_PARAMETRIZED || token.type === core_1.ApexTokenTypes.LITERAL.DATE_PARAMETRIZED_START_PARAM || token.type === core_1.ApexTokenTypes.LITERAL.DOUBLE || token.type === core_1.ApexTokenTypes.LITERAL.DOUBLE_INDICATOR || token.type === core_1.ApexTokenTypes.LITERAL.INTEGER || token.type === core_1.ApexTokenTypes.LITERAL.LONG || token.type === core_1.ApexTokenTypes.LITERAL.LONG_INDICATOR || token.type === core_1.ApexTokenTypes.LITERAL.NULL || token.type === core_1.ApexTokenTypes.LITERAL.TIME;
}
function isStringToken(token) {
return token.type === core_1.ApexTokenTypes.LITERAL.STRING || token.type === core_1.ApexTokenTypes.PUNCTUATION.QUOTTES_START || token.type === core_1.ApexTokenTypes.PUNCTUATION.QUOTTES_END;
}
function isCommentToken(token) {
return token.type === core_1.ApexTokenTypes.COMMENT.CONTENT || token.type === core_1.ApexTokenTypes.COMMENT.BLOCK_START || token.type === core_1.ApexTokenTypes.COMMENT.BLOCK_END || token.type === core_1.ApexTokenTypes.COMMENT.LINE || token.type === core_1.ApexTokenTypes.COMMENT.LINE_DOC;
}
function isDependentFlowStructure(token) {
return token.type === core_1.ApexTokenTypes.KEYWORD.FLOW_CONTROL.WHILE_DO || token.type === core_1.ApexTokenTypes.KEYWORD.FLOW_CONTROL.ELSE || token.type === core_1.ApexTokenTypes.KEYWORD.FLOW_CONTROL.ELSE_IF || token.type === core_1.ApexTokenTypes.KEYWORD.FLOW_CONTROL.CATCH || token.type === core_1.ApexTokenTypes.KEYWORD.FLOW_CONTROL.FINALLY;
}
function isLogicalOperator(token) {
switch (token.type) {
case core_1.ApexTokenTypes.OPERATOR.LOGICAL.AND:
case core_1.ApexTokenTypes.OPERATOR.LOGICAL.OR:
return true;
default:
return false;
}
}
function isCompareOperator(token) {
switch (token.type) {
case core_1.ApexTokenTypes.OPERATOR.LOGICAL.EQUALITY:
case core_1.ApexTokenTypes.OPERATOR.LOGICAL.EQUALITY_EXACT:
case core_1.ApexTokenTypes.OPERATOR.LOGICAL.GREATER_THAN:
case core_1.ApexTokenTypes.OPERATOR.LOGICAL.GREATER_THAN_EQUALS:
case core_1.ApexTokenTypes.OPERATOR.LOGICAL.INEQUALITY:
case core_1.ApexTokenTypes.OPERATOR.LOGICAL.INEQUALITY_EXACT:
case core_1.ApexTokenTypes.OPERATOR.LOGICAL.LESS_THAN:
case core_1.ApexTokenTypes.OPERATOR.LOGICAL.LESS_THAN_EQUALS:
return true;
default:
return false;
}
}
function isOperator(token) {
switch (token.type) {
case core_1.ApexTokenTypes.OPERATOR.ARITHMETIC.ADD:
case core_1.ApexTokenTypes.OPERATOR.ARITHMETIC.ADD_ASSIGN:
case core_1.ApexTokenTypes.OPERATOR.ARITHMETIC.DIVIDE:
case core_1.ApexTokenTypes.OPERATOR.ARITHMETIC.DIVIDE_ASSIGN:
case core_1.ApexTokenTypes.OPERATOR.ARITHMETIC.MULTIPLY:
case core_1.ApexTokenTypes.OPERATOR.ARITHMETIC.MULTIPLY_ASSIGN:
case core_1.ApexTokenTypes.OPERATOR.ARITHMETIC.SUBSTRACT:
case core_1.ApexTokenTypes.OPERATOR.ARITHMETIC.SUBSTRACT_ASSIGN:
case core_1.ApexTokenTypes.OPERATOR.ASSIGN.ASSIGN:
case core_1.ApexTokenTypes.OPERATOR.ASSIGN.MAP_KEY_VALUE:
case core_1.ApexTokenTypes.OPERATOR.BITWISE.AND:
case core_1.ApexTokenTypes.OPERATOR.BITWISE.EXCLUSIVE_OR:
case core_1.ApexTokenTypes.OPERATOR.BITWISE.EXCLUSIVE_OR_ASSIGN:
case core_1.ApexTokenTypes.OPERATOR.BITWISE.LEFT_ASSIGN:
case core_1.ApexTokenTypes.OPERATOR.BITWISE.OR:
case core_1.ApexTokenTypes.OPERATOR.BITWISE.SIGNED_LEFT:
case core_1.ApexTokenTypes.OPERATOR.BITWISE.SIGNED_RIGHT:
case core_1.ApexTokenTypes.OPERATOR.BITWISE.SIGNED_RIGTH_ASSIGN:
case core_1.ApexTokenTypes.OPERATOR.BITWISE.UNSIGNED_RIGHT:
case core_1.ApexTokenTypes.OPERATOR.BITWISE.UNSIGNED_RIGHT_ASSIGN:
case core_1.ApexTokenTypes.OPERATOR.LOGICAL.AND:
case core_1.ApexTokenTypes.OPERATOR.LOGICAL.AND_ASSIGN:
case core_1.ApexTokenTypes.OPERATOR.LOGICAL.EQUALITY:
case core_1.ApexTokenTypes.OPERATOR.LOGICAL.EQUALITY_EXACT:
case core_1.ApexTokenTypes.OPERATOR.LOGICAL.GREATER_THAN:
case core_1.ApexTokenTypes.OPERATOR.LOGICAL.GREATER_THAN_EQUALS:
case core_1.ApexTokenTypes.OPERATOR.LOGICAL.INEQUALITY:
case core_1.ApexTokenTypes.OPERATOR.LOGICAL.INEQUALITY_EXACT:
case core_1.ApexTokenTypes.OPERATOR.LOGICAL.LESS_THAN:
case core_1.ApexTokenTypes.OPERATOR.LOGICAL.LESS_THAN_EQUALS:
case core_1.ApexTokenTypes.OPERATOR.LOGICAL.OR:
case core_1.ApexTokenTypes.OPERATOR.LOGICAL.OR_ASSIGN:
case core_1.ApexTokenTypes.PUNCTUATION.EXMARK:
case core_1.ApexTokenTypes.PUNCTUATION.COLON:
return true;
default:
return false;
}
}
function isDatatype(token) {
switch (token.type) {
case core_1.ApexTokenTypes.DATATYPE.PRIMITIVE:
case core_1.ApexTokenTypes.DATATYPE.COLLECTION:
case core_1.ApexTokenTypes.DATATYPE.SOBJECT:
case core_1.ApexTokenTypes.DATATYPE.CUSTOM_CLASS:
case core_1.ApexTokenTypes.DATATYPE.SUPPORT_CLASS:
case core_1.ApexTokenTypes.DATATYPE.SUPPORT_NAMESPACE:
return true;
default:
return false;
}
}
function isQueryFunction(token) {
switch (token.type) {
case core_1.ApexTokenTypes.QUERY.FUNCTION:
return true;
default:
return false;
}
}
function isQueryClause(token) {
switch (token.type) {
case core_1.ApexTokenTypes.QUERY.CLAUSE.SELECT:
case core_1.ApexTokenTypes.QUERY.CLAUSE.FROM:
case core_1.ApexTokenTypes.QUERY.CLAUSE.WHERE:
case core_1.ApexTokenTypes.QUERY.CLAUSE.TYPE_OF:
case core_1.ApexTokenTypes.QUERY.CLAUSE.WHEN:
case core_1.ApexTokenTypes.QUERY.CLAUSE.ELSE:
case core_1.ApexTokenTypes.QUERY.CLAUSE.THEN:
case core_1.ApexTokenTypes.QUERY.CLAUSE.FOR:
case core_1.ApexTokenTypes.QUERY.CLAUSE.GROUP_BY:
case core_1.ApexTokenTypes.QUERY.CLAUSE.HAVING:
case core_1.ApexTokenTypes.QUERY.CLAUSE.END:
case core_1.ApexTokenTypes.QUERY.CLAUSE.FIND:
case core_1.ApexTokenTypes.QUERY.CLAUSE.LIMIT:
case core_1.ApexTokenTypes.QUERY.CLAUSE.NULLS:
case core_1.ApexTokenTypes.QUERY.CLAUSE.OFFSET:
case core_1.ApexTokenTypes.QUERY.CLAUSE.ORDER_BY:
case core_1.ApexTokenTypes.QUERY.CLAUSE.USING_SCOPE:
case core_1.ApexTokenTypes.QUERY.CLAUSE.WITH:
return true;
default:
return false;
}
}
function isInitializer(token) {
return token.type === core_1.ApexTokenTypes.BRACKET.INITIALIZER_OPEN;
}
function isOnSameLine(tokenA, tokenB) {
return tokenA && tokenB && tokenA.range.start.line === tokenB.range.start.line;
}
function isFieldInstructionDeclaration(tokens, index) {
var token = tokens[index];
for (; index >= 0; index--) {
token = tokens[index];
if (isFieldDeclaration(token)) {
return true;
}
if (token.type === core_1.ApexTokenTypes.BRACKET.CURLY_CLOSE || token.type === core_1.ApexTokenTypes.BRACKET.CURLY_OPEN) {
break;
}
}
return false;
}
function isNextInstructionFieldDeclaration(tokens, index) {
var token = tokens[index];
for (var len = tokens.length; index < len; index++) {
token = tokens[index];
if (isFieldDeclaration(token)) {
return true;
}
if (token.type === core_1.ApexTokenTypes.PUNCTUATION.SEMICOLON) {
break;
}
}
return false;
}
function isAnnotationToken(token) {
switch (token.type) {
case core_1.ApexTokenTypes.BRACKET.ANNOTATION_PARAM_OPEN:
case core_1.ApexTokenTypes.BRACKET.ANNOTATION_PARAM_CLOSE:
case core_1.ApexTokenTypes.ANNOTATION.CONTENT:
case core_1.ApexTokenTypes.ANNOTATION.ENTITY:
case core_1.ApexTokenTypes.ANNOTATION.NAME:
return true;
default:
return false;
}
}
function isFieldDeclaration(token) {
return token.type === core_1.ApexTokenTypes.DECLARATION.ENTITY.VARIABLE;
}
function isMemberDeclaration(token) {
switch (token.type) {
case core_1.ApexTokenTypes.DECLARATION.ENTITY.FUNCTION:
case core_1.ApexTokenTypes.DECLARATION.ENTITY.PROPERTY:
case core_1.ApexTokenTypes.DECLARATION.ENTITY.VARIABLE:
return true;
default:
return false;
}
}
function isUnaryOperator(token) {
switch (token.type) {
case core_1.ApexTokenTypes.OPERATOR.ARITHMETIC.POSITIVE:
case core_1.ApexTokenTypes.OPERATOR.ARITHMETIC.DECREMENT:
case core_1.ApexTokenTypes.OPERATOR.ARITHMETIC.INCREMENT:
case core_1.ApexTokenTypes.OPERATOR.ARITHMETIC.NEGATIVE:
return true;
default:
return false;
}
}
function isKeyword(token) {
switch (token.type) {
case core_1.ApexTokenTypes.KEYWORD.OTHER:
case core_1.ApexTokenTypes.KEYWORD.DECLARATION.CLASS:
case core_1.ApexTokenTypes.KEYWORD.DECLARATION.ENUM:
case core_1.ApexTokenTypes.KEYWORD.DECLARATION.EXTENDS:
case core_1.ApexTokenTypes.KEYWORD.DECLARATION.IMPLEMENTS:
case core_1.ApexTokenTypes.KEYWORD.DECLARATION.INTERFACE:
case core_1.ApexTokenTypes.DECLARATION.ENTITY.CLASS:
case core_1.ApexTokenTypes.DECLARATION.ENTITY.ENUM:
case core_1.ApexTokenTypes.DECLARATION.ENTITY.INTERFACE:
case core_1.ApexTokenTypes.KEYWORD.DECLARATION.INTERFACE:
case core_1.ApexTokenTypes.KEYWORD.DECLARATION.TRIGGER:
case core_1.ApexTokenTypes.KEYWORD.MODIFIER.ACCESS:
case core_1.ApexTokenTypes.KEYWORD.MODIFIER.DEFINITION:
case core_1.ApexTokenTypes.KEYWORD.MODIFIER.FINAL:
case core_1.ApexTokenTypes.KEYWORD.MODIFIER.OVERRIDE:
case core_1.ApexTokenTypes.KEYWORD.MODIFIER.SHARING:
case core_1.ApexTokenTypes.KEYWORD.MODIFIER.STATIC:
case core_1.ApexTokenTypes.KEYWORD.MODIFIER.TEST_METHOD:
case core_1.ApexTokenTypes.KEYWORD.MODIFIER.TRANSIENT:
case core_1.ApexTokenTypes.KEYWORD.MODIFIER.WEB_SERVICE:
case core_1.ApexTokenTypes.KEYWORD.OBJECT.NEW:
case core_1.ApexTokenTypes.DATABASE.TRIGGER_EXEC:
case core_1.ApexTokenTypes.DATABASE.DML:
case core_1.ApexTokenTypes.KEYWORD.FLOW_CONTROL.BREAK:
case core_1.ApexTokenTypes.KEYWORD.FLOW_CONTROL.SWITCH:
case core_1.ApexTokenTypes.KEYWORD.FLOW_CONTROL.SWITCH_CASE:
case core_1.ApexTokenTypes.KEYWORD.FLOW_CONTROL.CONTINUE:
case core_1.ApexTokenTypes.KEYWORD.FLOW_CONTROL.RETURN:
case core_1.ApexTokenTypes.KEYWORD.FLOW_CONTROL.THROW:
return true;
default:
return false;
}
}
function isEmptyProperty(tokens, index) {
var isEmptyProperty = true;
for (; index < tokens.length; index++) {
var token = tokens[index];
var nextToken = utils_1.LanguageUtils.getNextToken(tokens, index);
if ((token.type === core_1.ApexTokenTypes.KEYWORD.DECLARATION.PROPERTY_GETTER || token.type === core_1.ApexTokenTypes.KEYWORD.DECLARATION.PROPERTY_SETTER) && nextToken && nextToken.type !== core_1.ApexTokenTypes.PUNCTUATION.SEMICOLON) {
isEmptyProperty = false;
break;
}
if (token.type === core_1.ApexTokenTypes.BRACKET.CURLY_CLOSE) {
break;
}
}
return isEmptyProperty;
}
//# sourceMappingURL=formatter.js.map