/// <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);
        }
        /// <summary>
        /// Attempts to resolve a reference to a resource.
        /// </summary>
        /// <param name="fileName">The name of the file that contains the expression to be resolved.</param>
        /// <param name="document">The document that contains the expression to be resolved.</param>
        /// <param name="caretLine">The 0-based line in the file that contains the expression to be resolved.</param>
        /// <param name="caretColumn">The 0-based column position of the expression to be resolved.</param>
        /// <param name="caretOffset">The offset of the position of the expression to be resolved.</param>
        /// <param name="charTyped">The character that has been typed at the caret position but is not yet in the buffer (this is used when invoked from code completion), or <c>null</c>.</param>
        /// <returns>A <see cref="ResourceResolveResult"/> that describes which resource is referenced by the expression at the specified position in the specified file, or <c>null</c> if that expression does not reference a (known) resource.</returns>
        protected override ResourceResolveResult Resolve(string fileName, IDocument document, int caretLine, int caretColumn, int caretOffset, char?charTyped)
        {
            // If Resolve is invoked from code completion,
            // we are only interested in the ':' character of '${res:'.
            if (charTyped != null && charTyped != ':')
            {
                return(null);
            }

            // Find $ character to the left of the caret.
            char ch = document.GetCharAt(caretOffset);

            if (ch == '}' && caretOffset > 0)
            {
                ch = document.GetCharAt(--caretOffset);
            }
            while (!Char.IsWhiteSpace(ch) && ch != '$' && ch != '}' && caretOffset > 0)
            {
                ch = document.GetCharAt(--caretOffset);
            }

            if (caretOffset + 6 >= document.TextLength || document.GetText(caretOffset, 6) != ResourceReferenceToken)
            {
                return(null);
            }
            caretOffset += 6;

            // Read resource key.
            StringBuilder key = new StringBuilder();

            while (caretOffset < document.TextLength && !Char.IsWhiteSpace(ch = document.GetCharAt(caretOffset++)) && ch != '}')
            {
                key.Append(ch);
            }
            if (ch != '}')
            {
                key = null;
                                #if DEBUG
            }
            else
            {
                LoggingService.Debug("ResourceToolkit: ICSharpCodeCoreResourceResolver found resource key: " + key.ToString());
                                #endif
            }

            ResourceSetReference resource = ResolveICSharpCodeCoreResourceSet(key == null ? null : key.ToString(), fileName);

                        #if DEBUG
            if (resource.FileName == null)
            {
                LoggingService.Info("ResourceToolkit: ICSharpCodeCoreResourceResolver: Could not find the ICSharpCode.Core resource file name for the source file '" + fileName + "', key '" + (key == null ? "<null>" : key.ToString()) + "'.");
            }
                        #endif

            // TODO: Add information about callingClass, callingMember, returnType
            return(new ResourceResolveResult(null, null, null, resource, key == null ? null : key.ToString()));
        }
        /// <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);
        }
        // ********************************************************************************************************************************

        /// <summary>
        /// Tries to find the resource set which serves as source for
        /// the resources accessed by the ICSharpCode.Core.
        /// </summary>
        /// <param name="key">The resource key to look for. May be null.</param>
        /// <param name="sourceFileName">The name of the source code file that contains the reference to the resource.</param>
        public static ResourceSetReference ResolveICSharpCodeCoreResourceSet(string key, string sourceFileName)
        {
            // As there is no easy way to find out the actual location of the resources
            // based on the source code, we just look in some standard directories.

            // Local set (SD addin or standalone application with standard directory structure)
            ResourceSetReference local = GetICSharpCodeCoreLocalResourceSet(sourceFileName);

            // Prefer local set, especially if the key is there.
            if (local.ResourceFileContent != null)
            {
                if (key != null)
                {
                    if (local.ResourceFileContent.ContainsKey(key))
                    {
                        return(local);
                    }
                }
                else
                {
                    return(local);
                }
            }

            // Resource set of the host application
            ResourceSetReference host = GetICSharpCodeCoreHostResourceSet(sourceFileName);

            if (key != null)
            {
                if (host.ResourceFileContent != null)
                {
                    if (host.ResourceFileContent.ContainsKey(key))
                    {
                        return(host);
                    }
                }
            }

            // Use local file also if the key is not there
            // (allows adding of a new key)
            return(local.ResourceFileContent == null ? host : local);
        }
        /// <summary>
        /// Tries to find a resource reference in the specified expression.
        /// </summary>
        /// <param name="resolveResult">The ResolveResult that describes the referenced member.</param>
        /// <param name="expr">The AST representation of the full expression.</param>
        /// <returns>
        /// The ResourceResolveResult describing the referenced resource, if successful,
        /// or a null reference, if the referenced member is not a resource manager
        /// or if the resource file cannot be determined.
        /// </returns>
        static ResourceResolveResult ResolveResource(ResolveResult resolveResult, Expression expr)
        {
            ResourceSetReference rsr = null;

            MemberResolveResult mrr = resolveResult as MemberResolveResult;

            if (mrr != null)
            {
                rsr = ResolveResourceSet(mrr.ResolvedMember);
            }
            else
            {
                LocalResolveResult lrr = resolveResult as LocalResolveResult;
                if (lrr != null)
                {
                    if (!lrr.IsParameter)
                    {
                        rsr = ResolveResourceSet(lrr.Field);
                    }
                }
            }

            if (rsr != null)
            {
                bool   isPrefixOnly;
                string key = GetKeyFromExpression(expr, out isPrefixOnly);

                if (isPrefixOnly)
                {
                    return(new ResourcePrefixResolveResult(resolveResult.CallingClass, resolveResult.CallingMember, null, rsr, key));
                }
                else
                {
                    return(new ResourceResolveResult(resolveResult.CallingClass, resolveResult.CallingMember, null, rsr, key));
                }
            }

            return(null);
        }
            public override object TrackedVisitObjectCreateExpression(ObjectCreateExpression objectCreateExpression, object data)
            {
                if (data as bool? ?? false) {

                    #if DEBUG
                    LoggingService.Debug("ResourceToolkit: BclNRefactoryResourceResolver found object initialization: "+objectCreateExpression.ToString());
                    #endif

                    // Resolve the constructor.
                    // A type derived from the declaration type is also allowed.
                    MemberResolveResult mrr = this.Resolve(objectCreateExpression) as MemberResolveResult;

                    #if DEBUG
                    if (mrr != null) {
                        LoggingService.Debug("ResourceToolkit: BclNRefactoryResourceResolver: resolved constructor: "+mrr.ResolvedMember.ToString());
                    }
                    #endif

                    if (mrr != null &&
                        mrr.ResolvedMember is IMethod &&
                        (mrr.ResolvedMember.DeclaringType.CompareTo(this.resourceManagerMember.ReturnType.GetUnderlyingClass()) == 0 ||
                         mrr.ResolvedMember.DeclaringType.IsTypeInInheritanceTree(this.resourceManagerMember.ReturnType.GetUnderlyingClass()))
                       ) {

                        #if DEBUG
                        LoggingService.Debug("ResourceToolkit: BclNRefactoryResourceResolver: This is the correct constructor.");
                        #endif

                        // This most probably is the resource manager initialization we are looking for.
                        // Find a parameter that indicates the resources being referenced.

                        foreach (Expression param in objectCreateExpression.Parameters) {

                            PrimitiveExpression p = param as PrimitiveExpression;
                            if (p != null) {
                                string pValue = p.Value as string;
                                if (!String.IsNullOrEmpty(pValue)) {

                                    #if DEBUG
                                    LoggingService.Debug("ResourceToolkit: BclNRefactoryResourceResolver found string parameter: '"+pValue+"'");
                                    #endif

                                    this.foundResourceSet = NRefactoryResourceResolver.GetResourceSetReference(this.resourceManagerMember.DeclaringType.CompilationUnit.FileName, pValue);
                                    #if DEBUG
                                    if (this.foundResourceSet.FileName != null) {
                                        LoggingService.Debug("ResourceToolkit: BclNRefactoryResourceResolver found resource file: "+this.foundResourceSet.FileName);
                                    }
                                    #endif

                                    break;

                                }

                                continue;
                            }

                            // Support typeof(...)
                            TypeOfExpression t = param as TypeOfExpression;
                            if (t != null && this.PositionAvailable) {

                                #if DEBUG
                                LoggingService.Debug("ResourceToolkit: BclNRefactoryResourceResolver: Found TypeOfExpression in constructor call: "  + t.ToString());
                                #endif

                                ResolveResult rr = this.Resolve(new TypeReferenceExpression(t.TypeReference), ExpressionContext.Type);

                                #if DEBUG
                                if (rr == null) {
                                    LoggingService.Debug("ResourceToolkit: BclNRefactoryResourceResolver: The TypeReference of the TypeOfExpression could not be resolved.");
                                } else {
                                    LoggingService.Debug("ResourceToolkit: BclNRefactoryResourceResolver: The TypeReference resolved to: " + rr.ToString());
                                }
                                #endif

                                if (rr != null) {

                                    #if DEBUG
                                    LoggingService.Debug("ResourceToolkit: BclNRefactoryResourceResolver found typeof(...) parameter, type: '"+rr.ResolvedType.ToString()+"'");
                                    #endif

                                    this.foundResourceSet = NRefactoryResourceResolver.GetResourceSetReference(this.resourceManagerMember.DeclaringType.CompilationUnit.FileName, rr.ResolvedType.FullyQualifiedName);
                                    #if DEBUG
                                    if (this.foundResourceSet.FileName != null) {
                                        LoggingService.Debug("ResourceToolkit: BclNRefactoryResourceResolver found resource file: "+this.foundResourceSet.FileName);
                                    }
                                    #endif

                                    break;

                                }

                            }

                        }

                    }

                }

                return base.TrackedVisitObjectCreateExpression(objectCreateExpression, data);
            }
Exemplo n.º 7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ResourceResolveResult"/> class.
 /// </summary>
 /// <param name="callingClass">The class that contains the reference to the resource.</param>
 /// <param name="callingMember">The member that contains the reference to the resource.</param>
 /// <param name="returnType">The type of the resource being referenced.</param>
 /// <param name="resourceSetReference">The <see cref="ResourceSetReference"/> that describes the resource set being referenced.</param>
 /// <param name="key">The resource key being referenced.</param>
 public ResourceResolveResult(IClass callingClass, IMember callingMember, IReturnType returnType, ResourceSetReference resourceSetReference, string key)
     : base(callingClass, callingMember, returnType)
 {
     this.resourceSetReference = resourceSetReference;
     this.key = key;
 }
Exemplo n.º 8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ResourceResolveResult"/> class.
 /// </summary>
 /// <param name="callingClass">The class that contains the reference to the resource.</param>
 /// <param name="callingMember">The member that contains the reference to the resource.</param>
 /// <param name="returnType">The type of the resource being referenced.</param>
 /// <param name="resourceSetReference">The <see cref="ResourceSetReference"/> that describes the resource set being referenced.</param>
 /// <param name="prefix">The prefix of the resource keys being referenced.</param>
 public ResourcePrefixResolveResult(IClass callingClass, IMember callingMember, IReturnType returnType, ResourceSetReference resourceSetReference, string prefix)
     : base(callingClass, callingMember, returnType, resourceSetReference, prefix)
 {
 }
		/// <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;
		}
		/// <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;
		}
            public override object TrackedVisitObjectCreateExpression(ObjectCreateExpression objectCreateExpression, object data)
            {
                if (data as bool? ?? false)
                {
                                        #if DEBUG
                    LoggingService.Debug("ResourceToolkit: BclNRefactoryResourceResolver found object initialization: " + objectCreateExpression.ToString());
                                        #endif

                    // Resolve the constructor.
                    // A type derived from the declaration type is also allowed.
                    MemberResolveResult mrr = this.Resolve(objectCreateExpression) as MemberResolveResult;

                                        #if DEBUG
                    if (mrr != null)
                    {
                        LoggingService.Debug("ResourceToolkit: BclNRefactoryResourceResolver: resolved constructor: " + mrr.ResolvedMember.ToString());
                    }
                                        #endif

                    if (mrr != null &&
                        mrr.ResolvedMember is IMethod &&
                        (mrr.ResolvedMember.DeclaringType.CompareTo(this.resourceManagerMember.ReturnType.GetUnderlyingClass()) == 0 ||
                         mrr.ResolvedMember.DeclaringType.IsTypeInInheritanceTree(this.resourceManagerMember.ReturnType.GetUnderlyingClass()))
                        )
                    {
                                                #if DEBUG
                        LoggingService.Debug("ResourceToolkit: BclNRefactoryResourceResolver: This is the correct constructor.");
                                                #endif

                        // This most probably is the resource manager initialization we are looking for.
                        // Find a parameter that indicates the resources being referenced.

                        foreach (Expression param in objectCreateExpression.Parameters)
                        {
                            PrimitiveExpression p = param as PrimitiveExpression;
                            if (p != null)
                            {
                                string pValue = p.Value as string;
                                if (!String.IsNullOrEmpty(pValue))
                                {
                                                                        #if DEBUG
                                    LoggingService.Debug("ResourceToolkit: BclNRefactoryResourceResolver found string parameter: '" + pValue + "'");
                                                                        #endif

                                    this.foundResourceSet = NRefactoryResourceResolver.GetResourceSetReference(this.resourceManagerMember.DeclaringType.CompilationUnit.FileName, pValue);
                                                                        #if DEBUG
                                    if (this.foundResourceSet.FileName != null)
                                    {
                                        LoggingService.Debug("ResourceToolkit: BclNRefactoryResourceResolver found resource file: " + this.foundResourceSet.FileName);
                                    }
                                                                        #endif

                                    break;
                                }

                                continue;
                            }

                            // Support typeof(...)
                            TypeOfExpression t = param as TypeOfExpression;
                            if (t != null && this.PositionAvailable)
                            {
                                                                #if DEBUG
                                LoggingService.Debug("ResourceToolkit: BclNRefactoryResourceResolver: Found TypeOfExpression in constructor call: " + t.ToString());
                                                                #endif

                                ResolveResult rr = this.Resolve(new TypeReferenceExpression(t.TypeReference), ExpressionContext.Type);

                                                                #if DEBUG
                                if (rr == null)
                                {
                                    LoggingService.Debug("ResourceToolkit: BclNRefactoryResourceResolver: The TypeReference of the TypeOfExpression could not be resolved.");
                                }
                                else
                                {
                                    LoggingService.Debug("ResourceToolkit: BclNRefactoryResourceResolver: The TypeReference resolved to: " + rr.ToString());
                                }
                                                                #endif

                                if (rr != null)
                                {
                                                                        #if DEBUG
                                    LoggingService.Debug("ResourceToolkit: BclNRefactoryResourceResolver found typeof(...) parameter, type: '" + rr.ResolvedType.ToString() + "'");
                                                                        #endif

                                    this.foundResourceSet = NRefactoryResourceResolver.GetResourceSetReference(this.resourceManagerMember.DeclaringType.CompilationUnit.FileName, rr.ResolvedType.FullyQualifiedName);
                                                                        #if DEBUG
                                    if (this.foundResourceSet.FileName != null)
                                    {
                                        LoggingService.Debug("ResourceToolkit: BclNRefactoryResourceResolver found resource file: " + this.foundResourceSet.FileName);
                                    }
                                                                        #endif

                                    break;
                                }
                            }
                        }
                    }
                }

                return(base.TrackedVisitObjectCreateExpression(objectCreateExpression, data));
            }
		/// <summary>
		/// Initializes a new instance of the <see cref="ResourceResolveResult"/> class.
		/// </summary>
		/// <param name="callingClass">The class that contains the reference to the resource.</param>
		/// <param name="callingMember">The member that contains the reference to the resource.</param>
		/// <param name="returnType">The type of the resource being referenced.</param>
		/// <param name="resourceSetReference">The <see cref="ResourceSetReference"/> that describes the resource set being referenced.</param>
		/// <param name="key">The resource key being referenced.</param>
		public ResourceResolveResult(IClass callingClass, IMember callingMember, IReturnType returnType, ResourceSetReference resourceSetReference, string key)
			: base(callingClass, callingMember, returnType)
		{
			this.resourceSetReference = resourceSetReference;
			this.key = key;
		}
		/// <summary>
		/// Initializes a new instance of the <see cref="ResourceResolveResult"/> class.
		/// </summary>
		/// <param name="callingClass">The class that contains the reference to the resource.</param>
		/// <param name="callingMember">The member that contains the reference to the resource.</param>
		/// <param name="returnType">The type of the resource being referenced.</param>
		/// <param name="resourceSetReference">The <see cref="ResourceSetReference"/> that describes the resource set being referenced.</param>
		/// <param name="prefix">The prefix of the resource keys being referenced.</param>
		public ResourcePrefixResolveResult(IClass callingClass, IMember callingMember, IReturnType returnType, ResourceSetReference resourceSetReference, string prefix)
			: base(callingClass, callingMember, returnType, resourceSetReference, prefix)
		{
		}