All files / src/edits speculateChanges.js

26.67% Statements 4/15
100% Branches 0/0
0% Functions 0/2
26.67% Lines 4/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 321x 1x         1x 1x                                                
import CodeMirror from 'codemirror';
import SHARED from '../shared';
 
 
// TODO: For efficiency, we don't really need a full CodeMirror instance here:
// create a mock one.
const tmpDiv = document.createElement('div');
const tmpCM = CodeMirror(tmpDiv, {value: ""});
 
// Check if a set of codemirror changes parses successfully. Returns one of:
//
// - {successful: true, newAST}
// - {successful: false, exception}
export function speculateChanges(changeArr) {
  tmpCM.setValue(SHARED.cm.getValue());
  for (let c of changeArr) {
    tmpCM.replaceRange(c.text, c.from, c.to, c.origin);
  }
  let newText = tmpCM.getValue();
  try {
    let newAST = SHARED.parser.parse(newText);
    return {successful: true, newAST: newAST};
  } catch (exception) {
    return {successful: false, exception};
  }
}
 
export function getTempCM() {
  const tmpCM = CodeMirror(tmpDiv, {value: SHARED.cm.getValue()});
  tmpCM.setCursor(SHARED.cm.getCursor());
  return tmpCM;
}