/** * Get list of all start positions of a symbol in an inputfile * * @param symbolStartLine 0-based start offset for the symbol in file * @param symbolStartLineOffset 0-based end offset for the symbol in file */ @CheckForNull public List<ScannerReport.TextRange> symbolReferencesFor(InputFile file, int symbolStartLine, int symbolStartLineOffset) { int ref = ((DefaultInputComponent) file).scannerId(); try (CloseableIterator<Symbol> symbols = getReportReader().readComponentSymbols(ref)) { while (symbols.hasNext()) { Symbol symbol = symbols.next(); if (symbol.getDeclaration().getStartLine() == symbolStartLine && symbol.getDeclaration().getStartOffset() == symbolStartLineOffset) { return symbol.getReferenceList(); } } } return Collections.emptyList(); }
throw new UnsupportedOperationException("Trying to save symbol table twice for the same file is not supported: " + symbolTable.inputFile()); final ScannerReport.Symbol.Builder builder = ScannerReport.Symbol.newBuilder(); final ScannerReport.TextRange.Builder rangeBuilder = ScannerReport.TextRange.newBuilder(); writer.writeComponentSymbols(componentRef,
throw new UnsupportedOperationException("Trying to save symbol table twice for the same file is not supported: " + symbolTable.inputFile()); final ScannerReport.Symbol.Builder builder = ScannerReport.Symbol.newBuilder(); final ScannerReport.TextRange.Builder rangeBuilder = ScannerReport.TextRange.newBuilder(); writer.writeComponentSymbols(componentRef,
/** * Get list of all start positions of a symbol in an inputfile * * @param symbolStartLine 0-based start offset for the symbol in file * @param symbolStartLineOffset 0-based end offset for the symbol in file */ @CheckForNull public List<ScannerReport.TextRange> symbolReferencesFor(InputFile file, int symbolStartLine, int symbolStartLineOffset) { int ref = ((DefaultInputComponent) file).scannerId(); try (CloseableIterator<Symbol> symbols = getReportReader().readComponentSymbols(ref)) { while (symbols.hasNext()) { Symbol symbol = symbols.next(); if (symbol.getDeclaration().getStartLine() == symbolStartLine && symbol.getDeclaration().getStartOffset() == symbolStartLineOffset) { return symbol.getReferenceList(); } } } return Collections.emptyList(); }
private void processSymbols(DbFileSources.Line.Builder lineBuilder) { int line = lineBuilder.getLine(); List<ScannerReport.Symbol> lineSymbols = new ArrayList<>(this.symbolsPerLine.get(line)); // Sort symbols to have deterministic results and avoid false variation that would lead to an unnecessary update of the source files // data Collections.sort(lineSymbols, SymbolsComparator.INSTANCE); StringBuilder symbolString = new StringBuilder(); for (ScannerReport.Symbol lineSymbol : lineSymbols) { int symbolId = idsBySymbol.get(lineSymbol); appendSymbol(symbolString, lineSymbol.getDeclaration(), line, symbolId, lineBuilder.getSource()); for (ScannerReport.TextRange range : lineSymbol.getReferenceList()) { appendSymbol(symbolString, range, line, symbolId, lineBuilder.getSource()); } } if (symbolString.length() > 0) { lineBuilder.setSymbols(symbolString.toString()); } }
private static SetMultimap<Integer, ScannerReport.Symbol> buildSymbolsPerLine(List<ScannerReport.Symbol> symbols) { SetMultimap<Integer, ScannerReport.Symbol> res = HashMultimap.create(); for (ScannerReport.Symbol symbol : symbols) { putForTextRange(res, symbol, symbol.getDeclaration()); for (ScannerReport.TextRange textRange : symbol.getReferenceList()) { putForTextRange(res, symbol, textRange); } } return res; }
public CloseableIterator<ScannerReport.Symbol> readComponentSymbols(int componentRef) { File file = fileStructure.fileFor(FileStructure.Domain.SYMBOLS, componentRef); if (fileExists(file)) { return Protobuf.readStream(file, ScannerReport.Symbol.parser()); } return emptyCloseableIterator(); }
@Override public int compare(ScannerReport.Symbol o1, ScannerReport.Symbol o2) { if (o1.getDeclaration().getStartLine() == o2.getDeclaration().getStartLine()) { return Integer.compare(o1.getDeclaration().getStartOffset(), o2.getDeclaration().getStartOffset()); } else { return Integer.compare(o1.getDeclaration().getStartLine(), o2.getDeclaration().getStartLine()); } } }
@Test public void write_symbols() { // no data yet assertThat(underTest.hasComponentData(FileStructure.Domain.SYMBOLS, 1)).isFalse(); // write data ScannerReport.Symbol symbol = ScannerReport.Symbol.newBuilder() .setDeclaration(ScannerReport.TextRange.newBuilder() .setStartLine(1) .setStartOffset(3) .setEndLine(1) .setEndOffset(5) .build()) .addReference(ScannerReport.TextRange.newBuilder() .setStartLine(10) .setStartOffset(15) .setEndLine(11) .setEndOffset(2) .build()) .build(); underTest.writeComponentSymbols(1, asList(symbol)); assertThat(underTest.hasComponentData(FileStructure.Domain.SYMBOLS, 1)).isTrue(); File file = underTest.getFileStructure().fileFor(FileStructure.Domain.SYMBOLS, 1); assertThat(file).exists().isFile(); try (CloseableIterator<ScannerReport.Symbol> read = Protobuf.readStream(file, ScannerReport.Symbol.parser())) { assertThat(read).hasSize(1); } }
@Test public void read_symbols() { ScannerReportWriter writer = new ScannerReportWriter(dir); writer.writeMetadata(ScannerReport.Metadata.newBuilder() .setRootComponentRef(1) .build()); writer.writeComponent(ScannerReport.Component.newBuilder() .setRef(1).build()); writer.writeComponentSymbols(1, asList(ScannerReport.Symbol.newBuilder() .setDeclaration(ScannerReport.TextRange.newBuilder() .setStartLine(1) .setStartOffset(3) .setEndLine(1) .setEndOffset(5) .build()) .addReference(ScannerReport.TextRange.newBuilder() .setStartLine(10) .setStartOffset(15) .setEndLine(11) .setEndOffset(2) .build()) .build())); underTest = new ScannerReportReader(dir); assertThat(underTest.readComponentSymbols(1)).hasSize(1); }
private void processSymbols(DbFileSources.Line.Builder lineBuilder) { int line = lineBuilder.getLine(); List<ScannerReport.Symbol> lineSymbols = new ArrayList<>(this.symbolsPerLine.get(line)); // Sort symbols to have deterministic results and avoid false variation that would lead to an unnecessary update of the source files // data lineSymbols.sort(SymbolsComparator.INSTANCE); StringBuilder symbolString = new StringBuilder(); for (ScannerReport.Symbol lineSymbol : lineSymbols) { int symbolId = idsBySymbol.get(lineSymbol); appendSymbol(symbolString, lineSymbol.getDeclaration(), line, symbolId, lineBuilder.getSource()); for (ScannerReport.TextRange range : lineSymbol.getReferenceList()) { appendSymbol(symbolString, range, line, symbolId, lineBuilder.getSource()); } } if (symbolString.length() > 0) { lineBuilder.setSymbols(symbolString.toString()); } }
public CloseableIterator<ScannerReport.Symbol> readComponentSymbols(int componentRef) { File file = fileStructure.fileFor(FileStructure.Domain.SYMBOLS, componentRef); if (fileExists(file)) { return Protobuf.readStream(file, ScannerReport.Symbol.parser()); } return emptyCloseableIterator(); }
private static SetMultimap<Integer, ScannerReport.Symbol> buildSymbolsPerLine(List<ScannerReport.Symbol> symbols) { SetMultimap<Integer, ScannerReport.Symbol> res = HashMultimap.create(); for (ScannerReport.Symbol symbol : symbols) { putForTextRange(res, symbol, symbol.getDeclaration()); for (ScannerReport.TextRange textRange : symbol.getReferenceList()) { putForTextRange(res, symbol, textRange); } } return res; }
private ScannerReport.Symbol newSymbol(TextRange declaration, TextRange... references) { ScannerReport.Symbol.Builder builder = ScannerReport.Symbol.newBuilder() .setDeclaration(declaration); for (TextRange reference : references) { builder.addReference(reference); } return builder.build(); }
@Override public int compare(ScannerReport.Symbol o1, ScannerReport.Symbol o2) { if (o1.getDeclaration().getStartLine() == o2.getDeclaration().getStartLine()) { return Integer.compare(o1.getDeclaration().getStartOffset(), o2.getDeclaration().getStartOffset()); } else { return Integer.compare(o1.getDeclaration().getStartLine(), o2.getDeclaration().getStartLine()); } } }