Commit ce6c29a3 authored by Antoine Thevenet's avatar Antoine Thevenet
Browse files

Changed the process to format the new behavior text inserted in the editor ace

parent 6639e76b
......@@ -37,6 +37,7 @@ function aceContextMenuHandler() {
var booleanDisablePrimitive = disableMenuItems("behavior");
var booleanDisablePlugins = disableMenuItems("plugins");
var booleanDisableLoadprofile = disableMenuItems("loadprofile");
var booleanDisableBehavior = disableMenuItems("behaviors");
if (booleanDisablePrimitive == false) {
enableContextualSubmenu("NewStatementMenu");
......@@ -44,12 +45,15 @@ function aceContextMenuHandler() {
enableContextualSubmenu("parentTimerSubmenu");
enableContextualSubmenu("parentSampleSubmenu");
}
if (booleanDisablePlugins == false){
if (booleanDisablePlugins == false) {
enableContextualSubmenu("parentPluginSubmenu");
}
if(booleanDisableLoadprofile ==false){
if (booleanDisableLoadprofile == false) {
enableContextualSubmenu("parentLoadprofileSubmenu");
}
if (booleanDisableBehavior == false){
enableContextualSubmenu("parentBehaviorSubmenu");
}
}
......@@ -76,9 +80,9 @@ function disableMenuItems(tagType) {
cursorAbsolutePosition += arrayCompleteText[counterRowLine].length;
}
cursorAbsolutePosition += cursorPosition["column"];
//depanding on the tag type,
if (tagType == "behavior") {
//add to an array the <behavior and </behavior> positions
//the first value of behaviorBeginTagArray is shifted as the <behaviors> tag is taken into account
......@@ -104,7 +108,7 @@ function disableMenuItems(tagType) {
}
}
}
else if (tagType == "loadprofile"){
else if (tagType == "loadprofile") {
var loadprofileBeginTagArray = getMatchIndexes(completeText, "<loadprofile");
var loadprofileEndTagArray = getMatchIndexes(completeText, "</loadprofile>");
......@@ -114,6 +118,18 @@ function disableMenuItems(tagType) {
}
}
}
else if (tagType == "behaviors") {
var loadprofileBeginTagArray = getMatchIndexes(completeText, "<behaviors");
var loadprofileEndTagArray = getMatchIndexes(completeText, "</behaviors>");
for (var i = 0; i < loadprofileBeginTagArray.length; i++) {
if (cursorAbsolutePosition >= loadprofileBeginTagArray[i] && cursorAbsolutePosition <= loadprofileEndTagArray[i]) {
disable = false;
}
}
}
//We are inside the wanted tags, check if the cursor is between > and <
if (disable == false) {
......@@ -151,7 +167,7 @@ function disableMenuItems(tagType) {
else {
tagAfter = completeText.substring(openChevronPos + 1, openTagClose2);
}
if (tagType == "behavior") {
if (tagAfter == "control" || tagAfter == "sample" || tagAfter == "timer"
|| tagAfter == "if" || tagAfter == "while" || tagAfter == "preemption" || tagAfter == "nchoice"
......@@ -171,7 +187,7 @@ function disableMenuItems(tagType) {
disable = true;
}
}
else if (tagType == "loadprofile") {
if (tagAfter == "group" || tagAfter == "/loadprofile") {
disable = false;
......@@ -180,6 +196,14 @@ function disableMenuItems(tagType) {
disable = true;
}
}
else if (tagType == "behaviors") {
if (tagAfter == "behavior" || tagAfter == "/behaviors") {
disable = false;
}
else {
disable = true;
}
}
}
return disable;
......@@ -235,7 +259,7 @@ function enableContextualSubmenu(menuId) {
-------------------------------------------------------------------*/
/**
* This function adds the style class noHover to the statement submenu. This style class prevents the menu display
* This function adds the style class noHover to the statement submenu. This style class prevents the msubmenu to be clickable
*/
function clearContextMenu() {
var controlSubmenu = document.getElementById("parentControlSubmenu");
......@@ -244,6 +268,7 @@ function clearContextMenu() {
var statementSubmenu = document.getElementById("NewStatementMenu");
var pluginsSubmenu = document.getElementById("parentPluginSubmenu");
var loadprofileSubmenu = document.getElementById("parentLoadprofileSubmenu");
var behaviorSubmenu = document.getElementById("parentBehaviorSubmenu");
if (controlSubmenu.classList.contains("noHover") == false) {
controlSubmenu.classList.add("noHover");
......@@ -263,6 +288,10 @@ function clearContextMenu() {
if (loadprofileSubmenu.classList.contains("noHover") == false) {
loadprofileSubmenu.classList.add("noHover");
}
if (behaviorSubmenu.classList.contains("noHover") == false) {
behaviorSubmenu.classList.add("noHover");
}
}
......
......@@ -65,8 +65,6 @@ function addPluginEditor(pluginName, pluginId) {
}
}
/**
* This function inserts automatically the retrieved plugin text at a valid position.
* It will be placed before the tag </plugins>
......@@ -82,20 +80,6 @@ function insertPlugin(pluginContent) {
//insert the formatted xml
var Range = ace.require('ace/range').Range;
editor.session.replace(new Range(cursorPosition["row"], cursorPosition["column"], cursorPosition["row"], cursorPosition["column"]), xmlToInsert);
// var lineCount = 0;
// var responseInserted = false;
// editorLineNumber = editor.session.getLength();
// for (lineCount; lineCount < editorLineNumber && responseInserted == false; lineCount++) {
// var lineContent = editor.session.getLine(lineCount);
// //handles xml file written on a single line
// if (lineContent.includes('</plugins>')) {
// var newLine = formatPluginInsertion(lineContent, pluginContent);
// var Range = ace.require('ace/range').Range;
// editor.session.replace(new Range(lineCount, 0, lineCount, Number.MAX_VALUE), newLine);
// responseInserted = true;
// }
// }
}
/*------------------------------------------------------------------
......@@ -243,15 +227,17 @@ retrieveTest = function retrieveTest(pluginName, testName, cache) {
return retrievedJson;
}
/*------------------------------------------------------------------
-----------------------------Choice-------------------------------
-------------------------------------------------------------------*/
function insertChoice() {
var editor = ace.edit(getActiveEditor());
var cursorPosition = editor.getCursorPosition();
var xmlToInsert = "<nchoice>\n\t<choice proba=\"1\">\n\t</choice><choice proba=\"1\">\n\t</choice>\n</nchoice>";
xmlToInsert = formatXml(xmlToInsert);
//delete the \n added by format xml
xmlToInsert = xmlToInsert.substring(0, xmlToInsert.length - 1);
//format the insertion
var cursorPositionToInsert = [cursorPosition["row"], cursorPosition["column"]]
xmlToInsert = formatInsertion(xmlToInsert, cursorPositionToInsert);
......@@ -265,79 +251,33 @@ function insertChoice() {
-------------------------------------------------------------------*/
function insertBehavior() {
var edit = ace.edit(getActiveEditor());
var completeText = edit.getValue();
var editor = ace.edit(getActiveEditor());
var cursorPosition = editor.getCursorPosition();
// var completeText = edit.getValue();
var behaviorId = chooseBehaviorId();
//create the text that will be inserted
var xmlBegin = "<behavior id=\"" + behaviorId + "\">";
var xmlEnd = "</behavior>";
var xml = xmlBegin + xmlEnd;
var positionToInsert = positionInsertBehavior("</behaviors", completeText);
// xml = addTabXml(xml, positionToInsert, completeText);
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);
}
/**
*This function is used to know where a behavior or a plugin tag must be inserted
*@param {String} parentEndTag - a tag like /behaviors or /plugins
*@param {String} completeText - what is written on the ace editor
*@returns {Array} positionToInsert - An array of two elements:
*- the column where the primitive will be inserted
*- the row where the primitive will be inserted
*/
function positionInsertBehavior(parentEndTag, completeText) {
//number of character before reaching the cursor position
var cursorColumn = 0;
//counter
var counterRowLine = 0;
var counterRowChevron = 0;
//Split the text twice: once to keep the cursor position, and once to browse the text
var arraySplitChevron = completeText.split(">");
var arraySplitLine = completeText.split("\n");
for (var i = 0; i < arraySplitChevron.length; i++) {
if (arraySplitChevron[i].includes("<")) {
arraySplitChevron[i] += ">";
}
}
for (var i = 0; i < arraySplitLine.length; i++) {
arraySplitLine[i] += "\n";
}
//retrieve the position of the parentEndTag
var endTagReached = false;
var i = 0;
while (endTagReached == false) {
if (arraySplitChevron[i].includes(parentEndTag)) {
endTagReached = true;
}
else {
counterRowChevron += arraySplitChevron[i].length;
i++;
}
}
//Converts this position to a valid one for the editor
var line = 0;
while (counterRowLine < counterRowChevron) {
counterRowLine += arraySplitLine[line].length;
line++;
}
var xmlToInsert = xmlBegin + xmlEnd;
xmlToInsert = formatXml(xmlToInsert);
//delete the \n added by format xml
xmlToInsert = xmlToInsert.substring(0, xmlToInsert.length - 1);
//format the insertion
var cursorPositionToInsert = [cursorPosition["row"], cursorPosition["column"]]
xmlToInsert = formatInsertion(xmlToInsert, cursorPositionToInsert);
cursorColumn = arraySplitLine[line - 1].length - (counterRowLine - counterRowChevron);
var positionToInsert = [cursorColumn, line - 1]
return positionToInsert;
var Range = ace.require('ace/range').Range;
editor.session.replace(new Range(cursorPosition["row"], cursorPosition["column"], cursorPosition["row"], cursorPosition["column"]), xmlToInsert);
}
/*------------------------------------------------------------------
-----------------------------Loadprofile-------------------------------
-------------------------------------------------------------------*/
function insertLoadprofile(behaviorName) {
var editor = ace.edit(getActiveEditor());
var cursorPosition = editor.getCursorPosition();
//build the text to insert
var groupBegin = "<group behavior=\"" + behaviorName + "\" forceStop=\"true\">" + "\n";
var rampBegin = "\t" + "<ramp style=\"line\">" + "\n";
......@@ -355,7 +295,7 @@ function insertLoadprofile(behaviorName) {
//insert the formatted xml
var Range = ace.require('ace/range').Range;
editor.session.replace(new Range(cursorPosition["row"], cursorPosition["column"], cursorPosition["row"], cursorPosition["column"]), xmlToInsert);
}
/*------------------------------------------------------------------
------------------------------Shaping-------------------------------
......@@ -368,46 +308,6 @@ function removeAceContextMenu() {
contextMenuAceEditor.visibility = "hidden";
}
/**
* This function formats the retrieved plugin.xml content. For that, it checks the previous line
* @param {String}lineContent - The editor's line containing </plugin>
* @param {String}pluginContent - The text that was retrieved from the plugin.xml file
*/
function formatPluginInsertion(lineContent, pluginContent) {
var formattedPluginContent = pluginContent;
var tabCount = (lineContent.match(/\t/g) || []).length;
var tabToInsert = tabCount + 1;
var insertedCharacter = 0;
//insert the tabs at beginning
for (var i = 0; i < tabToInsert; i++) {
formattedPluginContent = "\t" + formattedPluginContent;
insertedCharacter += 1;
}
//format plugin.xml content
var pluginCharIndex = pluginContent.indexOf("\n");
while (pluginCharIndex != -1) {
var count = 0;
while (count < tabToInsert) {
formattedPluginContent = formattedPluginContent.slice(0, pluginCharIndex + 1 + insertedCharacter)
+ "\t" + formattedPluginContent.slice(pluginCharIndex + 1 + insertedCharacter);
count++;
insertedCharacter += 1;
}
pluginCharIndex = pluginContent.indexOf("\n", pluginCharIndex + 1);
}
//format plugin insertion
var index = lineContent.indexOf('</plugins>');
var firstHalf = lineContent.slice(0, index);
var secondHalf = lineContent.slice(index);
var newLine = firstHalf.concat('\n', formattedPluginContent);
newLine = newLine.concat('\n\t\t', secondHalf);
return newLine;
}
/**
*This function adds tabs to a formated xml String by checking the previous xml tags
*@param {String} xml - The formatted xml String
......@@ -485,9 +385,9 @@ function countTabulation(arrayCompleteText, cursorRow) {
charIndex++;
}
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") || row.includes("<plugins>")) {
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;
......
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