public void LocalCRMDeferredInitUsingApplyResources() { ResourceResolveResult rrr = Resolve(CodeLocalCRMDeferredInitUsingApplyResources, 8, 20, null); TestHelper.CheckReference(rrr, "A", null, "A", "A.B"); Assert.That(rrr, Is.InstanceOf(typeof(ResourcePrefixResolveResult))); ResourcePrefixResolveResult rprr = (ResourcePrefixResolveResult)rrr; Assert.That(rprr.Prefix, Is.EqualTo("$this"), "Resource key prefix not detected correctly."); }
/// <summary> /// Finds all unused resource keys in all resource files that are referenced /// in code at least once in the whole solution. /// </summary> /// <param name="monitor">An object implementing <see cref="IProgressMonitor"/> to report the progress of the operation. Can be <c>null</c>.</param> /// <returns>A collection of <see cref="ResourceItem"/> classes that represent the unused resource keys.</returns> public static ICollection <ResourceItem> FindUnusedKeys(IProgressMonitor monitor) { List <Reference> references = FindAllReferences(monitor, SearchScope.WholeSolution); if (references == null) { return(null); } if (monitor != null) { monitor.BeginTask(null, 0, false); } List <ResourceItem> unused = new List <ResourceItem>(); // Get a list of all referenced resource files. // Generate a dictonary of resource file names and the // corresponding referenced keys. Dictionary <string, List <string> > referencedKeys = new Dictionary <string, List <string> >(); Dictionary <string, List <string> > referencedPrefixes = new Dictionary <string, List <string> >(); foreach (Reference reference in references) { ResourceResolveResult rrr = (ResourceResolveResult)reference.ResolveResult; if (rrr.ResourceFileContent != null) { string fileName = rrr.FileName; if (!referencedKeys.ContainsKey(fileName)) { referencedKeys.Add(fileName, new List <string>()); referencedPrefixes.Add(fileName, new List <string>()); } if (rrr.Key != null && !referencedKeys[fileName].Contains(rrr.Key)) { referencedKeys[fileName].Add(rrr.Key); } else { ResourcePrefixResolveResult rprr = rrr as ResourcePrefixResolveResult; if (rprr != null && rprr.Prefix != null && !referencedPrefixes[fileName].Contains(rprr.Prefix)) { referencedPrefixes[fileName].Add(rprr.Prefix); } } } else { if (monitor != null) { monitor.ShowingDialog = true; } MessageService.ShowWarning("Found a resource reference that could not be resolved." + Environment.NewLine + (reference.FileName ?? "<null>") + ":" + reference.Offset + Environment.NewLine + "Expression: " + (reference.Expression ?? "<null>")); if (monitor != null) { monitor.ShowingDialog = false; } } } // Find keys that are not referenced anywhere. foreach (string fileName in referencedKeys.Keys) { #if DEBUG LoggingService.Debug("ResourceToolkit: FindUnusedKeys: Referenced resource file '" + fileName + "'"); #endif foreach (KeyValuePair <string, object> entry in ResourceFileContentRegistry.GetResourceFileContent(fileName).Data) { if (!referencedKeys[fileName].Contains(entry.Key) && !referencedPrefixes[fileName].Any(prefix => entry.Key.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))) { unused.Add(new ResourceItem(fileName, entry.Key)); } } } if (monitor != null) { monitor.Done(); } return(unused.AsReadOnly()); }