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
Addressed review comments, Fixed bug in convert to arrow function, mi…
…nor change in templates.json
  • Loading branch information
Naveen Choudhary committed Dec 19, 2017
commit d8e0060087cea85def1a9efde96d462921b4891b
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ define(function (require, exports, module) {
/**
* This will add template at given position/selection
*
* @param {string} template - name of templated defined in templates.json
* @param {string} template - name of the template defined in templates.json
* @param {Array} args- Check all arguments that exist in defined templated pass all that args as array
* @param {{line: number, ch: number}} rangeToReplace - Range which we want to rreplace
* @param {{line: number, ch: number}} rangeToReplace - Range which we want to replace
* @param {string} subTemplate - If template written under some category
*/
RefactoringSession.prototype.replaceTextFromTemplate = function (template, args, rangeToReplace, subTemplate) {
Expand Down
48 changes: 27 additions & 21 deletions src/extensions/default/JavaScriptRefactoring/RenameIdentifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ define(function (require, exports, module) {
//Post message to tern node domain that will request tern server to find refs
function getRefs(fileInfo, offset) {
ScopeManager.postMessage({
type: "getRefs",
type: MessageIds.TERN_REFS,
fileInfo: fileInfo,
offset: offset
});
Expand All @@ -54,14 +54,16 @@ define(function (require, exports, module) {

//Create info required to find reference
function requestFindRefs(session, document, offset) {
if (!document || !session) {
return;
}
var path = document.file.fullPath,
fileInfo = {
type: MessageIds.TERN_FILE_INFO_TYPE_FULL,
name: path,
offsetLines: 0,
text: ScopeManager.filterText(session.getJavascriptText())
};

var ternPromise = getRefs(fileInfo, offset);

return {promise: ternPromise};
Expand All @@ -72,6 +74,10 @@ define(function (require, exports, module) {
var editor = EditorManager.getActiveEditor(),
Copy link
Contributor

Choose a reason for hiding this comment

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

Are you going to allow rename inside quick edit session?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. Rename is working great in quick edit as well for local variables. Renaming function calls could be a problem. According to me we should allow rename in quick edit as well. Please let me know if want me to disable rename in quick edit.

Copy link
Contributor

Choose a reason for hiding this comment

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

OK. I think it is better to disable rename for quick edit. There are chances that once you invoke a quick edit on a function name, we only show the contents of the function. But there may be variables whose scope is outside the function and I am not sure how you are going to show the feedback for renaming variable references which are not shown inside quick edit.

Copy link
Contributor

Choose a reason for hiding this comment

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

Let us do this. I will merge this PR. But please raise another PR addressing this concern.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure @nethip I will do that. Thanks for suggestion.

offset, handleFindRefs;

if (!editor) {
return;
}

if (editor.getSelections().length > 1) {
editor.displayErrorMessageAtCursor(Strings.ERROR_RENAME_MULTICURSOR);
return;
Expand All @@ -85,6 +91,24 @@ define(function (require, exports, module) {

var result = new $.Deferred();

function isInSameFile(obj) {
return (obj && obj.file === refsResp.file);
}

/**
* Check if references are in this file only
* If yes then select all references
*/
function handleFindRefs (refsResp) {
if (refsResp && refsResp.references && refsResp.references.refs) {
if (refsResp.references.type === "local") {
EditorManager.getActiveEditor().setSelections(refsResp.references.refs, false, {scroll: false});
} else {
EditorManager.getActiveEditor().setSelections(refsResp.references.refs.filter(isInSameFile));
}
}
}

/**
* Make a find ref request.
* @param {Session} session - the session
Expand All @@ -93,31 +117,13 @@ define(function (require, exports, module) {
function requestFindReferences(session, offset) {
var response = requestFindRefs(session, session.editor.document, offset);

if (response || response.hasOwnProperty("promise")) {
if (response && response.hasOwnProperty("promise")) {
response.promise.done(handleFindRefs).fail(function () {
result.reject();
});
}
}

/**
* Check if references are in this file only
* If yes then select all references
*/
handleFindRefs = function (refsResp) {
function isInSameFile(obj) {
return (obj && obj.file === refsResp.file);
}

if (refsResp && refsResp.references && refsResp.references.refs) {
if (refsResp.references.type === "local") {
EditorManager.getActiveEditor().setSelections(refsResp.references.refs);
} else {
EditorManager.getActiveEditor().setSelections(refsResp.references.refs.filter(isInSameFile));
}
}
};

offset = session.getOffset();
requestFindReferences(session, offset);

Expand Down
4 changes: 2 additions & 2 deletions src/extensions/default/JavaScriptRefactoring/Templates.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"wrapCondition": "if (x) {\n${body}\n}",
"wrapCondition": "if (Condition) {\n${body}\n}",

"arrowFunction": {
"oneParamOneStament": "${params} => ${statement}",
Expand All @@ -8,7 +8,7 @@
"manyParamManyStament": "(${params}) => "
},

"tryCatch": "try {\n${body}\n} catch (e) {\nconsole.log(e.message);\n}",
"tryCatch": "try {\n${body}\n} catch (e) {\n\/\/Catch Statement\n}",

"gettersSetters": "\nget ${getName}() {\nreturn this.${tokenName};\n},\n\nset ${setName}(val) {\nthis.${tokenName} = val;\n}"
}
41 changes: 30 additions & 11 deletions src/extensions/default/JavaScriptRefactoring/WrapSelection.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ define(function (require, exports, module) {
/**
* Initialize session
*/
function initializeRefactoringSession() {
current = new RefactoringSession(EditorManager.getActiveEditor());
function initializeRefactoringSession(editor) {
current = new RefactoringSession(editor);
}

/**
Expand All @@ -62,7 +62,11 @@ define(function (require, exports, module) {
* @param {string} err- error message if we can't wrap selected code
*/
function _wrapSelectedStatements (wrapperName, err) {
initializeRefactoringSession();
var editor = EditorManager.getActiveEditor();
if (!editor) {
return;
}
initializeRefactoringSession(editor);

var startIndex = current.startIndex,
endIndex = current.endIndex,
Expand Down Expand Up @@ -96,28 +100,34 @@ define(function (require, exports, module) {
});

if (wrapperName === TRY_CATCH) {
current.editor.setSelection({"line": pos.start.line, "ch": pos.start.ch + 5});
var cursorLine = current.editor.getSelection().start.line - 1,
startCursorCh = current.document.getLine(cursorLine).indexOf("\/\/"),
endCursorCh = current.document.getLine(cursorLine).length;

current.editor.setSelection({"line": cursorLine, "ch": startCursorCh}, {"line": cursorLine, "ch": endCursorCh});
} else if (wrapperName === WRAP_IN_CONDITION) {
current.editor.setSelection({"line": pos.start.line, "ch": pos.start.ch + 4}, {"line": pos.start.line, "ch": pos.start.ch + 5});
current.editor.setSelection({"line": pos.start.line, "ch": pos.start.ch + 4}, {"line": pos.start.line, "ch": pos.start.ch + 13});
}
}


//Wrap selected statements in try catch block
function wrapInTryCatch() {
initializeRefactoringSession();
_wrapSelectedStatements(TRY_CATCH, Strings.ERROR_TRY_CATCH);
}

//Wrap selected statements in try condition
function wrapInCondition() {
initializeRefactoringSession();
_wrapSelectedStatements(WRAP_IN_CONDITION, Strings.ERROR_WRAP_IN_CONDITION);
}

//Convert function to arrow function
function convertToArrowFunction() {
initializeRefactoringSession();
var editor = EditorManager.getActiveEditor();
if (!editor) {
return;
}
initializeRefactoringSession(editor);
//Handle when there is no selected line
var funcExprNode = current.findSurroundASTNode(current.ast, {start: current.startIndex}, ["FunctionExpression"]);

Expand All @@ -127,8 +137,13 @@ define(function (require, exports, module) {
}
var noOfStatements = funcExprNode.body.body.length,
selectedText = current.text.substr(funcExprNode.start, funcExprNode.end - funcExprNode.start),
param = current.getParamsOfFunction(funcExprNode.start, funcExprNode.end, selectedText),
loc = {
param = [];

funcExprNode.params.forEach(function (item) {
param.push(item.name);
});

var loc = {
"fullFunctionScope": {
start: funcExprNode.start,
end: funcExprNode.end
Expand Down Expand Up @@ -178,7 +193,11 @@ define(function (require, exports, module) {

// Create gtteres and setters for a property
function createGettersAndSetters() {
initializeRefactoringSession();
var editor = EditorManager.getActiveEditor();
if (!editor) {
return;
}
initializeRefactoringSession(editor);

var startIndex = current.startIndex,
endIndex = current.endIndex,
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 @@ -35,7 +35,7 @@ define(function (require, exports, module) {

// 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_REFACTORIG
description: Strings.DESCRIPTION_CODE_REFACTORING
});


Expand Down
2 changes: 1 addition & 1 deletion src/nls/root/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ define({
"CMD_REFACTORING_CONDITION" : "Wrap in Condition",
"CMD_REFACTORING_GETTERS_SETTERS" : "Create Getters Setters",
"CMD_REFACTORING_ARROW_FUNCTION" : "Convert to Arrow Function",
"DESCRIPTION_CODE_REFACTORIG" : "Enable/disable JavaScript Code Refactoring",
"DESCRIPTION_CODE_REFACTORING" : "Enable/disable JavaScript Code Refactoring",
"ERROR_TRY_CATCH" : "Select valid code to wrap in a Try-catch block",
"ERROR_WRAP_IN_CONDITION" : "Select valid code to wrap in a Condition block",
"ERROR_ARROW_FUNCTION" : "Place the cursor inside a function expression",
Expand Down