From 41bfb3f1ca28b20646a302b1d71b5f0b6486e660 Mon Sep 17 00:00:00 2001 From: Mitchell Ludwig Date: Fri, 3 Apr 2015 04:00:05 -0600 Subject: [PATCH 1/4] Fixed labels, added to-tabs functionality --- main.js | 285 ++++++++++++++++++++++++++++++-------------------------- 1 file changed, 152 insertions(+), 133 deletions(-) diff --git a/main.js b/main.js index 96e1a78..14ab986 100644 --- a/main.js +++ b/main.js @@ -2,137 +2,156 @@ /* global brackets, define */ define(function (/* require, exports, module */) { - 'use strict'; - - var CommandManager = brackets.getModule('command/CommandManager'), - Commands = brackets.getModule('command/Commands'), - DocumentManager = brackets.getModule('document/DocumentManager'), - Editor = brackets.getModule('editor/Editor').Editor, - Menus = brackets.getModule('command/Menus'), - EXTENSION_KEY = 'com.github.dsbonev.WhitespaceNormalizer'; - - $(DocumentManager).on('documentSaved', main); - - function main(event, doc) { - if (!extensionEnabledPref.enabled()) return; - - doc.batchOperation(function () { - var line, - lineIndex = 0, - indent = new Array(getIndentSize(Editor) + 1).join(' '), - pattern, - match; - - while ((line = doc.getLine(lineIndex)) !== undefined) { - // trim trailing whitespaces - if (trimEnabledPref.enabled()) { - pattern = /[ \t]+$/g; - match = pattern.exec(line); - if (match) { - doc.replaceRange( - '', - {line: lineIndex, ch: match.index}, - {line: lineIndex, ch: pattern.lastIndex}); - - line = getUpdatedLineAt(doc, lineIndex); - } - } - - // transform tabs to spaces - if (transfEnabledPref.enabled()) { - pattern = /\t/g; - match = pattern.exec(line); - while (match) { - doc.replaceRange( - indent, - {line: lineIndex, ch: match.index}, - {line: lineIndex, ch: pattern.lastIndex}); - - line = getUpdatedLineAt(doc, lineIndex); - - match = pattern.exec(line); - } - } - - lineIndex += 1; - } - lineIndex -= 1 - - // ensure newline at the end of file - if (eofnlEnabledPref.enabled()) { - line = doc.getLine(lineIndex); - if (line !== undefined && line.length > 0 && line.slice(-1) !== '\n') { - doc.replaceRange( - '\n', - {line: lineIndex + 1, ch: line.slice(-1)}); - } - } - }); - - CommandManager.execute(Commands.FILE_SAVE, {doc: doc}); - } - - function getIndentSize(editor) { - return editor.getUseTabChar() ? - editor.getTabSize() : - editor.getSpaceUnits(); - } - - function getUpdatedLineAt(doc, lineIndex) { - return doc.getLine(lineIndex); - } - - function setEnabled(prefs, command, enabled) { - prefs.set('enabled', enabled); - prefs.save(); - command.setChecked(enabled); - } - - var PreferencesManager = brackets.getModule('preferences/PreferencesManager'), - PREFERENCES_KEY = EXTENSION_KEY, - prefs = PreferencesManager.getExtensionPrefs(PREFERENCES_KEY); - - Preference.prototype = { - constructor: Preference, - set: function(state) { - prefs.set(this.name, state) - prefs.save() - this.command.setChecked(state) - }, - toggle: function () { - this.set(!this.command.getChecked()) - }, - enabled: function () { - return this.value() === 'true' || this.value() === true - }, - value: function () { - return prefs.get(this.name) - }, - registerCommand: function () { - var self = this; - return CommandManager.register(this.label, this.commandId, function () { - self.toggle() - }) - } - } - - Preference.menu = Menus.getMenu(Menus.AppMenuBar.EDIT_MENU) - Preference.menu.addMenuDivider() - - var extensionEnabledPref = new Preference('enabled', '', '') - var trimEnabledPref = new Preference('trim', 'trim', 'trim') - var transfEnabledPref = new Preference('transf', 'transf', 'transf') - var eofnlEnabledPref = new Preference('eofnl', 'eofnl', 'eofnl') - - function Preference(name, label, commandIdSuffix) { - this.name = name - this.label = 'Whitespace Normalizer' + (label ? (' ' + label) : '') - this.commandId = EXTENSION_KEY + (commandIdSuffix ? ('.' + commandIdSuffix) : '') - this.command = this.registerCommand() - - prefs.definePreference(this.name, "boolean", "true") - this.set(prefs.get(this.name)) - - this.constructor.menu.addMenuItem(this.commandId) - } + 'use strict'; + + var CommandManager = brackets.getModule('command/CommandManager'), + Commands = brackets.getModule('command/Commands'), + DocumentManager = brackets.getModule('document/DocumentManager'), + Editor = brackets.getModule('editor/Editor').Editor, + Menus = brackets.getModule('command/Menus'), + EXTENSION_KEY = 'com.github.dsbonev.WhitespaceNormalizer', + PreferencesManager = brackets.getModule('preferences/PreferencesManager'), + PREFERENCES_KEY = EXTENSION_KEY, + prefs = PreferencesManager.getExtensionPrefs(PREFERENCES_KEY), + justModified = false; + + $(DocumentManager).on('documentSaved', main); + + function main(event, doc) { + if (!extensionEnabledPref.enabled()) return; + if (justModified) { + justModified = false; + return; + } + + doc.batchOperation(function () { + var line, + lineIndex = 0, + indentSize = getIndentSize(), + indent = new Array(indentSize + 1).join(' '), + tab = "\t", + pattern, + match; + + while ((line = doc.getLine(lineIndex)) !== undefined) { + // trim trailing whitespaces + if (trimEnabledPref.enabled()) { + pattern = /[ \t]+$/g; + match = pattern.exec(line); + if (match) { + doc.replaceRange( + '', + {line: lineIndex, ch: match.index}, + {line: lineIndex, ch: pattern.lastIndex}); + line = doc.getLine(lineIndex); + } + } + + // transform tabs to spaces + if (transfEnabledPref.enabled()) { + pattern = /^\t/g; + match = pattern.exec(line); + while (match) { + doc.replaceRange( + indent, + {line: lineIndex, ch: match.index}, + {line: lineIndex, ch: pattern.lastIndex}); + line = doc.getLine(lineIndex); + match = pattern.exec(line); + } + } + + // transform spaces to tabs + var spaceCount = 0, + tabCount = 0, + trailingSpaceCount = 0, + replString = ""; + if (toTabsEnabledPref.enabled()) { + pattern = /^ +/g; + match = pattern.exec(line); + if (match) { + spaceCount = match[0].length; + tabCount = Math.floor(spaceCount / indentSize); + trailingSpaceCount = spaceCount % indentSize; + replString = Array(tabCount + 1).join(tab) + Array(trailingSpaceCount + 1).join(" "); + doc.replaceRange( + replString, + {line: lineIndex, ch: match.index}, + {line: lineIndex, ch: pattern.lastIndex}); + line = doc.getLine(lineIndex); + } + } + + lineIndex += 1; + } + lineIndex -= 1 + + // ensure newline at the end of file + if (eofnlEnabledPref.enabled()) { + line = doc.getLine(lineIndex); + if (line !== undefined && line.length > 0 && line.slice(-1) !== '\n') { + doc.replaceRange( + '\n', + {line: lineIndex + 1, ch: line.slice(-1)}); + } + } + }); + justModified = true; + CommandManager.execute(Commands.FILE_SAVE, {doc: doc}); + } + + function getIndentSize() { + return Editor.getUseTabChar() ? + Editor.getTabSize() : + Editor.getSpaceUnits(); + } + + function setEnabled(prefs, command, enabled) { + prefs.set('enabled', enabled); + prefs.save(); + command.setChecked(enabled); + } + + function Preference(name, label, commandIdSuffix) { + this.name = name; + this.label = label; + this.commandId = EXTENSION_KEY + (commandIdSuffix ? ('.' + commandIdSuffix) : ''); + this.command = this.registerCommand(); + prefs.definePreference(this.name, "boolean", "true") + this.set(prefs.get(this.name)) + this.constructor.menu.addMenuItem(this.commandId) + } + Preference.prototype = { + constructor: Preference, + set: function(state) { + prefs.set(this.name, state) + prefs.save() + this.command.setChecked(state) + }, + toggle: function () { + this.set(!this.command.getChecked()) + }, + enabled: function () { + return this.value() === 'true' || this.value() === true + }, + value: function () { + return prefs.get(this.name) + }, + registerCommand: function () { + var self = this; + return CommandManager.register(this.label, this.commandId, function () { + self.toggle() + }) + } + } + + Preference.menu = Menus.getMenu(Menus.AppMenuBar.EDIT_MENU) + Preference.menu.addMenuDivider() + + var extensionEnabledPref = new Preference('enabled', 'Enable Whitespace Normalizer', ''), + trimEnabledPref = new Preference('trim', 'Trim trailing whitespace', 'trim'), + toTabsEnabledPref = new Preference('totabs', 'Normalize to tabs', 'totabs'), + transfEnabledPref = new Preference('transf', 'Normalize to spaces', 'transf'), + eofnlEnabledPref = new Preference('eofnl', 'End file with newline', 'eofnl'); + }); From 46588bff40268984876d8b2e8df45fac79700072 Mon Sep 17 00:00:00 2001 From: Mitchell Ludwig Date: Tue, 7 Apr 2015 09:13:37 -0600 Subject: [PATCH 2/4] Fixed global replace issue --- main.js | 286 +++++++++++++++++++++++++--------------------------- main.min.js | 1 + 2 files changed, 136 insertions(+), 151 deletions(-) create mode 100644 main.min.js diff --git a/main.js b/main.js index 14ab986..3f17f95 100644 --- a/main.js +++ b/main.js @@ -2,156 +2,140 @@ /* global brackets, define */ define(function (/* require, exports, module */) { - 'use strict'; - - var CommandManager = brackets.getModule('command/CommandManager'), - Commands = brackets.getModule('command/Commands'), - DocumentManager = brackets.getModule('document/DocumentManager'), - Editor = brackets.getModule('editor/Editor').Editor, - Menus = brackets.getModule('command/Menus'), - EXTENSION_KEY = 'com.github.dsbonev.WhitespaceNormalizer', - PreferencesManager = brackets.getModule('preferences/PreferencesManager'), - PREFERENCES_KEY = EXTENSION_KEY, - prefs = PreferencesManager.getExtensionPrefs(PREFERENCES_KEY), - justModified = false; - - $(DocumentManager).on('documentSaved', main); - - function main(event, doc) { - if (!extensionEnabledPref.enabled()) return; - if (justModified) { - justModified = false; - return; - } - - doc.batchOperation(function () { - var line, - lineIndex = 0, - indentSize = getIndentSize(), - indent = new Array(indentSize + 1).join(' '), - tab = "\t", - pattern, - match; - - while ((line = doc.getLine(lineIndex)) !== undefined) { - // trim trailing whitespaces - if (trimEnabledPref.enabled()) { - pattern = /[ \t]+$/g; - match = pattern.exec(line); - if (match) { - doc.replaceRange( - '', - {line: lineIndex, ch: match.index}, - {line: lineIndex, ch: pattern.lastIndex}); - line = doc.getLine(lineIndex); - } - } - - // transform tabs to spaces - if (transfEnabledPref.enabled()) { - pattern = /^\t/g; - match = pattern.exec(line); - while (match) { - doc.replaceRange( - indent, - {line: lineIndex, ch: match.index}, - {line: lineIndex, ch: pattern.lastIndex}); - line = doc.getLine(lineIndex); - match = pattern.exec(line); - } - } - - // transform spaces to tabs - var spaceCount = 0, - tabCount = 0, - trailingSpaceCount = 0, - replString = ""; - if (toTabsEnabledPref.enabled()) { - pattern = /^ +/g; - match = pattern.exec(line); - if (match) { - spaceCount = match[0].length; - tabCount = Math.floor(spaceCount / indentSize); - trailingSpaceCount = spaceCount % indentSize; - replString = Array(tabCount + 1).join(tab) + Array(trailingSpaceCount + 1).join(" "); - doc.replaceRange( - replString, - {line: lineIndex, ch: match.index}, - {line: lineIndex, ch: pattern.lastIndex}); - line = doc.getLine(lineIndex); - } - } - - lineIndex += 1; - } - lineIndex -= 1 - - // ensure newline at the end of file - if (eofnlEnabledPref.enabled()) { - line = doc.getLine(lineIndex); - if (line !== undefined && line.length > 0 && line.slice(-1) !== '\n') { - doc.replaceRange( - '\n', - {line: lineIndex + 1, ch: line.slice(-1)}); - } - } - }); - justModified = true; - CommandManager.execute(Commands.FILE_SAVE, {doc: doc}); - } - - function getIndentSize() { - return Editor.getUseTabChar() ? - Editor.getTabSize() : - Editor.getSpaceUnits(); - } - - function setEnabled(prefs, command, enabled) { - prefs.set('enabled', enabled); - prefs.save(); - command.setChecked(enabled); - } - - function Preference(name, label, commandIdSuffix) { - this.name = name; - this.label = label; - this.commandId = EXTENSION_KEY + (commandIdSuffix ? ('.' + commandIdSuffix) : ''); - this.command = this.registerCommand(); - prefs.definePreference(this.name, "boolean", "true") - this.set(prefs.get(this.name)) - this.constructor.menu.addMenuItem(this.commandId) - } - Preference.prototype = { - constructor: Preference, - set: function(state) { - prefs.set(this.name, state) - prefs.save() - this.command.setChecked(state) - }, - toggle: function () { - this.set(!this.command.getChecked()) - }, - enabled: function () { - return this.value() === 'true' || this.value() === true - }, - value: function () { - return prefs.get(this.name) - }, - registerCommand: function () { - var self = this; - return CommandManager.register(this.label, this.commandId, function () { - self.toggle() - }) - } - } - - Preference.menu = Menus.getMenu(Menus.AppMenuBar.EDIT_MENU) - Preference.menu.addMenuDivider() - - var extensionEnabledPref = new Preference('enabled', 'Enable Whitespace Normalizer', ''), - trimEnabledPref = new Preference('trim', 'Trim trailing whitespace', 'trim'), - toTabsEnabledPref = new Preference('totabs', 'Normalize to tabs', 'totabs'), - transfEnabledPref = new Preference('transf', 'Normalize to spaces', 'transf'), - eofnlEnabledPref = new Preference('eofnl', 'End file with newline', 'eofnl'); + 'use strict'; + + var CommandManager = brackets.getModule('command/CommandManager'), + Commands = brackets.getModule('command/Commands'), + DocumentManager = brackets.getModule('document/DocumentManager'), + Editor = brackets.getModule('editor/Editor').Editor, + Menus = brackets.getModule('command/Menus'), + EXTENSION_KEY = 'com.github.dsbonev.WhitespaceNormalizer', + PreferencesManager = brackets.getModule('preferences/PreferencesManager'), + PREFERENCES_KEY = EXTENSION_KEY, + prefs = PreferencesManager.getExtensionPrefs(PREFERENCES_KEY), + justModified = false; + + $(DocumentManager).on('documentSaved', main); + + function main(event, doc) { + if (!extensionEnabledPref.enabled()) return; + if (justModified) { + justModified = false; + return; + } + + doc.batchOperation(function () { + var line, + lineIndex = 0, + indentSize = getIndentSize(), + indent = new Array(indentSize + 1).join(' '), + pattern, + match; + + while ((line = doc.getLine(lineIndex)) !== undefined) { + // trim trailing whitespaces + if (trimEnabledPref.enabled()) { + pattern = /[ \t]+$/g; + match = pattern.exec(line); + if (match) { + doc.replaceRange( + '', + {line: lineIndex, ch: match.index}, + {line: lineIndex, ch: pattern.lastIndex}); + line = doc.getLine(lineIndex); + } + } + + // transform spaces to tabs + var replString = "", + pattern = /^[ \t]+/g, + rAllTabs = /\t/g, + rAllSpaces = new RegExp(indent,'g'); + match = pattern.exec(line); + if (match) { + replString = match[0]; + if (transfEnabledPref.enabled()) { + replString = match[0].replace(rAllTabs, indent); + } + if (toTabsEnabledPref.enabled()) { + //Flick tabs to spaces, then convert the spaces back to tabs, to handle intermediary spaces, ex "\t \t " + replString = match[0].replace(rAllTabs, indent).replace(rAllSpaces, "\t"); + } + doc.replaceRange( + replString, + {line: lineIndex, ch: match.index}, + {line: lineIndex, ch: pattern.lastIndex}); + } + lineIndex += 1; + } + lineIndex -= 1 + + // ensure newline at the end of file + if (eofnlEnabledPref.enabled()) { + line = doc.getLine(lineIndex); + if (line !== undefined && line.length > 0 && line.slice(-1) !== '\n') { + doc.replaceRange( + '\n', + {line: lineIndex + 1, ch: line.slice(-1)}); + } + } + }); + justModified = true; + CommandManager.execute(Commands.FILE_SAVE, {doc: doc}); + } + + function getIndentSize() { + return Editor.getUseTabChar() ? + Editor.getTabSize() : + Editor.getSpaceUnits(); + } + + function setEnabled(prefs, command, enabled) { + prefs.set('enabled', enabled); + prefs.save(); + command.setChecked(enabled); + } + + function Preference(name, label, commandIdSuffix) { + this.name = name; + this.label = label; + this.commandId = EXTENSION_KEY + (commandIdSuffix ? ('.' + commandIdSuffix) : ''); + this.command = this.registerCommand(); + prefs.definePreference(this.name, "boolean", "true") + this.set(prefs.get(this.name)) + this.constructor.menu.addMenuItem(this.commandId) + } + Preference.prototype = { + constructor: Preference, + set: function(state) { + prefs.set(this.name, state) + prefs.save() + this.command.setChecked(state) + }, + toggle: function () { + this.set(!this.command.getChecked()) + }, + enabled: function () { + return this.value() === 'true' || this.value() === true + }, + value: function () { + return prefs.get(this.name) + }, + registerCommand: function () { + var self = this; + return CommandManager.register(this.label, this.commandId, function () { + self.toggle() + }) + } + } + + Preference.menu = Menus.getMenu(Menus.AppMenuBar.EDIT_MENU) + Preference.menu.addMenuDivider() + + var extensionEnabledPref = new Preference('enabled', 'Enable Whitespace Normalizer', ''), + trimEnabledPref = new Preference('trim', 'Trim trailing whitespace', 'trim'), + toTabsEnabledPref = new Preference('totabs', 'Normalize to tabs', 'totabs'), + transfEnabledPref = new Preference('transf', 'Normalize to spaces', 'transf'), + eofnlEnabledPref = new Preference('eofnl', 'End file with newline', 'eofnl'); }); diff --git a/main.min.js b/main.min.js new file mode 100644 index 0000000..ee1ed97 --- /dev/null +++ b/main.min.js @@ -0,0 +1 @@ +define(function(){"use strict";function l(t,a){if(h.enabled()){if(d)return void(d=!1);a.batchOperation(function(){for(var e,r,s,n=0,t=u(),i=new Array(t+1).join(" ");void 0!==(e=a.getLine(n));){f.enabled()&&(r=/[ \t]+$/g,s=r.exec(e),s&&(a.replaceRange("",{line:n,ch:s.index},{line:n,ch:r.lastIndex}),e=a.getLine(n)));var c="",r=/^[ \t]+/g,o=/\t/g,d=new RegExp(i,"g");s=r.exec(e),s&&(c=s[0],p.enabled()&&(c=s[0].replace(o,i)),b.enabled()&&(c=s[0].replace(o,i).replace(d," ")),a.replaceRange(c,{line:n,ch:s.index},{line:n,ch:r.lastIndex})),n+=1}n-=1,M.enabled()&&(e=a.getLine(n),void 0!==e&&e.length>0&&"\n"!==e.slice(-1)&&a.replaceRange("\n",{line:n+1,ch:e.slice(-1)}))}),d=!0,e.execute(n.FILE_SAVE,{doc:a})}}function u(){return a.getUseTabChar()?a.getTabSize():a.getSpaceUnits()}function g(e,n,t){this.name=e,this.label=n,this.commandId=r+(t?"."+t:""),this.command=this.registerCommand(),o.definePreference(this.name,"boolean","true"),this.set(o.get(this.name)),this.constructor.menu.addMenuItem(this.commandId)}var e=brackets.getModule("command/CommandManager"),n=brackets.getModule("command/Commands"),t=brackets.getModule("document/DocumentManager"),a=brackets.getModule("editor/Editor").Editor,i=brackets.getModule("command/Menus"),r="com.github.dsbonev.WhitespaceNormalizer",s=brackets.getModule("preferences/PreferencesManager"),c=r,o=s.getExtensionPrefs(c),d=!1;$(t).on("documentSaved",l),g.prototype={constructor:g,set:function(e){o.set(this.name,e),o.save(),this.command.setChecked(e)},toggle:function(){this.set(!this.command.getChecked())},enabled:function(){return"true"===this.value()||this.value()===!0},value:function(){return o.get(this.name)},registerCommand:function(){var n=this;return e.register(this.label,this.commandId,function(){n.toggle()})}},g.menu=i.getMenu(i.AppMenuBar.EDIT_MENU),g.menu.addMenuDivider();var h=new g("enabled","Enable Whitespace Normalizer",""),f=new g("trim","Trim trailing whitespace","trim"),b=new g("totabs","Normalize to tabs","totabs"),p=new g("transf","Normalize to spaces","transf"),M=new g("eofnl","End file with newline","eofnl")}); \ No newline at end of file From c0dfc991da014a4aa8cf7276368c5ebf514142ba Mon Sep 17 00:00:00 2001 From: Mitchell Ludwig Date: Tue, 7 Apr 2015 09:20:34 -0600 Subject: [PATCH 3/4] Following indentation rules --- main.js | 244 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 122 insertions(+), 122 deletions(-) diff --git a/main.js b/main.js index 3f17f95..ae6c157 100644 --- a/main.js +++ b/main.js @@ -2,140 +2,140 @@ /* global brackets, define */ define(function (/* require, exports, module */) { - 'use strict'; + 'use strict'; - var CommandManager = brackets.getModule('command/CommandManager'), - Commands = brackets.getModule('command/Commands'), - DocumentManager = brackets.getModule('document/DocumentManager'), - Editor = brackets.getModule('editor/Editor').Editor, - Menus = brackets.getModule('command/Menus'), - EXTENSION_KEY = 'com.github.dsbonev.WhitespaceNormalizer', - PreferencesManager = brackets.getModule('preferences/PreferencesManager'), - PREFERENCES_KEY = EXTENSION_KEY, - prefs = PreferencesManager.getExtensionPrefs(PREFERENCES_KEY), - justModified = false; + var CommandManager = brackets.getModule('command/CommandManager'), + Commands = brackets.getModule('command/Commands'), + DocumentManager = brackets.getModule('document/DocumentManager'), + Editor = brackets.getModule('editor/Editor').Editor, + Menus = brackets.getModule('command/Menus'), + EXTENSION_KEY = 'com.github.dsbonev.WhitespaceNormalizer', + PreferencesManager = brackets.getModule('preferences/PreferencesManager'), + PREFERENCES_KEY = EXTENSION_KEY, + prefs = PreferencesManager.getExtensionPrefs(PREFERENCES_KEY), + justModified = false; - $(DocumentManager).on('documentSaved', main); + $(DocumentManager).on('documentSaved', main); - function main(event, doc) { - if (!extensionEnabledPref.enabled()) return; - if (justModified) { - justModified = false; - return; - } + function main(event, doc) { + if (!extensionEnabledPref.enabled()) return; + if (justModified) { + justModified = false; + return; + } - doc.batchOperation(function () { - var line, - lineIndex = 0, - indentSize = getIndentSize(), - indent = new Array(indentSize + 1).join(' '), - pattern, - match; + doc.batchOperation(function () { + var line, + lineIndex = 0, + indentSize = getIndentSize(), + indent = new Array(indentSize + 1).join(' '), + pattern, + match; - while ((line = doc.getLine(lineIndex)) !== undefined) { - // trim trailing whitespaces - if (trimEnabledPref.enabled()) { - pattern = /[ \t]+$/g; - match = pattern.exec(line); - if (match) { - doc.replaceRange( - '', - {line: lineIndex, ch: match.index}, - {line: lineIndex, ch: pattern.lastIndex}); - line = doc.getLine(lineIndex); - } - } + while ((line = doc.getLine(lineIndex)) !== undefined) { + // trim trailing whitespaces + if (trimEnabledPref.enabled()) { + pattern = /[ \t]+$/g; + match = pattern.exec(line); + if (match) { + doc.replaceRange( + '', + {line: lineIndex, ch: match.index}, + {line: lineIndex, ch: pattern.lastIndex}); + line = doc.getLine(lineIndex); + } + } - // transform spaces to tabs - var replString = "", - pattern = /^[ \t]+/g, - rAllTabs = /\t/g, - rAllSpaces = new RegExp(indent,'g'); - match = pattern.exec(line); - if (match) { - replString = match[0]; - if (transfEnabledPref.enabled()) { - replString = match[0].replace(rAllTabs, indent); - } - if (toTabsEnabledPref.enabled()) { - //Flick tabs to spaces, then convert the spaces back to tabs, to handle intermediary spaces, ex "\t \t " - replString = match[0].replace(rAllTabs, indent).replace(rAllSpaces, "\t"); - } - doc.replaceRange( - replString, - {line: lineIndex, ch: match.index}, - {line: lineIndex, ch: pattern.lastIndex}); - } - lineIndex += 1; - } - lineIndex -= 1 + // transform spaces to tabs + var replString = "", + pattern = /^[ \t]+/g, + rAllTabs = /\t/g, + rAllSpaces = new RegExp(indent,'g'); + match = pattern.exec(line); + if (match) { + replString = match[0]; + if (transfEnabledPref.enabled()) { + replString = match[0].replace(rAllTabs, indent); + } + if (toTabsEnabledPref.enabled()) { + //Flick tabs to spaces, then convert the spaces back to tabs, to handle intermediary spaces, ex "\t \t " + replString = match[0].replace(rAllTabs, indent).replace(rAllSpaces, "\t"); + } + doc.replaceRange( + replString, + {line: lineIndex, ch: match.index}, + {line: lineIndex, ch: pattern.lastIndex}); + } + lineIndex += 1; + } + lineIndex -= 1 - // ensure newline at the end of file - if (eofnlEnabledPref.enabled()) { - line = doc.getLine(lineIndex); - if (line !== undefined && line.length > 0 && line.slice(-1) !== '\n') { - doc.replaceRange( - '\n', - {line: lineIndex + 1, ch: line.slice(-1)}); - } - } - }); - justModified = true; - CommandManager.execute(Commands.FILE_SAVE, {doc: doc}); - } + // ensure newline at the end of file + if (eofnlEnabledPref.enabled()) { + line = doc.getLine(lineIndex); + if (line !== undefined && line.length > 0 && line.slice(-1) !== '\n') { + doc.replaceRange( + '\n', + {line: lineIndex + 1, ch: line.slice(-1)}); + } + } + }); + justModified = true; + CommandManager.execute(Commands.FILE_SAVE, {doc: doc}); + } - function getIndentSize() { - return Editor.getUseTabChar() ? - Editor.getTabSize() : - Editor.getSpaceUnits(); - } + function getIndentSize() { + return Editor.getUseTabChar() ? + Editor.getTabSize() : + Editor.getSpaceUnits(); + } - function setEnabled(prefs, command, enabled) { - prefs.set('enabled', enabled); - prefs.save(); - command.setChecked(enabled); - } + function setEnabled(prefs, command, enabled) { + prefs.set('enabled', enabled); + prefs.save(); + command.setChecked(enabled); + } - function Preference(name, label, commandIdSuffix) { - this.name = name; - this.label = label; - this.commandId = EXTENSION_KEY + (commandIdSuffix ? ('.' + commandIdSuffix) : ''); - this.command = this.registerCommand(); - prefs.definePreference(this.name, "boolean", "true") - this.set(prefs.get(this.name)) - this.constructor.menu.addMenuItem(this.commandId) - } - Preference.prototype = { - constructor: Preference, - set: function(state) { - prefs.set(this.name, state) - prefs.save() - this.command.setChecked(state) - }, - toggle: function () { - this.set(!this.command.getChecked()) - }, - enabled: function () { - return this.value() === 'true' || this.value() === true - }, - value: function () { - return prefs.get(this.name) - }, - registerCommand: function () { - var self = this; - return CommandManager.register(this.label, this.commandId, function () { - self.toggle() - }) - } + function Preference(name, label, commandIdSuffix) { + this.name = name; + this.label = label; + this.commandId = EXTENSION_KEY + (commandIdSuffix ? ('.' + commandIdSuffix) : ''); + this.command = this.registerCommand(); + prefs.definePreference(this.name, "boolean", "true") + this.set(prefs.get(this.name)) + this.constructor.menu.addMenuItem(this.commandId) + } + Preference.prototype = { + constructor: Preference, + set: function(state) { + prefs.set(this.name, state) + prefs.save() + this.command.setChecked(state) + }, + toggle: function () { + this.set(!this.command.getChecked()) + }, + enabled: function () { + return this.value() === 'true' || this.value() === true + }, + value: function () { + return prefs.get(this.name) + }, + registerCommand: function () { + var self = this; + return CommandManager.register(this.label, this.commandId, function () { + self.toggle() + }) } + } - Preference.menu = Menus.getMenu(Menus.AppMenuBar.EDIT_MENU) - Preference.menu.addMenuDivider() + Preference.menu = Menus.getMenu(Menus.AppMenuBar.EDIT_MENU) + Preference.menu.addMenuDivider() - var extensionEnabledPref = new Preference('enabled', 'Enable Whitespace Normalizer', ''), - trimEnabledPref = new Preference('trim', 'Trim trailing whitespace', 'trim'), - toTabsEnabledPref = new Preference('totabs', 'Normalize to tabs', 'totabs'), - transfEnabledPref = new Preference('transf', 'Normalize to spaces', 'transf'), - eofnlEnabledPref = new Preference('eofnl', 'End file with newline', 'eofnl'); + var extensionEnabledPref = new Preference('enabled', 'Enable Whitespace Normalizer', ''), + trimEnabledPref = new Preference('trim', 'Trim trailing whitespace', 'trim'), + toTabsEnabledPref = new Preference('totabs', 'Normalize to tabs', 'totabs'), + transfEnabledPref = new Preference('transf', 'Normalize to spaces', 'transf'), + eofnlEnabledPref = new Preference('eofnl', 'End file with newline', 'eofnl'); }); From ab03caba4be1c87c95c6141e922804bd8edebfbf Mon Sep 17 00:00:00 2001 From: Mitchell Ludwig Date: Tue, 7 Apr 2015 09:26:44 -0600 Subject: [PATCH 4/4] Fixed space issue, removed min.js --- main.js | 2 +- main.min.js | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) delete mode 100644 main.min.js diff --git a/main.js b/main.js index ae6c157..ec1d529 100644 --- a/main.js +++ b/main.js @@ -15,7 +15,7 @@ define(function (/* require, exports, module */) { prefs = PreferencesManager.getExtensionPrefs(PREFERENCES_KEY), justModified = false; - $(DocumentManager).on('documentSaved', main); + $(DocumentManager).on('documentSaved', main); function main(event, doc) { if (!extensionEnabledPref.enabled()) return; diff --git a/main.min.js b/main.min.js deleted file mode 100644 index ee1ed97..0000000 --- a/main.min.js +++ /dev/null @@ -1 +0,0 @@ -define(function(){"use strict";function l(t,a){if(h.enabled()){if(d)return void(d=!1);a.batchOperation(function(){for(var e,r,s,n=0,t=u(),i=new Array(t+1).join(" ");void 0!==(e=a.getLine(n));){f.enabled()&&(r=/[ \t]+$/g,s=r.exec(e),s&&(a.replaceRange("",{line:n,ch:s.index},{line:n,ch:r.lastIndex}),e=a.getLine(n)));var c="",r=/^[ \t]+/g,o=/\t/g,d=new RegExp(i,"g");s=r.exec(e),s&&(c=s[0],p.enabled()&&(c=s[0].replace(o,i)),b.enabled()&&(c=s[0].replace(o,i).replace(d," ")),a.replaceRange(c,{line:n,ch:s.index},{line:n,ch:r.lastIndex})),n+=1}n-=1,M.enabled()&&(e=a.getLine(n),void 0!==e&&e.length>0&&"\n"!==e.slice(-1)&&a.replaceRange("\n",{line:n+1,ch:e.slice(-1)}))}),d=!0,e.execute(n.FILE_SAVE,{doc:a})}}function u(){return a.getUseTabChar()?a.getTabSize():a.getSpaceUnits()}function g(e,n,t){this.name=e,this.label=n,this.commandId=r+(t?"."+t:""),this.command=this.registerCommand(),o.definePreference(this.name,"boolean","true"),this.set(o.get(this.name)),this.constructor.menu.addMenuItem(this.commandId)}var e=brackets.getModule("command/CommandManager"),n=brackets.getModule("command/Commands"),t=brackets.getModule("document/DocumentManager"),a=brackets.getModule("editor/Editor").Editor,i=brackets.getModule("command/Menus"),r="com.github.dsbonev.WhitespaceNormalizer",s=brackets.getModule("preferences/PreferencesManager"),c=r,o=s.getExtensionPrefs(c),d=!1;$(t).on("documentSaved",l),g.prototype={constructor:g,set:function(e){o.set(this.name,e),o.save(),this.command.setChecked(e)},toggle:function(){this.set(!this.command.getChecked())},enabled:function(){return"true"===this.value()||this.value()===!0},value:function(){return o.get(this.name)},registerCommand:function(){var n=this;return e.register(this.label,this.commandId,function(){n.toggle()})}},g.menu=i.getMenu(i.AppMenuBar.EDIT_MENU),g.menu.addMenuDivider();var h=new g("enabled","Enable Whitespace Normalizer",""),f=new g("trim","Trim trailing whitespace","trim"),b=new g("totabs","Normalize to tabs","totabs"),p=new g("transf","Normalize to spaces","transf"),M=new g("eofnl","End file with newline","eofnl")}); \ No newline at end of file