-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.js
executable file
·139 lines (107 loc) · 3.6 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*jslint vars: true, nomen: true */
/*global define, $, brackets */
define(function (require, exports, module) {
"use strict";
var CommandManager = brackets.getModule("command/CommandManager"),
EditorManager = brackets.getModule("editor/EditorManager"),
DocumentManager = brackets.getModule("document/DocumentManager"),
Menus = brackets.getModule("command/Menus"),
SINGLE_TO_DOUBLE = "me.drewh.singletodouble",
DOUBLE_TO_SINGLE = "me.drewh.doubletosingle";
/**
* ReplaceAll by Fagner Brack (MIT Licensed)
* Replaces all occurrences of a substring in a string
*/
function replaceAll(str, token, newToken, ignoreCase) {
var _token;
var i = -1;
if (typeof token === "string") {
if (ignoreCase) {
_token = token.toLowerCase();
while ((i = str.toLowerCase().indexOf(token, i >= 0 ? i + newToken.length : 0)) !== -1) {
str = str.substring(0, i) +
newToken +
str.substring(i + token.length);
}
} else {
return str.split(token).join(newToken);
}
}
return str;
}
/**
* Format
*/
function format(options) {
options = options || {};
var isSelection = false;
var unformattedText;
var editor = EditorManager.getCurrentFullEditor(),
selectedText = editor.getSelectedText(),
selection = editor.getSelection();
if (selectedText.length > 0) {
isSelection = true;
unformattedText = selectedText;
} else {
unformattedText = DocumentManager.getCurrentDocument().getText();
}
var cursor = editor.getCursorPos(),
scroll = editor.getScrollPos(),
doc = DocumentManager.getCurrentDocument();
doc.batchOperation(function () {
var formattedText,
single = options.single;
if (single) {
formattedText = replaceAll(unformattedText, '"', "'");
} else {
formattedText = replaceAll(unformattedText, "'", '"');
}
if (isSelection) {
doc.replaceRange(formattedText, selection.start, selection.end);
} else {
doc.setText(formattedText);
}
editor.setCursorPos(cursor);
editor.setScrollPos(scroll.x, scroll.y);
});
}
CommandManager.register("Double to single quote", DOUBLE_TO_SINGLE, function () {
format({
single: true
});
});
CommandManager.register("Single to double quote", SINGLE_TO_DOUBLE, function () {
format();
});
var menu = Menus.getMenu(Menus.AppMenuBar.EDIT_MENU);
var singleToDoubleCmd = {
windowsCommand: {
key: "Ctrl-Alt-'",
platform: "win"
},
macCommand: {
key: "Cmd-Alt-'",
platform: "mac"
}
};
var doubleToSingleCmd = {
windowsCommand: {
key: "Ctrl-Alt-Shift-'",
platform: "win"
},
macCommand: {
key: "Cmd-Alt-Shift-'",
platform: "mac"
}
};
var singleToDouble = [
singleToDoubleCmd.windowsCommand,
singleToDoubleCmd.macCommand
];
var doubleToSingle = [
doubleToSingleCmd.windowsCommand,
doubleToSingleCmd.macCommand
];
menu.addMenuItem(DOUBLE_TO_SINGLE, singleToDouble);
menu.addMenuItem(SINGLE_TO_DOUBLE, doubleToSingle);
});