Commit 0ad70bc5 authored by Antoine Thevenet's avatar Antoine Thevenet
Browse files

Changed the process to format the content inserted in the editor ace

parent b2e45288
......@@ -102,6 +102,7 @@ function insertPrimitive(primitiveType, pluginName, primitiveName, idPlugin) {
var completeText = editor.getValue();
var cache = localCache.get("pluginInfo");
var retrievedJson = retrievePrimitive(lowerCasePrimitiveType, pluginName, primitiveName, cache);
var cursorPosition = editor.getCursorPosition();
//transform the json object to a string
var xmlConverted = "";
......@@ -110,15 +111,6 @@ function insertPrimitive(primitiveType, pluginName, primitiveName, idPlugin) {
//xmlConverted += JSONtoXML(temp);
xmlConverted += temp;
}
//obtain the position where the text will be inserted
var resultPositionToInsertPrimitive = positionToInsert();
var cursorPositionToInsert = resultPositionToInsertPrimitive[0];
var insertLineBefore = resultPositionToInsertPrimitive[1];
var insertLineAfter = resultPositionToInsertPrimitive[2];
var oneMoreTab = resultPositionToInsertPrimitive[3];
editor.moveCursorTo(cursorPositionToInsert[1], cursorPositionToInsert[0]);
//format the retrieved text
var beginXml = "";
var primitiveTypeSingular = lowerCasePrimitiveType.substring(0, lowerCasePrimitiveType.length - 1);
......@@ -128,17 +120,14 @@ function insertPrimitive(primitiveType, pluginName, primitiveName, idPlugin) {
beginXml += "<params>";
endXml = "</params>" + endXml;
}
var completeXml = formatXml(beginXml + xmlConverted + endXml);
var xmlToInsert = formatXml(beginXml + xmlConverted + endXml);
//delete the "\n added by format xml
completeXml = completeXml.substring(0, completeXml.length - 1);
// completeXml = addTabXml(completeXml, cursorPositionToInsert, completeText);
completeXml = formatPreviousLine(completeXml, cursorPositionToInsert, insertLineAfter, oneMoreTab);
if (insertLineBefore == true) {
completeXml = "\n" + completeXml;
}
xmlToInsert = xmlToInsert.substring(0, xmlToInsert.length - 1);
var cursorPositionToInsert = [cursorPosition["row"], cursorPosition["column"]]
xmlToInsert = formatInsertion(xmlToInsert, cursorPositionToInsert);
var Range = ace.require('ace/range').Range;
editor.session.replace(new Range(cursorPositionToInsert[1], cursorPositionToInsert[0], cursorPositionToInsert[1], cursorPositionToInsert[0]), completeXml);
editor.session.replace(new Range(cursorPosition["row"], cursorPosition["column"], cursorPosition["row"], cursorPosition["column"]), xmlToInsert);
}
......@@ -313,7 +302,7 @@ function insertStatement(statementType, pluginName, testName, pluginId) {
var xmlConverted = "";
var retrievedJson = retrieveTest(pluginName, testName, cache);
for (var i = 0; i < retrievedJson.length; i++){
for (var i = 0; i < retrievedJson.length; i++) {
var temp = "<param name=\"" + retrievedJson[i] + "\" value=\"\"></param>" + "\n";
xmlConverted += temp;
}
......@@ -348,7 +337,7 @@ function insertStatement(statementType, pluginName, testName, pluginId) {
var Range = ace.require('ace/range').Range;
xml = formatXml(xml);
//xml = addTabXml(xml, cursorPositionToInsert, completeText);
xml = formatPreviousLine(xml, cursorPositionToInsert);
xml = formatInsertion(xml, cursorPositionToInsert);
editor.session.replace(new Range(cursorPositionToInsert[1], cursorPositionToInsert[0], cursorPositionToInsert[1], cursorPositionToInsert[0]), xml);
}
......@@ -386,7 +375,7 @@ function insertChoice() {
var answerPositionToInsertPrimitive = positionToInsert();
var cursorPositionToInsert = answerPositionToInsertPrimitive[0];
// xml = addTabXml(xml, cursorPositionToInsert, completeText);
xml = formatPreviousLine(xml, cursorPositionToInsert);
xml = formatInsertion(xml, cursorPositionToInsert);
var Range = ace.require('ace/range').Range;
editor.session.replace(new Range(cursorPositionToInsert[1], cursorPositionToInsert[0], cursorPositionToInsert[1], cursorPositionToInsert[0]), xml);
......@@ -404,7 +393,7 @@ function insertBehavior() {
var xml = xmlBegin + xmlEnd;
var positionToInsert = positionInsertBehavior("</behaviors", completeText);
// xml = addTabXml(xml, positionToInsert, completeText);
xml = formatPreviousLine(xml, positionToInsert, false, false);
xml = formatInsertion(xml, positionToInsert, false, false);
var completeXml = "\n" + xml;
var Range = ace.require('ace/range').Range;
edit.session.replace(new Range(positionToInsert[1], positionToInsert[0], positionToInsert[1], positionToInsert[0]), completeXml);
......@@ -478,7 +467,7 @@ function insertLoadprofile(behaviorName) {
var positionToInsert = positionInsertBehavior("</loadprofile", completeText);
// xml = addTabXml(xml, positionToInsert, completeText);
xml = formatPreviousLine(xml, positionToInsert, false, true);
xml = formatInsertion(xml, positionToInsert, false, true);
var completeXml = "\n" + xml;
var Range = ace.require('ace/range').Range;
edit.session.replace(new Range(positionToInsert[1], positionToInsert[0], positionToInsert[1], positionToInsert[0]), completeXml);
......@@ -619,42 +608,65 @@ addTabXml = function addTabXml(xml, cursorPositionToInsert, completeText) {
*@param {Array} cursorPositionInsert - The row and column where the xml must be inserted
*@returns {String} formatedXml - The xml with the inserted tabulation
*/
function formatPreviousLine(xml, cursorPositionToInsert, insertLineAfter, oneMoreTab) {
function formatInsertion(xml, cursorPositionToInsert) {
var formattedXml = "";
var cursorRow = cursorPositionToInsert[0];
var cursorColumn = cursorPositionToInsert[1];
//split the xml by its line break
var editor = ace.edit(getActiveEditor());
var completeText = editor.getValue();
if (insertLineAfter == true) {
xml += "\n";
}
var arrayXml = xml.split("\n");
for (var i = 0; i < arrayXml.length - 1; i++) {
arrayXml[i] += "\n";
}
//obtain the absolute position of the cursor
var cursorAbsolutePosition = 0;
//split the array according to "\n"
var arrayCompleteText = completeText.split("\n");
for (var i = 0; i < arrayCompleteText.length; i++) {
arrayCompleteText[i] += "\n";
}
for (var i = 0; i < cursorPositionToInsert[1]; i++) {
cursorAbsolutePosition += arrayCompleteText[i].length;
var tabCount = countTabulation(arrayCompleteText, cursorRow);
//add tabs to each line
for (var i = 0; i < arrayXml.length; i++) {
for (var j = 0; j < tabCount; j++) {
arrayXml[i] = "\t" + arrayXml[i];
}
formattedXml += arrayXml[i];
}
cursorAbsolutePosition += cursorPositionToInsert[0];
formattedXml = formatAdjacentLines(arrayCompleteText, cursorRow, cursorColumn, formattedXml, tabCount);
return formattedXml;
}
/** This function checks the the number of tabs on the current or previous line
* It also checks the previous tag line to determine if an extra tab is required
* @param arrayCompleteText {Array} - the text contained in the editor divided by line breaks
* @param cursorRow {number} - the cursor row number
**/
function countTabulation(arrayCompleteText, cursorRow) {
var validRow = false;
var count = cursorRow;
var tabCount = 0;
//check if the current row is empty. Else, we use the previous row to check if we must add a tab
var row = arrayCompleteText[count];
for (count; count >= 0 && validRow == false; count--) {
row = arrayCompleteText[count];
if (row.includes("<") && row.includes(">")) {
validRow = true;
}
}
//count the number of tabs on the chosen row
var stopTabCount = false;
var charIndex = 0;
var spaceCount = 0;
var tabCount = 0;
while (stopTabCount != true && charIndex < arrayCompleteText[cursorPositionToInsert[1]].length) {
if (arrayCompleteText[cursorPositionToInsert[1]][charIndex] == "\t") {
while (stopTabCount != true && charIndex < row.indexOf("<")) {
if (row[charIndex] == "\t") {
tabCount++;
spaceCount = 0;
}
else if (arrayCompleteText[cursorPositionToInsert[1]][charIndex] == " ") {
else if (row[charIndex] == " ") {
spaceCount++;
}
else {
......@@ -662,21 +674,60 @@ function formatPreviousLine(xml, cursorPositionToInsert, insertLineAfter, oneMor
}
if (spaceCount == 4) {
spaceCount = 0;
tabCount++
tabCount++;
}
charIndex++;
}
if (oneMoreTab == true) {
if (row.includes("scenario") || row.includes("behaviors") || row.includes("plugins") ||
row.includes("loadprofile") || row.includes("behavior") || row.includes("then") ||
row.includes("else") || row.includes("choice")) {
tabCount++;
}
return tabCount;
}
//add tabs to each line
for (var i = 0; i < arrayXml.length; i++) {
for (var j = 0; j < tabCount; j++) {
arrayXml[i] = "\t" + arrayXml[i];
/**
* According to the content on the line where the user's cursor is, this function will insert lines before or after
* the inserted text and add the required tabulations
* @param arrayCompleteText {Array} - the text contained in the editor divided by line breaks
* @param cursorRow {number} - the cursor row number
* @param cursorColumn {number} - the cursor column number
* @param formattedXml {String} - the text to insert
* @param tabCount {number} - the number of tabs to insert
*/
function formatAdjacentLines(arrayCompleteText, cursorRow, cursorColumn, formattedXml, tabCount) {
//check if we need to insert \n before or after the inserted text
var row = arrayCompleteText[cursorRow];
var editor = ace.edit(getActiveEditor());
if ((row.includes("<") || row.includes(">")) && cursorColumn <= row.indexOf("<")) { //right click before <
formattedXml = formattedXml + "\n";
var tabAfterCursor = 0;
//remove tabs before what is to be inserted
for (var i = 0; i < row.indexOf("<") - cursorColumn; i++) {
tabAfterCursor++;
}
formattedXml += arrayXml[i];
//add tabs after
for (var i = 0; i < row.indexOf("<") - tabAfterCursor; i++) {
formattedXml = formattedXml + "\t";
formattedXml = formattedXml.substring(1);
}
}
else if ((row.includes("<") || row.includes(">")) && cursorColumn <= row.lastIndexOf("<")) {
formattedXml = formattedXml + "\n";
for (var i = 0; i < tabCount - 1; i++) {
formattedXml += "\t";
}
}
if ((row.includes("<") || row.includes(">")) && cursorColumn >= row.indexOf(">")) { //right click after >
formattedXml = "\n" + formattedXml;
}
if (row.indexOf("<") == -1 && row.indexOf(">") == -1) {
//delete the empty line
var Range = ace.require('ace/range').Range;
editor.session.replace(new Range(cursorRow, 0, cursorRow, Number.MAX_VALUE), "");;
}
return formattedXml;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment