<commit_before>'use babel';

import { CompositeDisposable } from 'atom';
import { AtomAutocompleteProvider } from './AtomAutocompleteProvider';
import { JavaClassLoader } from './JavaClassLoader';
import ioUtil from './ioUtil';
let fs = require('fs');

class AtomAutocompletePackage {

  constructor() {
    this.config = require('./config.json');
    this.subscriptions = undefined;
    this.provider = undefined;
    this.classLoader = undefined;
    this.classpath = '.';
  }

  activate() {
    this.classLoader = new JavaClassLoader();
    this.provider = new AtomAutocompleteProvider(this.classLoader);
    this.subscriptions = new CompositeDisposable();
    // Listen for refresh command
    this.subscriptions.add(
      atom.commands.add('atom-workspace', 'autocomplete-java:refresh', () => {
        this._refresh();
      })
    );
    // Listen for save
    atom.workspace.observeTextEditors(editor => {
      this.subscriptions.add(editor.getBuffer().onWillSave(() => {
        this._onSave(editor);
      }));
    });
    // Refresh all classes
    this._refresh();
  }

  deactivate() {
    this.subscriptions.dispose();
    this.provider = null;
    this.classLoader = null;
    this.subscriptions = null;
  }

  getProvider() {
    return this.provider;
  }

  _refresh() {
    // TODO observe config changes
    this.provider.configure(atom.config.get('autocomplete-java'));

    // Load all project classes using classpath read from .classpath file
    let rootDir = this._getProjectRootDir();
    if (rootDir) {
      return ioUtil.readFile(rootDir + '/' +
        atom.config.get('autocomplete-java.classpathFilePath'), 'utf8')
      .then(classpath => {
        if (rootDir.indexOf(':') !== -1) {
          // Convert classpath to Windows format
          classpath = classpath.replace(/\:/g, ';');
        }
        this.classpath = classpath.replace(/\./g, rootDir + '/.').trim();
        this.classLoader.loadClasses(this.classpath,
          atom.config.get('autocomplete-java.loadClassMembers'));
      });
    }
  }

  _onSave(editor) {
    // TODO
    let fileMatch = editor.getPath().match(/\/([^\/]*)\.java/);
    if (fileMatch) {
      // Organize imports on save
      if (atom.config.get('autocomplete-java.organizeImportsOnSave')) {
        this.provider.organizeImports(editor);
      }

      // Refresh saved class after it has been compiled
      let packageMatch = editor.getText().match(/package\s(.*);/);
      if (packageMatch) {
        // TODO use file watcher instead of hardcoded timeout
        setTimeout(() => {
            this.classLoader.loadClass(
              packageMatch[1] + '.' + fileMatch[1],
              this.classpath,
              atom.config.get('autocomplete-java.loadClassMembers'));
        }, 2000);
      }
    }
  }
<commit_msg>refresh on save<commit_after>'use babel';

import { CompositeDisposable } from 'atom';
import { AtomAutocompleteProvider } from './AtomAutocompleteProvider';
import { JavaClassLoader } from './JavaClassLoader';
import ioUtil from './ioUtil';
let fs = require('fs');

class AtomAutocompletePackage {

  constructor() {
    this.config = require('./config.json');
    this.subscriptions = undefined;
    this.provider = undefined;
    this.classLoader = undefined;
    this.classpath = '.';
  }

  activate() {
    this.classLoader = new JavaClassLoader();
    this.provider = new AtomAutocompleteProvider(this.classLoader);
    this.subscriptions = new CompositeDisposable();
    // Listen for refresh command
    this.subscriptions.add(
      atom.commands.add('atom-workspace', 'autocomplete-java:refresh', () => {
        this._refresh();
      })
    );
    // Listen for save
    atom.workspace.observeTextEditors(editor => {
      this.subscriptions.add(editor.getBuffer().onWillSave(() => {
        this._onSave(editor);
      }));
    });
    // Refresh all classes
    this._refresh();
  }

  deactivate() {
    this.subscriptions.dispose();
    this.provider = null;
    this.classLoader = null;
    this.subscriptions = null;
  }

  getProvider() {
    return this.provider;
  }

  _refresh() {
    // TODO observe config changes
    this.provider.configure(atom.config.get('autocomplete-java'));

    // Load all project classes using classpath read from .classpath file
    let rootDir = this._getProjectRootDir();
    if (rootDir) {
      return ioUtil.readFile(rootDir + '/' +
        atom.config.get('autocomplete-java.classpathFilePath'), 'utf8')
      .then(classpath => {
        if (rootDir.indexOf(':') !== -1) {
          // Convert classpath to Windows format
          classpath = classpath.replace(/\:/g, ';');
        }
        this.classpath = classpath.replace(/\./g, rootDir + '/.').trim();
        this.classLoader.loadClasses(this.classpath,
          atom.config.get('autocomplete-java.loadClassMembers'));
      });
    }
  }

  _refreshClass(className, delayMillis) {
    setTimeout(() => {
      return this.classLoader.loadClass(className, this.classpath,
        atom.config.get('autocomplete-java.loadClassMembers'));
      }, delayMillis);
  }

  _onSave(editor) {
    // TODO
    let fileMatch = editor.getPath().match(/\/([^\/]*)\.java/);
    if (fileMatch) {
      // Organize imports on save
      if (atom.config.get('autocomplete-java.organizeImportsOnSave')) {
        this.provider.organizeImports(editor);
      }

      // Refresh saved class after it has been compiled
      let packageMatch = editor.getText().match(/package\s(.*);/);
      if (packageMatch) {
        // TODO use file watcher instead of hardcoded timeout
        let className = packageMatch[1] + '.' + fileMatch[1];
        this._refreshClass(className, 2000);
        this._refreshClass(className, 20000);
      }
    }
  }

{'fraction_non_alphanumeric': 0.14497041420118342, 'fraction_numerical': 0.0036547163243995824, 'mean_word_length': 2.6887034659820284, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '219872', 'n_tokens_mistral': 1725, 'n_tokens_neox': 1673, 'n_words': 323}