-
Notifications
You must be signed in to change notification settings - Fork 0
Remove file access; custom dictionary as AdditionalFiles #18
Description
This is a larger topic. Would be nice to activate
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>in the csproj.
However, can't do this right now, because the code uses the File class to load the custom dictionaries which are obviously needed for spellchecking. The File class is forbidden for analyzers with EnforceExtendedAnalyzerRules=true, so before activating these extended rules we need to find a different way to load/access the dictionaries.
One approach is to make the dictionaries available as <AdditionalFiles>. But this would require manual setup by the user. Or maybe a .props/.targets file can be added which automatically finds dictionaries (extension .dic) which are nearby.
Roslyn provides a way to access these additional files through the AnalyzerOptions parameter in your diagnostic analyzer.
Example:
public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.EnableConcurrentExecution();
context.RegisterCompilationStartAction(compilationStartContext =>
{
var additionalFiles = compilationStartContext.Options.AdditionalFiles;
AdditionalText dictionaryFile = additionalFiles.FirstOrDefault(file => file.Path.EndsWith("myDictionary.txt"));
if (dictionaryFile != null)
{
var dictionaryContent = dictionaryFile.GetText(compilationStartContext.CancellationToken).ToString();
// Load your dictionary here
}
});
}There's also a codefix to add a flagged word to the custom dictionary. Currently this writes into this file. An alternative would be to
prompt the user to save:
Rather than writing directly to a file system, consider creating a code fix that prepares the necessary changes and prompts the user to save these changes themselves. For instance, if adding a word to a dictionary, your code fix could open the dictionary file in the editor with the new word prepared for insertion.