<commit_before>'use babel';

import { Point } from 'atom';

export default {
  PACKAGE_REGEX: /package\s+([^;]+);/,
  IMPORT_REGEX: /import\s+([^;]+);/g,

  provideImport() {
    return ::this.addImport;
  },

  getImports(editor) {
    const imports = [];
    editor.scan(this.IMPORT_REGEX, ({ match }) => imports.push(match[1]));
    return imports;
  },

  /**
   * Checks if the fullyQualifiedClass exists in the array of imports.
   * Supports wildcard imports (such as `java.util.*;`).
   */
  importsIncludes(imports, fullyQualifiedClass) {
    for (const imp of imports) {
      if (imp === fullyQualifiedClass) {
        return true;
      }
      if (this.getClassFromFullyQualifiedClass(imp) === '*') {
        // This is a wildcard import. Check if package is the same.
        if (this.getPackageFromFullyQualifiedClass(fullyQualifiedClass) === this.getPackageFromFullyQualifiedClass(imp)) {
          return true;
        }
      }
    }
    return false;
  },

  getPackageFromEditor(editor) {
    let pkg;
    editor.scan(this.PACKAGE_REGEX, ({ match }) => (pkg = match[1]));
    return pkg;
  },

  getPackageFromFullyQualifiedClass(fullyQualifiedClass) {
    return fullyQualifiedClass.substr(0, fullyQualifiedClass.lastIndexOf('.'));
  },

  getClassFromFullyQualifiedClass(fullyQualifiedClass) {
    return fullyQualifiedClass.substr(fullyQualifiedClass.lastIndexOf('.') + 1);
  },

  getPackageDefinitionEndPoint(editor) {
    let point;
    editor.scan(this.PACKAGE_REGEX, ({ range }) => (point = range.end.traverse(new Point(1, 0))));
    return point;
  },

  getLastImportEndPoint(editor) {
    let point;
    editor.scan(this.IMPORT_REGEX, ({ range }) => (point = range.end.traverse(new Point(1, 0))));
    return point;
  },

  getImportInsertPoint(editor) {
    let point = this.getLastImportEndPoint(editor);
    if (!point) {
      point = this.getPackageDefinitionEndPoint(editor);
    }
    if (!point) {
      point = new Point(0, 0);
    }
    return point;
  },

  addImport(editor, fullyQualifiedClass) {
    if (this.importsIncludes(this.getImports(editor), fullyQualifiedClass)) {
      return;
    }

    const pkg = this.getPackageFromEditor(editor);
    const importPkg = this.getPackageFromFullyQualifiedClass(fullyQualifiedClass);
    if (importPkg === pkg || 'java.lang' === importPkg) {
      return;
    }

    const point = this.getImportInsertPoint(editor);
    editor.getBuffer().insert(point, `import ${fullyQualifiedClass};\n`);
  }

};<commit_msg>Don't import nested classes<commit_after>'use babel';

import { Point } from 'atom';

export default {
  PACKAGE_REGEX: /package\s+([^;]+);/,
  IMPORT_REGEX: /import\s+([^;]+);/g,

  provideImport() {
    return ::this.addImport;
  },

  getImports(editor) {
    const imports = [];
    editor.scan(this.IMPORT_REGEX, ({ match }) => imports.push(match[1]));
    return imports;
  },

  /**
   * Checks if the fullyQualifiedClass exists in the array of imports.
   * Supports wildcard imports (such as `java.util.*;`).
   */
  importsIncludes(imports, fullyQualifiedClass) {
    for (const imp of imports) {
      if (imp === fullyQualifiedClass) {
        return true;
      }
      if (this.getClassFromFullyQualifiedClass(imp) === '*') {
        // This is a wildcard import. Check if package is the same.
        if (this.getPackageFromFullyQualifiedClass(fullyQualifiedClass) === this.getPackageFromFullyQualifiedClass(imp)) {
          return true;
        }
      }
    }
    return false;
  },

  getPackageFromEditor(editor) {
    let pkg;
    editor.scan(this.PACKAGE_REGEX, ({ match }) => (pkg = match[1]));
    return pkg;
  },

  getPackageFromFullyQualifiedClass(fullyQualifiedClass) {
    return fullyQualifiedClass.substr(0, fullyQualifiedClass.lastIndexOf('.'));
  },

  getClassFromFullyQualifiedClass(fullyQualifiedClass) {
    return fullyQualifiedClass.substr(fullyQualifiedClass.lastIndexOf('.') + 1);
  },

  getPackageDefinitionEndPoint(editor) {
    let point;
    editor.scan(this.PACKAGE_REGEX, ({ range }) => (point = range.end.traverse(new Point(1, 0))));
    return point;
  },

  getLastImportEndPoint(editor) {
    let point;
    editor.scan(this.IMPORT_REGEX, ({ range }) => (point = range.end.traverse(new Point(1, 0))));
    return point;
  },

  getImportInsertPoint(editor) {
    let point = this.getLastImportEndPoint(editor);
    if (!point) {
      point = this.getPackageDefinitionEndPoint(editor);
    }
    if (!point) {
      point = new Point(0, 0);
    }
    return point;
  },

  addImport(editor, fullyQualifiedClass) {
    let rootClass = fullyQualifiedClass;
    const subClassIndex = fullyQualifiedClass.indexOf('$'); // A nested class, e.g. javax.ws.rs.core.Response$Status
    if (subClassIndex !== -1) {
      rootClass = fullyQualifiedClass.substr(0, subClassIndex);
    }

    if (this.importsIncludes(this.getImports(editor), rootClass)) {
      return;
    }

    const pkg = this.getPackageFromEditor(editor);
    const importPkg = this.getPackageFromFullyQualifiedClass(rootClass);
    if (importPkg === pkg || 'java.lang' === importPkg) {
      return;
    }

    const point = this.getImportInsertPoint(editor);
    editor.getBuffer().insert(point, `import ${rootClass};\n`);
  }

};
{'fraction_non_alphanumeric': 0.14133129887469006, 'fraction_numerical': 0.004196070951745184, 'mean_word_length': 3.414141414141414, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '1913854', 'n_tokens_mistral': 1723, 'n_tokens_neox': 1678, 'n_words': 320}