Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
170 lines (144 loc) · 7.35 KB
/
Copy pathmain.js
File metadata and controls
170 lines (144 loc) · 7.35 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
* GNU AGPL-3.0 License
*
* Copyright (c) 2021 - present core.ai . All rights reserved.
* Original work Copyright (c) 2013 - 2021 Adobe Systems Incorporated. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
* for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://opensource.org/licenses/AGPL-3.0.
*
*/
define(function (require, exports, module) {
var AppInit = brackets.getModule("utils/AppInit"),
PreferencesManager = brackets.getModule("preferences/PreferencesManager"),
Strings = brackets.getModule("strings"),
RenameIdentifier = require("RenameIdentifier"),
ExtractToVariable = require("ExtractToVariable"),
ExtractToFunction = require("ExtractToFunction"),
WrapSelection = require("WrapSelection"),
CommandManager = brackets.getModule("command/CommandManager"),
Menus = brackets.getModule("command/Menus"),
Metrics = brackets.getModule("utils/Metrics"),
_ = brackets.getModule("thirdparty/lodash"),
EditorManager = brackets.getModule("editor/EditorManager");
require("HighLightReferences");
var jsRefactoringEnabled = true;
var KeyboardPrefs = JSON.parse(require("text!keyboard.json"));
// Command ids
var EXTRACTTO_VARIABLE = "refactoring.extractToVariable",
EXTRACTTO_FUNCTION = "refactoring.extractToFunction",
REFACTOR_RENAME = "refactoring.renamereference",
REFACTORWRAPINTRYCATCH = "refactoring.wrapintrycatch",
REFACTORWRAPINCONDITION = "refactoring.wrapincondition",
REFACTORCONVERTTOARROWFN = "refactoring.converttoarrowfunction",
REFACTORCREATEGETSET = "refactoring.creategettersandsetters";
// This preference controls whether to create a session and process all JS files or not.
PreferencesManager.definePreference("refactoring.JSRefactoring", "boolean", true, {
description: Strings.DESCRIPTION_CODE_REFACTORING
});
/**
* Check whether any of refactoring hints preferences for JS Refactoring is disabled
* @return {boolean} enabled/disabled
*/
function _isRefactoringEnabled() {
return (PreferencesManager.get("refactoring.JSRefactoring") !== false);
}
PreferencesManager.on("change", "refactoring.JSRefactoring", function () {
jsRefactoringEnabled = _isRefactoringEnabled();
});
function _handleRefactor(functionName) {
var eventName, eventType = "";
switch (functionName) {
case REFACTOR_RENAME:
eventName = REFACTOR_RENAME;
eventType = "rename";
RenameIdentifier.handleRename();
break;
case EXTRACTTO_VARIABLE:
eventName = EXTRACTTO_VARIABLE;
eventType = "extractToVariable";
ExtractToVariable.handleExtractToVariable();
break;
case EXTRACTTO_FUNCTION:
eventName = EXTRACTTO_FUNCTION;
eventType = "extractToFunction";
ExtractToFunction.handleExtractToFunction();
break;
case REFACTORWRAPINTRYCATCH:
eventName = REFACTORWRAPINTRYCATCH;
eventType = "wrapInTryCatch";
WrapSelection.wrapInTryCatch();
break;
case REFACTORWRAPINCONDITION:
eventName = REFACTORWRAPINCONDITION;
eventType = "wrapInCondition";
WrapSelection.wrapInCondition();
break;
case REFACTORCONVERTTOARROWFN:
eventName = REFACTORCONVERTTOARROWFN;
eventType = "convertToFunction";
WrapSelection.convertToArrowFunction();
break;
case REFACTORCREATEGETSET:
eventName = REFACTORCREATEGETSET;
eventType = "createGetterSetter";
WrapSelection.createGettersAndSetters();
break;
}
if (eventName) {
var editor = EditorManager.getActiveEditor();
// Logging should be done only when the context is javascript
if (!editor || editor.getModeForSelection() !== "javascript") {
return;
}
// Send analytics data for js refactoring
Metrics.countEvent(
Metrics.EVENT_TYPE.CODE_HINTS,
"jsRefactor",
eventType
);
}
}
AppInit.appReady(function () {
if (jsRefactoringEnabled) {
var subMenu = Menus.getContextMenu(Menus.ContextMenuIds.EDITOR_MENU).addSubMenu(Strings.CMD_REFACTOR, "refactor-submenu");
var menuLocation = Menus.AppMenuBar.EDIT_MENU;
Menus.getMenu(menuLocation).addMenuDivider();
// Rename Identifier
CommandManager.register(Strings.CMD_REFACTORING_RENAME, REFACTOR_RENAME, _.partial(_handleRefactor, REFACTOR_RENAME));
subMenu.addMenuItem(REFACTOR_RENAME);
Menus.getMenu(menuLocation).addMenuItem(REFACTOR_RENAME, KeyboardPrefs.renameIdentifier);
// Extract to Variable
CommandManager.register(Strings.CMD_EXTRACTTO_VARIABLE, EXTRACTTO_VARIABLE, _.partial(_handleRefactor, EXTRACTTO_VARIABLE));
subMenu.addMenuItem(EXTRACTTO_VARIABLE);
Menus.getMenu(menuLocation).addMenuItem(EXTRACTTO_VARIABLE, KeyboardPrefs.extractToVariable);
// Extract to Function
CommandManager.register(Strings.CMD_EXTRACTTO_FUNCTION, EXTRACTTO_FUNCTION, _.partial(_handleRefactor, EXTRACTTO_FUNCTION));
subMenu.addMenuItem(EXTRACTTO_FUNCTION);
Menus.getMenu(menuLocation).addMenuItem(EXTRACTTO_FUNCTION, KeyboardPrefs.extractToFunction);
// Wrap Selection
CommandManager.register(Strings.CMD_REFACTORING_TRY_CATCH, REFACTORWRAPINTRYCATCH, _.partial(_handleRefactor, REFACTORWRAPINTRYCATCH));
subMenu.addMenuItem(REFACTORWRAPINTRYCATCH);
Menus.getMenu(menuLocation).addMenuItem(REFACTORWRAPINTRYCATCH);
CommandManager.register(Strings.CMD_REFACTORING_CONDITION, REFACTORWRAPINCONDITION, _.partial(_handleRefactor, REFACTORWRAPINCONDITION));
subMenu.addMenuItem(REFACTORWRAPINCONDITION);
Menus.getMenu(menuLocation).addMenuItem(REFACTORWRAPINCONDITION);
CommandManager.register(Strings.CMD_REFACTORING_ARROW_FUNCTION, REFACTORCONVERTTOARROWFN, _.partial(_handleRefactor, REFACTORCONVERTTOARROWFN));
subMenu.addMenuItem(REFACTORCONVERTTOARROWFN);
Menus.getMenu(menuLocation).addMenuItem(REFACTORCONVERTTOARROWFN);
CommandManager.register(Strings.CMD_REFACTORING_GETTERS_SETTERS, REFACTORCREATEGETSET, _.partial(_handleRefactor, REFACTORCREATEGETSET));
subMenu.addMenuItem(REFACTORCREATEGETSET);
Menus.getMenu(menuLocation).addMenuItem(REFACTORCREATEGETSET);
}
});
});