<commit_before>/*
 * Copyright 2010 <NAME>, <EMAIL>
 * File: BashPathCommandCompletion.java, Class: BashPathCommandCompletion
 * Last modified: 2013-02-03
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ansorgit.plugins.bash.editor.codecompletion;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.ApplicationComponent;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.FileFilter;
import java.util.Arrays;
import java.util.TreeSet;

/**
 * Implements cached lookup of the global commands which are available in the configured paths of $PATH.
 *
 * @author jansorg
 */
public class BashPathCommandCompletion implements ApplicationComponent {
    public static BashPathCommandCompletion getInstance() {
        return ApplicationManager.getApplication().getComponent(BashPathCommandCompletion.class);
    }

    //may only be modified in the initComponent method, because it's not serialzed
    private final TreeSet<String> cachedCommands = new TreeSet<String>();

    @Override
    public void initComponent() {
        String envPath = System.getenv("PATH");
        if (envPath != null) {
            String[] split = envPath.split(":");
            if (split != null) {
                //fixme better do this in a background task?
                for (String path : Arrays.asList(split)) {
                    File dir = new File(path);
                    if (dir.exists() && dir.isDirectory()) {
                        File[] commands = dir.listFiles(new ExecutableFileFilter());

                        for (File command : commands) {
                            cachedCommands.add(command.getName());
                        }
                    }
                }
            }
        }
    }

    @Override
    public void disposeComponent() {
    }

    @NotNull
    @Override
    public String getComponentName() {
        return "Bash $PATH command completion component";
    }

    public Iterable<String> findCommands(String commandPrefix) {
        return cachedCommands.subSet(commandPrefix, findUpperLimit(commandPrefix));
    }
<commit_msg>Fixed NPE reported by a user<commit_after>/*
 * Copyright 2010 <NAME>, <EMAIL>
 * File: BashPathCommandCompletion.java, Class: BashPathCommandCompletion
 * Last modified: 2013-02-03
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ansorgit.plugins.bash.editor.codecompletion;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.ApplicationComponent;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.FileFilter;
import java.util.Arrays;
import java.util.TreeSet;

/**
 * Implements cached lookup of the global commands which are available in the configured paths of $PATH.
 *
 * @author jansorg
 */
public class BashPathCommandCompletion implements ApplicationComponent {
    public static BashPathCommandCompletion getInstance() {
        return ApplicationManager.getApplication().getComponent(BashPathCommandCompletion.class);
    }

    //may only be modified in the initComponent method, because it's not serialzed
    private final TreeSet<String> cachedCommands = new TreeSet<String>();

    @Override
    public void initComponent() {
        String envPath = System.getenv("PATH");
        if (envPath != null) {
            String[] split = envPath.split(":");
            if (split != null) {
                //fixme better do this in a background task?
                for (String path : Arrays.asList(split)) {
                    File dir = new File(path);
                    if (dir.exists() && dir.isDirectory()) {
                        File[] commands = dir.listFiles(new ExecutableFileFilter());

                        if (commands != null) {
                            for (File command : commands) {
                                cachedCommands.add(command.getName());
                            }
                        }
                    }
                }
            }
        }
    }

    @Override
    public void disposeComponent() {
    }

    @NotNull
    @Override
    public String getComponentName() {
        return "Bash $PATH command completion component";
    }

    public Iterable<String> findCommands(String commandPrefix) {
        return cachedCommands.subSet(commandPrefix, findUpperLimit(commandPrefix));
    }

{'fraction_non_alphanumeric': 0.08788380216951645, 'fraction_numerical': 0.0058834344548630265, 'mean_word_length': 2.5978835978835977, 'pattern_counts': {'":': 2, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4560434', 'n_tokens_mistral': 1390, 'n_tokens_neox': 1328, 'n_words': 446}