From 70b356cec6718dbb580d242b0da3a38d82fcfab9 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Wed, 29 Jan 2014 18:23:59 +0100 Subject: [PATCH] Stop reading code source when to param is reached --- .../source/DeprecatedSourceDecorator.java | 6 +- .../server/source/HtmlTextDecorator.java | 70 ++++++++++++------- .../server/source/HtmlTextDecoratorTest.java | 6 +- 3 files changed, 50 insertions(+), 32 deletions(-) diff --git a/sonar-server/src/main/java/org/sonar/server/source/DeprecatedSourceDecorator.java b/sonar-server/src/main/java/org/sonar/server/source/DeprecatedSourceDecorator.java index 352d7df4d91..3eac6043204 100644 --- a/sonar-server/src/main/java/org/sonar/server/source/DeprecatedSourceDecorator.java +++ b/sonar-server/src/main/java/org/sonar/server/source/DeprecatedSourceDecorator.java @@ -85,8 +85,10 @@ public class DeprecatedSourceDecorator implements ServerComponent { List splitSource = newArrayList(Splitter.onPattern("\r?\n|\r").split(htmlSource)); List result = newArrayList(); for (int i = 0; i < splitSource.size(); i++) { - int currentLine = i+1; - if ((from == null || currentLine >= from) && (to == null || to >= currentLine)) { + int currentLine = i + 1; + if (to != null && to < currentLine) { + break; + } else if (from == null || currentLine >= from) { result.add(splitSource.get(currentLine - 1)); } } diff --git a/sonar-server/src/main/java/org/sonar/server/source/HtmlTextDecorator.java b/sonar-server/src/main/java/org/sonar/server/source/HtmlTextDecorator.java index 41ca1ba0d37..a1f481a089f 100644 --- a/sonar-server/src/main/java/org/sonar/server/source/HtmlTextDecorator.java +++ b/sonar-server/src/main/java/org/sonar/server/source/HtmlTextDecorator.java @@ -19,7 +19,6 @@ */ package org.sonar.server.source; -import com.google.common.collect.Lists; import com.google.common.io.Closeables; import org.slf4j.LoggerFactory; @@ -31,6 +30,7 @@ import java.io.StringReader; import java.util.Collection; import java.util.List; +import static com.google.common.collect.Lists.newArrayList; /** @@ -54,7 +54,7 @@ class HtmlTextDecorator { List decorateTextWithHtml(String text, DecorationDataHolder decorationDataHolder, @Nullable Integer from, @Nullable Integer to) { StringBuilder currentHtmlLine = new StringBuilder(); - List decoratedHtmlLines = Lists.newArrayList(); + List decoratedHtmlLines = newArrayList(); int currentLine = 1; BufferedReader stringBuffer = null; @@ -64,30 +64,17 @@ class HtmlTextDecorator { CharactersReader charsReader = new CharactersReader(stringBuffer); while (charsReader.readNextChar()) { - + if (shouldStop(currentLine, to)) { + break; + } if (shouldStartNewLine(charsReader)) { - addLine(decoratedHtmlLines, currentHtmlLine.toString(), currentLine, from, to); + if (canAddLine(currentLine, from)) { + decoratedHtmlLines.add(currentHtmlLine.toString()); + } currentLine++; currentHtmlLine = new StringBuilder(); - if (shouldReopenPendingTags(charsReader)) { - reopenCurrentSyntaxTags(charsReader, currentHtmlLine); - } - } - - int numberOfTagsToClose = getNumberOfTagsToClose(charsReader.getCurrentIndex(), decorationDataHolder); - closeCompletedTags(charsReader, numberOfTagsToClose, currentHtmlLine); - - if (shouldClosePendingTags(charsReader)) { - closeCurrentSyntaxTags(charsReader, currentHtmlLine); - } - - Collection tagsToOpen = getTagsToOpen(charsReader.getCurrentIndex(), decorationDataHolder); - openNewTags(charsReader, tagsToOpen, currentHtmlLine); - - if (shouldAppendCharToHtmlOutput(charsReader)) { - char currentChar = (char) charsReader.getCurrentValue(); - currentHtmlLine.append(normalize(currentChar)); } + addCharToCurrentLine(charsReader, currentHtmlLine, decorationDataHolder); } closeCurrentSyntaxTags(charsReader, currentHtmlLine); @@ -96,10 +83,8 @@ class HtmlTextDecorator { addLine(decoratedHtmlLines, currentHtmlLine.toString(), currentLine, from, to); currentLine++; addLine(decoratedHtmlLines, "", currentLine, from, to); - currentLine++; } else if (currentHtmlLine.length() > 0) { addLine(decoratedHtmlLines, currentHtmlLine.toString(), currentLine, from, to); - currentLine++; } } catch (IOException exception) { @@ -113,13 +98,44 @@ class HtmlTextDecorator { return decoratedHtmlLines; } + private void addCharToCurrentLine(CharactersReader charsReader, StringBuilder currentHtmlLine, DecorationDataHolder decorationDataHolder) { + if (shouldStartNewLine(charsReader)) { + if (shouldReopenPendingTags(charsReader)) { + reopenCurrentSyntaxTags(charsReader, currentHtmlLine); + } + } + + int numberOfTagsToClose = getNumberOfTagsToClose(charsReader.getCurrentIndex(), decorationDataHolder); + closeCompletedTags(charsReader, numberOfTagsToClose, currentHtmlLine); + + if (shouldClosePendingTags(charsReader)) { + closeCurrentSyntaxTags(charsReader, currentHtmlLine); + } + + Collection tagsToOpen = getTagsToOpen(charsReader.getCurrentIndex(), decorationDataHolder); + openNewTags(charsReader, tagsToOpen, currentHtmlLine); + + if (shouldAppendCharToHtmlOutput(charsReader)) { + char currentChar = (char) charsReader.getCurrentValue(); + currentHtmlLine.append(normalize(currentChar)); + } + } + private void addLine(List decoratedHtmlLines, String line, int currentLine, @Nullable Integer from, @Nullable Integer to) { - if ((from == null || currentLine >= from) - && (to == null || to >= currentLine)) { + if (canAddLine(currentLine, from) && !shouldStop(currentLine, to)) { decoratedHtmlLines.add(line); } } + private boolean canAddLine(int currentLine, @Nullable Integer from) { + return from == null || currentLine >= from; + } + + private boolean shouldStop(int currentLine, @Nullable Integer to) { + return to != null && to < currentLine; + } + + private char[] normalize(char currentChar) { char[] normalizedChars; if (currentChar == HTML_OPENING) { @@ -149,7 +165,7 @@ class HtmlTextDecorator { } private Collection getTagsToOpen(int currentIndex, DecorationDataHolder dataHolder) { - Collection tagsToOpen = Lists.newArrayList(); + Collection tagsToOpen = newArrayList(); while (dataHolder.getCurrentOpeningTagEntry() != null && currentIndex == dataHolder.getCurrentOpeningTagEntry().getStartOffset()) { tagsToOpen.add(dataHolder.getCurrentOpeningTagEntry().getCssClass()); dataHolder.nextOpeningTagEntry(); diff --git a/sonar-server/src/test/java/org/sonar/server/source/HtmlTextDecoratorTest.java b/sonar-server/src/test/java/org/sonar/server/source/HtmlTextDecoratorTest.java index 3e5de01690a..a6b046ce2a3 100644 --- a/sonar-server/src/test/java/org/sonar/server/source/HtmlTextDecoratorTest.java +++ b/sonar-server/src/test/java/org/sonar/server/source/HtmlTextDecoratorTest.java @@ -303,7 +303,7 @@ public class HtmlTextDecoratorTest { } @Test - public void begin_from_given_line() throws Exception { + public void returned_code_begin_from_given_param() throws Exception { String javadocWithHtml = "/**\n" + @@ -339,7 +339,7 @@ public class HtmlTextDecoratorTest { } @Test - public void end_to_given_line() throws Exception { + public void returned_code_end_to_given_param() throws Exception { String javadocWithHtml = "/**\n" + @@ -370,7 +370,7 @@ public class HtmlTextDecoratorTest { } @Test - public void return_code_from_given_lint_given_end_line() throws Exception { + public void returned_code_is_between_from_and_to_params() throws Exception { String javadocWithHtml = "/**\n" + -- 2.39.5