<commit_before>package org.metaborg.spoofax.core.language;

import java.io.IOException;
import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.Nullable;

import org.apache.commons.vfs2.FileName;
import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSelectInfo;
import org.apache.commons.vfs2.FileSelector;
import org.apache.commons.vfs2.FileSystemException;
import org.metaborg.core.MetaborgConstants;
import org.metaborg.core.MetaborgException;
import org.metaborg.core.analysis.AnalyzerFacet;
import org.metaborg.core.config.ConfigRequest;
import org.metaborg.core.config.ILanguageComponentConfig;
import org.metaborg.core.config.ILanguageComponentConfigService;
import org.metaborg.core.context.ContextFacet;
import org.metaborg.core.context.IContextFactory;
import org.metaborg.core.context.IContextStrategy;
import org.metaborg.core.context.ProjectContextStrategy;
import org.metaborg.core.language.ILanguageComponent;
import org.metaborg.core.language.ILanguageDiscoveryRequest;
import org.metaborg.core.language.ILanguageDiscoveryService;
import org.metaborg.core.language.ILanguageService;
import org.metaborg.core.language.IdentificationFacet;
import org.metaborg.core.language.LanguageContributionIdentifier;
import org.metaborg.core.language.LanguageCreationRequest;
import org.metaborg.core.language.LanguageIdentifier;
import org.metaborg.core.language.ResourceExtensionFacet;
import org.metaborg.core.language.ResourceExtensionsIdentifier;
import org.metaborg.core.messages.IMessage;
import org.metaborg.core.syntax.ParseFacet;
import org.metaborg.spoofax.core.action.ActionFacet;
import org.metaborg.spoofax.core.action.ActionFacetFromESV;
import org.metaborg.spoofax.core.analysis.AnalysisFacet;
import org.metaborg.spoofax.core.analysis.AnalysisFacetFromESV;
import org.metaborg.spoofax.core.analysis.ISpoofaxAnalyzer;
import org.metaborg.spoofax.core.analysis.legacy.StrategoAnalyzer;
import org.metaborg.spoofax.core.analysis.taskengine.TaskEngineAnalyzer;
import org.metaborg.spoofax.core.context.ContextFacetFromESV;
import org.metaborg.spoofax.core.context.IndexTaskContextFactory;
import org.metaborg.spoofax.core.context.LegacyContextFactory;
import org.metaborg.spoofax.core.esv.ESVReader;
import org.metaborg.spoofax.core.outline.OutlineFacet;
import org.metaborg.spoofax.core.outline.OutlineFacetFromESV;
import org.metaborg.spoofax.core.shell.ShellFacet;
import org.metaborg.spoofax.core.shell.ShellFacetFromESV;
import org.metaborg.spoofax.core.stratego.StrategoRuntimeFacet;
import org.metaborg.spoofax.core.stratego.StrategoRuntimeFacetFromESV;
import org.metaborg.spoofax.core.style.StylerFacet;
import org.metaborg.spoofax.core.style.StylerFacetFromESV;
import org.metaborg.spoofax.core.syntax.ParseFacetFromESV;
import org.metaborg.spoofax.core.syntax.SyntaxFacet;
import org.metaborg.spoofax.core.syntax.SyntaxFacetFromESV;
import org.metaborg.spoofax.core.terms.ITermFactoryService;
import org.metaborg.spoofax.core.tracing.HoverFacet;
import org.metaborg.spoofax.core.tracing.ResolverFacet;
import org.metaborg.spoofax.core.tracing.ResolverFacetFromESV;
import org.metaborg.util.iterators.Iterables2;
import org.metaborg.util.log.ILogger;
import org.metaborg.util.log.LoggerUtils;
import org.spoofax.interpreter.terms.IStrategoAppl;
import org.spoofax.interpreter.terms.IStrategoTerm;
import org.spoofax.terms.ParseError;
import org.spoofax.terms.io.binary.TermReader;

import com.google.common.base.Joiner;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.inject.Inject;

public class LanguageDiscoveryService implements ILanguageDiscoveryService {
    private static final ILogger logger = LoggerUtils.logger(LanguageDiscoveryService.class);

    private final ILanguageService languageService;
    private final ILanguageComponentConfigService componentConfigService;
    private final ITermFactoryService termFactoryService;
    private final Map<String, IContextFactory> contextFactories;
    private final Map<String, IContextStrategy> contextStrategies;
    private final Map<String, ISpoofaxAnalyzer> analyzers;


    @Inject public LanguageDiscoveryService(ILanguageService languageService,
        ILanguageComponentConfigService componentConfigService, ITermFactoryService termFactoryService,
        Map<String, IContextFactory> contextFactories, Map<String, IContextStrategy> contextStrategies,
        Map<String, ISpoofaxAnalyzer> analyzers) {
        this.languageService = languageService;
        this.componentConfigService = componentConfigService;
        this.termFactoryService = termFactoryService;
        this.contextFactories = contextFactories;
        this.contextStrategies = contextStrategies;
        this.analyzers = analyzers;
    }


    @Override public Collection<ILanguageDiscoveryRequest> request(FileObject location) throws MetaborgException {
        final Collection<ILanguageDiscoveryRequest> requests = Lists.newLinkedList();
        final FileObject[] configFiles;
        try {
            configFiles = location.findFiles(new FileSelector() {
                @Override public boolean traverseDescendents(FileSelectInfo fileInfo) throws Exception {
                    final String baseName = fileInfo.getFile().getName().getBaseName();
                    return !baseName.equals("bin");
                }

                @Override public boolean includeFile(FileSelectInfo fileInfo) throws Exception {
                    return fileInfo.getFile().getName().getBaseName().equals(MetaborgConstants.FILE_COMPONENT_CONFIG);
                }
            });
        } catch(FileSystemException e) {
            throw new MetaborgException("Searching for language components failed unexpectedly", e);
        }
<commit_msg>Don't discover languages in target directories.<commit_after>package org.metaborg.spoofax.core.language;

import java.io.IOException;
import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.Nullable;

import org.apache.commons.vfs2.FileName;
import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSelectInfo;
import org.apache.commons.vfs2.FileSelector;
import org.apache.commons.vfs2.FileSystemException;
import org.metaborg.core.MetaborgConstants;
import org.metaborg.core.MetaborgException;
import org.metaborg.core.analysis.AnalyzerFacet;
import org.metaborg.core.config.ConfigRequest;
import org.metaborg.core.config.ILanguageComponentConfig;
import org.metaborg.core.config.ILanguageComponentConfigService;
import org.metaborg.core.context.ContextFacet;
import org.metaborg.core.context.IContextFactory;
import org.metaborg.core.context.IContextStrategy;
import org.metaborg.core.context.ProjectContextStrategy;
import org.metaborg.core.language.ILanguageComponent;
import org.metaborg.core.language.ILanguageDiscoveryRequest;
import org.metaborg.core.language.ILanguageDiscoveryService;
import org.metaborg.core.language.ILanguageService;
import org.metaborg.core.language.IdentificationFacet;
import org.metaborg.core.language.LanguageContributionIdentifier;
import org.metaborg.core.language.LanguageCreationRequest;
import org.metaborg.core.language.LanguageIdentifier;
import org.metaborg.core.language.ResourceExtensionFacet;
import org.metaborg.core.language.ResourceExtensionsIdentifier;
import org.metaborg.core.messages.IMessage;
import org.metaborg.core.syntax.ParseFacet;
import org.metaborg.spoofax.core.action.ActionFacet;
import org.metaborg.spoofax.core.action.ActionFacetFromESV;
import org.metaborg.spoofax.core.analysis.AnalysisFacet;
import org.metaborg.spoofax.core.analysis.AnalysisFacetFromESV;
import org.metaborg.spoofax.core.analysis.ISpoofaxAnalyzer;
import org.metaborg.spoofax.core.analysis.legacy.StrategoAnalyzer;
import org.metaborg.spoofax.core.analysis.taskengine.TaskEngineAnalyzer;
import org.metaborg.spoofax.core.context.ContextFacetFromESV;
import org.metaborg.spoofax.core.context.IndexTaskContextFactory;
import org.metaborg.spoofax.core.context.LegacyContextFactory;
import org.metaborg.spoofax.core.esv.ESVReader;
import org.metaborg.spoofax.core.outline.OutlineFacet;
import org.metaborg.spoofax.core.outline.OutlineFacetFromESV;
import org.metaborg.spoofax.core.shell.ShellFacet;
import org.metaborg.spoofax.core.shell.ShellFacetFromESV;
import org.metaborg.spoofax.core.stratego.StrategoRuntimeFacet;
import org.metaborg.spoofax.core.stratego.StrategoRuntimeFacetFromESV;
import org.metaborg.spoofax.core.style.StylerFacet;
import org.metaborg.spoofax.core.style.StylerFacetFromESV;
import org.metaborg.spoofax.core.syntax.ParseFacetFromESV;
import org.metaborg.spoofax.core.syntax.SyntaxFacet;
import org.metaborg.spoofax.core.syntax.SyntaxFacetFromESV;
import org.metaborg.spoofax.core.terms.ITermFactoryService;
import org.metaborg.spoofax.core.tracing.HoverFacet;
import org.metaborg.spoofax.core.tracing.ResolverFacet;
import org.metaborg.spoofax.core.tracing.ResolverFacetFromESV;
import org.metaborg.util.iterators.Iterables2;
import org.metaborg.util.log.ILogger;
import org.metaborg.util.log.LoggerUtils;
import org.spoofax.interpreter.terms.IStrategoAppl;
import org.spoofax.interpreter.terms.IStrategoTerm;
import org.spoofax.terms.ParseError;
import org.spoofax.terms.io.binary.TermReader;

import com.google.common.base.Joiner;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.inject.Inject;

public class LanguageDiscoveryService implements ILanguageDiscoveryService {
    private static final ILogger logger = LoggerUtils.logger(LanguageDiscoveryService.class);

    private final ILanguageService languageService;
    private final ILanguageComponentConfigService componentConfigService;
    private final ITermFactoryService termFactoryService;
    private final Map<String, IContextFactory> contextFactories;
    private final Map<String, IContextStrategy> contextStrategies;
    private final Map<String, ISpoofaxAnalyzer> analyzers;


    @Inject public LanguageDiscoveryService(ILanguageService languageService,
        ILanguageComponentConfigService componentConfigService, ITermFactoryService termFactoryService,
        Map<String, IContextFactory> contextFactories, Map<String, IContextStrategy> contextStrategies,
        Map<String, ISpoofaxAnalyzer> analyzers) {
        this.languageService = languageService;
        this.componentConfigService = componentConfigService;
        this.termFactoryService = termFactoryService;
        this.contextFactories = contextFactories;
        this.contextStrategies = contextStrategies;
        this.analyzers = analyzers;
    }


    @Override public Collection<ILanguageDiscoveryRequest> request(FileObject location) throws MetaborgException {
        final Collection<ILanguageDiscoveryRequest> requests = Lists.newLinkedList();
        final FileObject[] configFiles;
        try {
            configFiles = location.findFiles(new FileSelector() {
                @Override public boolean traverseDescendents(FileSelectInfo fileInfo) throws Exception {
                    final String baseName = fileInfo.getFile().getName().getBaseName();
                    return !baseName.equals("bin") && !baseName.equals("target");
                }

                @Override public boolean includeFile(FileSelectInfo fileInfo) throws Exception {
                    return fileInfo.getFile().getName().getBaseName().equals(MetaborgConstants.FILE_COMPONENT_CONFIG);
                }
            });
        } catch(FileSystemException e) {
            throw new MetaborgException("Searching for language components failed unexpectedly", e);
        }

{'fraction_non_alphanumeric': 0.087877271969318, 'fraction_numerical': 0.0010005002501250625, 'mean_word_length': 5.3600212089077415, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '3408877', 'n_tokens_mistral': 3712, 'n_tokens_neox': 3601, 'n_words': 534}