public CodePackage[] ResolvePackage(string packageName, bool caseSensitive) { if (caseSensitive) { using (_updateLock.ReadLock()) { if (!_packages.ContainsKey(packageName)) { string prefix = packageName + '.'; if (!_packages.Keys.Any(i => i.StartsWith(prefix))) { return(new CodePackage[0]); } } } CodeElement parentPackage = CodeElement.Intrinsic; string name = packageName.Substring(packageName.LastIndexOf('.') + 1); if (name.Length < packageName.Length) { parentPackage = ResolvePackage(packageName.Substring(0, packageName.LastIndexOf('.')), true).Single(); } return(new CodePackage[] { new CodePackage(this, name, packageName, parentPackage) }); } else { // get the case-sensitive package names matching this name HashSet <string> matchingNames = new HashSet <string>(StringComparer.Ordinal); CodePhysicalFile[] files = GetPackageFiles(packageName, caseSensitive); foreach (var file in files) { CodePackageStatement packageStatement = file.Children.OfType <CodePackageStatement>().FirstOrDefault(); if (packageStatement != null) { matchingNames.Add(packageStatement.FullName); } } return(matchingNames.SelectMany(name => ResolvePackage(name, true)).ToArray()); } }
private void UpdateFile(CodeFileBuilder fileBuilder) { Contract.Requires(fileBuilder != null); CodeElement fileElement = fileBuilder.BuildElement(null); CodePhysicalFile file = fileElement as CodePhysicalFile; if (file == null) { throw new NotSupportedException(); } CodeElement[] elementsToAdd = file.GetDescendents(true).ToArray(); // apply changes to the cache using (_updateLock.WriteLock()) { CodePhysicalFile existingFile; if (_files.TryGetValue(file.FullName, out existingFile)) { // remove the file CodeElement[] elementsToRemove = existingFile.GetDescendents(true).ToArray(); foreach (var type in elementsToRemove.OfType <CodeType>()) { _types[type.Name].Remove(type); } string previousPackage = string.Empty; CodePackageStatement previousPackageStatement = elementsToRemove.OfType <CodePackageStatement>().FirstOrDefault(); if (previousPackageStatement != null) { previousPackage = previousPackageStatement.FullName; } _packages[previousPackage].Remove(existingFile); _packagesIgnoreCase[previousPackage].Remove(existingFile); _files.Remove(existingFile.FullName); } // now add the new file foreach (var type in elementsToAdd.OfType <CodeType>()) { HashSet <CodeType> types; if (!_types.TryGetValue(type.Name, out types)) { types = new HashSet <CodeType>(CodeElementLocationEqualityComparer.Default); _types.Add(type.Name, types); } types.Add(type); } string package = string.Empty; CodePackageStatement packageStatement = elementsToAdd.OfType <CodePackageStatement>().FirstOrDefault(); if (packageStatement != null) { package = packageStatement.FullName; } HashSet <CodePhysicalFile> packageFiles; if (!_packages.TryGetValue(package, out packageFiles)) { packageFiles = new HashSet <CodePhysicalFile>(ObjectReferenceEqualityComparer <CodePhysicalFile> .Default); _packages.Add(package, packageFiles); } packageFiles.Add(file); if (!_packagesIgnoreCase.TryGetValue(package, out packageFiles)) { packageFiles = new HashSet <CodePhysicalFile>(ObjectReferenceEqualityComparer <CodePhysicalFile> .Default); _packagesIgnoreCase.Add(package, packageFiles); } packageFiles.Add(file); _files.Add(file.FullName, file); } }