From b8ec0fae519efefd704c27ecefde6f268b4822a0 Mon Sep 17 00:00:00 2001 From: volodya-lombrozo Date: Wed, 18 Dec 2024 18:04:38 +0300 Subject: [PATCH] feat(#3332): fix all the code offences --- .../java/org/eolang/parser/ParsingErrors.java | 4 ++ .../org/eolang/parser/UnderlinedMessage.java | 56 +++++++++++++++++-- .../eolang/parser/UnderlinedMessageTest.java | 28 +++++++++- 3 files changed, 81 insertions(+), 7 deletions(-) diff --git a/eo-parser/src/main/java/org/eolang/parser/ParsingErrors.java b/eo-parser/src/main/java/org/eolang/parser/ParsingErrors.java index 3c32057bfe..31247fe267 100644 --- a/eo-parser/src/main/java/org/eolang/parser/ParsingErrors.java +++ b/eo-parser/src/main/java/org/eolang/parser/ParsingErrors.java @@ -83,6 +83,10 @@ public void syntaxError( final String msg, final RecognitionException error ) { + // @checkstyle MethodBodyCommentsCheck (20 lines) + // @todo #3332:30min Add more specific error messages. + // Currently we write just "error: no viable alternative at input" for all errors. + // It's better to use {@link #recognizer} to get more specific error messages. if (error instanceof NoViableAltException) { final Token token = (Token) symbol; this.errors.add( diff --git a/eo-parser/src/main/java/org/eolang/parser/UnderlinedMessage.java b/eo-parser/src/main/java/org/eolang/parser/UnderlinedMessage.java index d036e65068..8cd9a20b40 100644 --- a/eo-parser/src/main/java/org/eolang/parser/UnderlinedMessage.java +++ b/eo-parser/src/main/java/org/eolang/parser/UnderlinedMessage.java @@ -25,18 +25,54 @@ import java.util.Collections; +/** + * Underlined message. + * For example, if you have a message "Problem is here" and you want to underline + * the word "is", you can create an instance of this class with the following + * parameters: origin="Problem is here", from=8, length=2. + * The result will be: + * {@code + * Problem is here + * ^^ + * } + * @since 0.50 + * @todo #3332:30min Add more decorators for the error message. + * For example, {@link ParsingErrors} currently contains logic related to the message formatting. + * It's better to create a separate class for this purpose. + */ final class UnderlinedMessage { + /** + * The message. + */ private final String origin; + + /** + * The position from which to start underlining. + */ private final int from; + + /** + * The length of the underline. + */ private final int length; + /** + * Ctor. + * @param origin The message. + * @param from The position from which to start underlining. + * @param length The length of the underline. + */ UnderlinedMessage(final String origin, final int from, final int length) { this.origin = origin; this.from = from; this.length = length; } + /** + * Formatted message. + * @return The formatted message. + */ String formatted() { return String.format( "%s\n%s", @@ -45,23 +81,33 @@ String formatted() { ); } + /** + * Underline. + * @return The underlined string. + */ private String underline() { final String result; if (this.origin.isEmpty() || this.length <= 0 || this.from >= this.origin.length()) { result = ""; } else if (this.from < 0) { - result = this.repeat("^", this.origin.length()); + result = UnderlinedMessage.repeat("^", this.origin.length()); } else { result = String.format( "%s%s", - this.repeat(" ", this.from), - this.repeat("^", Math.min(this.length, this.origin.length())) + UnderlinedMessage.repeat(" ", this.from), + UnderlinedMessage.repeat("^", Math.min(this.length, this.origin.length())) ); } return result; } - private String repeat(final String symbol, final int n) { - return String.join("", Collections.nCopies(n, symbol)); + /** + * Repeat a symbol n times. + * @param symbol The symbol. + * @param times The number of times to repeat the symbol. + * @return The repeated symbol. + */ + private static String repeat(final String symbol, final int times) { + return String.join("", Collections.nCopies(times, symbol)); } } diff --git a/eo-parser/src/test/java/org/eolang/parser/UnderlinedMessageTest.java b/eo-parser/src/test/java/org/eolang/parser/UnderlinedMessageTest.java index 409aeca712..bcfa6495a0 100644 --- a/eo-parser/src/test/java/org/eolang/parser/UnderlinedMessageTest.java +++ b/eo-parser/src/test/java/org/eolang/parser/UnderlinedMessageTest.java @@ -1,3 +1,26 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2024 Objectionary.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ package org.eolang.parser; import java.util.stream.Stream; @@ -10,7 +33,9 @@ /** * Test case for {@link UnderlinedMessage}. * @since 0.1 + * @checkstyle ParameterNumberCheck (500 lines) */ +@SuppressWarnings("PMD.AvoidDuplicateLiterals") final class UnderlinedMessageTest { @ParameterizedTest @@ -47,5 +72,4 @@ static Stream examples() { Arguments.of("", 1, 10, "\n") ); } - -} \ No newline at end of file +}