@Override public void execute(SensorContext context) { for (InputFile file : context.fileSystem().inputFiles(context.fileSystem().predicates().hasLanguages(Xoo.KEY))) { processFileSymbol(file, context); } } }
private void processFileSymbol(InputFile inputFile, SensorContext context) { File ioFile = inputFile.file(); File symbolFile = new File(ioFile.getParentFile(), ioFile.getName() + SYMBOL_EXTENSION); if (symbolFile.exists()) { LOG.debug("Processing " + symbolFile.getAbsolutePath()); try { List<String> lines = FileUtils.readLines(symbolFile, context.fileSystem().encoding().name()); int lineNumber = 0; NewSymbolTable symbolTable = context.newSymbolTable() .onFile(inputFile); for (String line : lines) { lineNumber++; if (StringUtils.isBlank(line) || line.startsWith("#")) { continue; } processLine(symbolFile, lineNumber, symbolTable, line); } symbolTable.save(); } catch (IOException e) { throw new IllegalStateException(e); } } }
private static void parseReferences(NewSymbol s, Iterator<String> split, @Nullable Integer defaultLen) { while (split.hasNext()) { addReference(s, split.next(), defaultLen); } }
private static void processLine(File symbolFile, int lineNumber, NewSymbolTable symbolTable, String line) { try { Iterator<String> split = Splitter.on(",").split(line).iterator(); String[] symbolOffsets = split.next().split(":"); if (symbolOffsets.length == 2) { int startOffset = parseInt(symbolOffsets[0]); int endOffset = parseInt(symbolOffsets[1]); NewSymbol s = symbolTable.newSymbol(startOffset, endOffset); parseReferences(s, split, endOffset - startOffset); } else if (symbolOffsets.length == 4) { int startLine = parseInt(symbolOffsets[0]); int startLineOffset = parseInt(symbolOffsets[1]); int endLine = parseInt(symbolOffsets[2]); int endLineOffset = parseInt(symbolOffsets[3]); NewSymbol s = symbolTable.newSymbol(startLine, startLineOffset, endLine, endLineOffset); parseReferences(s, split, null); } else { throw new IllegalStateException("Illegal number of elements separated by ':'. " + "Must either be startOffset:endOffset (offset in whole file) or startLine:startLineOffset:endLine:endLineOffset"); } } catch (Exception e) { throw new IllegalStateException("Error processing line " + lineNumber + " of file " + symbolFile.getAbsolutePath(), e); } }
@Test public void testNoExecutionIfNoSymbolFile() { InputFile inputFile = new TestInputFileBuilder("foo", "src/foo.xoo").setLanguage("xoo").setModuleBaseDir(baseDir.toPath()).build(); context.fileSystem().add(inputFile); sensor.execute(context); }
@Test public void testDescriptor() { sensor.describe(new DefaultSensorDescriptor()); }
private static void processLine(File symbolFile, int lineNumber, NewSymbolTable symbolTable, String line) { try { Iterator<String> split = Splitter.on(",").split(line).iterator(); String[] symbolOffsets = split.next().split(":"); if (symbolOffsets.length == 2) { int startOffset = parseInt(symbolOffsets[0]); int endOffset = parseInt(symbolOffsets[1]); NewSymbol s = symbolTable.newSymbol(startOffset, endOffset); parseReferences(s, split, endOffset - startOffset); } else if (symbolOffsets.length == 4) { int startLine = parseInt(symbolOffsets[0]); int startLineOffset = parseInt(symbolOffsets[1]); int endLine = parseInt(symbolOffsets[2]); int endLineOffset = parseInt(symbolOffsets[3]); NewSymbol s = symbolTable.newSymbol(startLine, startLineOffset, endLine, endLineOffset); parseReferences(s, split, null); } else { throw new IllegalStateException("Illegal number of elements separated by ':'. " + "Must either be startOffset:endOffset (offset in whole file) or startLine:startLineOffset:endLine:endLineOffset"); } } catch (Exception e) { throw new IllegalStateException("Error processing line " + lineNumber + " of file " + symbolFile.getAbsolutePath(), e); } }
@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))); }
@Override public void execute(SensorContext context) { for (InputFile file : context.fileSystem().inputFiles(context.fileSystem().predicates().hasLanguages(Xoo.KEY))) { processFileSymbol(file, context); } } }
private void processFileSymbol(InputFile inputFile, SensorContext context) { File ioFile = inputFile.file(); File symbolFile = new File(ioFile.getParentFile(), ioFile.getName() + SYMBOL_EXTENSION); if (symbolFile.exists()) { LOG.debug("Processing " + symbolFile.getAbsolutePath()); try { List<String> lines = FileUtils.readLines(symbolFile, context.fileSystem().encoding().name()); int lineNumber = 0; NewSymbolTable symbolTable = context.newSymbolTable() .onFile(inputFile); for (String line : lines) { lineNumber++; if (StringUtils.isBlank(line) || line.startsWith("#")) { continue; } processLine(symbolFile, lineNumber, symbolTable, line); } symbolTable.save(); } catch (IOException e) { throw new IllegalStateException(e); } } }
private static void parseReferences(NewSymbol s, Iterator<String> split, @Nullable Integer defaultLen) { while (split.hasNext()) { addReference(s, split.next(), defaultLen); } }
@Override public void execute(SensorContext context) { for (InputFile file : context.fileSystem().inputFiles(context.fileSystem().predicates().hasLanguages(Xoo.KEY))) { processFileSymbol(file, context); } } }
private void processFileSymbol(InputFile inputFile, SensorContext context) { File ioFile = inputFile.file(); File symbolFile = new File(ioFile.getParentFile(), ioFile.getName() + SYMBOL_EXTENSION); if (symbolFile.exists()) { LOG.debug("Processing " + symbolFile.getAbsolutePath()); try { List<String> lines = FileUtils.readLines(symbolFile, context.fileSystem().encoding().name()); int lineNumber = 0; Symbolizable symbolizable = perspectives.as(Symbolizable.class, inputFile); if (symbolizable != null) { Symbolizable.SymbolTableBuilder symbolTableBuilder = symbolizable.newSymbolTableBuilder(); for (String line : lines) { lineNumber++; if (StringUtils.isBlank(line) || line.startsWith("#")) { continue; } processLine(symbolFile, lineNumber, symbolTableBuilder, line); } symbolizable.setSymbolTable(symbolTableBuilder.build()); } } catch (IOException e) { throw new IllegalStateException(e); } } }