diff --git a/src/app.js b/src/app.js index 69dddd82bd..28a02c3141 100644 --- a/src/app.js +++ b/src/app.js @@ -12,6 +12,7 @@ var OffsetToLineColumnConverter = require('./lib/offsetToLineColumnConverter') var QueryParams = require('./lib/query-params') var GistHandler = require('./lib/gist-handler') +var helpers = require('./lib/helpers') var Storage = require('./storage') var Browserfiles = require('./app/files/browser-files') var chromeCloudStorageSync = require('./app/files/chromeCloudStorageSync') @@ -215,19 +216,10 @@ function run () { filesProviders: filesProviders }) - function createNonClashingName (path) { - var counter = '' - if (path.endsWith('.sol')) path = path.substring(0, path.lastIndexOf('.sol')) - while (filesProviders['browser'].exists(path + counter + '.sol')) { - counter = (counter | 0) + 1 - } - return path + counter + '.sol' - } - // Add files received from remote instance (i.e. another browser-solidity) function loadFiles (filesSet) { for (var f in filesSet) { - filesProviders['browser'].set(createNonClashingName(f), filesSet[f].content) + filesProviders['browser'].set(helpers.createNonClashingName(f, filesProviders['browser']), filesSet[f].content) } fileManager.switchFile() } @@ -273,7 +265,6 @@ function run () { // ---------------- FilePanel -------------------- var FilePanelAPI = { - createName: createNonClashingName, switchFile: function (path) { fileManager.switchFile(path) }, diff --git a/src/app/panels/file-panel.js b/src/app/panels/file-panel.js index af237eab3e..b569ff1959 100644 --- a/src/app/panels/file-panel.js +++ b/src/app/panels/file-panel.js @@ -10,6 +10,7 @@ var modalDialog = require('../ui/modaldialog') var modalDialogCustom = require('../ui/modal-dialog-custom') var QueryParams = require('../../lib/query-params') var queryParams = new QueryParams() +var helpers = require('../../lib/helpers') var remix = require('ethereum-remix') var styleGuide = remix.ui.styleGuide @@ -245,7 +246,7 @@ function filepanel (appAPI, filesProvider) { } function createNewFile () { - var newName = filesProvider['browser'].type + '/' + appAPI.createName('Untitled.sol') + var newName = filesProvider['browser'].type + '/' + helpers.createNonClashingName('Untitled.sol', filesProvider['browser']) if (!filesProvider['browser'].set(newName, '')) { modalDialogCustom.alert('Failed to create file ' + newName) } else { diff --git a/src/lib/helper.js b/src/lib/helper.js index 5fe7cc2447..917a547b3a 100644 --- a/src/lib/helper.js +++ b/src/lib/helper.js @@ -7,5 +7,13 @@ module.exports = { if (data.length < 5) return data var len = data.length return data.slice(0, 5) + '...' + data.slice(len - 5, len) + }, + createNonClashingName (path, fileProvider) { + var counter = '' + if (path.endsWith('.sol')) path = path.substring(0, path.lastIndexOf('.sol')) + while (fileProvider.exists(path + counter + '.sol')) { + counter = (counter | 0) + 1 + } + return path + counter + '.sol' } }