Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v1.1 +spaceDefaultToTab (4 to arbitrary), Moved into Edit menu, impoved Readme #5

Merged
merged 2 commits into from Jun 15, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 21 additions & 4 deletions README.md
@@ -1,16 +1,33 @@
Brackets Tab to space
=================

A brackets extension to convert tab based identation to space based identation and vice versa.
A [Brackets](https://github.com/adobe/brackets/) extension to convert tab based identation to space based identation and vice versa.

To install, place the ```tabtospace``` folder inside the ```brackets/src/extensions/user``` folder.
How to install
--------------

**Brackets: Menu > File > Extension Manager, [Install from URL...]** and paste ZIP url above.


####Or manually
1. Download ZIP
2. Unzip in **user** folder in **Brackets > Help > Show Extensions Folder**
3. Restart Brackets

**Compatible with Brackets Sprint10**


Usage
=====

Simply open a project in Brackets, and select ```Convert indentation to spaces``` or ```Convert indentation to tabs``` from the ```File``` menu.
Simply open a project in Brackets, and select ```Convert indentation to spaces``` or ```Convert indentation to tabs``` from the ```Edit``` menu.


This will replace tabs with N spaces, and replace N spaces at the begining of each line with a tab (N being the tab length defined in the editor, 4 being the default).

```Convert 4-space indentation to tabs``` will replace 4 spaces at the begining of each line, regardless of editor defaults.

This will replace tabs with N spaces, and replace each group of N spaces with a tab (N being the tab length defined in the editor, 4 being the default).
History
-------
v 1.1
+ Convert 4-space indentation to tabs
59 changes: 38 additions & 21 deletions main.js
Expand Up @@ -24,7 +24,10 @@
/*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50 */
/*global define, $, brackets, window */

/** extension to convert indentation to tabs or spaces */
/* https://github.com/davidderaedt/tabtospace-extension
extension to convert indentation to tabs or spaces
v1.1: + 4-space indentation to tabs */

define(function (require, exports, module) {
'use strict';

Expand All @@ -34,13 +37,15 @@ define(function (require, exports, module) {
Menus = brackets.getModule("command/Menus");


var TAB2S_COMMAND = "tabtospace.tabtospace";
var S2TAB_COMMAND = "tabtospace.spacetotab";
var TAB2S_MENU_NAME = "Convert indentation to spaces";
var S2TAB_MENU_NAME = "Convert indentation to tabs";
var DEFAULT_TAB_WIDTH = 4;
var TAB2S_COMMAND = "tabtospace.tabtospace";
var S2TAB_COMMAND = "tabtospace.spacetotab";
var SD2TAB_COMMAND = "tabtospace.spaceDToTab";
var TAB2S_MENU_NAME = "Convert indentation to spaces";
var S2TAB_MENU_NAME = "Convert indentation to tabs";
var SD2TAB_MENU_NAME = "Convert 4-space indentation to tabs";
var DEFAULT_TAB_WIDTH = 4;
var INDENTATION_MATCHER = /^[ \t]+/gm;


function getTabWidth() {
var editor = EditorManager.getCurrentFullEditor();
Expand All @@ -50,8 +55,8 @@ define(function (require, exports, module) {
console.log("Using default tab width");
return DEFAULT_TAB_WIDTH;
}


function replaceInDocument(re, textOrFunc) {
var txt = DocumentManager.getCurrentDocument().getText();
var txt2 = txt.replace(re, textOrFunc);
Expand Down Expand Up @@ -79,17 +84,17 @@ define(function (require, exports, module) {

console.assert(0 === lengthOfIndentation("", tabWidth));
console.assert(tabWidth === lengthOfIndentation("\t", tabWidth));

console.assert(1 === lengthOfIndentation(" ", tabWidth));
console.assert(2 === lengthOfIndentation(" ", tabWidth));
console.assert(3 === lengthOfIndentation(" ", tabWidth));
console.assert(4 === lengthOfIndentation(" ", tabWidth));

console.assert(tabWidth + 1 === lengthOfIndentation("\t ", tabWidth));
console.assert(tabWidth + 2 === lengthOfIndentation("\t ", tabWidth));
console.assert(tabWidth + 3 === lengthOfIndentation("\t ", tabWidth));
console.assert(tabWidth + 4 === lengthOfIndentation("\t ", tabWidth));

console.assert(tabWidth === lengthOfIndentation(" \t", tabWidth));
console.assert(tabWidth === lengthOfIndentation(" \t", tabWidth));
console.assert(tabWidth === lengthOfIndentation(" \t", tabWidth));
Expand All @@ -99,15 +104,15 @@ define(function (require, exports, module) {

function indentationWithLength(length, tabWidth) {
var indentation = "";

// Use tabs if tabWidth is not undefined, null or 0
if (tabWidth) {
while (length >= tabWidth) {
indentation += "\t";
length -= tabWidth;
}
}

while (length > 0) {
indentation += " ";
length -= 1;
Expand All @@ -124,8 +129,8 @@ define(function (require, exports, module) {
console.assert(i === lengthOfIndentation(indentationWithLength(i), tabWidth));
}
}


function tabToSpaceReplacer(indentation) {
var length = lengthOfIndentation(indentation, getTabWidth());
return indentationWithLength(length);
Expand All @@ -141,20 +146,32 @@ define(function (require, exports, module) {
var length = lengthOfIndentation(indentation, editorTabWidth);
return indentationWithLength(length, editorTabWidth);
}

function spaceToTab() {
replaceInDocument(INDENTATION_MATCHER, spaceToTabReplacer);
}


CommandManager.register(TAB2S_MENU_NAME, TAB2S_COMMAND, tabToSpace);
CommandManager.register(S2TAB_MENU_NAME, S2TAB_COMMAND, spaceToTab);
function spaceDefaultToTabReplacer(indentation) {
var length = lengthOfIndentation(indentation, DEFAULT_TAB_WIDTH);
return indentationWithLength(length, DEFAULT_TAB_WIDTH);
}

function spaceDefaultToTab() {
replaceInDocument(INDENTATION_MATCHER, spaceDefaultToTabReplacer);
}


CommandManager.register(TAB2S_MENU_NAME, TAB2S_COMMAND, tabToSpace);
CommandManager.register(S2TAB_MENU_NAME, S2TAB_COMMAND, spaceToTab);
CommandManager.register(SD2TAB_MENU_NAME, SD2TAB_COMMAND, spaceDefaultToTab);

var menu = Menus.getMenu(Menus.AppMenuBar.FILE_MENU);
var menu = Menus.getMenu(Menus.AppMenuBar.EDIT_MENU);
menu.addMenuDivider();
menu.addMenuItem(TAB2S_COMMAND);
menu.addMenuItem(S2TAB_COMMAND);
menu.addMenuItem(SD2TAB_COMMAND);

testLengthOfIndentation();
testIndentationWithLength();
});
});
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -3,7 +3,7 @@
"title": "Tab To Space",
"description": "Converts indentation to tabs or spaces.",
"homepage": "https://github.com/davidderaedt/tabtospace-extension",
"version": "1.0.0",
"version": "1.1.0",
"author": "David Deraedt <dderaedt@adobe.com> (http://www.dehats.com)",
"license": "MIT",
"engines": {
Expand Down