Beispiel #1
0
            private void MergeExtensions(
                List <string> extensionList,
                HashSet <Delimiter> delimiters,
                HashSet <Language> languageValues,
                HashSet <string> binaryExtensions)
            {
                ScanInfo scanInfo = new ScanInfo(delimiters, languageValues);

                foreach (string extension in extensionList)
                {
                    if (binaryExtensions.Contains(extension))
                    {
                        // If we get this error, then ScanInfo.xml has assigned the extension as both binary and non-binary.
                        throw new InvalidOperationException(extension + " has already been listed as a binary extension in ScanInfo.xml.");
                    }

                    ScanInfo existingScanInfo;
                    if (this.extensions.TryGetValue(extension, out existingScanInfo))
                    {
                        ScanInfo mergedScanInfo = ScanInfo.Merge(scanInfo, existingScanInfo);
                        this.extensions[extension] = mergedScanInfo;
                    }
                    else
                    {
                        this.extensions.TryAdd(extension, scanInfo);
                    }
                }
            }
Beispiel #2
0
        public static ScanInfo Get(FileItem file)
        {
            ScanInfo result;
            Cache    cache     = LazyCache.Value;
            string   extension = Path.GetExtension(file.FileName);

            if (cache.TryGet(extension, out result))
            {
                // Binary extensions will return the Unscannable instance.
                if (result.IsScannable)
                {
                    // If the file is open in a document that is using a language that doesn't match one
                    // associated with its file extension, then we need to include the language's info too.
                    // We won't do this for PlainText because it has no comment delimiter, which can
                    // cause ambiguities and duplicates if it's paired with an extension with known
                    // delimiters.  For example, .bat files have delimiters, but they open as PlainText.
                    Language docLanguage = file.DocumentLanguage;
                    if (docLanguage != Language.Unknown &&
                        docLanguage != Language.PlainText &&
                        !result.languages.Contains(docLanguage))
                    {
                        ScanInfo languageScanInfo;
                        if (cache.TryGet(docLanguage, out languageScanInfo))
                        {
                            result = ScanInfo.Merge(result, languageScanInfo);
                        }
                    }
                }
            }
            else
            {
                // The file's extension is unknown, so try to match it by language if a document is open.
                // This allows us to handle user-assigned extensions (e.g., .scs for C#).
                Language docLanguage = file.DocumentLanguage;
                if (docLanguage != Language.Unknown)
                {
                    cache.TryGet(docLanguage, out result);
                }

                if (result == null)
                {
                    result = Infer(file, cache);
                }
            }

            return(result ?? Unscannable);
        }