private static TextRange range(int line, int startOffset, int endOffset) { return new DefaultTextRange(new DefaultTextPointer(line, startOffset), new DefaultTextPointer(line, endOffset)); } }
private static TextRange rangeOf(int startLine, int startOffset, int endLine, int endOffset) { return new DefaultTextRange(new DefaultTextPointer(startLine, startOffset), new DefaultTextPointer(endLine, endOffset)); }
@Override public TextPointer newPointer(int line, int lineOffset) { checkMetadata(); DefaultTextPointer textPointer = new DefaultTextPointer(line, lineOffset); checkValid(textPointer, "pointer"); return textPointer; }
@Override public TextRange textRange() { if (!rawIssue.hasTextRange()) { return null; } return new DefaultTextRange( new DefaultTextPointer(rawIssue.getTextRange().getStartLine(), rawIssue.getTextRange().getStartOffset()), new DefaultTextPointer(rawIssue.getTextRange().getEndLine(), rawIssue.getTextRange().getEndOffset())); }
/** * 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; }
public TextPointer newPointer(int globalOffset) { checkMetadata(); Preconditions.checkArgument(globalOffset >= 0, "%s is not a valid offset for a file", globalOffset); Preconditions.checkArgument(globalOffset <= lastValidOffset(), "%s is not a valid offset for file %s. Max offset is %s", globalOffset, this, lastValidOffset()); int line = findLine(globalOffset); int startLineOffset = originalLineStartOffsets()[line - 1]; // In case the global offset is between \r and \n, move the pointer to a valid location return new DefaultTextPointer(line, Math.min(globalOffset, originalLineEndOffsets()[line - 1]) - startLineOffset); }
/** * 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 = 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; }
@Test public void testExecution() throws IOException { File symbol = new File(baseDir, "src/foo.xoo.symbol"); FileUtils.write(symbol, "1:4,7\n12:15,23: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, 13)) .containsOnly(new DefaultTextRange(new DefaultTextPointer(1, 23), new DefaultTextPointer(1,33))); }
@Before public void setUp() { inputFile = new TestInputFileBuilder("module1", "src/File.java").build(); textPointer = new DefaultTextPointer(5, 2); storage = mock(SensorStorage.class); }
@Test public void testAnalysisErrors() { assertThat(tester.allAnalysisErrors()).isEmpty(); NewAnalysisError newAnalysisError = tester.newAnalysisError(); InputFile file = new TestInputFileBuilder("foo", "src/Foo.java").build(); newAnalysisError.onFile(file) .message("error") .at(new DefaultTextPointer(5, 2)) .save(); assertThat(tester.allAnalysisErrors()).hasSize(1); AnalysisError analysisError = tester.allAnalysisErrors().iterator().next(); assertThat(analysisError.inputFile()).isEqualTo(file); assertThat(analysisError.message()).isEqualTo("error"); assertThat(analysisError.location()).isEqualTo(new DefaultTextPointer(5, 2)); }
@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(); 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"); } }
@Override public TextPointer newPointer(int line, int lineOffset) { checkMetadata(); return new DefaultTextPointer(line, lineOffset); }
private TextRange textRange(int startLine, int startColumn, int endLine, int endColumn) { return new DefaultTextRange(new DefaultTextPointer(startLine, startColumn), new DefaultTextPointer(endLine, endColumn)); }
@Override public TextPointer newPointer(int line, int lineOffset) { checkMetadata(); DefaultTextPointer textPointer = new DefaultTextPointer(line, lineOffset); checkValid(textPointer, "pointer"); return textPointer; }
@Override public TextRange textRange() { if (!rawIssue.hasTextRange()) { return null; } return new DefaultTextRange( new DefaultTextPointer(rawIssue.getTextRange().getStartLine(), rawIssue.getTextRange().getStartOffset()), new DefaultTextPointer(rawIssue.getTextRange().getEndLine(), rawIssue.getTextRange().getEndOffset())); }
/** * 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; }
/** * 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 = 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; }
public TextPointer newPointer(int globalOffset) { checkMetadata(); Preconditions.checkArgument(globalOffset >= 0, "%s is not a valid offset for a file", globalOffset); Preconditions.checkArgument(globalOffset <= lastValidOffset(), "%s is not a valid offset for file %s. Max offset is %s", globalOffset, this, lastValidOffset()); int line = findLine(globalOffset); int startLineOffset = originalLineStartOffsets()[line - 1]; // In case the global offset is between \r and \n, move the pointer to a valid location return new DefaultTextPointer(line, Math.min(globalOffset, originalLineEndOffsets()[line - 1]) - startLineOffset); }