public DetectionResult DetectIfRequired(CodeFile codeFile, IEnumerable <Language> languages = null) { DetectionResult result = null; if ((languages?.Count() ?? 0) == 1) { result = new DetectionResult(languages.First()); } else if (!string.IsNullOrEmpty(codeFile.Name)) { string[] extensions = GetExtensions(codeFile.Name); List <Language> finalLanguages = GetLanguagesIntersection(extensions, languages); if (finalLanguages.Count == 1 || finalLanguages.Any(final => final.Key == "CSharp")) { result = new DetectionResult(finalLanguages[0]); } else if (finalLanguages.Count > 1) { result = Detect(codeFile, finalLanguages); LogDetection(result, finalLanguages, codeFile); } } else { result = Detect(codeFile, languages); LogDetection(result, languages ?? LanguageUtils.Languages.Values, codeFile); } return(result); }
protected void LogDetection(DetectionResult detectionResult, IEnumerable <Language> languages, CodeFile codeFile) { string languagesString = string.Join(", ", languages.Select(lang => lang.Title)); if (detectionResult != null) { Logger.LogDebug($"Language {detectionResult.Language} (from {languagesString}) has been detected for file \"{codeFile}\". "); } else { Logger.LogDebug($"Language has not been detected from ({languagesString}) for file \"{codeFile}\". "); } }
public DetectionResult DetectIfRequired(TextFile sourceFile, out TimeSpan detectionTimeSpan) { DetectionResult result = null; List <Language> languages = GetLanguages(sourceFile.Name); if (languages.Count == 1 || languages.Any(language => language == Language.CSharp)) { result = new DetectionResult(languages[0]); } else if (languages.Count > 1) { if (languages.Count == 2 && languages.Contains(Language.Html) && languages.Contains(Language.Php)) { result = new DetectionResult(Language.Php); } else { var stopwatch = Stopwatch.StartNew(); if (languages.Any(lang => lang.IsSql())) { List <Language> sqls = SqlDialectDetector.Detect(sourceFile); Logger.LogInfo($"File: {sourceFile}; Sql detection: {string.Join(", ", sqls)}"); if (sqls.Count == 1) { stopwatch.Stop(); detectionTimeSpan = stopwatch.Elapsed; return(new DetectionResult(sqls[0])); } languages.RemoveAll(lang => lang.IsSql()); languages.AddRange(sqls); } ParserLanguageDetector.MaxStackSize = MaxStackSize; result = ParserLanguageDetector.Detect(sourceFile, previousLanguage, languages); previousLanguage = result.Language; stopwatch.Stop(); detectionTimeSpan = stopwatch.Elapsed; } } return(result); }