diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2015-11-13 16:24:18 +0100 |
---|---|---|
committer | Duarte Meneses <duarte.meneses@sonarsource.com> | 2015-11-17 09:36:04 +0100 |
commit | d986919813676f22b0d0f34f4e1cb8a65e7a0481 (patch) | |
tree | ab43781547ce73275a4516130d34ee96846050b5 /sonar-batch | |
parent | aef7ab9d126129f9e6382c1f06eb46cfb5565d8f (diff) | |
download | sonarqube-d986919813676f22b0d0f34f4e1cb8a65e7a0481.tar.gz sonarqube-d986919813676f22b0d0f34f4e1cb8a65e7a0481.zip |
SONAR-5894 Support symbol references with different length
Diffstat (limited to 'sonar-batch')
4 files changed, 64 insertions, 3 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/source/DefaultSymbolTable.java b/sonar-batch/src/main/java/org/sonar/batch/source/DefaultSymbolTable.java index 39d65df0cd1..99fa5a92f82 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/source/DefaultSymbolTable.java +++ b/sonar-batch/src/main/java/org/sonar/batch/source/DefaultSymbolTable.java @@ -99,10 +99,15 @@ public class DefaultSymbolTable implements Symbolizable.SymbolTable { @Override public void newReference(Symbol symbol, int fromOffset) { + newReference(symbol, fromOffset, fromOffset + ((DefaultSymbol) symbol).getLength()); + } + + @Override + public void newReference(Symbol symbol, int fromOffset, int toOffset) { if (!referencesBySymbol.containsKey(symbol)) { throw new UnsupportedOperationException("Cannot add reference to a symbol in another file"); } - TextRange referenceRange = inputFile.newRange(fromOffset, fromOffset + ((DefaultSymbol) symbol).getLength()); + TextRange referenceRange = inputFile.newRange(fromOffset, toOffset); if (referenceRange.overlap(((DefaultSymbol) symbol).range())) { throw new UnsupportedOperationException("Cannot add reference (" + fromOffset + ") overlapping " + symbol + " in " + inputFile.key()); diff --git a/sonar-batch/src/main/java/org/sonar/batch/source/DefaultSymbolizable.java b/sonar-batch/src/main/java/org/sonar/batch/source/DefaultSymbolizable.java index 5089a9cca47..5b4781e14a1 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/source/DefaultSymbolizable.java +++ b/sonar-batch/src/main/java/org/sonar/batch/source/DefaultSymbolizable.java @@ -46,6 +46,11 @@ public class DefaultSymbolizable implements Symbolizable { } @Override + public void newReference(Symbol symbol, int fromOffset, int toOffset) { + // Do nothing + } + + @Override public SymbolTable build() { return NO_OP_SYMBOL_TABLE; } diff --git a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/symbol/SymbolMediumTest.java b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/symbol/SymbolMediumTest.java index 15bb71b6c75..f918f14c341 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/symbol/SymbolMediumTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/symbol/SymbolMediumTest.java @@ -66,7 +66,7 @@ public class SymbolMediumTest { File xooSymbolFile = new File(srcDir, "sample.xoo.symbol"); FileUtils.write(xooFile, "Sample xoo\ncontent\nanother xoo"); // Highlight xoo symbol - FileUtils.write(xooSymbolFile, "7,10,27"); + FileUtils.write(xooSymbolFile, "7:10,27"); TaskResult result = tester.newTask() .properties(ImmutableMap.<String, String>builder() @@ -84,4 +84,33 @@ public class SymbolMediumTest { assertThat(result.symbolReferencesFor(file, 1, 7)).containsOnly(BatchReport.TextRange.newBuilder().setStartLine(3).setStartOffset(8).setEndLine(3).setEndOffset(11).build()); } + @Test + public void computeSymbolReferencesWithVariableLength() throws IOException { + + File baseDir = temp.getRoot(); + File srcDir = new File(baseDir, "src"); + srcDir.mkdir(); + + File xooFile = new File(srcDir, "sample.xoo"); + File xooSymbolFile = new File(srcDir, "sample.xoo.symbol"); + FileUtils.write(xooFile, "Sample xoo\ncontent\nanother xoo\nyet another"); + // Highlight xoo symbol + FileUtils.write(xooSymbolFile, "7:10,27:32"); + + TaskResult result = tester.newTask() + .properties(ImmutableMap.<String, String>builder() + .put("sonar.task", "scan") + .put("sonar.projectBaseDir", baseDir.getAbsolutePath()) + .put("sonar.projectKey", "com.foo.project") + .put("sonar.projectName", "Foo Project") + .put("sonar.projectVersion", "1.0-SNAPSHOT") + .put("sonar.projectDescription", "Description of Foo Project") + .put("sonar.sources", "src") + .build()) + .start(); + + InputFile file = result.inputFile("src/sample.xoo"); + assertThat(result.symbolReferencesFor(file, 1, 7)).containsOnly(BatchReport.TextRange.newBuilder().setStartLine(3).setStartOffset(8).setEndLine(4).setEndOffset(1).build()); + } + } diff --git a/sonar-batch/src/test/java/org/sonar/batch/source/DefaultSymbolTableTest.java b/sonar-batch/src/test/java/org/sonar/batch/source/DefaultSymbolTableTest.java index 465c26fbf67..22bcfef439d 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/source/DefaultSymbolTableTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/source/DefaultSymbolTableTest.java @@ -20,8 +20,12 @@ package org.sonar.batch.source; +import org.sonar.api.batch.fs.TextRange; import com.google.common.base.Strings; + import java.io.StringReader; +import java.util.Set; + import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -30,7 +34,6 @@ import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.batch.fs.internal.FileMetadata; import org.sonar.api.source.Symbol; import org.sonar.api.source.Symbolizable; - import static org.assertj.core.api.Assertions.assertThat; public class DefaultSymbolTableTest { @@ -61,6 +64,25 @@ public class DefaultSymbolTableTest { } @Test + public void variable_length_references() { + Symbolizable.SymbolTableBuilder symbolTableBuilder = new DefaultSymbolTable.Builder(inputFile); + Symbol firstSymbol = symbolTableBuilder.newSymbol(10, 20); + symbolTableBuilder.newReference(firstSymbol, 32); + symbolTableBuilder.newReference(firstSymbol, 44, 47); + + DefaultSymbolTable symbolTable = (DefaultSymbolTable) symbolTableBuilder.build(); + + assertThat(symbolTable.symbols()).containsExactly(firstSymbol); + + Set<TextRange> references = symbolTable.getReferencesBySymbol().get(firstSymbol); + assertThat(references).containsExactly(range(32, 42), range(44, 47)); + } + + private TextRange range(int start, int end) { + return inputFile.newRange(start, end); + } + + @Test public void should_reject_reference_conflicting_with_declaration() { throwable.expect(UnsupportedOperationException.class); |