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;
}
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;
}
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");
}
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)));
}
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");
}
Aggregations