-
Notifications
You must be signed in to change notification settings - Fork 25
/
main.js
139 lines (116 loc) · 5.38 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
/*
* The MIT License (MIT)
* Copyright (c) 2013-2017 Lance Campbell. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
/*jslint vars: true, plusplus: true, devel: true, regexp: true, nomen: true, indent: 4, maxerr: 50 */
/*global define, brackets, XMLHttpRequest, $ */
define(function (require, exports, module) {
"use strict";
// Brackets modules
var PreferencesManager = brackets.getModule("preferences/PreferencesManager"),
EditorManager = brackets.getModule("editor/EditorManager"),
AppInit = brackets.getModule("utils/AppInit"),
CommandManager = brackets.getModule("command/CommandManager"),
KeyBindingManager = brackets.getModule("command/KeyBindingManager");
// Extension modules
var LoremIpsum = require("LoremIpsum");
// Constants
var LOREM_COMMAND_NAME = "Lorem Ipsum",
LOREM_COMMAND_ID = "lkcampbell.loremIpsum",
LOREM_KEY = "Ctrl-Shift-L";
// Define extension preferences
var onLoremCommand = "lorem",
onNoCommand = "nothing",
prefs = PreferencesManager.getExtensionPrefs("brackets-lorem-ipsum");
prefs.definePreference("onLoremCommand", "string", onLoremCommand, {
description: 'This value determines the command used for the lorem command. Value is any valid lorem command including the default value of "lorem".'
});
prefs.definePreference("onNoCommand", "string", onNoCommand, {
description: 'This value determines command used when there is no command at all. Value is any valid lorem command or the default value of "nothing".'
});
function _getLoremCommand(editor) {
var document = editor.document,
pos = editor.getCursorPos(),
line = document.getLine(pos.line),
start = pos.ch,
end = pos.ch,
command = "";
while (start > 0 && (/\S/).test(line.charAt(start - 1))) {
--start;
}
command = document.getRange({line: pos.line, ch: start}, {line: pos.line, ch: end});
if (command.match(/lorem/)) {
command = command.substring(command.match(/lorem/).index);
}
return ((command.split("_")[0] === "lorem") ? command : "nothing");
}
// Event handlers
function _handleLoremIpsum() {
var editor = EditorManager.getFocusedEditor(),
command = editor ? _getLoremCommand(editor) : null,
text = "",
start = 0,
end = 0,
codemirror = null,
i = 0;
if (!command) {return;}
// Apply preferences if appropriate
if (command === "lorem") {
command = onLoremCommand;
} else if (command === "nothing") {
if (onNoCommand === "nothing") {
return;
} else {
command = onNoCommand;
}
}
// Parse command and inject the Lorem Ipsum into the document
text = LoremIpsum.parseCommand(command);
end = editor.getCursorPos();
start = {line: end.line, ch: end.ch - command.length};
editor.document.replaceRange(text, start, end);
// Fix the line indentation
codemirror = editor._codeMirror;
if (codemirror) {
end = editor.getCursorPos();
for (i = (start.line); i <= end.line; i++) {
codemirror.indentLine(i);
}
}
}
function _applyPreferences() {
onLoremCommand = prefs.get("onLoremCommand");
onNoCommand = prefs.get("onNoCommand");
}
// Initialize extension
AppInit.appReady(function () {
// Register commands and bind default keyboard shortcut (same for all platforms)
CommandManager.register(LOREM_COMMAND_NAME, LOREM_COMMAND_ID, _handleLoremIpsum);
KeyBindingManager.addBinding(LOREM_COMMAND_ID,
[{key: LOREM_KEY},
{key: LOREM_KEY, platform: "mac"}]);
// Set up event listeners
prefs.on("change", _applyPreferences);
// Apply preferences when the extension first initializes
_applyPreferences();
});
});