//===================================================================== /// <summary> /// Load the configuration from the given file /// </summary> /// <param name="filename">The configuration file to load</param> /// <remarks>Any properties not in the configuration file retain their current values. If the file does /// not exist, the configuration will remain unchanged.</remarks> public void Load(string filename) { HashSet <string> tempHashSet; try { // Nothing to do if the file doesn't exist if (!File.Exists(filename)) { return; } var configuration = new SpellingConfigurationFile(filename, this); this.SpellCheckAsYouType = configuration.ToBoolean(PropertyNames.SpellCheckAsYouType); if (configuration.ConfigurationType != ConfigurationType.Global) { // This option is always true for the global configuration this.IncludeInProjectSpellCheck = configuration.ToBoolean(PropertyNames.IncludeInProjectSpellCheck); } else { // These only apply to the global configuration if (configuration.HasProperty(PropertyNames.VisualStudioIdExclusions)) { this.EnableWpfTextBoxSpellChecking = configuration.ToBoolean(PropertyNames.EnableWpfTextBoxSpellChecking); visualStudioExclusions = new List <Regex>(configuration.ToRegexes(PropertyNames.VisualStudioIdExclusions, PropertyNames.VisualStudioIdExclusionItem)); } } this.DetectDoubledWords = configuration.ToBoolean(PropertyNames.DetectDoubledWords); this.IgnoreWordsWithDigits = configuration.ToBoolean(PropertyNames.IgnoreWordsWithDigits); this.IgnoreWordsInAllUppercase = configuration.ToBoolean(PropertyNames.IgnoreWordsInAllUppercase); this.IgnoreWordsInMixedCase = configuration.ToBoolean(PropertyNames.IgnoreWordsInMixedCase); this.IgnoreFormatSpecifiers = configuration.ToBoolean(PropertyNames.IgnoreFormatSpecifiers); this.IgnoreFilenamesAndEMailAddresses = configuration.ToBoolean( PropertyNames.IgnoreFilenamesAndEMailAddresses); this.IgnoreXmlElementsInText = configuration.ToBoolean(PropertyNames.IgnoreXmlElementsInText); this.IgnoreHtmlComments = configuration.ToBoolean(PropertyNames.IgnoreHtmlComments); this.IgnoreXmlComments = configuration.ToBoolean(PropertyNames.IgnoreXmlComments); this.TreatUnderscoreAsSeparator = configuration.ToBoolean(PropertyNames.TreatUnderscoreAsSeparator); this.IgnoreMnemonics = configuration.ToBoolean(PropertyNames.IgnoreMnemonics); this.IgnoreCharacterClass = configuration.ToEnum <IgnoredCharacterClass>( PropertyNames.IgnoreCharacterClass); this.DetermineResourceFileLanguageFromName = configuration.ToBoolean( PropertyNames.DetermineResourceFileLanguageFromName); csharpOptions.IgnoreXmlDocComments = configuration.ToBoolean( PropertyNames.CSharpOptionsIgnoreXmlDocComments); csharpOptions.IgnoreDelimitedComments = configuration.ToBoolean( PropertyNames.CSharpOptionsIgnoreDelimitedComments); csharpOptions.IgnoreStandardSingleLineComments = configuration.ToBoolean( PropertyNames.CSharpOptionsIgnoreStandardSingleLineComments); csharpOptions.IgnoreQuadrupleSlashComments = configuration.ToBoolean( PropertyNames.CSharpOptionsIgnoreQuadrupleSlashComments); csharpOptions.IgnoreNormalStrings = configuration.ToBoolean( PropertyNames.CSharpOptionsIgnoreNormalStrings); csharpOptions.IgnoreVerbatimStrings = configuration.ToBoolean( PropertyNames.CSharpOptionsIgnoreVerbatimStrings); csharpOptions.IgnoreInterpolatedStrings = configuration.ToBoolean( PropertyNames.CSharpOptionsIgnoreInterpolatedStrings); csharpOptions.ApplyToAllCStyleLanguages = configuration.ToBoolean( PropertyNames.CSharpOptionsApplyToAllCStyleLanguages); cadOptions.ImportCodeAnalysisDictionaries = configuration.ToBoolean( PropertyNames.CadOptionsImportCodeAnalysisDictionaries); cadOptions.RecognizedWordHandling = configuration.ToEnum <RecognizedWordHandling>( PropertyNames.CadOptionsRecognizedWordHandling); cadOptions.TreatUnrecognizedWordsAsMisspelled = configuration.ToBoolean( PropertyNames.CadOptionsTreatUnrecognizedWordsAsMisspelled); cadOptions.TreatDeprecatedTermsAsMisspelled = configuration.ToBoolean( PropertyNames.CadOptionsTreatDeprecatedTermsAsMisspelled); cadOptions.TreatCompoundTermsAsMisspelled = configuration.ToBoolean( PropertyNames.CadOptionsTreatCompoundTermsAsMisspelled); cadOptions.TreatCasingExceptionsAsIgnoredWords = configuration.ToBoolean( PropertyNames.CadOptionsTreatCasingExceptionsAsIgnoredWords); this.InheritAdditionalDictionaryFolders = configuration.ToBoolean( PropertyNames.InheritAdditionalDictionaryFolders); if (configuration.HasProperty(PropertyNames.AdditionalDictionaryFolders)) { tempHashSet = new HashSet <string>(StringComparer.OrdinalIgnoreCase); foreach (string folder in configuration.ToValues(PropertyNames.AdditionalDictionaryFolders, PropertyNames.AdditionalDictionaryFoldersItem)) { // Fully qualify relative paths with the configuration file path if (folder.IndexOf('%') != -1 || Path.IsPathRooted(folder)) { tempHashSet.Add(folder); } else { tempHashSet.Add(Path.GetFullPath(Path.Combine(Path.GetDirectoryName(filename), folder))); } } if (this.InheritAdditionalDictionaryFolders) { if (tempHashSet.Count != 0) { foreach (string folder in tempHashSet) { additionalDictionaryFolders.Add(folder); } } } else { additionalDictionaryFolders = tempHashSet.ToList(); } } this.InheritIgnoredWords = configuration.ToBoolean(PropertyNames.InheritIgnoredWords); if (configuration.HasProperty(PropertyNames.IgnoredWords)) { tempHashSet = new HashSet <string>(configuration.ToValues(PropertyNames.IgnoredWords, PropertyNames.IgnoredWordsItem), StringComparer.OrdinalIgnoreCase); // For global configurations, we always want to replace the default set if (this.InheritIgnoredWords && configuration.ConfigurationType != ConfigurationType.Global) { if (tempHashSet.Count != 0) { foreach (string word in tempHashSet) { ignoredWords.Add(word); } } } else { ignoredWords = tempHashSet; } } this.InheritExclusionExpressions = configuration.ToBoolean(PropertyNames.InheritExclusionExpressions); if (configuration.HasProperty(PropertyNames.ExclusionExpressions)) { var tempList = new List <Regex>(configuration.ToRegexes(PropertyNames.ExclusionExpressions, PropertyNames.ExclusionExpressionItem)); if (this.InheritExclusionExpressions) { if (tempList.Count != 0) { tempHashSet = new HashSet <string>(exclusionExpressions.Select(r => r.ToString())); foreach (Regex exp in tempList) { if (!tempHashSet.Contains(exp.ToString())) { exclusionExpressions.Add(exp); tempHashSet.Add(exp.ToString()); } } } } else { exclusionExpressions = tempList; } } this.InheritIgnoredFilePatterns = configuration.ToBoolean(PropertyNames.InheritIgnoredFilePatterns); if (configuration.HasProperty(PropertyNames.IgnoredFilePatterns)) { var tempList = new List <string>(configuration.ToValues(PropertyNames.IgnoredFilePatterns, PropertyNames.IgnoredFilePatternItem)); // For global configurations, we always want to replace the default set if (!this.InheritIgnoredFilePatterns || configuration.ConfigurationType == ConfigurationType.Global) { ignoredFilePatterns.Clear(); } if (tempList.Count != 0) { tempHashSet = new HashSet <string>(ignoredFilePatterns.Select(r => r.ToString())); foreach (string exp in tempList) { Regex pattern = exp.RegexFromFilePattern(); if (!tempHashSet.Contains(pattern.ToString())) { ignoredFilePatterns.Add(pattern); tempHashSet.Add(pattern.ToString()); } } } } this.InheritXmlSettings = configuration.ToBoolean(PropertyNames.InheritXmlSettings); if (configuration.HasProperty(PropertyNames.IgnoredXmlElements)) { tempHashSet = new HashSet <string>(configuration.ToValues(PropertyNames.IgnoredXmlElements, PropertyNames.IgnoredXmlElementsItem)); // For global configurations, we always want to replace the default set if (this.InheritXmlSettings && configuration.ConfigurationType != ConfigurationType.Global) { if (tempHashSet.Count != 0) { foreach (string element in tempHashSet) { ignoredXmlElements.Add(element); } } } else { ignoredXmlElements = tempHashSet; } } if (configuration.HasProperty(PropertyNames.SpellCheckedXmlAttributes)) { tempHashSet = new HashSet <string>(configuration.ToValues(PropertyNames.SpellCheckedXmlAttributes, PropertyNames.SpellCheckedXmlAttributesItem)); // For global configurations, we always want to replace the default set if (this.InheritXmlSettings && configuration.ConfigurationType != ConfigurationType.Global) { if (tempHashSet.Count != 0) { foreach (string attr in tempHashSet) { spellCheckedXmlAttributes.Add(attr); } } } else { spellCheckedXmlAttributes = tempHashSet; } } // Load the dictionary languages and, if merging settings, handle inheritance if (configuration.HasProperty(PropertyNames.SelectedLanguages)) { var languages = configuration.ToValues(PropertyNames.SelectedLanguages, PropertyNames.SelectedLanguagesItem, true).Distinct(StringComparer.OrdinalIgnoreCase).ToList(); // Is there a blank entry that marks the inherited languages placeholder? int idx = languages.IndexOf(String.Empty); if (idx != -1) { languages.RemoveAt(idx); // If there are other languages, insert the inherited languages at the desired location. // If an inherited language matches a language in the configuration file, it is left at // its new location this overriding the inherited language location. if (languages.Count != 0) { foreach (var lang in dictionaryLanguages) { if (!languages.Contains(lang.Name)) { languages.Insert(idx, lang.Name); idx++; } } } } if (languages.Count != 0) { dictionaryLanguages = languages.Select(l => new CultureInfo(l)).ToList(); } } } catch (Exception ex) { // Ignore errors and just use the defaults System.Diagnostics.Debug.WriteLine(ex); } finally { // Always ensure we have at least the default language if none are specified in the global // configuration file. if (dictionaryLanguages.Count == 0) { dictionaryLanguages.Add(new CultureInfo("en-US")); } } }
//===================================================================== /// <summary> /// Load the configuration from the given file /// </summary> /// <param name="filename">The configuration file to load</param> /// <remarks>Any properties not in the configuration file retain their current values. If the file does /// not exist, the configuration will remain unchanged.</remarks> public void Load(string filename) { HashSet <string> tempHashSet; try { // Nothing to do if the file doesn't exist if (!File.Exists(filename)) { return; } var configuration = new SpellingConfigurationFile(filename, this); this.DefaultLanguage = configuration.ToCultureInfo(PropertyNames.DefaultLanguage); this.SpellCheckAsYouType = configuration.ToBoolean(PropertyNames.SpellCheckAsYouType); this.IgnoreWordsWithDigits = configuration.ToBoolean(PropertyNames.IgnoreWordsWithDigits); this.IgnoreWordsInAllUppercase = configuration.ToBoolean(PropertyNames.IgnoreWordsInAllUppercase); this.IgnoreFormatSpecifiers = configuration.ToBoolean(PropertyNames.IgnoreFormatSpecifiers); this.IgnoreFilenamesAndEMailAddresses = configuration.ToBoolean( PropertyNames.IgnoreFilenamesAndEMailAddresses); this.IgnoreXmlElementsInText = configuration.ToBoolean(PropertyNames.IgnoreXmlElementsInText); this.TreatUnderscoreAsSeparator = configuration.ToBoolean(PropertyNames.TreatUnderscoreAsSeparator); this.IgnoreCharacterClass = configuration.ToEnum <IgnoredCharacterClass>( PropertyNames.IgnoreCharacterClass); this.DetermineResourceFileLanguageFromName = configuration.ToBoolean( PropertyNames.DetermineResourceFileLanguageFromName); csharpOptions.IgnoreXmlDocComments = configuration.ToBoolean( PropertyNames.CSharpOptionsIgnoreXmlDocComments); csharpOptions.IgnoreDelimitedComments = configuration.ToBoolean( PropertyNames.CSharpOptionsIgnoreDelimitedComments); csharpOptions.IgnoreStandardSingleLineComments = configuration.ToBoolean( PropertyNames.CSharpOptionsIgnoreStandardSingleLineComments); csharpOptions.IgnoreQuadrupleSlashComments = configuration.ToBoolean( PropertyNames.CSharpOptionsIgnoreQuadrupleSlashComments); csharpOptions.IgnoreNormalStrings = configuration.ToBoolean( PropertyNames.CSharpOptionsIgnoreNormalStrings); csharpOptions.IgnoreVerbatimStrings = configuration.ToBoolean( PropertyNames.CSharpOptionsIgnoreVerbatimStrings); this.InheritExcludedExtensions = configuration.ToBoolean(PropertyNames.InheritExcludedExtensions); if (configuration.HasProperty(PropertyNames.ExcludedExtensions)) { tempHashSet = new HashSet <string>(configuration.ToValues(PropertyNames.ExcludedExtensions, PropertyNames.ExcludedExtensionsItem), StringComparer.OrdinalIgnoreCase); if (this.InheritExcludedExtensions) { if (tempHashSet.Count != 0) { foreach (string ext in tempHashSet) { excludedExtensions.Add(ext); } } } else { excludedExtensions = tempHashSet; } } this.InheritAdditionalDictionaryFolders = configuration.ToBoolean( PropertyNames.InheritAdditionalDictionaryFolders); if (configuration.HasProperty(PropertyNames.AdditionalDictionaryFolders)) { tempHashSet = new HashSet <string>(StringComparer.OrdinalIgnoreCase); foreach (string folder in configuration.ToValues(PropertyNames.AdditionalDictionaryFolders, PropertyNames.AdditionalDictionaryFoldersItem)) { // Fully qualify relative paths with the configuration file path if (Path.IsPathRooted(folder)) { tempHashSet.Add(folder); } else { tempHashSet.Add(Path.GetFullPath(Path.Combine(Path.GetDirectoryName(filename), folder))); } } if (this.InheritAdditionalDictionaryFolders) { if (tempHashSet.Count != 0) { foreach (string folder in tempHashSet) { additionalDictionaryFolders.Add(folder); } } } else { additionalDictionaryFolders = tempHashSet.ToList(); } } this.InheritIgnoredWords = configuration.ToBoolean(PropertyNames.InheritIgnoredWords); if (configuration.HasProperty(PropertyNames.IgnoredWords)) { tempHashSet = new HashSet <string>(configuration.ToValues(PropertyNames.IgnoredWords, PropertyNames.IgnoredWordsItem), StringComparer.OrdinalIgnoreCase); if (this.InheritIgnoredWords) { if (tempHashSet.Count != 0) { foreach (string word in tempHashSet) { ignoredWords.Add(word); } } } else { ignoredWords = tempHashSet; } } this.InheritXmlSettings = configuration.ToBoolean(PropertyNames.InheritXmlSettings); if (configuration.HasProperty(PropertyNames.IgnoredXmlElements)) { tempHashSet = new HashSet <string>(configuration.ToValues(PropertyNames.IgnoredXmlElements, PropertyNames.IgnoredXmlElementsItem)); if (this.InheritXmlSettings) { if (tempHashSet.Count != 0) { foreach (string element in tempHashSet) { ignoredXmlElements.Add(element); } } } else { ignoredXmlElements = tempHashSet; } } if (configuration.HasProperty(PropertyNames.SpellCheckedXmlAttributes)) { tempHashSet = new HashSet <string>(configuration.ToValues(PropertyNames.SpellCheckedXmlAttributes, PropertyNames.SpellCheckedXmlAttributesItem)); if (this.InheritXmlSettings) { if (tempHashSet.Count != 0) { foreach (string attr in tempHashSet) { spellCheckedXmlAttributes.Add(attr); } } } else { spellCheckedXmlAttributes = tempHashSet; } } } catch (Exception ex) { // Ignore errors and just use the defaults System.Diagnostics.Debug.WriteLine(ex); } }