From 55e7e481d4e88f42c761af4f52401d6a0074d032 Mon Sep 17 00:00:00 2001 From: Rahul Shirsat Date: Thu, 9 Sep 2021 16:04:59 +0530 Subject: [PATCH] Fixed an issue where collapse and expand arrows mismatch in case of nested IF. Fixes #6712 --- docs/en_US/release_notes_6_0.rst | 1 + .../addon/fold/pgadmin-sqlfoldcode.js | 43 ++++++++++++++++++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/docs/en_US/release_notes_6_0.rst b/docs/en_US/release_notes_6_0.rst index 7115c462c..611492f21 100644 --- a/docs/en_US/release_notes_6_0.rst +++ b/docs/en_US/release_notes_6_0.rst @@ -21,3 +21,4 @@ Housekeeping Bug fixes ********* +| `Issue #6712 `_ - Fixed an issue where collapse and expand arrows mismatch in case of nested IF. diff --git a/web/pgadmin/static/js/codemirror/addon/fold/pgadmin-sqlfoldcode.js b/web/pgadmin/static/js/codemirror/addon/fold/pgadmin-sqlfoldcode.js index 2ef304119..a79fc2a85 100644 --- a/web/pgadmin/static/js/codemirror/addon/fold/pgadmin-sqlfoldcode.js +++ b/web/pgadmin/static/js/codemirror/addon/fold/pgadmin-sqlfoldcode.js @@ -28,6 +28,7 @@ endTkn = tokenSet[tokenSetNo].end; while (at > 0) { var found = lineText.toUpperCase().lastIndexOf(startTkn, at); + found = checkStartTokenFoundOnEndToken(found, lineText.toUpperCase(), endTkn, startTkn); var startToken = startTkn; var endToken = endTkn; @@ -59,8 +60,10 @@ pos = 0; var whileloopvar = 0; while (whileloopvar < 1) { - var nextOpen = text.indexOf(startToken, pos), - nextClose = text.indexOf(endToken, pos); + var nextOpen = text.indexOf(startToken, pos); + nextOpen = checkStartTokenFoundOnEndToken(nextOpen, text, endToken, startToken); + + var nextClose = text.indexOf(endToken, pos); if (nextOpen < 0) nextOpen = text.length; if (nextClose < 0) nextClose = text.length; pos = Math.min(nextOpen, nextClose); @@ -83,6 +86,42 @@ }; }; + /** + * This function is responsible for finding whether the startToken is present + * in the endToken as well, to avoid mismatch of start and end points. + * e.g. In case of IF and END IF, IF is detected in both tokens, which creates + * confusion. The said function will resolve such issues. + * @function checkStartTokenFoundOnEndToken + * @returns {Number} - returns found + */ + function checkStartTokenFoundOnEndToken(found, text, endToken, startToken) { + if(found > 0) { + if(text.includes(endToken) + || !checkTokenMixedWithOtherAlphabets(text, startToken)) { + found = -1; + } + } + return found; + } + + /** + * This function is responsible for finding whether the startToken is mixed + * with other alphabets of the text. To avoid word like NOTIFY to be mistakenly treat as keyword. + * e.g. to avoid the IF detected as keyword in the word NOTIFY. + * Function also works with other tokens like LOOP, CASE, etc. + * @function checkTokenMixedWithOtherAlphabets + * @returns {Boolean} - returns true/false + */ + function checkTokenMixedWithOtherAlphabets(text, startToken) { + //this reg will check the token should be in format as - IF condition or IF(condition) + let reg = `\\b\\${startToken}\\s*\\(\\w*\\)(?!\\w)|\\b\\${startToken}\\(\\w*\\)(?!\\w)|\\b\\${startToken}\\s*(?!\\w)`; + let regex = RegExp(reg, 'g'); + if(regex.exec(text) !== null) { + return true; + } + return false; + } + CodeMirror.registerHelper('fold', 'sql', function(cm, start) { var fromToPos = pgadminKeywordRangeFinder(cm, start, [ {start: 'BEGIN', end:'END;'},