示例#1
0
        public static string GetDirectory(CsDeclaration csDeclaration)
        {
            var type  = csDeclaration.CsType.OriginalType;
            var parts = GetFullName(type).Replace("+", ".").Split('.');

            return(Path.Combine(parts.Take(parts.Length - 1).ToArray()));
        }
示例#2
0
 public virtual TsFile Rewrite(CsDeclaration csDeclaration)
 {
     return(new TsFile
     {
         Name = csDeclaration.Name,
         Directory = Helpers.GetDirectory(csDeclaration),
         Declarations = new[]
         {
             RewriteInternal(csDeclaration)
         }
     });
 }
示例#3
0
        protected virtual TsDeclaration RewriteInternal(CsDeclaration csDeclaration)
        {
            switch (csDeclaration)
            {
            case CsEnum csEnum:
                return(Rewrite(csEnum));

            case CsClass csClass:
                return(Rewrite(csClass));

            case CsInterface csInterface:
                return(Rewrite(csInterface));

            case CsStruct csStruct:
                return(Rewrite(csStruct));

            default:
                throw new ArgumentOutOfRangeException(nameof(csDeclaration), csDeclaration,
                                                      null);
            }
        }
		private string DoGetName(CsDeclaration dec)
		{
			string name = string.Empty;
			
			do
			{
				var member = dec as CsMember;
				if (member != null)
				{
					name = member.Name;
					break;
				}
				
				var ns = dec as CsNamespace;
				if (ns != null)
				{
					name = ns.Name;
					break;
				}
				
				var type = dec as CsType;
				if (type != null)
				{
					name = type.Name;
					break;
				}
			}
			while (false);
			
			return name;
		}
		private CsNamespace DoGetNamespace(CsDeclaration first)
		{
			CsNamespace ns = null;
			
			// If the declaration is a type then use whatever namespace the
			// type is declared in.
			CsTypeScope type = first as CsTypeScope;
			if (type != null)
			{
				ns = type.Namespace;
			}
			else
			{
				// If the declaration is a member then use whatever namespace
				// its declaring type was declared in.
				CsMember member = first as CsMember;
				if (member != null && member.DeclaringType != null)
				{
					ns = member.DeclaringType.Namespace;
				}
				
				// Special case enums and delegates declared in a namespace
				// scope.
				CsDelegate d = first as CsDelegate;
				if (ns == null && d != null)
					ns = d.Namespace;
				
				CsEnum e = first as CsEnum;
				if (ns == null && e != null)
					ns = e.Namespace;
			}
			
			return ns;
		}
		private void DoBuildNewFile(string name, CsGlobalNamespace globals, CsDeclaration first, StringBuilder builder, string text, int offset, int length)
		{
			// If the file starts with a comment then write it out.
			if (text.Length > 2 && text[0] == '/' && text[1] == '/')
				DoWriteSingleLineComments(builder, text);
			else if (text.Length > 2 && text[0] == '/' && text[1] == '*')
				DoWriteDelimitedComment(builder, text);
			
			// Write the global using directives.
			DoWriteUsing(builder, globals, string.Empty);
			
			// Write the namespace the declaration was in.
			CsNamespace ns = DoGetNamespace(first);
			if (ns != null && ns.Name != "<globals>")
			{
				builder.WriteLine("namespace {0}{1}{2}", ns.Name, Constants.Bullet, "{");
				DoWriteUsing(builder, ns, "\t");
			}
			
			// If we're moving a member then create a dummy type.
			CsMember member = first as CsMember;
			if (member != null && member.DeclaringType != null)
			{
				string keyword = "?";
				if (member.DeclaringType is CsClass)
					keyword = "class";
				else if (member.DeclaringType is CsInterface)
					keyword = "interface";
				else if (member.DeclaringType is CsStruct)
					keyword = "struct";
					
				string modifiers;
				if (member.DeclaringType != null)
					modifiers = member.DeclaringType.Modifiers.ToString().ToLower();
				else
					modifiers = member.Modifiers.ToString().ToLower();
				modifiers = modifiers.Replace(",", string.Empty);
				
				builder.WriteLine("\t{0} {1} {2}", modifiers, keyword, name);
				builder.WriteLine("\t{");
			}
			
			// Write the selection.
			builder.Write(text.Substring(offset, length));
			
			// Close up type and namespaces.
			if (member != null && member.DeclaringType != null)
				builder.WriteLine("\t}");
			
			if (ns  != null && ns.Name != "<globals>")
				builder.WriteLine("}");
		}
示例#7
0
        private CsDeclaration DoFindScope(CsDeclaration declaration, int offset)
        {
            CsTypeScope outer = declaration as CsTypeScope;
            if (outer != null)
            {
                foreach (CsDeclaration nested in outer.Declarations)
                {
                    if (offset >= nested.Offset && offset < nested.Offset + nested.Length)
                        return DoFindScope(nested, offset);
                }
            }

            return declaration;
        }