private void generateTypeScriptSourceMapFile(SourceFile sourceFile) throws IOException { if (sourceFile.getSourceMap() == null) { return; } SourceMapGenerator generator = SourceMapGeneratorFactory.getInstance(SourceMapFormat.V3); String javaSourceFilePath = sourceFile.getTsFile().getAbsoluteFile().getCanonicalFile().getParentFile().toPath() .relativize(sourceFile.getJavaFile().getAbsoluteFile().getCanonicalFile().toPath()).toString(); for (Entry entry : sourceFile.getSourceMap().getSortedEntries(new Comparator<SourceMap.Entry>() { @Override public int compare(Entry e1, Entry e2) { return e1.getOutputPosition().compareTo(e2.getOutputPosition()); } })) { generator.addMapping(javaSourceFilePath, null, new FilePosition(entry.getInputPosition().getLine(), entry.getInputPosition().getColumn()), new FilePosition(entry.getOutputPosition().getLine(), entry.getOutputPosition().getColumn()), new FilePosition(entry.getOutputPosition().getLine(), entry.getOutputPosition().getColumn() + 1)); } File outputFile = new File(sourceFile.getTsFile().getPath() + ".map"); try (FileWriter writer = new FileWriter(outputFile, false)) { generator.appendTo(writer, sourceFile.getTsFile().getName()); } catch (Exception ex) { ex.printStackTrace(); } }
originPosition.getFile().getCanonicalFile().toPath()) .toString(), null, new FilePosition(originPosition.getStartLine() - 1, 0), new FilePosition(line - 1, 0), new FilePosition(line - 1, lineContent.length() - 1));
/** * {@inheritDoc} */ @Override public void reset() { mappings.clear(); lastMapping = null; sourceFileMap.clear(); originalNameMap.clear(); lastSourceFile = null; lastSourceFileIndex = -1; offsetPosition = new FilePosition(0, 0); prefixPosition = new FilePosition(0, 0); }
/** * Sets the source code that exists in the buffer for which the * generated code is being generated. This ensures that the source map * accurately reflects the fact that the source is being appended to * an existing buffer and as such, does not start at line 0, position 0 * but rather some other line and position. * * @param offsetLine The index of the current line being printed. * @param offsetIndex The column index of the current character being printed. */ @Override public void setStartingPosition(int offsetLine, int offsetIndex) { checkState(offsetLine >= 0); checkState(offsetIndex >= 0); offsetPosition = new FilePosition(offsetLine, offsetIndex); }
/** * {@inheritDoc} */ @Override public void reset() { mappings.clear(); lastMapping = null; sourceFileMap.clear(); sourceFileContentMap.clear(); originalNameMap.clear(); lastSourceFile = null; lastSourceFileIndex = -1; offsetPosition = new FilePosition(0, 0); prefixPosition = new FilePosition(0, 0); }
/** * Sets the source code that exists in the buffer for which the * generated code is being generated. This ensures that the source map * accurately reflects the fact that the source is being appended to * an existing buffer and as such, does not start at line 0, position 0 * but rather some other line and position. * * @param offsetLine The index of the current line being printed. * @param offsetIndex The column index of the current character being printed. */ @Override public void setStartingPosition(int offsetLine, int offsetIndex) { Preconditions.checkState(offsetLine >= 0); Preconditions.checkState(offsetIndex >= 0); offsetPosition = new FilePosition(offsetLine, offsetIndex); }
/** * Sets the prefix used for wrapping the generated source file before * it is written. This ensures that the source map is adjusted for the * change in character offsets. * * @param prefix The prefix that is added before the generated source code. */ @Override public void setWrapperPrefix(String prefix) { // Determine the current line and character position. int prefixLine = 0; int prefixIndex = 0; for (int i = 0; i < prefix.length(); ++i) { if (prefix.charAt(i) == '\n') { prefixLine++; prefixIndex = 0; } else { prefixIndex++; } } prefixPosition = new FilePosition(prefixLine, prefixIndex); }
/** * Finishes the source mapping for the given node at the current position. * This is intended to be called immediately after the whole node is written to the buffer. * * @param node the {@link CssNode} to be processed * @param endLine the last character's line number when it ends writing output * @param endCharIndex the last character's character index when it ends writing output */ @Override public void endSourceMapping(CssNode node, int endLine, int endCharIndex) { Preconditions.checkState(node != null); Preconditions.checkState(endLine >= 0); // -1 when a node contributes no content at the start of the buffer, // as when a CssImportBlockNode is encountered, and there is no // copyright comment. Preconditions.checkState(endCharIndex >= -1); endCharIndex++; // if (!mappings.isEmpty() && mappings.peek().node == node) { Mapping mapping = mappings.pop(); mapping.end = new FilePosition(endLine, endCharIndex); } }
/** * Finishes the source mapping for the given * node at the current position. */ @Override void endSourceMapping(Node node) { if (createSrcMap && !mappings.isEmpty() && mappings.peek().node == node) { Mapping mapping = mappings.pop(); int line = getCurrentLineIndex(); int index = getCurrentCharIndex(); checkState(line >= 0); mapping.end = new FilePosition(line, index); } }
/** * Finishes the source mapping for the given * node at the current position. */ @Override void endSourceMapping(Node node) { if (createSrcMap && !mappings.isEmpty() && mappings.peek().node == node) { Mapping mapping = mappings.pop(); int line = getCurrentLineIndex(); int index = getCurrentCharIndex(); Preconditions.checkState(line >= 0); mapping.end = new FilePosition(line, index); } }
public void endMapping(ISourceLocation node) { if (isBufferWrite()) { return; } if (lastMapping == null) { throw new IllegalStateException("Cannot end mapping when a mapping has not been started"); } lastMapping.destEndPosition = new FilePosition(getCurrentLine(), getCurrentColumn()); sourceMapMappings.add(lastMapping); lastMapping = null; }
public void endMapping(ISourceLocation node) { if (isBufferWrite()) { return; } if (lastMapping == null) { throw new IllegalStateException("Cannot end mapping when a mapping has not been started"); } lastMapping.destEndPosition = new FilePosition(getCurrentLine(), getCurrentColumn()); sourceMapMappings.add(lastMapping); lastMapping = null; }
/** * Adjusts the line numbers saved in the source map when a line should be * removed during post processing. * * @param lineIndex */ protected void removeLineFromMappings(int lineIndex) { for (SourceMapMapping mapping : sourceMapMappings) { FilePosition destStartPosition = mapping.destStartPosition; int startLine = destStartPosition.getLine(); if(startLine > lineIndex) { mapping.destStartPosition = new FilePosition(startLine - 1, destStartPosition.getColumn()); FilePosition destEndPosition = mapping.destEndPosition; mapping.destEndPosition = new FilePosition(destEndPosition.getLine() - 1, destEndPosition.getColumn()); } } }
/** * Adjusts the line numbers saved in the source map when a line should be * removed during post processing. * * @param lineIndex */ protected void removeLineFromMappings(int lineIndex) { for (SourceMapMapping mapping : sourceMapMappings) { FilePosition destStartPosition = mapping.destStartPosition; int startLine = destStartPosition.getLine(); if(startLine > lineIndex) { mapping.destStartPosition = new FilePosition(startLine - 1, destStartPosition.getColumn()); FilePosition destEndPosition = mapping.destEndPosition; mapping.destEndPosition = new FilePosition(destEndPosition.getLine() - 1, destEndPosition.getColumn()); } } }
/** * Adjusts the line numbers saved in the source map when a line should be * added during post processing. * * @param lineIndex */ protected void addLineToMappings(int lineIndex) { for (SourceMapMapping mapping : sourceMapMappings) { FilePosition destStartPosition = mapping.destStartPosition; int startLine = destStartPosition.getLine(); if(startLine > lineIndex) { mapping.destStartPosition = new FilePosition(startLine + 1, destStartPosition.getColumn()); FilePosition destEndPosition = mapping.destEndPosition; mapping.destEndPosition = new FilePosition(destEndPosition.getLine() + 1, destEndPosition.getColumn()); } } }
/** * Adjusts the line numbers saved in the source map when a line should be * added during post processing. * * @param lineIndex */ protected void addLineToMappings(int lineIndex) { for (SourceMapMapping mapping : sourceMapMappings) { FilePosition destStartPosition = mapping.destStartPosition; int startLine = destStartPosition.getLine(); if(startLine > lineIndex) { mapping.destStartPosition = new FilePosition(startLine + 1, destStartPosition.getColumn()); FilePosition destEndPosition = mapping.destEndPosition; mapping.destEndPosition = new FilePosition(destEndPosition.getLine() + 1, destEndPosition.getColumn()); } } }
CompleteMapping(Mapping mapping) { CssNode node = mapping.node; this.sourceFile = getSourceFileName(node); this.inputStart = new FilePosition( getStartLineno(node), getStartCharIndex(node)); this.outputStart = mapping.start; this.outputEnd = mapping.end; }
private void appendExtraMappingToGenerator(SourceMapGeneratorV3 generator, String sourceName, String symbolName, FilePosition sourceStartPosition, FilePosition startPosition, FilePosition endPosition) { //add an extra mapping because there seems to be a bug in //SourceMapGeneratorV3's appendTo() that omits the last //entry, for some reason FilePosition newEndPosition = new FilePosition(endPosition.getLine(), endPosition.getColumn() + 1); generator.addMapping(sourceName, null, sourceStartPosition, endPosition, newEndPosition); }
/** * Starts the source mapping for the given * node at the current position. */ @Override void startSourceMapping(Node node) { Preconditions.checkState(sourceMapDetailLevel != null); Preconditions.checkState(node != null); if (createSrcMap && node.getSourceFileName() != null && node.getLineno() > 0 && sourceMapDetailLevel.apply(node)) { int line = getCurrentLineIndex(); int index = getCurrentCharIndex(); Preconditions.checkState(line >= 0); Mapping mapping = new Mapping(); mapping.node = node; mapping.start = new FilePosition(line, index); mappings.push(mapping); allMappings.add(mapping); } }
public void endMapping(ISourceLocation node) { if (isBufferWrite()) { return; } IEmitter parentEmitter = getParentEmitter(); if (parentEmitter != null && parentEmitter instanceof IMappingEmitter) { IMappingEmitter mappingParent = (IMappingEmitter) parentEmitter; mappingParent.endMapping(node); return; } if (lastMapping == null) { throw new IllegalStateException("Cannot end mapping when a mapping has not been started"); } lastMapping.destEndPosition = new FilePosition(getCurrentLine(), getCurrentColumn()); sourceMapMappings.add(lastMapping); lastMapping = null; }