getCompletions: function()

in metron-interface/metron-config/src/app/shared/ace-editor/ace-editor.component.ts [141:190]


      getCompletions: function(editor, session, pos, prefix, callback) {
        let autoCompletePrefix = '';
        let autoCompleteSuffix = '';
        let options = _this.options;

        let currentToken = editor.getSession().getTokenAt(pos.row, pos.column);

        // No value or user typed just a char
        if (currentToken === null || currentToken.type === 'comment') {
          autoCompletePrefix = '%{';
          autoCompleteSuffix = ':$0}';
        } else {
          // }any<here>
          if (currentToken.type === 'invalid') {
            let lastToken = editor.getSession().getTokenAt(pos.row, (pos.column - currentToken.value.length));
            autoCompletePrefix = lastToken.value.endsWith('}') ? ' %{' : '%{';
            autoCompleteSuffix = ':$0}';
          }

          // In %{<here>}
          if (currentToken.type === 'paren.rparen') {
            autoCompletePrefix = currentToken.value.endsWith(' ') ? '%{' : ' %{';
            autoCompleteSuffix = ':$0}';
          }

          // %{NUM<here>:}
          if (currentToken.type === 'paren.lparen' || currentToken.type === 'variable') {
            let nextToken = editor.getSession().getTokenAt(pos.row, pos.column + 1);
            autoCompletePrefix = '';
            autoCompleteSuffix = (nextToken && nextToken.value.indexOf(':') > -1) ? '' : ':$0}';
          }

          // %{NUMBER:<here>}
          if (currentToken.type === 'seperator' || currentToken.type === 'string') {
            let autocompleteVal = currentToken.value.replace(/:/g, '');
            let autocompletes = autocompleteVal.length === 0 ? 'variable' : '';
            options = [new AutocompleteOption(autocompletes)];
          }
        }

        callback(null, options.map(function(autocompleteOption) {
          return {
            caption: autocompleteOption.name,
            snippet: autoCompletePrefix + autocompleteOption.name + autoCompleteSuffix,
            meta: 'grok-pattern',
            score: Number.MAX_VALUE
          };
        }));

      }