Skip to content

Refactor trimIndent Method #2891

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 8, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -32,7 +32,8 @@
import java.util.Map;

import io.swagger.v3.oas.models.SpecVersion;
import org.apache.commons.lang3.StringUtils;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springdoc.core.properties.SpringDocConfigProperties;
@@ -114,23 +115,24 @@ public String resolve(String parameterProperty, Locale locale) {
* Returns a string where all leading indentation has been removed from each line.
* It detects the smallest common indentation of all the lines in the input string,
* and removes it.
* If the input text is {@code null}, the method returns {@code null}.
*
* @param text The original string with possible leading indentation.
* @return The string with leading indentation removed from each line.
* @param text The original string with possible leading indentation.
* @return The string with the smallest common leading indentation removed from each line,
* or {@code null} if the input text is {@code null}.
*/
public String trimIndent(String text) {
if (text == null) {
return null;
}
final String newLine = "\n";
String[] lines = text.split("\\r?\\n");
int minIndent = resolveMinIndent(lines);
try {
if (text == null) {
return null;
}
final String newLine = "\n";
String[] lines = text.split(newLine);
int minIndent = resolveMinIndent(lines);
return Arrays.stream(lines)
.map(line -> line.substring(Math.min(line.length(), minIndent)))
.reduce((a, b) -> a + newLine + b)
.orElse(StringUtils.EMPTY);
} catch (Exception ex){
.map(line -> line.substring(Math.min(line.length(), minIndent)))
.collect(Collectors.joining(newLine));
} catch (Exception ex) {
LOGGER.warn(ex.getMessage());
return text;
}
@@ -239,4 +241,4 @@ public Map<String, Object> resolveExtensions(Locale locale, Map<String, Object>
else
return extensions;
}
}
}