/// <summary>
        /// Tries to find the local string resource set used for ICSharpCode.Core resource access.
        /// </summary>
        /// <param name="sourceFileName">The name of the source code file which to find the ICSharpCode.Core resource set for.</param>
        /// <returns>A <see cref="ResourceSetReference"/> that describes the referenced resource set. The contained file name may be <c>null</c> if the file cannot be determined.</returns>
        public static ResourceSetReference GetICSharpCodeCoreLocalResourceSet(string sourceFileName)
        {
            IProject project = ProjectFileDictionaryService.GetProjectForFile(sourceFileName);

            if (project == null || String.IsNullOrEmpty(project.Directory))
            {
                return(EmptyLocalResourceSetReference);
            }

            string localFile;
            ResourceSetReference local = null;

            if (!NRefactoryAstCacheService.CacheEnabled || !cachedLocalResourceSets.TryGetValue(project, out local))
            {
                foreach (string relativePath in AddInTree.BuildItems <string>("/AddIns/ResourceToolkit/ICSharpCodeCoreResourceResolver/LocalResourcesLocations", null, false))
                {
                    if ((localFile = FindICSharpCodeCoreResourceFile(Path.GetFullPath(Path.Combine(project.Directory, relativePath)))) != null)
                    {
                        local = new ResourceSetReference(ICSharpCodeCoreLocalResourceSetName, localFile);
                        if (NRefactoryAstCacheService.CacheEnabled)
                        {
                            cachedLocalResourceSets.Add(project, local);
                        }
                        break;
                    }
                }
            }

            return(local ?? EmptyLocalResourceSetReference);
        }
コード例 #2
0
        /// <summary>
        /// Determines if the specified type is a ResourceManager type that can
        /// be handled by this resolver.
        /// </summary>
        /// <param name="type">The type that will be checked if it is a ResourceManager.</param>
        /// <param name="sourceFileName">The name of the source code file where the reference to this type occurs.</param>
        static bool IsResourceManager(IReturnType type, string sourceFileName)
        {
            IProject        p = ProjectFileDictionaryService.GetProjectForFile(sourceFileName);
            IProjectContent pc;

            if (p == null)
            {
                pc = ParserService.CurrentProjectContent;
            }
            else
            {
                pc = ResourceResolverService.GetProjectContent(p);
            }

            if (pc == null)
            {
                return(false);
            }

            IClass c = type.GetUnderlyingClass();

            if (c == null)
            {
                return(false);
            }

            IClass resourceManager = pc.GetClass("System.Resources.ResourceManager", 0);

            if (resourceManager == null)
            {
                return(false);
            }

            return(c.CompareTo(resourceManager) == 0 || c.IsTypeInInheritanceTree(resourceManager));
        }
        /// <summary>
        /// Tries to find the string resource set of the host application for ICSharpCode.Core resource access.
        /// </summary>
        /// <param name="sourceFileName">The name of the source code file which to find the ICSharpCode.Core resource set for.</param>
        /// <returns>A <see cref="ResourceSetReference"/> that describes the referenced resource set. The contained file name may be <c>null</c> if the file cannot be determined.</returns>
        public static ResourceSetReference GetICSharpCodeCoreHostResourceSet(string sourceFileName)
        {
            IProject             project = ProjectFileDictionaryService.GetProjectForFile(sourceFileName);
            ResourceSetReference host    = null;
            string hostFile;

            if (project == null ||
                !NRefactoryAstCacheService.CacheEnabled || !cachedHostResourceSets.TryGetValue(project, out host))
            {
                // Get SD directory using the reference to ICSharpCode.Core
                string coreAssemblyFullPath = GetICSharpCodeCoreFullPath(project);

                if (coreAssemblyFullPath == null)
                {
                    // Look for the ICSharpCode.Core project using all available projects.
                    if (ProjectService.OpenSolution != null)
                    {
                        foreach (IProject p in ProjectService.OpenSolution.Projects)
                        {
                            if ((coreAssemblyFullPath = GetICSharpCodeCoreFullPath(p)) != null)
                            {
                                break;
                            }
                        }
                    }
                }

                if (coreAssemblyFullPath == null)
                {
                    return(EmptyHostResourceSetReference);
                }

                                #if DEBUG
                LoggingService.Debug("ResourceToolkit: ICSharpCodeCoreResourceResolver coreAssemblyFullPath = " + coreAssemblyFullPath);
                                #endif

                foreach (string relativePath in AddInTree.BuildItems <string>("/AddIns/ResourceToolkit/ICSharpCodeCoreResourceResolver/HostResourcesLocations", null, false))
                {
                    if ((hostFile = FindICSharpCodeCoreResourceFile(Path.GetFullPath(Path.Combine(Path.GetDirectoryName(coreAssemblyFullPath), relativePath)))) != null)
                    {
                        host = new ResourceSetReference(ICSharpCodeCoreHostResourceSetName, hostFile);
                        if (NRefactoryAstCacheService.CacheEnabled && project != null)
                        {
                            cachedHostResourceSets.Add(project, host);
                        }
                        break;
                    }
                }
            }

            return(host ?? EmptyHostResourceSetReference);
        }
コード例 #4
0
        public static ResolveResult ResolveLowLevel(string fileName, string fileContent, int caretLine, int caretColumn, CompilationUnit compilationUnit, string expressionString, Expression expression, ExpressionContext context)
        {
            if (fileName == null)
            {
                throw new ArgumentNullException("fileName");
            }
            if (fileContent == null)
            {
                throw new ArgumentNullException("fileName");
            }
            if (expression == null)
            {
                throw new ArgumentNullException("expression");
            }

            IProject p = ProjectFileDictionaryService.GetProjectForFile(fileName);

            if (p == null)
            {
                LoggingService.Info("ResourceToolkit: NRefactoryAstCacheService: ResolveLowLevel failed. Project is null for file '" + fileName + "'");
                return(null);
            }

            IProjectContent pc = ResourceResolverService.GetProjectContent(p);

            if (pc == null)
            {
                LoggingService.Info("ResourceToolkit: NRefactoryAstCacheService: ResolveLowLevel failed. ProjectContent is null for project '" + p.ToString() + "'");
                return(null);
            }

            NRefactoryResolver resolver = ResourceResolverService.CreateResolver(fileName) as NRefactoryResolver;

            if (resolver == null)
            {
                resolver = new NRefactoryResolver(LanguageProperties.CSharp);
            }

            if (compilationUnit == null)
            {
                compilationUnit = GetFullAst(resolver.Language, fileName, fileContent);
            }
            if (compilationUnit == null)
            {
                LoggingService.Info("ResourceToolkit: NRefactoryAstCacheService: ResolveLowLevel failed due to the compilation unit being unavailable.");
                return(null);
            }

            if (!resolver.Initialize(ParserService.GetParseInformation(fileName), caretLine, caretColumn))
            {
                LoggingService.Info("ResourceToolkit: NRefactoryAstCacheService: ResolveLowLevel failed. NRefactoryResolver.Initialize returned false.");
                return(null);
            }

            if (resolver.CallingClass != null)
            {
                ResolveResult rr;
                if (expressionString == null)
                {
                    // HACK: Re-generate the code for the expression from the expression object by using the code generator.
                    // This is necessary when invoking from inside an AST visitor where the
                    // code belonging to this expression is unavailable.
                    expressionString = resolver.LanguageProperties.CodeGenerator.GenerateCode(expression, String.Empty);
                }
                if ((rr = CtrlSpaceResolveHelper.GetResultFromDeclarationLine(resolver.CallingClass, resolver.CallingMember as IMethodOrProperty, caretLine, caretColumn, new ExpressionResult(expressionString))) != null)
                {
                    return(rr);
                }
            }

            if (resolver.CallingMember != null)
            {
                // Cache member->node mappings to improve performance
                // (if cache is enabled)
                INode memberNode;
                if (!CacheEnabled || !cachedMemberMappings.TryGetValue(resolver.CallingMember, out memberNode))
                {
                    MemberFindAstVisitor visitor = new MemberFindAstVisitor(resolver.CallingMember);
                    compilationUnit.AcceptVisitor(visitor, null);
                    memberNode = visitor.MemberNode;
                    if (CacheEnabled && memberNode != null)
                    {
                        cachedMemberMappings.Add(resolver.CallingMember, memberNode);
                    }
                }

                if (memberNode == null)
                {
                    LoggingService.Info("ResourceToolkit: NRefactoryAstCacheService: Could not find member in AST: " + resolver.CallingMember.ToString());
                }
                else
                {
                    resolver.RunLookupTableVisitor(memberNode);
                }
            }

            return(resolver.ResolveInternal(expression, context));
        }
コード例 #5
0
        // ********************************************************************************************************************************

        /// <summary>
        /// Determines the file which contains the resources referenced by the specified manifest resource name.
        /// </summary>
        /// <param name="sourceFileName">The name of the source code file which the reference occurs in.</param>
        /// <param name="resourceName">The manifest resource name to find the resource file for.</param>
        /// <returns>A <see cref="ResourceSetReference"/> with the specified resource set name and the name of the file that contains the resources with the specified manifest resource name, or <c>null</c> if the file name cannot be determined.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="resourceName"/> parameter is <c>null</c>.</exception>
        public static ResourceSetReference GetResourceSetReference(string sourceFileName, string resourceName)
        {
            if (resourceName == null)
            {
                throw new ArgumentNullException("resourceName");
            }

            IProject p = ProjectFileDictionaryService.GetProjectForFile(sourceFileName);

            if (p != null)
            {
                string fileName;

                if ((fileName = TryGetResourceFileNameFromProjectDirect(resourceName, p)) != null)
                {
                    return(new ResourceSetReference(resourceName, fileName));
                }

                // SharpDevelop silently strips the (hard-coded) folder names
                // "src" and "source" when generating the default namespace name
                // for new files.
                // When MSBuild generates the manifest resource names for the
                // forms designer resources, it uses the type name of the
                // first class in the file. So we should find all files
                // that contain a type with the name in resourceName
                // and then look for dependent resource files or resource files
                // with the same name in the same directory as the source files.

                // Find all source files that contain a type with the same
                // name as the resource we are looking for.
                List <string>   possibleSourceFiles = new List <string>();
                IProjectContent pc = ResourceResolverService.GetProjectContent(p);
                if (pc != null)
                {
                    IClass resourceClass = pc.GetClass(resourceName, 0);

                    if (resourceClass != null)
                    {
                        CompoundClass cc = resourceClass.GetCompoundClass() as CompoundClass;

                        foreach (IClass c in (cc == null ? (IList <IClass>) new IClass[] { resourceClass } : cc.Parts))
                        {
                            if (c.CompilationUnit != null && c.CompilationUnit.FileName != null)
                            {
                                                                #if DEBUG
                                LoggingService.Debug("ResourceToolkit: NRefactoryResourceResolver found file '" + c.CompilationUnit.FileName + "' to contain the type '" + resourceName + "'");
                                                                #endif

                                possibleSourceFiles.Add(c.CompilationUnit.FileName);
                            }
                        }
                    }
                }

                foreach (string possibleSourceFile in possibleSourceFiles)
                {
                    string possibleSourceFileName = Path.GetFileName(possibleSourceFile);

                    // Find resource files dependent on these source files.
                    foreach (ProjectItem pi in p.Items)
                    {
                        FileProjectItem fpi = pi as FileProjectItem;
                        if (fpi != null)
                        {
                            if (fpi.DependentUpon != null &&
                                (fpi.ItemType == ItemType.EmbeddedResource || fpi.ItemType == ItemType.Resource || fpi.ItemType == ItemType.None) &&
                                FileUtility.IsEqualFileName(fpi.DependentUpon, possibleSourceFileName))
                            {
                                                                #if DEBUG
                                LoggingService.Debug("ResourceToolkit: NRefactoryResourceResolver trying to use dependent file '" + fpi.FileName + "' as resource file");
                                                                #endif

                                if ((fileName = FindResourceFileName(fpi.FileName)) != null)
                                {
                                    // Prefer culture-invariant resource file
                                    // over localized resource file
                                    IResourceFileContent rfc = ResourceFileContentRegistry.GetResourceFileContent(fileName);
                                    if (rfc.Culture.Equals(CultureInfo.InvariantCulture))
                                    {
                                        return(new ResourceSetReference(resourceName, fileName));
                                    }
                                }
                            }
                        }
                    }

                    // Fall back to any found resource file
                    // if no culture-invariant resource file was found
                    if (fileName != null)
                    {
                        return(new ResourceSetReference(resourceName, fileName));
                    }

                    // Find resource files with the same name as the source file
                    // and in the same directory.
                    if ((fileName = FindResourceFileName(possibleSourceFile)) != null)
                    {
                        return(new ResourceSetReference(resourceName, fileName));
                    }
                }
            }
            else
            {
                                #if DEBUG
                LoggingService.Info("ResourceToolkit: NRefactoryResourceResolver.GetResourceSetReference could not determine the project for the source file '" + (sourceFileName ?? "<null>") + "'.");
                                #endif

                if (sourceFileName != null)
                {
                    // The project could not be determined.
                    // Try a simple file search.

                    string directory    = Path.GetDirectoryName(sourceFileName);
                    string resourcePart = resourceName;
                    string fileName;

                    while (true)
                    {
                                                #if DEBUG
                        LoggingService.Debug("ResourceToolkit: NRefactoryResourceResolver.GetResourceSetReference: looking for a resource file like '" + Path.Combine(directory, resourcePart) + "'");
                                                #endif

                        if ((fileName = FindResourceFileName(Path.Combine(directory, resourcePart.Replace('.', Path.DirectorySeparatorChar)))) != null)
                        {
                            return(new ResourceSetReference(resourceName, fileName));
                        }
                        if ((fileName = FindResourceFileName(Path.Combine(directory, resourcePart))) != null)
                        {
                            return(new ResourceSetReference(resourceName, fileName));
                        }

                        if (resourcePart.Contains("."))
                        {
                            resourcePart = resourcePart.Substring(resourcePart.IndexOf('.') + 1);
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }

                        #if DEBUG
            LoggingService.Info("ResourceToolkit: NRefactoryResourceResolver.GetResourceSetReference is unable to find a suitable resource file for '" + resourceName + "'");
                        #endif

            return(new ResourceSetReference(resourceName, null));
        }