Exemplo n.º 1
0
 public ResolveExpr(ITargetDatabase db, CsGlobalNamespace globals, ResolveName nameResolver)
 {
     m_globals = globals;
     m_nameResolver = nameResolver;
     m_typeResolver = new ResolveType(db);
     m_memberResolver = new ResolveMembers(db);
 }
Exemplo n.º 2
0
        public Context(Script script, CsGlobalNamespace globals, string text, int selStart, int selLen)
        {
            Contract.Requires(script != null, "script is null");
            Contract.Requires(globals != null, "globals is null");
            Contract.Requires(text != null, "text is null");
            Contract.Requires(selStart >= 0, "selStart is negative");
            Contract.Requires(selLen >= 0, "selLen is negative");

            Script = script;
            Text = text;
            Globals = globals;
            SelStart = selStart;
            SelLen = selLen;
        }
Exemplo n.º 3
0
        public Parse(int edit, string key, int index, int length, CsGlobalNamespace globals, Token[] comments, Token[] tokens)
        {
            Contract.Requires(!string.IsNullOrEmpty(key), "key is null or empty");
            Contract.Requires(index >= 0, "index is negative");
            Contract.Requires(length >= 0, "length is negative");
            Contract.Requires(comments != null, "comments is null");
            Contract.Requires(tokens != null, "tokens is null");
            Contract.Requires(globals != null || length >= 0, "null globals but error length is not set");

            Key = key;
            Edit = edit;
            ErrorIndex = index;
            ErrorLength = length;
            Globals = globals;
            Comments = comments;
            Tokens = tokens;
        }
Exemplo n.º 4
0
        public ResolveName(CsMember context, ITargetDatabase database, ICsLocalsParser locals, string text, int offset, CsGlobalNamespace globals)
        {
            Profile.Start("ResolveName::ctor");
            m_database = database;
            m_typeResolver = new ResolveType(database);
            m_globals = globals;
            m_offset = offset;
            m_context = context;

            Profile.Start("DoFindMember");
            m_member = DoFindMember(m_globals);
            Profile.Stop("DoFindMember");

            Profile.Start("DoGetVariables");
            m_variables = DoGetVariables(text, locals);
            Profile.Stop("DoGetVariables");

            Profile.Stop("ResolveName::ctor");
        }
Exemplo n.º 5
0
        // Usually this will return zero or one name, but it can return more (eg for explicit interface
        // implementations).
        public Item[] Find(CsMember context, ResolvedTarget target, CsGlobalNamespace globals, string name, int arity)
        {
            Profile.Start("ResolveMembers::Find");
            var items = new List<Item>();

            var types = new List<CsType>();
            var baseNames = new List<string>();
            var interfaceNames = new List<string>();
            string[] allNames = DoGetBases(globals, target.TypeName, types, baseNames, interfaceNames);

            bool includeProtected = context != null && allNames.Contains(context.DeclaringType.FullName);
            foreach (CsType type in types)
            {
                var candidates = new List<Item>();

                bool includePrivate = context != null && type.FullName == context.DeclaringType.FullName;
                DoGetParsedMembers(type, target.IsInstance, target.IsStatic, candidates, includePrivate, includeProtected);
                items = (from m in candidates where DoMatch(m, name, arity) select m).ToList();
            }

            // Note that we need to make two GetMembers queries to ensure that interface
            // methods are not used in place of base methods (this gives us better results
            // when the context menu is used to filter out methods associated with types).
            DoAddIfMissingRange("Fields:", items, m_database.GetFields(baseNames.ToArray(), target.IsInstance, target.IsStatic, name, includeProtected));
            DoAddIfMissingRange("Base Members:", items, m_database.GetMembers(baseNames.ToArray(), target.IsInstance, target.IsStatic, name, arity, includeProtected));
            DoAddIfMissingRange("Interface Members:", items, m_database.GetMembers(interfaceNames.ToArray(), target.IsInstance, target.IsStatic, name, arity, includeProtected));

            if (target.IsInstance)
            {
                var namespaces = new List<string>();

                for (int i = 0; i < globals.Namespaces.Length; ++i)
                    namespaces.Add(globals.Namespaces[i].Name);

                for (int i = 0; i < globals.Uses.Length; ++i)
                    namespaces.Add(globals.Uses[i].Namespace);

                DoAddIfMissingRange("extension methods:", items, m_database.GetExtensionMethods(allNames, namespaces.ToArray(), name, arity));
            }

            Profile.Stop("ResolveMembers::Find");
            return items.ToArray();
        }
Exemplo n.º 6
0
		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("}");
		}
Exemplo n.º 7
0
        private void DoGetParsedExtensions(CsGlobalNamespace globals, string targetType, List<Item> items)
        {
            CsType[] types = DoGetAllParsedTypes(globals);

            foreach (CsType type in types)
            {
                foreach (CsMethod method in type.Methods)
                {
                    if (method.IsExtension)
                    {
                        string fullName = DoGetFullParsedName(globals, method.Parameters[0].Type);

                        if (targetType == fullName)
                        {
                            List<string> argTypes = (from p in method.Parameters select p.ModifiedType).ToList();
                            List<string> argNames = (from p in method.Parameters select p.Name).ToList();

                            argTypes.RemoveAt(0);
                            argNames.RemoveAt(0);

                            // TODO: should add gargs if they cannot be deduced
                            items.AddIfMissing(new MethodItem(method.ReturnType, method.Name, null, argTypes.ToArray(), argNames.ToArray(), method.ReturnType, "extension methods"));
                        }
                    }
                }
            }
        }
Exemplo n.º 8
0
        private void DoGetParsedBases(CsGlobalNamespace globals, CsType type, List<string> baseNames, List<string> interfaceNames, List<string> allNames)
        {
            baseNames.AddIfMissing("System.Object");
            allNames.AddIfMissing("System.Object");

            foreach (string name in type.Bases.Names)
            {
                string fullName = DoGetFullParsedName(globals, name);

                if (fullName != null)
                {
                    if (CsHelpers.IsInterface(fullName))
                        interfaceNames.AddIfMissing(fullName);
                    else
                        baseNames.AddIfMissing(fullName);

                    allNames.AddIfMissing(fullName);
                }
            }
        }
Exemplo n.º 9
0
        private void DoAddRealTypes(List<Item> items, CsGlobalNamespace globals, string stem)
        {
            var namespaces = new List<string>();

            DoAddParsedTypes(items, (string) null, stem);
            namespaces.AddIfMissing(string.Empty);

            for (int i = 0; i < globals.Namespaces.Length; ++i)
            {
                DoAddParsedTypes(items, globals.Namespaces[i].Name, stem);
                namespaces.AddIfMissing(globals.Namespaces[i].Name);
            }

            for (int i = 0; i < globals.Uses.Length; ++i)
            {
                DoAddParsedTypes(items, globals.Uses[i].Namespace, stem);
                namespaces.AddIfMissing(globals.Uses[i].Namespace);
            }

            items.AddIfMissingRange(m_database.GetStemmedTypes(namespaces.ToArray(), stem));
        }
Exemplo n.º 10
0
        private string[] DoGetBases(CsGlobalNamespace globals, string typeName, List<CsType> types, List<string> baseNames, List<string> interfaceNames)
        {
            #if TEST
            var parses = new CsParser.Parses();
            #else
            Boss boss = ObjectModel.Create("CsParser");
            var parses = boss.Get<IParses>();
            #endif

            var allNames = new List<string>();
            allNames.Add(typeName);

            int i = 0;
            while (i < allNames.Count)
            {
                string name = allNames[i++];

                // Note that we want to use CsType instead of the database where possible
                // because it should be more up to date.
                CsType type = parses.FindType(name);
                if (type != null)
                    types.Add(type);

                if (type is CsEnum)
                {
                    name = "System.Enum";
                    type = null;
                }

                // If the type is partial then the parsed types (probably) do not include all
                // of the items so we need to include both the parsed and database
                // info to ensure we get everything.
                if (type == null || (type.Modifiers & MemberModifiers.Partial) != 0)
                    if (CsHelpers.IsInterface(name))
                        interfaceNames.AddIfMissing(name);
                    else
                        baseNames.AddIfMissing(name);

                if (type != null)
                    DoGetParsedBases(globals, type, baseNames, interfaceNames, allNames);
                else
                    m_database.GetBases(name, baseNames, interfaceNames, allNames);
            }

            return allNames.ToArray();
        }
Exemplo n.º 11
0
        private CsType[] DoGetAllParsedTypes(CsGlobalNamespace globals)
        {
            var types = new List<CsType>();

            #if !TEST
            types.AddRange(m_parses.FindTypes(null, string.Empty));

            for (int i = 0; i < globals.Namespaces.Length; ++i)
                types.AddRange(m_parses.FindTypes(globals.Namespaces[i].Name, string.Empty));

            for (int i = 0; i < globals.Uses.Length; ++i)
                types.AddRange(m_parses.FindTypes(globals.Uses[i].Namespace, string.Empty));
            #endif

            return types.ToArray();
        }
Exemplo n.º 12
0
        // Note that we take some pains to recover from parser errors. This is important
        // because they are quite common while code is being edited and we don't want
        // to lose all information about a type just because a brace is missing.
        public void TryParse(string text, out int offset, out int length, out CsGlobalNamespace globals, out Token[] tokens, out Token[] comments)
        {
            Contract.Requires(Thread.CurrentThread.ManagedThreadId == m_threadID, "can only be used with one thread");

            m_try = true;
            m_bad = new Token();
            try
            {
                DoInit(text);
            }
            catch (ScannerException e)
            {
                if (m_scanner != null)
                    m_bad = m_scanner.Token;
                if (m_bad.Length == 0)
                    m_bad = new Token(text, 0, 1, 1, TokenKind.Other);
                Log.WriteLine(TraceLevel.Verbose, "CsParser", "couldn't scan the first token: {0}", e.Message);
            }

            if (m_bad.Length == 0)
                globals = DoParseCompilationUnit();
            else
                globals = null;

            if (m_bad.Length == 0)
            {
                offset = 0;
                length = 0;
                globals.Malformed = false;
                Log.WriteLine(TraceLevel.Verbose, "CsParser", "parsed ok");
            }
            else
            {
                offset = m_bad.Offset;
                length = m_bad.Length;
                if (globals != null)
                    globals.Malformed = true;
                Log.WriteLine(TraceLevel.Verbose, "CsParser", "{0} was bad at offset {1}", text.Substring(offset, length), offset);
            }

            comments = m_scanner.Comments;
            tokens = m_scanner.Tokens;
        }
Exemplo n.º 13
0
        private Item[] DoGetNames(CsGlobalNamespace globals, int location, string stem, ref bool isInstance, ref bool isStatic)
        {
            Profile.Start("AutoComplete::DoGetNames");
            var result = new List<Item>();

            var context = FindDeclaration(globals, location) as CsMember;
            var nameResolver = new ResolveName(context, m_database, m_locals, m_text.Text, location, globals);
            ResolvedTarget target = nameResolver.Resolve("<this>");
            if (target != null)
            {
                var items = new List<Item>(m_members.Resolve(context, target, globals));
                foreach (Variable v in nameResolver.Variables)
                {
                    items.AddIfMissing(new NameItem(v.Name, v.Type + ' ' + v.Name, v.Filter, v.Type));
                }

                if (stem.Length > 0)
                    items.RemoveAll(m => !m.Text.StartsWith(stem));

                result = items;
                isInstance = target.IsInstance;
                isStatic = target.IsStatic;
            }

            if (stem.Length > 0)
            {
                DoAddAliasedTypes(result, stem);
                DoAddRealTypes(result, globals, stem);
            }

            Profile.Stop("AutoComplete::DoGetNames");
            return result.ToArray();
        }
Exemplo n.º 14
0
        private Item[] DoGetConstructorsNamed(CsGlobalNamespace globals, ref string stem)
        {
            Profile.Start("AutoComplete::DoGetConstructorsNamed");
            var items = new List<Item>();
            var namespaces = new List<string>();

            int j = stem.LastIndexOf('.');
            if (j > 0)
            {
                string ns = stem.Substring(0, j);
                stem = stem.Substring(j + 1);
                DoAddConstructors(ns, stem, items);
            }
            else
            {
                DoAddConstructors(null, stem, items);
                namespaces.AddIfMissing(string.Empty);

                for (int i = 0; i < globals.Namespaces.Length; ++i)
                {
                    DoAddConstructors(globals.Namespaces[i].Name, stem, items);
                    namespaces.AddIfMissing(globals.Namespaces[i].Name);
                }

                for (int i = 0; i < globals.Uses.Length; ++i)
                {
                    DoAddConstructors(globals.Uses[i].Namespace, stem, items);
                    namespaces.AddIfMissing(globals.Uses[i].Namespace);
                }
            }

            items.AddIfMissingRange(m_database.GetStemmedCtors(namespaces.ToArray(), stem));

            Profile.Stop("AutoComplete::DoGetConstructorsNamed");
            return items.ToArray();
        }
Exemplo n.º 15
0
        private Item[] DoGetAliasedConstructorsNamed(CsGlobalNamespace globals, string stem)
        {
            var items = new List<Item>();

            foreach (string alias in CsHelpers.GetAliasedNames())
            {
                if (alias.StartsWith(stem))
                {
                    string name = CsHelpers.GetRealName(alias);
                    items.AddRange(DoGetConstructorsNamed(globals, ref name));
                }
            }

            return items.ToArray();
        }
Exemplo n.º 16
0
 public void AddParse(string key, CsGlobalNamespace globals)
 {
     var parse = new Parse(0, "test.cs", 0, 0, globals, new Token[0], new Token[0]);
     lock (m_mutex)
     {
         m_parses[key] = parse;
     }
 }
Exemplo n.º 17
0
 private object DoGetAttributes(CsGlobalNamespace globals)
 {
     return globals.Attributes;
 }
Exemplo n.º 18
0
        private string DoGetFullParsedName(CsGlobalNamespace globals, string inName)
        {
            string typeName = null;
            string name = DoGetRootName(inName);

            string candidate = name;
            if (DoHasType(candidate))
                typeName = candidate;

            for (int i = 0; i < globals.Namespaces.Length && typeName == null; ++i)
            {
                candidate = globals.Namespaces[i].Name + "." + name;
                if (DoHasType(candidate))
                    typeName = candidate;
            }

            for (int i = 0; i < globals.Uses.Length && typeName == null; ++i)
            {
                candidate = globals.Uses[i].Namespace + "." + name;
                if (DoHasType(candidate))
                    typeName = candidate;
            }

            return typeName;
        }