Search in sources :

Example 1 with DefaultSymbolTable

use of org.sonar.api.batch.sensor.symbol.internal.DefaultSymbolTable 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) ImmutableMap(com.google.common.collect.ImmutableMap) DefaultTextPointer(org.sonar.api.batch.fs.internal.DefaultTextPointer) CheckForNull(javax.annotation.CheckForNull)

Example 2 with DefaultSymbolTable

use of org.sonar.api.batch.sensor.symbol.internal.DefaultSymbolTable in project sonarqube by SonarSource.

the class DefaultSensorStorageTest method duplicateSymbolTable.

@Test(expected = UnsupportedOperationException.class)
public void duplicateSymbolTable() throws Exception {
    InputFile inputFile = new TestInputFileBuilder("foo", "src/Foo.java").setModuleBaseDir(temp.newFolder().toPath()).build();
    DefaultSymbolTable st = new DefaultSymbolTable(null).onFile(inputFile);
    underTest.store(st);
    underTest.store(st);
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) DefaultSymbolTable(org.sonar.api.batch.sensor.symbol.internal.DefaultSymbolTable) InputFile(org.sonar.api.batch.fs.InputFile) Test(org.junit.Test)

Example 3 with DefaultSymbolTable

use of org.sonar.api.batch.sensor.symbol.internal.DefaultSymbolTable in project sonarqube by SonarSource.

the class DefaultSymbolizableTest method should_update_cache_when_done.

@Test
public void should_update_cache_when_done() {
    DefaultSensorStorage sensorStorage = mock(DefaultSensorStorage.class);
    DefaultInputFile inputFile = new TestInputFileBuilder("foo", "src/Foo.php").initMetadata(Strings.repeat("azerty\n", 20)).build();
    DefaultSymbolizable symbolPerspective = new DefaultSymbolizable(inputFile, sensorStorage, mock(AnalysisMode.class));
    Symbolizable.SymbolTableBuilder symbolTableBuilder = symbolPerspective.newSymbolTableBuilder();
    Symbol firstSymbol = symbolTableBuilder.newSymbol(4, 8);
    symbolTableBuilder.newReference(firstSymbol, 12);
    symbolTableBuilder.newReference(firstSymbol, 70);
    Symbol otherSymbol = symbolTableBuilder.newSymbol(25, 33);
    symbolTableBuilder.newReference(otherSymbol, 44);
    symbolTableBuilder.newReference(otherSymbol, 60);
    symbolTableBuilder.newReference(otherSymbol, 108);
    Symbolizable.SymbolTable symbolTable = symbolTableBuilder.build();
    symbolPerspective.setSymbolTable(symbolTable);
    ArgumentCaptor<DefaultSymbolTable> argCaptor = ArgumentCaptor.forClass(DefaultSymbolTable.class);
    verify(sensorStorage).store(argCaptor.capture());
    // Map<Symbol, Set<TextRange>>
    assertThat(argCaptor.getValue().getReferencesBySymbol().keySet()).hasSize(2);
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) Symbol(org.sonar.api.source.Symbol) DefaultSensorStorage(org.sonar.scanner.sensor.DefaultSensorStorage) AnalysisMode(org.sonar.api.batch.AnalysisMode) DefaultSymbolizable(org.sonar.scanner.source.DefaultSymbolizable) DefaultSymbolTable(org.sonar.api.batch.sensor.symbol.internal.DefaultSymbolTable) Symbolizable(org.sonar.api.source.Symbolizable) DefaultSymbolizable(org.sonar.scanner.source.DefaultSymbolizable) Test(org.junit.Test)

Example 4 with DefaultSymbolTable

use of org.sonar.api.batch.sensor.symbol.internal.DefaultSymbolTable in project sonarqube by SonarSource.

the class DeprecatedDefaultSymbolTableTest method should_reject_reference_conflicting_with_declaration.

@Test
public void should_reject_reference_conflicting_with_declaration() {
    throwable.expect(IllegalArgumentException.class);
    Symbolizable.SymbolTableBuilder symbolTableBuilder = new DeprecatedDefaultSymbolTable.Builder(new DefaultSymbolTable(null).onFile(inputFile));
    Symbol symbol = symbolTableBuilder.newSymbol(10, 20);
    symbolTableBuilder.newReference(symbol, 15);
}
Also used : Symbol(org.sonar.api.source.Symbol) TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) DeprecatedDefaultSymbolTable(org.sonar.scanner.source.DeprecatedDefaultSymbolTable) DefaultSymbolTable(org.sonar.api.batch.sensor.symbol.internal.DefaultSymbolTable) Symbolizable(org.sonar.api.source.Symbolizable) Test(org.junit.Test)

Example 5 with DefaultSymbolTable

use of org.sonar.api.batch.sensor.symbol.internal.DefaultSymbolTable in project sonarqube by SonarSource.

the class DeprecatedDefaultSymbolTableTest method variable_length_references.

@Test
public void variable_length_references() {
    Symbolizable.SymbolTableBuilder symbolTableBuilder = new DeprecatedDefaultSymbolTable.Builder(new DefaultSymbolTable(null).onFile(inputFile));
    Symbol firstSymbol = symbolTableBuilder.newSymbol(10, 20);
    symbolTableBuilder.newReference(firstSymbol, 32);
    symbolTableBuilder.newReference(firstSymbol, 44, 47);
    DeprecatedDefaultSymbolTable symbolTable = (DeprecatedDefaultSymbolTable) symbolTableBuilder.build();
    assertThat(symbolTable.getWrapped().getReferencesBySymbol().keySet()).containsExactly(range(10, 20));
    Set<TextRange> references = symbolTable.getWrapped().getReferencesBySymbol().get(range(10, 20));
    assertThat(references).containsExactly(range(32, 42), range(44, 47));
}
Also used : Symbol(org.sonar.api.source.Symbol) DeprecatedDefaultSymbolTable(org.sonar.scanner.source.DeprecatedDefaultSymbolTable) TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) TextRange(org.sonar.api.batch.fs.TextRange) DeprecatedDefaultSymbolTable(org.sonar.scanner.source.DeprecatedDefaultSymbolTable) DefaultSymbolTable(org.sonar.api.batch.sensor.symbol.internal.DefaultSymbolTable) Symbolizable(org.sonar.api.source.Symbolizable) Test(org.junit.Test)

Aggregations

DefaultSymbolTable (org.sonar.api.batch.sensor.symbol.internal.DefaultSymbolTable)6 Test (org.junit.Test)5 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)5 Symbol (org.sonar.api.source.Symbol)4 Symbolizable (org.sonar.api.source.Symbolizable)4 DeprecatedDefaultSymbolTable (org.sonar.scanner.source.DeprecatedDefaultSymbolTable)3 TextRange (org.sonar.api.batch.fs.TextRange)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Map (java.util.Map)1 Set (java.util.Set)1 CheckForNull (javax.annotation.CheckForNull)1 AnalysisMode (org.sonar.api.batch.AnalysisMode)1 InputFile (org.sonar.api.batch.fs.InputFile)1 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)1 DefaultTextPointer (org.sonar.api.batch.fs.internal.DefaultTextPointer)1 DefaultSensorStorage (org.sonar.scanner.sensor.DefaultSensorStorage)1 DefaultSymbolizable (org.sonar.scanner.source.DefaultSymbolizable)1