<commit_before>
angular.module('dataCollectorApp.codemirrorDirectives')
  .directive('codemirrorEl', function($parse, $timeout) {
    'use strict';
    return {
      restrict: 'A',
      priority: 2, // higher than ui-codemirror which is 1.
      compile: function compile() {
        var ctrlSpaceKey = false,
          cmPos = CodeMirror.Pos,
          cmpPos = CodeMirror.cmpPos,
          cls = "CodeMirror-EL-",
          cachedArgHints = null,
          activeArgHints = null,
          activeAutoComplete = false;

        if (angular.isUndefined(window.CodeMirror)) {
          throw new Error('codemirror-el needs CodeMirror to work.');
        }

        return function postLink(scope, iElement, iAttrs) {
          var dictionary = {},
            fieldPaths = [];

          // Register our custom Codemirror hint plugin.
          window.CodeMirror.registerHelper('hint', 'dictionaryHint', function(editor, cm, c) {
            var cur = editor.getCursor(), curLine = editor.getLine(cur.line);
            var start = cur.ch, end = start;

            while (end < curLine.length && /[\w:/$]+/.test(curLine.charAt(end))) {
              ++end;
            }
            while (start && /[\w:/$]+/.test(curLine.charAt(start - 1))) {
              --start;
            }
            var curWord = start != end && curLine.slice(start, end);
            var regex = new RegExp('^' + curWord, 'i');
            var completions =[];

            if(curWord || ctrlSpaceKey) {
              angular.forEach(dictionary.elConstantDefinitions, function(elConstantDefn) {
                if(!curWord || elConstantDefn.name.match(regex)) {
                  completions.push({
                    text: elConstantDefn.name,
                    displayText: elConstantDefn.name,
                    className: 'CodeMirror-EL-completion CodeMirror-EL-completion-constant',
                    data: elConstantDefn
                  });
                }
              });


              angular.forEach(dictionary.elFunctionDefinitions, function(elFunctionDefn) {
                if(!curWord || elFunctionDefn.name.match(regex)) {
                  completions.push({
                    text: elFunctionDefn.name + (elFunctionDefn.elFunctionArgumentDefinition.length ? '(' : '()'),
                    displayText: elFunctionDefn.name,
                    className: 'CodeMirror-EL-completion CodeMirror-EL-completion-fn',
                    data: elFunctionDefn
                  });
                }
              });

              angular.forEach(fieldPaths, function(fieldPath) {
                if(!curWord || fieldPath.match(regex)) {
                  completions.push({
                    text: fieldPath,
                    displayText: fieldPath,
                    className: 'CodeMirror-EL-completion CodeMirror-EL-completion-field-path',
                    data: fieldPath
                  });
                }
              });
            }

            completions = _.sortBy(completions, 'text');

            if(completions.length > 0 ) {
              activeAutoComplete = true;
              closeArgHints();
            } else {
              activeAutoComplete = false;
            }

            var obj = {
              list: completions,
              from: CodeMirror.Pos(cur.line, start),
              to: CodeMirror.Pos(cur.line, end)
            };

            var tooltip = null;
            CodeMirror.on(obj, "close", function() {
              remove(tooltip);
              activeAutoComplete = false;
            });

            CodeMirror.on(obj, "update", function() {
              remove(tooltip);
            });

            CodeMirror.on(obj, "select", function(cur, node) {
              remove(tooltip);
              var content = cur.data.description;
              if (content) {
                tooltip = makeTooltip(node.parentNode.getBoundingClientRect().right + window.pageXOffset,
                  node.getBoundingClientRect().top + window.pageYOffset, content);
                tooltip.className += " " + cls + "hint-doc";
              }
            });

            return obj;
          });


          // Check if the ui-codemirror directive is present.
          if (!iAttrs.hasOwnProperty('uiCodemirror') && iElement[0].tagName.toLowerCase() !== 'ui-codemirror') {
            throw new Error('The codemirror-el directive can only be used either ' +
            'on a ui-codemirror element or an element with the ui-codemirror attribute set.');
          }

          if (iAttrs.codemirrorEl) {
            scope.$watch('iAttrs.codemirrorEl', function() {
              dictionary = $parse(iAttrs.codemirrorEl)(scope);
            });
          }

          if (iAttrs.fieldPaths) {
            scope.$watch('iAttrs.fieldPaths', function() {
              fieldPaths = $parse(iAttrs.fieldPaths)(scope);
            });
          }

          scope.$on('fieldPathsUpdated', function(event, _fieldPaths) {
            fieldPaths = _fieldPaths;
          });

          // The ui-codemirror directive allows us to receive a reference to the Codemirror instance on demand.
          scope.$broadcast('CodeMirror', function(cm) {
            cm.on('change', function(instance, change) {
              if (change.origin !== 'complete') {
                instance.showHint({ hint: window.CodeMirror.hint.dictionaryHint, completeSingle: false });
              }
              $timeout(function() {});
            });

            cm.on('keyHandled', function(instance, name, event) {
              if(name === 'Ctrl-Space') {
                ctrlSpaceKey = true;
                instance.showHint({ hint: window.CodeMirror.hint.dictionaryHint, completeSingle: false });
                ctrlSpaceKey = false;
              }
            });

            cm.on('cursorActivity', function(cm) {
              updateArgHints(cm);
            });

            cm.on("blur", function(){
              closeArgHints();
            });

          });

          function elt(tagname, cls /*, ... elts*/) {
            var e = document.createElement(tagname);
            if (cls) {
              e.className = cls;
            }<commit_msg>SDC-276 [ui] Improve Expression Language widget, fixed CTRL+Space random issue<commit_after>
angular.module('dataCollectorApp.codemirrorDirectives')
  .directive('codemirrorEl', function($parse, $timeout) {
    'use strict';
    return {
      restrict: 'A',
      priority: 2, // higher than ui-codemirror which is 1.
      compile: function compile() {
        var cmPos = CodeMirror.Pos,
          cmpPos = CodeMirror.cmpPos,
          cls = "CodeMirror-EL-",
          cachedArgHints = null,
          activeArgHints = null,
          activeAutoComplete = false;

        if (angular.isUndefined(window.CodeMirror)) {
          throw new Error('codemirror-el needs CodeMirror to work.');
        }

        return function postLink(scope, iElement, iAttrs) {
          var dictionary = {},
            fieldPaths = [];

          // Register our custom Codemirror hint plugin.
          window.CodeMirror.registerHelper('hint', 'dictionaryHint', function(editor, options, c) {
            var cur = editor.getCursor(), curLine = editor.getLine(cur.line);
            var start = cur.ch, end = start;

            while (end < curLine.length && /[\w:/$]+/.test(curLine.charAt(end))) {
              ++end;
            }
            while (start && /[\w:/$]+/.test(curLine.charAt(start - 1))) {
              --start;
            }
            var curWord = start != end && curLine.slice(start, end);
            var regex = new RegExp('^' + curWord, 'i');
            var completions =[];

            if(curWord || options.ctrlSpaceKey) {
              angular.forEach(dictionary.elConstantDefinitions, function(elConstantDefn) {
                if(!curWord || elConstantDefn.name.match(regex)) {
                  completions.push({
                    text: elConstantDefn.name,
                    displayText: elConstantDefn.name,
                    className: 'CodeMirror-EL-completion CodeMirror-EL-completion-constant',
                    data: elConstantDefn
                  });
                }
              });


              angular.forEach(dictionary.elFunctionDefinitions, function(elFunctionDefn) {
                if(!curWord || elFunctionDefn.name.match(regex)) {
                  completions.push({
                    text: elFunctionDefn.name + (elFunctionDefn.elFunctionArgumentDefinition.length ? '(' : '()'),
                    displayText: elFunctionDefn.name,
                    className: 'CodeMirror-EL-completion CodeMirror-EL-completion-fn',
                    data: elFunctionDefn
                  });
                }
              });

              angular.forEach(fieldPaths, function(fieldPath) {
                if(!curWord || fieldPath.match(regex)) {
                  completions.push({
                    text: fieldPath,
                    displayText: fieldPath,
                    className: 'CodeMirror-EL-completion CodeMirror-EL-completion-field-path',
                    data: fieldPath
                  });
                }
              });
            }

            completions = _.sortBy(completions, 'text');

            if(completions.length > 0 ) {
              activeAutoComplete = true;
              closeArgHints();
            } else {
              activeAutoComplete = false;
            }

            var obj = {
              list: completions,
              from: CodeMirror.Pos(cur.line, start),
              to: CodeMirror.Pos(cur.line, end)
            };

            var tooltip = null;
            CodeMirror.on(obj, "close", function() {
              remove(tooltip);
              activeAutoComplete = false;
            });

            CodeMirror.on(obj, "update", function() {
              remove(tooltip);
            });

            CodeMirror.on(obj, "select", function(cur, node) {
              remove(tooltip);
              var content = cur.data.description;
              if (content) {
                tooltip = makeTooltip(node.parentNode.getBoundingClientRect().right + window.pageXOffset,
                  node.getBoundingClientRect().top + window.pageYOffset, content);
                tooltip.className += " " + cls + "hint-doc";
              }
            });

            return obj;
          });


          // Check if the ui-codemirror directive is present.
          if (!iAttrs.hasOwnProperty('uiCodemirror') && iElement[0].tagName.toLowerCase() !== 'ui-codemirror') {
            throw new Error('The codemirror-el directive can only be used either ' +
            'on a ui-codemirror element or an element with the ui-codemirror attribute set.');
          }

          if (iAttrs.codemirrorEl) {
            scope.$watch('iAttrs.codemirrorEl', function() {
              dictionary = $parse(iAttrs.codemirrorEl)(scope);
            });
          }

          if (iAttrs.fieldPaths) {
            scope.$watch('iAttrs.fieldPaths', function() {
              fieldPaths = $parse(iAttrs.fieldPaths)(scope);
            });
          }

          scope.$on('fieldPathsUpdated', function(event, _fieldPaths) {
            fieldPaths = _fieldPaths;
          });

          // The ui-codemirror directive allows us to receive a reference to the Codemirror instance on demand.
          scope.$broadcast('CodeMirror', function(cm) {
            cm.on('change', function(instance, change) {
              if (change.origin !== 'complete') {
                instance.showHint({ hint: window.CodeMirror.hint.dictionaryHint, completeSingle: false });
              }
              $timeout(function() {});
            });

            cm.on('keyHandled', function(instance, name, event) {
              if(name === 'Ctrl-Space') {
                instance.showHint({
                  hint: window.CodeMirror.hint.dictionaryHint,
                  completeSingle: false,
                  ctrlSpaceKey: true
                });
              }
            });

            cm.on('cursorActivity', function(cm) {
              updateArgHints(cm);
            });

            cm.on("blur", function(){
              closeArgHints();
            });

          });

          function elt(tagname, cls /*, ... elts*/) {
            var e = document.createElement(tagname);
            if (cls) {
              e.className = cls;
            }
{'fraction_non_alphanumeric': 0.11746770440503891, 'fraction_numerical': 0.0010430875391157827, 'mean_word_length': 1.535910478128179, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5998732', 'n_tokens_mistral': 3345, 'n_tokens_neox': 3153, 'n_words': 623}