Skip to content
This repository has been archived by the owner on Sep 6, 2021. It is now read-only.

JavaScript Refactoring - Rename Identifier, Wrap selection, Convert to arrow function, Create Getters & Setters #13965

Merged
merged 16 commits into from
Dec 20, 2017
Merged
Prev Previous commit
Next Next commit
Code clean up
  • Loading branch information
Naveen Choudhary committed Dec 12, 2017
commit 756fb9b9448bdf7b6528436c5d5986faba14422b
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ define(function (require, exports, module) {

if (editor.getSelections().length > 1) {
editor.displayErrorMessageAtCursor("Rename doesn't work in case of multicursor");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This string needs to be externalized. And correct the wording as well.

return;
}
initializeSession(editor);

Expand Down
98 changes: 47 additions & 51 deletions src/extensions/default/JavaScriptRefactoring/WrapSelection.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,90 +24,86 @@
define(function (require, exports, module) {
"use strict";

var EditorManager = brackets.getModule("editor/EditorManager"),
ScopeManager = brackets.getModule("JSUtils/ScopeManager"),
Session = brackets.getModule("JSUtils/Session"),
MessageIds = brackets.getModule("JSUtils/MessageIds");


var session = null; // object that encapsulates the current session state
var EditorManager = brackets.getModule("editor/EditorManager");

// Removes the leading and trailing spaces from selection and the trailing semicolons
function normalizeSelection() {
var selection = this.editor.getSelection(),
text = this.editor.getSelectedText(),
trimmedText;

// Remove leading spaces
trimmedText = text.trim();
function _getPositionOfSelectedText() {
var editor = EditorManager.getActiveEditor(),
selection = editor.getSelection();

if (editor.getSelections().length > 1) {
editor.displayErrorMessageAtCursor("Wrap Selection doesn't work in case of multicursor");
return;
}

var start = editor.indexFromPos(selection.start),
end = editor.indexFromPos(selection.end),
startPos = editor._codeMirror.posFromIndex(start),
endPos = editor._codeMirror.posFromIndex(end);

};
return {
start: startPos,
end: endPos
};
}

function getTextWrapedInTryCatch(text) {
function _getTextWrapedInTryCatch(text) {
var trytext = "try {\n",
catchText = "\n} catch (e) {\nconsole.log(e);\n}";
var formattedText = trytext + text + catchText;
catchText = "\n} catch (e) {\nconsole.log(e.message);\n}",
formattedText = trytext + text + catchText;

return formattedText;
}

function wrapInTryCatch() {
var editor = EditorManager.getActiveEditor(),
selection = editor.getSelection(),
text = editor.getSelectedText();


if (editor.getSelections().length > 1) {
editor.displayErrorMessageAtCursor("Wrap in try catch doesn't work in case of multicursor");
var editor = EditorManager.getActiveEditor(),
newText = _getTextWrapedInTryCatch(editor.getSelectedText().trim()),
pos = _getPositionOfSelectedText();

if (!pos) {
return;
}

var newText = getTextWrapedInTryCatch(text.trim()),
doc = editor.document,
start = editor.indexFromPos(selection.start),
end = editor.indexFromPos(selection.end),
startPos = editor._codeMirror.posFromIndex(start),
endPos = editor._codeMirror.posFromIndex(end);

doc.replaceRange(newText, startPos, endPos);
editor.document.replaceRange(newText, pos.start, pos.end);

var startLine = startPos.line,
endLine = startLine + newText.split("\n").length;
var startLine = pos.start.line,
endLine = startLine + newText.split("\n").length;

for (var i = startLine + 1; i < endLine; i++) {
editor._codeMirror.indentLine(i);
}

//Place cursor or selection
}

function getTextWrapedInCondition(text) {
function _getTextWrapedInCondition(text) {
var ifText = "if () {\n",
closeIf = "\n}";
var formattedText = ifText + text + closeIf;
closeIf = "\n}",
formattedText = ifText + text + closeIf;

return formattedText;
}


function wrapInCondition() {

var editor = EditorManager.getActiveEditor(),
selection = editor.getSelection(),
text = editor.getSelectedText();
newText = _getTextWrapedInCondition(editor.getSelectedText().trim()),
pos = _getPositionOfSelectedText();

if (editor.getSelections().length > 1) {
editor.displayErrorMessageAtCursor("Wrap in condition doesn't work in case of multicursor");
if (!pos) {
return;
}

var newText = getTextWrapedInCondition(text.trim()),
doc = editor.document,
start = editor.indexFromPos(selection.start),
end = editor.indexFromPos(selection.end),
startPos = editor._codeMirror.posFromIndex(start),
endPos = editor._codeMirror.posFromIndex(end);

doc.replaceRange(newText, startPos, endPos);
editor.document.replaceRange(newText, pos.start, pos.end);

var startLine = startPos.line,
var startLine = pos.start.line,
endLine = startLine + newText.split("\n").length;
for (var i = startLine + 1; i < endLine; i++) {
editor._codeMirror.indentLine(i);
}

//Place cursor at if
}

exports.wrapInTryCatch = wrapInTryCatch;
Expand Down
2 changes: 1 addition & 1 deletion src/extensions/default/JavaScriptRefactoring/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ define(function (require, exports, module) {

CommandManager.register("Wrap in Condition", refactorWrapInCondition, WrapSelection.wrapInCondition);

var menuLocation = Menus.AppMenuBar.NAVIGATE_MENU;
var menuLocation = Menus.AppMenuBar.EDIT_MENU;

var keysRename = [
{key: "Ctrl-R", platform:"mac"}, // don't translate to Cmd-R on mac
Expand Down
6 changes: 0 additions & 6 deletions src/extensions/default/JavaScriptRefactoring/package.json

This file was deleted.