예제 #1
0
        static LanguageUtils()
        {
            foreach (var pair in LanguageInfos)
            {
                Language     language     = pair.Key;
                LanguageInfo languageInfo = pair.Value;

                if (languageInfo.IsPattern)
                {
                    PatternLanguages.Add(language);
                }

                foreach (Language sublanguage in languageInfo.Sublanguages)
                {
                    if (!SuperLanguages.TryGetValue(sublanguage, out HashSet <Language> superLanguages))
                    {
                        superLanguages = new HashSet <Language>();
                        SuperLanguages.Add(sublanguage, superLanguages);
                    }

                    superLanguages.Add(language);
                }
            }
        }
예제 #2
0
        public static HashSet <Language> ParseLanguages(this IEnumerable <string> languageStrings, bool allByDefault = true,
                                                        bool patternLanguages = false)
        {
            string[]           languageStringsArray = languageStrings?.ToArray() ?? ArrayUtils <string> .EmptyArray;
            var                languages            = !patternLanguages ? Languages : PatternLanguages;
            HashSet <Language> negationLangs        = new HashSet <Language>(languages);

            if (allByDefault && languageStringsArray.Length == 0)
            {
                return(negationLangs);
            }

            var  langs            = new HashSet <Language>();
            bool containsNegation = false;

            foreach (string languageString in languageStringsArray)
            {
                bool   negation = false;
                string langStr  = languageString;
                if (langStr.StartsWith("~") || langStr.StartsWith("!"))
                {
                    negation = true;
                    langStr  = langStr.Substring(1);
                }

                if (langStr.EqualsIgnoreCase("all"))
                {
                    return(negation
                        ? new HashSet <Language>()
                        : new HashSet <Language>(!patternLanguages ? Languages : PatternLanguages));
                }

                bool isSql = langStr.EqualsIgnoreCase("sql");

                foreach (Language language in languages)
                {
                    LanguageInfo languageInfo = LanguageInfos[language];
                    bool         result       = isSql
                        ? language.IsSql()
                        : (language.ToString().EqualsIgnoreCase(langStr) || languageInfo.Title.EqualsIgnoreCase(langStr) ||
                           languageInfo.Extensions.Any(ext => (ext.StartsWith(".") ? ext.Substring(1) : ext).EqualsIgnoreCase(langStr)));
                    if (negation)
                    {
                        containsNegation = true;
                        if (result)
                        {
                            negationLangs.Remove(language);
                        }
                    }
                    else
                    {
                        if (result)
                        {
                            langs.Add(language);
                        }
                    }
                }
                ;
            }

            if (containsNegation)
            {
                foreach (Language lang in negationLangs)
                {
                    langs.Add(lang);
                }
            }

            return(langs);
        }