From 00e15ce8156abe13d78b8f6ac9d72b50dbdcf90b Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Wed, 10 Oct 2018 14:50:08 -0400 Subject: [PATCH] add offsetToLineColumnConverter to remix-lib --- remix-lib/index.js | 2 ++ remix-lib/src/offsetToLineColumnConverter.js | 32 ++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 remix-lib/src/offsetToLineColumnConverter.js diff --git a/remix-lib/index.js b/remix-lib/index.js index 64304009cc..97a2f68b17 100644 --- a/remix-lib/index.js +++ b/remix-lib/index.js @@ -5,6 +5,7 @@ var uiHelper = require('./src/helpers/uiHelper') var compilerHelper = require('./src/helpers/compilerHelper') var SourceMappingDecoder = require('./src/sourceMappingDecoder') var SourceLocationTracker = require('./src/sourceLocationTracker') +var OffsetToColumnConverter = require('./src/offsetToLineColumnConverter') var init = require('./src/init') var util = require('./src/util') var Web3Providers = require('./src/web3Provider/web3Providers') @@ -56,6 +57,7 @@ function modules () { }, SourceMappingDecoder: SourceMappingDecoder, SourceLocationTracker: SourceLocationTracker, + OffsetToColumnConverter: OffsetToColumnConverter, Storage: Storage, init: init, util: util, diff --git a/remix-lib/src/offsetToLineColumnConverter.js b/remix-lib/src/offsetToLineColumnConverter.js new file mode 100644 index 0000000000..44d0458a09 --- /dev/null +++ b/remix-lib/src/offsetToLineColumnConverter.js @@ -0,0 +1,32 @@ +'use strict' +var SourceMappingDecoder = require('./sourceMappingDecoder') + +function offsetToColumnConverter (compilerEvent) { + this.lineBreakPositionsByContent = {} + this.sourceMappingDecoder = new SourceMappingDecoder() + var self = this + if (compilerEvent) { + compilerEvent.register('compilationFinished', function (success, data, source) { + self.clear() + }) + } +} + +offsetToColumnConverter.prototype.offsetToLineColumn = function (rawLocation, file, sources, asts) { + if (!this.lineBreakPositionsByContent[file]) { + for (var filename in asts) { + var source = asts[filename] + if (source.id === file) { + this.lineBreakPositionsByContent[file] = this.sourceMappingDecoder.getLinebreakPositions(sources[filename].content) + break + } + } + } + return this.sourceMappingDecoder.convertOffsetToLineColumn(rawLocation, this.lineBreakPositionsByContent[file]) +} + +offsetToColumnConverter.prototype.clear = function () { + this.lineBreakPositionsByContent = {} +} + +module.exports = offsetToColumnConverter