packages/insomnia-app/app/ui/components/codemirror/extensions/autocomplete.js
function replaceHintMatch(cm, self, data) {
  const cur = cm.getCursor();
  const from = CodeMirror.Pos(cur.line, cur.ch - data.segment.length);
  const to = CodeMirror.Pos(cur.line, cur.ch);

  const prevStart = CodeMirror.Pos(from.line, from.ch - 10);
  const prevChars = cm.getRange(prevStart, from);

  const nextEnd = CodeMirror.Pos(to.line, to.ch + 10);
  const nextChars = cm.getRange(to, nextEnd);

  let prefix = '';
  let suffix = '';

  if (data.type === TYPE_VARIABLE && !prevChars.match(/{{[^}]*$/)) {
    prefix = '{{ '; // If no closer before
  } else if (data.type === TYPE_VARIABLE && prevChars.match(/{{$/)) {
    prefix = ' '; // If no space after opener
  } else if (data.type === TYPE_TAG && prevChars.match(/{%$/)) {
    prefix = ' '; // If no space after opener
  } else if (data.type === TYPE_TAG && !prevChars.match(/{%[^%]*$/)) {
    prefix = '{% '; // If no closer before
  }

  if (data.type === TYPE_VARIABLE && !nextChars.match(/^\s*}}/)) {
    suffix = ' }}'; // If no closer after
  } else if (data.type === TYPE_VARIABLE && nextChars.match(/^}}/)) {
    suffix = ' '; // If no space before closer
  } else if (data.type === TYPE_TAG && nextChars.match(/^%}/)) {
    suffix = ' '; // If no space before closer
  } else if (data.type === TYPE_TAG && nextChars.match(/^\s*}/)) {
    // Edge case because "%" doesn't auto-close tags so sometimes you end
    // up in the scenario of {% foo}
    suffix = ' %';
  } else if (data.type === TYPE_TAG && !nextChars.match(/^\s*%}/)) {
    suffix = ' %}'; // If no closer after
  }

  cm.replaceRange(`${prefix}${data.text}${suffix}`, from, to);
}
function replaceHintMatch(cm, self, data) {
  const cur = cm.getCursor();
  const from = CodeMirror.Pos(cur.line, cur.ch - data.segment.length);
  const to = CodeMirror.Pos(cur.line, cur.ch);

  const prevStart = CodeMirror.Pos(from.line, from.ch - 10);
  const prevChars = cm.getRange(prevStart, from);

  const nextEnd = CodeMirror.Pos(to.line, to.ch + 10);
  const nextChars = cm.getRange(to, nextEnd);

  let prefix = '';
  let suffix = '';

  if (data.type === TYPE_VARIABLE && !prevChars.match(/{{[^}]*$/)) {
    prefix = '{{ '; // If no closer before
  } else if (data.type === TYPE_VARIABLE && prevChars.match(/{{$/)) {
    prefix = ' '; // If no space after opener
  } else if (data.type === TYPE_TAG && prevChars.match(/{%$/)) {
    prefix = ' '; // If no space after opener
  } else if (data.type === TYPE_TAG && !prevChars.match(/{%[^%]*$/)) {
    prefix = '{% '; // If no closer before
  }

  if (data.type === TYPE_VARIABLE && !nextChars.match(/^\s*}}/)) {
    suffix = ' }}'; // If no closer after
  } else if (data.type === TYPE_VARIABLE && nextChars.match(/^}}/)) {
    suffix = ' '; // If no space before closer
  } else if (data.type === TYPE_TAG && nextChars.match(/^%}/)) {
    suffix = ' '; // If no space before closer
  } else if (data.type === TYPE_TAG && nextChars.match(/^\s*}/)) {
    // Edge case because "%" doesn't auto-close tags so sometimes you end
    // up in the scenario of {% foo}
    suffix = ' %';
  } else if (data.type === TYPE_TAG && !nextChars.match(/^\s*%}/)) {
    suffix = ' %}'; // If no closer after
  }

  cm.replaceRange(`${prefix}${data.text}${suffix}`, from, to);
}
function replaceHintMatch ( cm , self , data ) { const cur = cm . getCursor ( ) ; const from = CodeMirror . Pos ( cur . line , cur . ch - data . segment . length ) ; const to = CodeMirror . Pos ( cur . line , cur . ch ) ; const prevStart = CodeMirror . Pos ( from . line , from . ch - 10 ) ; const prevChars = cm . getRange ( prevStart , from ) ; const nextEnd = CodeMirror . Pos ( to . line , to . ch + 10 ) ; const nextChars = cm . getRange ( to , nextEnd ) ; let prefix = '' ; let suffix = '' ; if ( data . type === TYPE_VARIABLE && ! prevChars . match ( / {{[^}]*$ / ) ) { prefix = '{{ ' ; // If no closer before } else if ( data . type === TYPE_VARIABLE && prevChars . match ( / {{$ / ) ) { prefix = ' ' ; // If no space after opener } else if ( data . type === TYPE_TAG && prevChars . match ( / {%$ / ) ) { prefix = ' ' ; // If no space after opener } else if ( data . type === TYPE_TAG && ! prevChars . match ( / {%[^%]*$ / ) ) { prefix = '{% ' ; // If no closer before } if ( data . type === TYPE_VARIABLE && ! nextChars . match ( / ^\s*}} / ) ) { suffix = ' }}' ; // If no closer after } else if ( data . type === TYPE_VARIABLE && nextChars . match ( / ^}} / ) ) { suffix = ' ' ; // If no space before closer } else if ( data . type === TYPE_TAG && nextChars . match ( / ^%} / ) ) { suffix = ' ' ; // If no space before closer } else if ( data . type === TYPE_TAG && nextChars . match ( / ^\s*} / ) ) { // Edge case because "%" doesn't auto-close tags so sometimes you end // up in the scenario of {% foo} suffix = ' %' ; } else if ( data . type === TYPE_TAG && ! nextChars . match ( / ^\s*%} / ) ) { suffix = ' %}' ; // If no closer after } cm . replaceRange ( ` ${ prefix } ${ data . text } ${ suffix } ` , from , to ) ; }
Replace the text in the editor when a hint is selected.
This also makes sure there is whitespace surrounding it
@param cm
@param self
@param data
Replace the text in the editor when a hint is selected . This also makes sure there is whitespace surrounding it