Search in sources :

Example 1 with DefaultTextPointer

use of org.sonar.api.batch.fs.internal.DefaultTextPointer in project sonarqube by SonarSource.

the class SensorContextTester method referencesForSymbolAt.

/**
 * Return list of symbol references ranges for the symbol at a given position in a file.
 *
 * @param componentKey Key of the file like 'myProjectKey:src/foo.php'
 * @param line         Line you want to query
 * @param lineOffset   Offset you want to query.
 * @return List of references for the symbol (potentially empty) or null if there is no symbol at this position.
 */
@CheckForNull
public Collection<TextRange> referencesForSymbolAt(String componentKey, int line, int lineOffset) {
    DefaultSymbolTable symbolTable = sensorStorage.symbolsPerComponent.get(componentKey);
    if (symbolTable == null) {
        return null;
    }
    DefaultTextPointer location = new DefaultTextPointer(line, lineOffset);
    for (Map.Entry<TextRange, Set<TextRange>> symbol : symbolTable.getReferencesBySymbol().entrySet()) {
        if (symbol.getKey().start().compareTo(location) <= 0 && symbol.getKey().end().compareTo(location) > 0) {
            return symbol.getValue();
        }
    }
    return null;
}
Also used : Set(java.util.Set) TextRange(org.sonar.api.batch.fs.TextRange) DefaultSymbolTable(org.sonar.api.batch.sensor.symbol.internal.DefaultSymbolTable) Map(java.util.Map) Collections.unmodifiableMap(java.util.Collections.unmodifiableMap) DefaultTextPointer(org.sonar.api.batch.fs.internal.DefaultTextPointer) CheckForNull(javax.annotation.CheckForNull)

Example 2 with DefaultTextPointer

use of org.sonar.api.batch.fs.internal.DefaultTextPointer in project sonarqube by SonarSource.

the class SensorContextTester method highlightingTypeAt.

/**
 * Return list of syntax highlighting applied for a given position in a file. The result is a list because in theory you
 * can apply several styles to the same range.
 *
 * @param componentKey Key of the file like 'myProjectKey:src/foo.php'
 * @param line         Line you want to query
 * @param lineOffset   Offset you want to query.
 * @return List of styles applied to this position or empty list if there is no highlighting at this position.
 */
public List<TypeOfText> highlightingTypeAt(String componentKey, int line, int lineOffset) {
    DefaultHighlighting syntaxHighlightingData = (DefaultHighlighting) sensorStorage.highlightingByComponent.get(componentKey);
    if (syntaxHighlightingData == null) {
        return Collections.emptyList();
    }
    List<TypeOfText> result = new ArrayList<>();
    DefaultTextPointer location = new DefaultTextPointer(line, lineOffset);
    for (SyntaxHighlightingRule sortedRule : syntaxHighlightingData.getSyntaxHighlightingRuleSet()) {
        if (sortedRule.range().start().compareTo(location) <= 0 && sortedRule.range().end().compareTo(location) > 0) {
            result.add(sortedRule.getTextType());
        }
    }
    return result;
}
Also used : TypeOfText(org.sonar.api.batch.sensor.highlighting.TypeOfText) SyntaxHighlightingRule(org.sonar.api.batch.sensor.highlighting.internal.SyntaxHighlightingRule) DefaultHighlighting(org.sonar.api.batch.sensor.highlighting.internal.DefaultHighlighting) ArrayList(java.util.ArrayList) DefaultTextPointer(org.sonar.api.batch.fs.internal.DefaultTextPointer)

Example 3 with DefaultTextPointer

use of org.sonar.api.batch.fs.internal.DefaultTextPointer in project sonarlint-core by SonarSource.

the class DefaultClientIssueTest method transformIssue.

@Test
public void transformIssue() {
    textRange = new DefaultTextRange(new DefaultTextPointer(1, 2), new DefaultTextPointer(2, 3));
    IssueLocation location1 = mock(IssueLocation.class);
    when(location1.textRange()).thenReturn(new DefaultTextRange(new DefaultTextPointer(4, 4), new DefaultTextPointer(5, 5)));
    when(location1.message()).thenReturn("location1");
    IssueLocation location2 = mock(IssueLocation.class);
    when(location2.textRange()).thenReturn(new DefaultTextRange(new DefaultTextPointer(6, 6), new DefaultTextPointer(7, 7)));
    when(location2.message()).thenReturn("location2");
    when(rule.name()).thenReturn("name");
    Flow flow1 = mock(Flow.class);
    when(flow1.locations()).thenReturn(Collections.singletonList(location1));
    Flow flow2 = mock(Flow.class);
    when(flow2.locations()).thenReturn(Arrays.asList(location1, location2));
    issue = new DefaultClientIssue("MAJOR", "BUG", activeRule, rule, "msg", textRange, clientInputFile, Arrays.asList(flow1, flow2));
    assertThat(issue.getStartLine()).isEqualTo(1);
    assertThat(issue.getStartLineOffset()).isEqualTo(2);
    assertThat(issue.getEndLine()).isEqualTo(2);
    assertThat(issue.getEndLineOffset()).isEqualTo(3);
    assertThat(issue.getMessage()).isEqualTo("msg");
    assertThat(issue.getSeverity()).isEqualTo("MAJOR");
    assertThat(issue.getInputFile()).isEqualTo(clientInputFile);
    assertThat(issue.getRuleName()).isEqualTo("name");
    assertThat(issue.flows()).hasSize(2);
    assertThat(issue.flows().get(0).locations()).hasSize(1);
    assertThat(issue.flows().get(0).locations().get(0)).extracting("startLine", "startLineOffset", "endLine", "endLineOffset", "message").containsExactly(4, 4, 5, 5, "location1");
    assertThat(issue.flows().get(1).locations()).hasSize(2);
    assertThat(issue.flows().get(1).locations().get(0)).extracting("startLine", "startLineOffset", "endLine", "endLineOffset", "message").containsExactly(4, 4, 5, 5, "location1");
    assertThat(issue.flows().get(1).locations().get(1)).extracting("startLine", "startLineOffset", "endLine", "endLineOffset", "message").containsExactly(6, 6, 7, 7, "location2");
}
Also used : DefaultTextRange(org.sonar.api.batch.fs.internal.DefaultTextRange) DefaultTextPointer(org.sonar.api.batch.fs.internal.DefaultTextPointer) IssueLocation(org.sonar.api.batch.sensor.issue.IssueLocation) Flow(org.sonar.api.batch.sensor.issue.Issue.Flow) Test(org.junit.Test)

Example 4 with DefaultTextPointer

use of org.sonar.api.batch.fs.internal.DefaultTextPointer in project sonarqube by SonarSource.

the class SymbolReferencesSensorTest method testExecution.

@Test
public void testExecution() throws IOException {
    File symbol = new File(baseDir, "src/foo.xoo.symbol");
    FileUtils.write(symbol, "1:1:1:4,1:7:1:10\n1:11:1:13,1:14:1:33\n\n#comment");
    InputFile inputFile = new TestInputFileBuilder("foo", "src/foo.xoo").initMetadata("xoo file with some source code and length over 33").setLanguage(Xoo.KEY).setModuleBaseDir(baseDir.toPath()).build();
    context.fileSystem().add(inputFile);
    sensor.execute(context);
    assertThat(context.referencesForSymbolAt("foo:src/foo.xoo", 1, 2)).containsOnly(new DefaultTextRange(new DefaultTextPointer(1, 7), new DefaultTextPointer(1, 10)));
    assertThat(context.referencesForSymbolAt("foo:src/foo.xoo", 1, 12)).containsOnly(new DefaultTextRange(new DefaultTextPointer(1, 14), new DefaultTextPointer(1, 33)));
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) InputFile(org.sonar.api.batch.fs.InputFile) File(java.io.File) DefaultTextRange(org.sonar.api.batch.fs.internal.DefaultTextRange) DefaultTextPointer(org.sonar.api.batch.fs.internal.DefaultTextPointer) InputFile(org.sonar.api.batch.fs.InputFile) Test(org.junit.Test)

Example 5 with DefaultTextPointer

use of org.sonar.api.batch.fs.internal.DefaultTextPointer in project sonarqube by SonarSource.

the class AnalysisErrorSensorTest method test.

@Test
public void test() throws IOException {
    Path baseDir = temp.newFolder().toPath().toAbsolutePath();
    createErrorFile(baseDir);
    int[] startOffsets = { 10, 20, 30, 40 };
    int[] endOffsets = { 19, 29, 39, 49 };
    DefaultInputFile inputFile = new TestInputFileBuilder("foo", "src/foo.xoo").setLanguage("xoo").setOriginalLineStartOffsets(startOffsets).setOriginalLineEndOffsets(endOffsets).setModuleBaseDir(baseDir).setLines(4).build();
    SensorContextTester context = SensorContextTester.create(baseDir);
    context.fileSystem().add(inputFile);
    sensor.execute(context);
    assertThat(context.allAnalysisErrors()).hasSize(1);
    AnalysisError error = context.allAnalysisErrors().iterator().next();
    assertThat(error.inputFile()).isEqualTo(inputFile);
    assertThat(error.location()).isEqualTo(new DefaultTextPointer(1, 4));
    assertThat(error.message()).isEqualTo("my error");
}
Also used : Path(java.nio.file.Path) TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) AnalysisError(org.sonar.api.batch.sensor.error.AnalysisError) DefaultTextPointer(org.sonar.api.batch.fs.internal.DefaultTextPointer) Test(org.junit.Test)

Aggregations

DefaultTextPointer (org.sonar.api.batch.fs.internal.DefaultTextPointer)7 Test (org.junit.Test)4 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)4 InputFile (org.sonar.api.batch.fs.InputFile)2 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)2 DefaultTextRange (org.sonar.api.batch.fs.internal.DefaultTextRange)2 AnalysisError (org.sonar.api.batch.sensor.error.AnalysisError)2 File (java.io.File)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 Collections.unmodifiableMap (java.util.Collections.unmodifiableMap)1 Map (java.util.Map)1 Set (java.util.Set)1 CheckForNull (javax.annotation.CheckForNull)1 Before (org.junit.Before)1 TextRange (org.sonar.api.batch.fs.TextRange)1 NewAnalysisError (org.sonar.api.batch.sensor.error.NewAnalysisError)1 TypeOfText (org.sonar.api.batch.sensor.highlighting.TypeOfText)1 DefaultHighlighting (org.sonar.api.batch.sensor.highlighting.internal.DefaultHighlighting)1 SyntaxHighlightingRule (org.sonar.api.batch.sensor.highlighting.internal.SyntaxHighlightingRule)1