{"size": 1530, "ext": "js", "max_stars_repo_path": "lib/provider.js", "max_stars_repo_name": "magneval/atom-gherkin-autocomplete", "max_stars_repo_stars_event_min_datetime": "2018-03-02T15:04:54.000Z", "max_stars_repo_stars_event_max_datetime": "2021-06-23T16:46:34.000Z", "max_issues_repo_path": "lib/provider.js", "max_issues_repo_name": "magneval/atom-gherkin-autocomplete", "max_issues_repo_issues_event_min_datetime": "2016-07-10T05:55:03.000Z", "max_issues_repo_issues_event_max_datetime": "2021-12-14T14:55:32.000Z", "max_forks_repo_path": "lib/provider.js", "max_forks_repo_name": "magneval/atom-gherkin-autocomplete", "max_forks_repo_forks_event_min_datetime": "2018-03-02T15:41:56.000Z", "max_forks_repo_forks_event_max_datetime": "2020-04-23T10:07:46.000Z"}
{"max_stars_count": 2, "max_issues_count": 7, "max_forks_count": 5, "avg_line_length": 35.5813953488, "max_line_length": 141, "alphanum_fraction": 0.6816993464}
'use babel';
import {Directory} from 'atom';
import async from 'async';
import FileSearch from './fileSearch';

const PATH_CONFIG_KEY = 'cucumber-complete.path';
const PROPERTY_PREFIX_PATTERN = /(?:^|\[|\(|,|=|:|\s)\s*((?:And|Given|Then|When)\s(?:[a-zA-Z]+\.?){0,2})$/;


module.exports = {
  selector: '.source.feature, .feature',
  filterSuggestions: true,
  load: function() {},
  getSuggestions: function({bufferPosition, editor}) {
    let line = editor.getTextInRange([[bufferPosition.row, 0], bufferPosition]);
    const options = this.getConfigOptions();

    if(!line.match(PROPERTY_PREFIX_PATTERN)){
      return new Promise(resolve => resolve([]));
    }
    const featuresDirectory = new Directory(`${this.rootDirectory()}${options.featurePath}`);
    if(!featuresDirectory.existsSync()){
      const notificationDetails = {
        detail: 'You can change the path for your feature directory in the gherkin-autocomplete settings'
      }
      atom.notifications.addError(`Not directory found at feature path: ${this.rootDirectory()}${options.featurePath}`, notificationDetails);
      return new Promise(resolve => resolve([]));
    }
    if(options.searchFeatures){
      const searcher = new FileSearch(options);
      return searcher.searchFiles(featuresDirectory);
    }

    return new Promise(resolve => resolve([]));
  },
  rootDirectory: function() {
    return atom.project.rootDirectories[0].path;
  },
  getConfigOptions: function(){
    return atom.config.getAll('gherkin-autocomplete')[0].value;
  }
};
