<commit_before>"use babel";
import path from 'path';

function log(obj) {
  if ( atom.config.get('atom-hot-package-loader.outputLog') ) {
    console.log(obj);
  }
}

function unloadPackage(packageName) {
  return new Promise((resolve) => {
    let disposable = atom.packages.onDidUnloadPackage((pack) => {
      if (packageName === pack.name) {

        Object.keys(require.cache)
        .filter((p) => {
          return p.indexOf(pack.path + path.sep) === 0;
        })
        .forEach((p) => {
          delete require.cache[p];
        });
        delete atom.packages.loadedPackages[packageName];

        resolve(packageName);
        log('didunloadPackage');
        disposable.dispose();
      }
    });
    atom.packages.unloadPackage(packageName);
  });
}

function activatePackage(packageName) {
  return new Promise((resolve, reject) => {
    let disposable = atom.packages.onDidActivatePackage((pack) => {
      if (packageName === pack.name) {
        if (atom.packages.enablePackage(packageName)) {
          resolve();
        } else {
          log(`Can't loading ${packageName}`);
        }
        disposable.dispose();
      }
    });
    atom.packages.activatePackage(packageName);
  });
}

function deactivatePackage(packageName) {
  return new Promise((resolve) => {
    let disposable = atom.packages.onDidDeactivatePackage((pack) => {
      if (packageName === pack.name) {
        resolve();
        log('deactivatePackage');
        disposable.dispose();
      }
    });
    atom.packages.deactivatePackage(packageName);
  });
}

export default async function reloadPackage(packageName, reloadGrammars = false) {

	if (packageName === null) return;

  await deactivatePackage(packageName);

  if (!atom.packages.isPackageDisabled(packageName)) {
    atom.packages.disablePackage(packageName);
  }

  if (atom.packages.loadedPackages[packageName]) {
    await unloadPackage(packageName);
  }

	await activatePackage(packageName).then(() => {

    if (reloadGrammars) {
      atom.workspace.getTextEditors().forEach((editor) => {
        editor.reloadGrammar();
      });
    }

    log(`Reload ${packageName}.`);
  }).catch(() => {
    log(`Can't loading ${packageName}`);
  });
}<commit_msg>Refresh grammars on editors based on grammar path. And skip editors, not affected by grammar.<commit_after>"use babel";
import path from 'path';

function log(obj) {
  if ( atom.config.get('atom-hot-package-loader.outputLog') ) {
    console.log(obj);
  }
}

function unloadPackage(packageName) {
  return new Promise((resolve) => {
    let disposable = atom.packages.onDidUnloadPackage((pack) => {
      if (packageName === pack.name) {

        Object.keys(require.cache)
        .filter((p) => {
          return p.indexOf(pack.path + path.sep) === 0;
        })
        .forEach((p) => {
          delete require.cache[p];
        });
        delete atom.packages.loadedPackages[packageName];

        resolve(packageName);
        log('didunloadPackage');
        disposable.dispose();
      }
    });
    atom.packages.unloadPackage(packageName);
  });
}

function activatePackage(packageName) {
  return new Promise((resolve, reject) => {
    let disposable = atom.packages.onDidActivatePackage((pack) => {
      if (packageName === pack.name) {
        if (atom.packages.enablePackage(packageName)) {
          resolve();
        } else {
          log(`Can't loading ${packageName}`);
        }
        disposable.dispose();
      }
    });
    atom.packages.activatePackage(packageName);
  });
}

function deactivatePackage(packageName) {
  return new Promise((resolve) => {
    let disposable = atom.packages.onDidDeactivatePackage((pack) => {
      if (packageName === pack.name) {
        resolve();
        log('deactivatePackage');
        disposable.dispose();
      }
    });
    atom.packages.deactivatePackage(packageName);
  });
}

function reloadGrammars(pack) {
  const {grammars} = pack;
  atom.workspace.getTextEditors().forEach((editor) => {
    const {packageName, path: grammarPath} = editor.getGrammar();
    if (packageName !== pack.name) return;

    const grammarFilename = path.basename(grammarPath);
    const newGrammar = grammars.find((grammar) => path.basename(grammar.path) === grammarFilename);
    if (newGrammar) {
      editor.setGrammar(newGrammar);
    }
    else {
      editor.reloadGrammar();
    }
  });
}

export default async function reloadPackage(packageName, needReloadGrammars = false) {

	if (packageName === null) return;

  await deactivatePackage(packageName);

  if (!atom.packages.isPackageDisabled(packageName)) {
    atom.packages.disablePackage(packageName);
  }

  if (atom.packages.loadedPackages[packageName]) {
    await unloadPackage(packageName);
  }

	await activatePackage(packageName).then(() => {
    const pack = atom.packages.getActivePackage(packageName);

    if (needReloadGrammars) {
      reloadGrammars(pack);
    }

    log(`Reload ${packageName}.`);
  }).catch(() => {
    log(`Can't loading ${packageName}`);
  });
}
{'fraction_non_alphanumeric': 0.14840637450199204, 'fraction_numerical': 0.00039840637450199205, 'mean_word_length': 3.075487012987013, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 26, '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': '6052884', 'n_tokens_mistral': 1531, 'n_tokens_neox': 1498, 'n_words': 254}