Skip to content

Commit

Permalink
Update option names
Browse files Browse the repository at this point in the history
  • Loading branch information
poorna2152 committed Apr 4, 2024
1 parent d25bf3e commit ad4ddd5
Show file tree
Hide file tree
Showing 37 changed files with 192 additions and 178 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ indentSize = 2
[wrapping]
maxLineLength = 80
simpleBlocksInOneLine = true
simpleMethodsInOneLine = true
simpleFunctionsInOneLine = true

[methodDeclaration]
[functionDeclaration]
alignMultilineParameters = true

[ifStatement]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,10 @@ static void execute(List<String> argList, boolean helpFlag, String moduleName, S
generateChangeReport(formattedFiles, dryRun);
}
}
} catch (IOException | NullPointerException | FormatterException e) {
} catch (IOException | NullPointerException e) {
throw LauncherUtils.createLauncherException(Messages.getException() + e);
} catch (FormatterException e) {
throw LauncherUtils.createLauncherException(Messages.getException() + e.getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class Messages {

private static final String NO_MODULE_FOUND = "couldn't find an existing module by the name: ";

private static final String EXCEPTION = "something went wrong when formatting." + System.lineSeparator();
private static final String EXCEPTION = "formatting failed - ";

private static final String NO_BALLERINA_FILE = "couldn't find an existing ballerina file by the name: ";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ indentSize = 2
maxLineLength = 120

[braces]
methodBraceStyle = "NewLine"
functionBraceStyle = "NewLine"

[ifStatement]
elseOnNewLine = true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[braces]
classBraceStyle = "NewLine"
methodBraceStyle = "NewLine"
functionBraceStyle = "NewLine"
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[methodCall]
parametersWrap = "ChopDown"
alignMultilineParameters = true
[functionCall]
argumentsWrap = "ChopDown"
alignMultilineArguments = true
newLineAfterLeftParen = true

[wrapping]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[methodCall]
parametersWrap = "NoWrap"
[functionCall]
argumentsWrap = "NoWrap"
newLineAfterLeftParen = true

[wrapping]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[methodCall]
parametersWrap = "Wrap"
alignMultilineParameters = true
[functionCall]
argumentsWrap = "Wrap"
alignMultilineArguments = true
newLineAfterLeftParen = true
rightParenOnNewLine = true

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[methodDeclaration]
[functionDeclaration]
parametersWrap = "ChopDown"
alignMultilineParameters = true

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[methodDeclaration]
[functionDeclaration]
parametersWrap = "NoWrap"

[wrapping]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[methodDeclaration]
[functionDeclaration]
parametersWrap = "Wrap"
alignMultilineParameters = true
newLineAfterLeftParen = true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[indent]
indentSize = 2
continuationIndentSize = 2
continuationIndentSize = 4
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[wrapping]
maxLineLength = 80
simpleBlocksInOneLine = true
simpleMethodsInOneLine = true
simpleFunctionsInOneLine = true
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ continuationIndentSize = 4
maxLineLength = 80


[methodDeclaration]
[functionDeclaration]
alignMultilineParameters = true
newLineAfterLeftParen = true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -188,7 +189,7 @@ public static Map<String, Object> getFormattingConfigurations(Path root, String
content = Files.readString(absPath.get(), StandardCharsets.UTF_8);
} catch (IOException e) {
throw new FormatterException(
"Failed to retrieve local formatting configuration file: " + configurationFilePath);
"failed to retrieve local formatting configuration file: " + configurationFilePath);
}
} else {
content = readRemoteFormatFile(root, configurationFilePath);
Expand All @@ -215,7 +216,7 @@ private static String readRemoteFormatFile(Path root, String fileUrl) throws For
try {
return Files.readString(cachePath, StandardCharsets.UTF_8);
} catch (IOException e) {
throw new FormatterException("Failed to read cached formatting configuration file");
throw new FormatterException("failed to read cached formatting configuration file");
}
}

Expand All @@ -224,13 +225,13 @@ private static String readRemoteFormatFile(Path root, String fileUrl) throws For
URL url = new URL(fileUrl);
URLConnection connection = url.openConnection();
if (!(connection instanceof HttpURLConnection)) {
throw new FormatterException("Configuration file remote url is not an HTTP url: " + fileUrl);
throw new FormatterException("configuration file remote url is not an HTTP url: " + fileUrl);
}
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
int responseCode = httpURLConnection.getResponseCode();
if (responseCode != HttpURLConnection.HTTP_OK) {
httpURLConnection.disconnect();
throw new FormatterException("Failed to retrieve remote file. HTTP response code: " + responseCode);
throw new FormatterException("failed to retrieve remote file. HTTP response code: " + responseCode);
}
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(httpURLConnection.getInputStream(), StandardCharsets.UTF_8))) {
Expand All @@ -241,7 +242,7 @@ private static String readRemoteFormatFile(Path root, String fileUrl) throws For
}
httpURLConnection.disconnect();
} catch (IOException e) {
throw new FormatterException("Failed to retrieve formatting configuration file: " + fileUrl);
throw new FormatterException("failed to retrieve formatting configuration file: " + fileUrl);
}
cacheRemoteConfigurationFile(root, fileContent.toString());

Expand All @@ -258,14 +259,14 @@ private static void cacheRemoteConfigurationFile(Path root, String content) thro
try {
Files.createDirectories(formatDir);
} catch (IOException e) {
throw new FormatterException("Failed to create format configuration cache directory");
throw new FormatterException("failed to create format configuration cache directory");
}
}
String filePath = formatDir.resolve(DEFAULT_FORMAT_OPTION_FILE).toString();
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath, StandardCharsets.UTF_8))) {
writer.write(content);
} catch (IOException e) {
throw new FormatterException("Failed to write format configuration cache file");
throw new FormatterException("failed to write format configuration cache file");
}
}

Expand All @@ -274,7 +275,7 @@ private static void cacheRemoteConfigurationFile(Path root, String content) thro
*
* @param document formatting configuration toml file
* @return the formatting options
* @throws FormatterException if the configuration file validation fails
* @throws FormatterException if the configuration file X`n fails
*/
public static Map<String, Object> parseConfigurationToml(TomlDocument document) throws FormatterException {
Toml toml = document.toml();
Expand All @@ -290,15 +291,15 @@ public static Map<String, Object> parseConfigurationToml(TomlDocument document)
formatTomlValidator.validate(toml);

List<Diagnostic> diagnostics = toml.diagnostics();
boolean hasErrors = false;
ArrayList<String> errMessages = new ArrayList<>();
for (Diagnostic diagnostic: diagnostics) {
if (diagnostic.diagnosticInfo().severity().equals(DiagnosticSeverity.ERROR)) {
errStream.println(diagnostic.message());
hasErrors = true;
errMessages.add(diagnostic.message());
}
}
if (hasErrors) {
throw new FormatterException("Invalid Format.toml file");
if (errMessages.size() > 0) {
throw new FormatterException("invalid formatting configuration file" + System.lineSeparator() +
String.join(System.lineSeparator(), errMessages));
}
return toml.toMap();
}
Expand Down Expand Up @@ -432,11 +433,19 @@ static int openBraceTrailingNLs(WrappingFormattingOptions options, Node node) {
return 1;
}

SyntaxKind parentKind = node.parent().kind();
if (options.isSimpleBlocksInOneLine() && parentKind != SyntaxKind.METHOD_DECLARATION) {
boolean isParentAFunction = isFunctionNode(node.parent());
if (options.isSimpleBlocksInOneLine() && !isParentAFunction) {
return 0;
}
return (options.isSimpleMethodsInOneLine() && parentKind == SyntaxKind.METHOD_DECLARATION) ? 0 : 1;
return (options.isSimpleFunctionsInOneLine() && isParentAFunction) ? 0 : 1;
}

private static boolean isFunctionNode(Node node) {
return switch (node.kind()) {
case FUNCTION_DEFINITION, METHOD_DECLARATION, OBJECT_METHOD_DEFINITION, RESOURCE_ACCESSOR_DEFINITION ->
true;
default -> false;
};
}

static int getConstDefWidth(ConstantDeclarationNode node) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ public FunctionDefinitionNode transform(FunctionDefinitionNode functionDefinitio
functionName = formatToken(functionDefinitionNode.functionName(), 1, 0);
}
NodeList<Node> relativeResourcePath = formatNodeList(functionDefinitionNode.relativeResourcePath(), 0, 0, 0, 0);
int trailingNL = options.braceFormattingOptions().methodBraceStyle() == BraceStyle.NewLine ? 1 : 0;
int trailingNL = options.braceFormattingOptions().functionBraceStyle() == BraceStyle.NewLine ? 1 : 0;
FunctionSignatureNode functionSignatureNode =
formatNode(functionDefinitionNode.functionSignature(), invert(trailingNL), trailingNL);
FunctionBodyNode functionBodyNode =
Expand Down Expand Up @@ -1094,13 +1094,13 @@ public FunctionCallExpressionNode transform(FunctionCallExpressionNode functionC
NameReferenceNode functionName = formatNode(functionCallExpressionNode.functionName(), 0, 0);
FunctionCallFormattingOptions callOptions = options.functionCallFormattingOptions();
int prevIndentation = env.currentIndentation;
alignOrIndent(callOptions.alignMultilineParameters(),
alignOrIndent(callOptions.alignMultilineArguments(),
options.indentFormattingOptions().continuationIndentSize());
Token functionCallOpenPara = formatToken(functionCallExpressionNode.openParenToken(), 0,
callOptions.newLineAfterLeftParen() ? 1 : 0);

int closeParenLeadingNL = callOptions.rightParenOnNewLine() ? 1 : 0;
int separatorTrailingWS = callOptions.parametersWrap() == WrappingMethod.ChopDown ? 0 : 1;
int separatorTrailingWS = callOptions.argumentsWrap() == WrappingMethod.ChopDown ? 0 : 1;
SeparatedNodeList<FunctionArgumentNode> arguments =
formatSeparatedNodeList(functionCallExpressionNode.arguments(), 0, 0, separatorTrailingWS,
invert(separatorTrailingWS), 0, closeParenLeadingNL, true);
Expand Down Expand Up @@ -4278,7 +4278,7 @@ private boolean shouldWrapLine(Node node, Node parent) {
case POSITIONAL_ARG:
case NAMED_ARG:
case REST_ARG:
if (options.functionCallFormattingOptions().parametersWrap() == WrappingMethod.NoWrap) {
if (options.functionCallFormattingOptions().argumentsWrap() == WrappingMethod.NoWrap) {
return false;
}
return true;
Expand Down Expand Up @@ -4550,16 +4550,16 @@ private List<Minutiae> removeTrailingWS(List<Minutiae> trailingMinutiae) {
* Indent the code by the number of white-spaces defined by tab-size.
*/
private void indent() {
indent(1);
indent(options.indentFormattingOptions().indentSize());
}

/**
* Indent the code by the number of white-spaces defined by tab-size.
*
* @param step Number of tabs.
* @param step Number of spaces.
*/
private void indent(int step) {
env.currentIndentation += (options.indentFormattingOptions().indentSize() * step);
env.currentIndentation += step;
}

/**
Expand All @@ -4574,7 +4574,7 @@ private void align() {
* Undo the indentation of the code by the number of white-spaces defined by tab-size.
*/
private void unindent() {
unindent(1);
unindent(options.indentFormattingOptions().indentSize());
}

/**
Expand All @@ -4583,12 +4583,12 @@ private void unindent() {
* @param step Number of tabs.
*/
private void unindent(int step) {
if (env.currentIndentation < (options.indentFormattingOptions().indentSize() * step)) {
if (env.currentIndentation < step) {
env.currentIndentation = 0;
return;
}

env.currentIndentation -= (options.indentFormattingOptions().indentSize() * step);
env.currentIndentation -= step;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@
public class BraceFormattingOptions {

private final BraceStyle classBraceStyle;
private final BraceStyle methodBraceStyle;
private final BraceStyle functionBraceStyle;

private BraceFormattingOptions(BraceStyle classBraceStyle, BraceStyle methodBraceStyle) {
private BraceFormattingOptions(BraceStyle classBraceStyle, BraceStyle functionBraceStyle) {
this.classBraceStyle = classBraceStyle;
this.methodBraceStyle = methodBraceStyle;
this.functionBraceStyle = functionBraceStyle;
}

public BraceStyle classBraceStyle() {
return classBraceStyle;
}

public BraceStyle methodBraceStyle() {
return methodBraceStyle;
public BraceStyle functionBraceStyle() {
return functionBraceStyle;
}

public static BraceFormattingOptions.BraceFormattingOptionsBuilder builder() {
Expand All @@ -51,24 +51,24 @@ public static BraceFormattingOptions.BraceFormattingOptionsBuilder builder() {
public static class BraceFormattingOptionsBuilder {

private static final String CLASS_BRACE_STYLE = "classBraceStyle";
private static final String METHOD_BRACE_STYLE = "methodBraceStyle";
private static final String FUNCTION_BRACE_STYLE = "functionBraceStyle";
private BraceStyle classBraceStyle =
BraceStyle.valueOf(getDefaultString(FormatSection.BRACES, CLASS_BRACE_STYLE));
private BraceStyle methodBraceStyle =
BraceStyle.valueOf(getDefaultString(FormatSection.BRACES, METHOD_BRACE_STYLE));
private BraceStyle functionBraceStyle =
BraceStyle.valueOf(getDefaultString(FormatSection.BRACES, FUNCTION_BRACE_STYLE));

public BraceFormattingOptionsBuilder setClassBraceStyle(BraceStyle classBraceStyle) {
this.classBraceStyle = classBraceStyle;
return this;
}

public BraceFormattingOptionsBuilder setMethodBraceStyle(BraceStyle methodBraceStyle) {
this.methodBraceStyle = methodBraceStyle;
public BraceFormattingOptionsBuilder setFunctionBraceStyle(BraceStyle functionBraceStyle) {
this.functionBraceStyle = functionBraceStyle;
return this;
}

public BraceFormattingOptions build() {
return new BraceFormattingOptions(classBraceStyle, methodBraceStyle);
return new BraceFormattingOptions(classBraceStyle, functionBraceStyle);
}

public BraceFormattingOptions build(Map<String, Object> configs) throws FormatterException {
Expand All @@ -77,8 +77,8 @@ public BraceFormattingOptions build(Map<String, Object> configs) throws Formatte
BraceStyle style = BraceStyle.fromString((String) bracesEntry.getValue());
switch (bracesKey) {
case CLASS_BRACE_STYLE -> setClassBraceStyle(style);
case METHOD_BRACE_STYLE -> setMethodBraceStyle(style);
default -> throw new FormatterException("Invalid Brace Option: " + bracesKey);
case FUNCTION_BRACE_STYLE -> setFunctionBraceStyle(style);
default -> throw new FormatterException("invalid Brace Option: " + bracesKey);
}
}
return build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static BraceStyle fromString(String value) throws FormatterException {
try {
return BraceStyle.valueOf(value);
} catch (IllegalArgumentException e) {
throw new FormatterException("Invalid Brace style: " + value);
throw new FormatterException("invalid Brace style: " + value);
}
}
}
Loading

0 comments on commit ad4ddd5

Please sign in to comment.