Beispiel #1
0
        private void AddBaseTypes(Type baseType, TreeNode rootNode)
        {
            List <Type> types = new List <Type>();

            if (baseType.BaseType != null)
            {
                types.Add(baseType.BaseType);
            }

            foreach (Type iface in baseType.GetInterfaces())
            {
                if (iface != null)
                {
                    types.Add(iface);
                }
            }

            foreach (Type type in types)
            {
                TreeNode node = new TreeNode();
                node.Text             = CSharpFormattingTools.GetTypeSignature(type);
                node.SelectedImageKey = node.ImageKey = GetTypeImageKey(type);
                node.Tag  = type;
                node.Name = type.GUID.ToString();

                AddBaseTypes(type, node);

                rootNode.Nodes.Add(node);
            }
        }
Beispiel #2
0
        private string GetParameterNames(MethodDefinition method)
        {
            string parameters = method.Parameters.Trim();

            if (parameters == String.Empty)
            {
                return(String.Empty);
            }

            parameters = CSharpFormattingTools.
                         RemoveUnwantedBracketText(parameters);

            parameters = CSharpFormattingTools.
                         RemoveUnwantedAngleBracketText(parameters);

            StringBuilder sb = new StringBuilder();

            foreach (string s in parameters.Split(','))
            {
                string   p     = s.Trim();
                string[] split = p.Split();

                sb.Append(split[split.Length - 1]);
                sb.Append(", ");
            }

            return(sb.ToString().Trim().TrimEnd(','));
        }
Beispiel #3
0
        private AspNetTag GetAspNetTag(string text)
        {
            text = CSharpFormattingTools.RemoveUnwantedText(text);

            string[] split1 = text.Split('<');
            string[] split2 = split1[split1.Length - 1].Split('>');
            string   tag    = split2[0];

            AspNetTag aspNetTag = new AspNetTag(text);

            if (split2.Length > 1)
            {
                return(null);
            }

            /*
             * Check tag is ASP.NET and get the type.
             */

            Regex re = new Regex(@"^/?(\w+):(\w*)(\s*)");
            Match m  = re.Match(tag);

            if (m.Success)
            {
                aspNetTag.TagPrefix      = m.Groups[1].Value;
                aspNetTag.LookAhead      = m.Groups[2].Value;
                aspNetTag.ClosingTag     = tag.StartsWith("/");
                aspNetTag.WantAttributes =
                    m.Groups[3].Value != String.Empty;

                return(aspNetTag);
            }

            return(null);
        }
Beispiel #4
0
        /*
         * Get a list of all the types from which the current
         * class is derived. This includes the base class and
         * any interfacess. If the class is not explicitly
         * derived from a class return System.Object as the
         * base class.
         */

        protected List <String> GetCodeBaseTypeList(string source)
        {
            List <String> list = new List <String>();

            source = CSharpFormattingTools.RemoveClassDefinitions(source);

            Regex re = new Regex(@"(?s)class\s+[\w]+\s*:?\s*[global::]*([\w\s\.,]*)\s*\{");
            Match m  = re.Match(source);

            if (m.Success)
            {
                string types = m.Groups[1].Value;

                if (!String.IsNullOrEmpty(types))
                {
                    string[] split = types.Split(',');
                    foreach (string s in split)
                    {
                        list.Add(s.Trim());
                    }
                }
            }

            return(list);
        }
        private void AddField(
            Dictionary <String, LookupListItem> foundItems,
            MemberInfo memberInfo)
        {
            FieldInfo fi = (FieldInfo)memberInfo;

            if (fi.IsSpecialName)
            {
                return;
            }
            if (fi.IsPrivate)
            {
                return;
            }

            if (!foundItems.ContainsKey(fi.Name))
            {
                LookupListItem li = new LookupListItem();
                li.DisplayText = fi.Name;
                li.InsertText  = fi.Name;
                li.ToolTipText = String.Format("{0} {1}",
                                               CSharpFormattingTools.GetTypeSignature(fi.FieldType),
                                               fi.Name);

                if (fi.IsLiteral)
                {
                    if (fi.IsPublic)
                    {
                        li.Category =
                            QuickSharp.CodeAssist.Constants.CONSTANT;
                    }
                    else
                    {
                        li.Category =
                            QuickSharp.CodeAssist.Constants.CONSTANT_FRIEND;
                    }
                }
                else
                {
                    if (fi.IsPublic)
                    {
                        li.Category =
                            QuickSharp.CodeAssist.Constants.FIELD;
                    }
                    else if (fi.IsFamily)
                    {
                        li.Category =
                            QuickSharp.CodeAssist.Constants.FIELD_PROTECTED;
                    }
                    else
                    {
                        return;
                    }
                }

                foundItems.Add(li.DisplayText, li);
            }
        }
        private void ColourizeVariablesAndTypes(
            QuickSharp.Editor.ScintillaEditForm document)
        {
            string source = document.GetContent() as string;

            source = CSharpFormattingTools.RemoveUnwantedText(source);
            source = CSharpFormattingTools.RemoveUnwantedBracketText(source);

            List <string> namespaces = GetNamespaceList(source);

            if (settingsManager.ColorizeTypes)
            {
                // Lexer WORD2 - types

                List <string> assemblies = new List <string>();

                foreach (string ns in namespaces)
                {
                    List <string> names =
                        referenceManager.GetNamespaceAssemblies(ns);

                    foreach (string name in names)
                    {
                        if (!assemblies.Contains(name))
                        {
                            assemblies.Add(name);
                        }
                    }
                }

                assemblies.AddRange(workspaceAssemblyList);

                document.Editor.Lexing.Keywords[1] =
                    "Array Boolean Date Enumerator Error Function Number Object RegExp String VBArray " +
                    CodeAssistTools.GetNamespaceTypesAsString(
                        namespaces, assemblies);
            }

            if (settingsManager.ColorizeVariables)
            {
                // Lexer GLOBALCLASS - variables
                DeclaredVariables declaredVariables =
                    new DeclaredVariables(source, fullNamespaceList, false);

                StringBuilder sb = new StringBuilder();

                foreach (Variable v in declaredVariables.Items)
                {
                    sb.Append(v.Name);
                    sb.Append(" ");
                }

                document.Editor.Lexing.Keywords[3] = sb.ToString();
            }

            document.Editor.Lexing.Colorize();
        }
Beispiel #7
0
        /*
         * Create a LookupListItem from a type.
         */

        protected LookupListItem GetItem(Type type)
        {
            string typeName = CSharpFormattingTools.GetTypeSignature(type);
            string fullName = CSharpFormattingTools.GetTypeSignature(type, true);

            LookupListItem item = new LookupListItem();

            item.DisplayText = typeName;
            item.InsertText  = type.ContainsGenericParameters ?
                               CSharpFormattingTools.RemoveGenericSignature(typeName) :
                               typeName;

            // Classify the type - order is important here
            if (type.IsClass && type.IsSubclassOf(typeof(System.Delegate)))
            {
                item.Category    = QuickSharp.CodeAssist.Constants.DELEGATE;
                item.ToolTipText = String.Format("delegate {0}", fullName);
            }
            else if (type.IsEnum)
            {
                item.Category    = QuickSharp.CodeAssist.Constants.ENUM;
                item.ToolTipText = String.Format("enum {0}", fullName);
            }
            else if (type.IsValueType)
            {
                item.Category    = QuickSharp.CodeAssist.Constants.VALUETYPE;
                item.ToolTipText = String.Format("struct {0}", fullName);

                AddConstructors(type, item);
            }
            else if (type.IsInterface)
            {
                item.Category    = QuickSharp.CodeAssist.Constants.INTERFACE;
                item.ToolTipText = String.Format("interface {0}", fullName);
            }
            else
            {
                if (type.IsSealed)
                {
                    item.Category = QuickSharp.CodeAssist.Constants.CLASS_SEALED;
                }
                else
                {
                    item.Category = QuickSharp.CodeAssist.Constants.CLASS;
                }

                item.ToolTipText = String.Format("class {0}", fullName);

                AddConstructors(type, item);
            }

            return(item);
        }
Beispiel #8
0
        private void UpdateListView(List <MemberTagData> memberList)
        {
            listView.SuspendLayout();
            listView.Items.Clear();

            foreach (MemberTagData mtd in memberList)
            {
                ListViewItem lvi = new ListViewItem();
                lvi.SubItems[0].Text = mtd.Name;
                lvi.ImageKey         = mtd.ImageKey;
                lvi.Tag = mtd.Info;

                switch (mtd.Info.MemberType)
                {
                case MemberTypes.Constructor:
                    lvi.ToolTipText = mtd.Name;
                    break;

                case MemberTypes.Property:
                    PropertyInfo pi = (PropertyInfo)mtd.Info;
                    lvi.ToolTipText = String.Format("{0} {1}",
                                                    CSharpFormattingTools.GetTypeSignature(pi.PropertyType),
                                                    pi.Name);
                    break;

                case MemberTypes.Method:
                    MethodInfo mi = (MethodInfo)mtd.Info;
                    lvi.ToolTipText = String.Format("{0} {1}",
                                                    CSharpFormattingTools.GetTypeSignature(mi.ReturnType),
                                                    CSharpFormattingTools.GetMethodSignature(mi));
                    break;

                case MemberTypes.Field:
                    FieldInfo fi = (FieldInfo)mtd.Info;
                    lvi.ToolTipText = String.Format("{0} {1}",
                                                    CSharpFormattingTools.GetTypeSignature(fi.FieldType),
                                                    fi.Name);
                    break;

                case MemberTypes.Event:
                    EventInfo ei = (EventInfo)mtd.Info;
                    lvi.ToolTipText = ei.EventHandlerType.Name;
                    break;
                }

                listView.Items.Add(lvi);
            }

            listView.ResumeLayout(true);
        }
        public DeclaredVariables(string source,
                                 List <string> namespaceList,
                                 bool visibleScopesOnly,
                                 DeclarationContext declarationContext)
        {
            _declaredVariables  = new List <Variable>();
            _declarationContext = declarationContext;

            source = CSharpFormattingTools.
                     RemoveNamespaceDeclarations(source);

            if (visibleScopesOnly)
            {
                source = CSharpFormattingTools.
                         RemoveInaccessibleScopes(source);
            }

            FindVariableDeclaratons(source);

            foreach (Variable v in _declaredVariables)
            {
                /*
                 * To allow for nested types we need to replace any '.'
                 * that's not part of a namespace with '+'.
                 */

                if (v.Type.IndexOf('.') != -1)
                {
                    int pos = 0;

                    /*
                     * Need to replace all '.' occurring
                     * after the longest possible namespace
                     * match.
                     */

                    foreach (string ns in namespaceList)
                    {
                        if (v.Type.StartsWith(ns) &&
                            pos <= ns.Length)
                        {
                            pos = ns.Length + 1;
                        }
                    }

                    v.Type = v.Type.Substring(0, pos) +
                             v.Type.Substring(pos).Replace(".", "+");
                }
            }
        }
        private void AddExtensionMethod(
            Dictionary <String, LookupListItem> foundItems,
            MethodInfo mi)
        {
            if (!foundItems.ContainsKey(mi.Name))
            {
                LookupListItem lli = new LookupListItem();
                lli.DisplayText = mi.Name;
                lli.InsertText  = mi.Name;
                lli.ToolTipText = String.Format(
                    "{0} {1}_OVR_\r\nDeclared in: {2}",
                    CSharpFormattingTools.GetTypeSignature(mi.ReturnType),
                    CSharpFormattingTools.GetMethodSignature(mi, true, true),
                    mi.DeclaringType.FullName);

                lli.Category = QuickSharp.CodeAssist.Constants.METHOD_EXTENSION;

                LookupMenuItem lmi = new LookupMenuItem();

                lmi.DisplayText = String.Format("{0} {1}",
                                                CSharpFormattingTools.GetTypeSignature(mi.ReturnType),
                                                CSharpFormattingTools.GetMethodSignature(mi, true, true));

                lmi.InsertText =
                    CSharpFormattingTools.GetMethodSignature(mi, true, true);

                lli.MenuItems.Add(lmi);

                foundItems.Add(lli.DisplayText, lli);
            }
            else
            {
                // Overloaded methods
                LookupListItem lli = foundItems[mi.Name];
                LookupMenuItem lmi = new LookupMenuItem();

                lmi.DisplayText = String.Format("{0} {1}",
                                                CSharpFormattingTools.GetTypeSignature(mi.ReturnType),
                                                CSharpFormattingTools.GetMethodSignature(mi, true, true));

                lmi.InsertText =
                    CSharpFormattingTools.GetMethodSignature(mi, true, true);

                lli.MenuItems.Add(lmi);
            }
        }
Beispiel #11
0
        public LookupContext(string fullSource, string preSource,
                             string line, int lineStartPos, int currentPos, bool insideClass)
        {
            _fullSource   = fullSource;
            _preSource    = preSource;
            _line         = line;
            _lineStartPos = lineStartPos;
            _currentPos   = currentPos;
            _beforeClass  = insideClass;

            /*
             * Cleanup the source code.
             */

            _preSource = CSharpFormattingTools.RemoveUnwantedText(_preSource);
            _preSource = CSharpFormattingTools.RemoveUnwantedBracketText(_preSource);
            _line      = CSharpFormattingTools.RemoveUnwantedText(_line);
            _line      = CSharpFormattingTools.RemoveUnwantedBracketText(_line);

            /*
             * Get rid of 'global::' - we don't do anything with them
             * so we might as well not have them.
             */

            _line = _line.Replace("global::", String.Empty);

            /*
             * We remove the content of any balanced brackets to
             * allow indexed variables to identified and classified.
             */

            _line = CSharpFormattingTools.RemoveUnwantedParentheses(Line);

            /*
             * Create the target.
             */

            _target = new LookupTarget(_line);

            /*
             * Find the visible methods and properties.
             */

            _localMethods    = new LocalMethods(_fullSource);
            _localProperties = new LocalProperties(_fullSource);
        }
        private void AddProperty(
            Dictionary <String, LookupListItem> foundItems,
            MemberInfo memberInfo)
        {
            PropertyInfo pi = (PropertyInfo)memberInfo;

            if (!foundItems.ContainsKey(pi.Name))
            {
                LookupListItem li = new LookupListItem();
                li.DisplayText = pi.Name;
                li.InsertText  = pi.Name;
                li.Category    = QuickSharp.CodeAssist.Constants.PROPERTIES;

                li.ToolTipText = String.Format("{0} {1}",
                                               CSharpFormattingTools.GetTypeSignature(pi.PropertyType),
                                               pi.Name);

                foundItems.Add(pi.Name, li);
            }
        }
Beispiel #13
0
        protected void AddConstructors(Type type, LookupListItem item)
        {
            BindingFlags bindingAttr =
                BindingFlags.Public |
                BindingFlags.FlattenHierarchy |
                BindingFlags.Instance;

            ConstructorInfo[] cia = type.GetConstructors(bindingAttr);

            foreach (ConstructorInfo ci in cia)
            {
                LookupMenuItem lmi = new LookupMenuItem();
                lmi.DisplayText = CSharpFormattingTools.
                                  GetConstructorSignature(ci, type);
                lmi.InsertText = CSharpFormattingTools.
                                 GetMinimalConstructorSignature(ci, type);

                item.MenuItems.Add(lmi);
            }
        }
        private void AddEventHandlerMenuItems(
            string targetName, string eventName,
            Type handlerType, int indent,
            LookupListItem item)
        {
            BindingFlags bindingAttr =
                BindingFlags.Public |
                BindingFlags.FlattenHierarchy |
                BindingFlags.Instance;

            MethodInfo[] mia = handlerType.GetMethods(bindingAttr);

            foreach (MethodInfo mi in mia)
            {
                if (mi.Name != "Invoke")
                {
                    continue;
                }

                string paddingFull  = new String(' ', indent);
                string paddingShort = new String(' ',
                                                 (indent > 4 ? indent - 4 : indent));

                string methodParameters =
                    CSharpFormattingTools.GetMethodParameters(
                        mi.GetParameters());

                string methodParameterNames =
                    CSharpFormattingTools.GetMethodParameterNames(
                        mi.GetParameters());

                /*
                 * Full event handler.
                 */

                LookupMenuItem lmi1 = new LookupMenuItem();

                string displayText1 = Resources.InsertEventHandler;

                string insertText1 = String.Format(
                    @"{0} += new {1}({2}_{0});

{4}private void {2}_{0}{3}
{4}{{
{4}}}
",
                    eventName, handlerType.Name,
                    FormatEventHandlerName(targetName),
                    methodParameters, paddingShort);

                lmi1.DisplayText = displayText1;
                lmi1.InsertText  = insertText1;
                item.MenuItems.Add(lmi1);

                /*
                 * Anonymous event handler.
                 */

                LookupMenuItem lmi2 = new LookupMenuItem();

                string displayText2 = Resources.InsertAnonEventHandler;

                string insertText2 = String.Format(
                    @"{0} += delegate {1}
{2}{{
{2}}};",
                    eventName, methodParameters, paddingFull);

                lmi2.DisplayText = displayText2;
                lmi2.InsertText  = insertText2;
                item.MenuItems.Add(lmi2);

                /*
                 * Lambda event handler.
                 */

                LookupMenuItem lmi3 = new LookupMenuItem();

                string displayText3 = Resources.InsertLambdaEventHandler;

                string insertText3 = String.Format(
                    @"{0} += {1} =>
{2}{{
{2}}};",
                    eventName, methodParameterNames, paddingFull);

                lmi3.DisplayText = displayText3;
                lmi3.InsertText  = insertText3;
                item.MenuItems.Add(lmi3);
            }
        }
Beispiel #15
0
        public LocalMethods(string source)
        {
            _methods = new List <MethodDefinition>();

            /*
             * Get all the method declarations in the source.
             */

            Regex methodRegex = new Regex(@"(public\s+|private\s+|protected\s+|internal\s+)(internal\s+)?(abstract\s+|virtual\s+|override\s+|static\s+)?(.+)\s+([\w\d_]+)\s*\(([\d\.\w\s,<>\[\]_]*)\)\s+{");

            foreach (Match m in methodRegex.Matches(source))
            {
                MethodDefinition method = new MethodDefinition();

                method.Name                = m.Groups[5].Value.Trim();
                method.ReturnType          = m.Groups[4].Value.Trim();
                method.Parameters          = m.Groups[6].Value.Trim();
                method.Visibility          = m.Groups[1].Value.Trim();
                method.IsProtectedInternal = (m.Groups[2].Value.Trim() == "internal");
                method.IsStatic            = (m.Groups[3].Value.Trim() == "static");
                method.StartPos            = m.Index;

                method.ReturnType =
                    CSharpFormattingTools.ToCTSType(method.ReturnType);

                /*
                 * The parameter types should ideally be formatted to
                 * show the CLR type names to make them consistent with
                 * the display of other types but we'll leave that for
                 * now and make do with the formatted typed by the user.
                 */

                _methods.Add(method);
            }

            /*
             * Now find the constructors.
             */

            Regex constructorRegex = new Regex(@"(public\s+|private\s+|protected\s+|internal\s+|static\s+)(internal\s+)?([\w\d_]+)\s*\(([\d\.\w\s,<>\[\]_]*)\)\s+({|:)");

            foreach (Match m in constructorRegex.Matches(source))
            {
                MethodDefinition method = new MethodDefinition();

                method.Name                = m.Groups[3].Value.Trim();
                method.ReturnType          = null;
                method.Parameters          = m.Groups[4].Value.Trim();
                method.Visibility          = m.Groups[1].Value.Trim();
                method.IsProtectedInternal = (m.Groups[2].Value.Trim() == "internal");
                method.IsStatic            = (m.Groups[1].Value.Trim() == "static");
                method.StartPos            = m.Index;

                _methods.Add(method);
            }

            /*
             * Now we have the methods and their start positions we need
             * to find their end positions to mark out the method 'zones'.
             */

            foreach (MethodDefinition method in _methods)
            {
                method.EndPos = FindMethodEnd(method.StartPos, source);
            }
        }
        private void ColourizeVariablesAndTypes(ScintillaEditForm document)
        {
            string source = CSharpFormattingTools.
                            RemoveUnwantedText(document.GetContent() as string);

            source = CSharpFormattingTools.
                     RemoveUnwantedBracketText(source);

            List <string> namespaces = GetNamespaceList(source);

            if (settingsManager.ColorizeTypes)
            {
                /*
                 * Lexer WORD2 - types
                 */

                List <string> assemblies = new List <string>();

                foreach (string ns in namespaces)
                {
                    List <string> names =
                        referenceManager.GetNamespaceAssemblies(ns);

                    foreach (string name in names)
                    {
                        if (!assemblies.Contains(name))
                        {
                            assemblies.Add(name);
                        }
                    }
                }

                assemblies.AddRange(workspaceAssemblyList);

                document.Editor.Lexing.Keywords[1] =
                    CodeAssistTools.GetNamespaceTypesAsString(
                        namespaces, assemblies);
            }

            if (settingsManager.ColorizeVariables)
            {
                /*
                 * Lexer GLOBALCLASS - variables
                 */

                DeclaredVariables declaredVariables =
                    new DeclaredVariables(
                        source, namespaces, false,
                        DeclarationContext.All);

                InheritedVariablesBase inheritedVariables
                    = new InheritedVariablesCode(
                          source,
                          DeclarationContext.All,
                          workspaceAssemblyList,
                          fullNamespaceList,
                          rootNamespaceList,
                          configNamespaceList);

                StringBuilder sb = new StringBuilder();

                foreach (Variable v in declaredVariables.Items)
                {
                    sb.Append(v.Name);
                    sb.Append(" ");
                }

                foreach (Variable v in inheritedVariables.Items)
                {
                    sb.Append(v.Name);
                    sb.Append(" ");
                }

                document.Editor.Lexing.Keywords[3] = sb.ToString();
            }

            document.Editor.Lexing.Colorize();
        }
        /*
         * Create a LookupListItem from a type.
         */

        protected LookupListItem GetItem(Type type)
        {
            string typeName = CSharpFormattingTools.GetTypeSignature(type);
            string fullName = CSharpFormattingTools.GetTypeSignature(type, true);

            LookupListItem item = new LookupListItem();

            item.DisplayText = typeName;
            item.InsertText  = type.ContainsGenericParameters ?
                               CSharpFormattingTools.RemoveGenericSignature(typeName) :
                               typeName;

            // Classify the type - order is important here
            if (type.IsClass && type.IsSubclassOf(typeof(System.Delegate)))
            {
                item.Category    = QuickSharp.CodeAssist.Constants.DELEGATE;
                item.ToolTipText = String.Format("delegate {0}", fullName);
            }
            else if (type.IsEnum)
            {
                item.Category    = QuickSharp.CodeAssist.Constants.ENUM;
                item.ToolTipText = String.Format("enum {0}", fullName);
            }
            else if (type.IsValueType)
            {
                item.Category    = QuickSharp.CodeAssist.Constants.VALUETYPE;
                item.ToolTipText = String.Format("struct {0}", fullName);
            }
            else if (type.IsInterface)
            {
                item.Category    = QuickSharp.CodeAssist.Constants.INTERFACE;
                item.ToolTipText = String.Format("interface {0}", fullName);
            }
            else
            {
                if (type.IsSealed)
                {
                    item.Category = QuickSharp.CodeAssist.Constants.CLASS_SEALED;
                }
                else
                {
                    item.Category = QuickSharp.CodeAssist.Constants.CLASS;
                }

                item.ToolTipText = String.Format("class {0}", fullName);

                BindingFlags bindingAttr =
                    BindingFlags.Public |
                    BindingFlags.FlattenHierarchy |
                    BindingFlags.Instance;

                ConstructorInfo[] cia = type.GetConstructors(bindingAttr);

                foreach (ConstructorInfo ci in cia)
                {
                    LookupMenuItem lmi = new LookupMenuItem();
                    lmi.DisplayText = JScriptFormattingTools.
                                      GetConstructorSignature(ci, type);
                    lmi.InsertText = JScriptFormattingTools.
                                     GetMinimalConstructorSignature(ci, type);

                    item.MenuItems.Add(lmi);
                }
            }

            return(item);
        }
Beispiel #18
0
        private void GetNamespaceTypes(TreeNode node)
        {
            node.Nodes.Clear();

            // Keep track of found types so we don't add dupes.
            List <string> typeNames = new List <string>();

            /*
             * Go through each assembly containing the namespace
             * and get the types contained within it.
             */

            List <string> assemblyNames = new List <string>();

            /*
             * If using namespace view add all assemblies containing the
             * namespace, otherwise just include the selected assembly.
             */

            if (_showModules)
            {
                if (node.Parent != null)
                {
                    /*
                     * Add the selected assembly.
                     */

                    ModuleData moduleData =
                        node.Parent.Tag as ModuleData;

                    if (moduleData != null)
                    {
                        Assembly assembly = moduleData.Module.Assembly;

                        if (!moduleData.IsWorkspaceAssembly)
                        {
                            assemblyNames.Add(assembly.FullName);
                        }
                        else
                        {
                            assemblyNames.Add("?" + moduleData.FullName);
                        }
                    }
                }
            }
            else
            {
                /*
                 * Add the assemblies that are known to cantain the namespace
                 * and all the local ones (no way of telling if they do).
                 */

                assemblyNames.AddRange(
                    _referenceManager.GetNamespaceAssemblies(node.Text));

                if (_includeWorkspace)
                {
                    assemblyNames.AddRange(_workspaceAssemblies);
                }
            }

            foreach (string name in assemblyNames)
            {
                Assembly assembly = CodeAssistTools.LoadAssembly(name);
                if (assembly == null)
                {
                    continue;
                }

                try
                {
                    foreach (Type type in assembly.GetTypes())
                    {
                        if (typeNames.Contains(type.FullName))
                        {
                            continue;
                        }
                        if (type.IsNested)
                        {
                            continue;
                        }
                        if (_hideNonPublicMembers && type.IsNotPublic)
                        {
                            continue;
                        }

                        // Reject types not in the namespace we're looking for.
                        if (String.IsNullOrEmpty(type.Namespace))
                        {
                            continue;
                        }
                        if (type.Namespace != node.Text)
                        {
                            continue;
                        }

                        string typeName =
                            CSharpFormattingTools.GetTypeSignature(type);

                        TreeNode typeNode = CreateTreeNode(type, typeName);

                        node.Nodes.Add(typeNode);

                        typeNames.Add(type.FullName);

                        AddNestedTypes(type, typeName, node);
                    }
                }
                catch
                {
                    // Just suppress any errors from problematic assemblies.
                }
            }
        }
Beispiel #19
0
        private List <MemberTagData> GetTypeMembers(Type type)
        {
            List <MemberTagData> memberList = new List <MemberTagData>();

            BindingFlags flags =
                BindingFlags.Public |
                BindingFlags.Instance |
                BindingFlags.Static;

            if (!_hideNonPublicMembers)
            {
                flags |= BindingFlags.NonPublic;
            }

            if (_hideInheritedMembers)
            {
                flags |= BindingFlags.DeclaredOnly;
            }
            else
            {
                flags |= BindingFlags.FlattenHierarchy;
            }

            MemberInfo[] members = type.GetMembers(flags);

            bool hasExtensionMethods = false;

            hasExtensionMethods = type.GetCustomAttributes(
                typeof(ExtensionAttribute), false).Length > 0;

            foreach (MemberInfo memberInfo in members)
            {
                MemberTagData mtd = new MemberTagData();
                mtd.Name = memberInfo.Name;
                mtd.Info = memberInfo;

                switch (memberInfo.MemberType)
                {
                case MemberTypes.NestedType:
                    continue;

                case MemberTypes.Constructor:
                    ConstructorInfo ci = (ConstructorInfo)memberInfo;

                    /*
                     * Even though constructors are special names
                     * for convenience sake we don't hide them.
                     */
                    //if (_hideSpecialNames && ci.IsSpecialName) continue;
                    mtd.Name     = CSharpFormattingTools.GetConstructorSignature(ci, type);
                    mtd.ImageKey = GetMemberImageKey(Constants.METHOD,
                                                     ci.IsPrivate, ci.IsFamily, ci.IsFamilyAndAssembly);
                    break;

                case MemberTypes.Method:
                    MethodInfo mi = (MethodInfo)memberInfo;
                    if (_hideSpecialNames && mi.IsSpecialName)
                    {
                        continue;
                    }

                    bool isExtensionMethod = false;

                    if (hasExtensionMethods)
                    {
                        isExtensionMethod = mi.GetCustomAttributes(
                            typeof(ExtensionAttribute), false).Length > 0;
                    }

                    mtd.Name = CSharpFormattingTools.
                               GetMethodSignature(mi, isExtensionMethod);

                    if (isExtensionMethod)
                    {
                        mtd.ImageKey = Constants.METHOD_EXTENSION;
                    }
                    else
                    {
                        mtd.ImageKey = GetMemberImageKey(Constants.METHOD,
                                                         mi.IsPrivate, mi.IsFamily, mi.IsFamilyAndAssembly);
                    }

                    break;

                case MemberTypes.Field:
                    FieldInfo fi = (FieldInfo)memberInfo;
                    if (_hideSpecialNames && fi.IsSpecialName)
                    {
                        continue;
                    }
                    mtd.ImageKey = GetMemberImageKey(
                        (fi.IsLiteral) ? Constants.CONSTANT : Constants.FIELD,
                        fi.IsPrivate, fi.IsFamily, fi.IsFamilyAndAssembly);
                    break;

                case MemberTypes.Property:
                    mtd.ImageKey = Constants.PROPERTIES;
                    break;

                case MemberTypes.Event:
                    mtd.ImageKey = Constants.EVENT;
                    break;
                }

                memberList.Add(mtd);
            }

            return(memberList);
        }
        private void AddMethod(
            Dictionary <String, LookupListItem> foundItems,
            MemberInfo memberInfo)
        {
            MethodInfo mi = (MethodInfo)memberInfo;

            if (mi.IsSpecialName)
            {
                return;
            }
            if (mi.IsPrivate)
            {
                return;
            }
            if (mi.Name == "Finalize")
            {
                return;
            }

            if (!foundItems.ContainsKey(mi.Name))
            {
                bool isExtensionMethod =
                    mi.GetCustomAttributes(
                        typeof(ExtensionAttribute),
                        false).Length > 0;

                LookupListItem lli = new LookupListItem();
                lli.DisplayText = mi.Name;
                lli.InsertText  = mi.Name;
                lli.ToolTipText = String.Format("{0} {1}_OVR_",
                                                CSharpFormattingTools.GetTypeSignature(mi.ReturnType),
                                                CSharpFormattingTools.GetMethodSignature(mi));

                if (isExtensionMethod)
                {
                    lli.Category = QuickSharp.CodeAssist.Constants.METHOD_EXTENSION;
                }
                else if (mi.IsPublic)
                {
                    lli.Category = QuickSharp.CodeAssist.Constants.METHOD;
                }
                else if (mi.IsFamily)
                {
                    lli.Category = QuickSharp.CodeAssist.Constants.METHOD_PROTECTED;
                }
                else
                {
                    return;
                }

                LookupMenuItem lmi = new LookupMenuItem();

                lmi.DisplayText = String.Format("{0} {1}",
                                                CSharpFormattingTools.GetTypeSignature(mi.ReturnType),
                                                CSharpFormattingTools.GetMethodSignature(mi));

                lmi.InsertText = CSharpFormattingTools.GetMinimalMethodSignature(mi);

                lli.MenuItems.Add(lmi);

                foundItems.Add(mi.Name, lli);
            }
            else
            {
                // Overloaded method
                LookupListItem lli = foundItems[mi.Name];
                LookupMenuItem lmi = new LookupMenuItem();

                lmi.DisplayText = String.Format("{0} {1}",
                                                CSharpFormattingTools.GetTypeSignature(mi.ReturnType),
                                                CSharpFormattingTools.GetMethodSignature(mi));

                lmi.InsertText = CSharpFormattingTools.GetMinimalMethodSignature(mi);

                if (mi.IsPublic)
                {
                    lli.Category = QuickSharp.CodeAssist.Constants.METHODOVERLOAD;
                }
                else if (mi.IsFamily)
                {
                    lli.Category = QuickSharp.CodeAssist.Constants.METHODOVERLOAD_PROTECTED;
                }
                else
                {
                    return;
                }

                lli.MenuItems.Add(lmi);
            }
        }
        protected string GetFullSource(string text, ref int pos)
        {
            /*
             * This is pretty crude - it just attempts to remove
             * class definitions outside the current one from
             * the text so that we don't get out of scope methods
             * and properties showing up. It will also get confused
             * by nested classes - the scope of the current class will start
             * at the beginning of the last nested class before the caret.
             *
             * We also need to preserve the current cursor location within
             * the source so that the method/property and declaration
             * context detection will work correctly.
             */

            text = text.Insert(pos, "¬¬");
            text = CSharpFormattingTools.RemoveUnwantedText(text);

            pos = text.IndexOf("¬¬");
            if (pos == -1)
            {
                return(String.Empty);
            }

            string src1 = text.Substring(0, pos);
            string src2 = text.Substring(pos);

            int start = src1.LastIndexOf("class ");
            int end   = src2.IndexOf("class ");

            if (start != -1)
            {
                src1 = src1.Substring(start);
            }

            if (end != -1)
            {
                src2 = src2.Substring(0, end);
            }

            text = src1 + src2;

            pos = text.IndexOf("¬¬");

            text = text.Replace("¬¬", String.Empty);

            /*
             * Find the location of the closing '}'.
             */

            int i          = 0;
            int braceLevel = 0;

            while (i < text.Length && text[i] != '{')
            {
                i++;
            }

            while (i < text.Length)
            {
                if (text[i] == '{')
                {
                    braceLevel++;
                }
                if (text[i] == '}')
                {
                    braceLevel--;
                }

                if (braceLevel == 0)
                {
                    break;
                }

                i++;
            }

            return(text.Substring(0, i));
        }
        protected LookupList GetCSharpLookupList(LookupContext context)
        {
            List <LookupListItem> lookupItemList = null;

            /******************************************************************
            * CodeAssist lookups start here. The type of lookup performed
            * depends on the context of the search target and the result
            * of each lookup so order is important. At each stage we ask
            * "what are we looking for?" and "what are we expecting to find?"
            ******************************************************************/

            /*
             * Are we looking for namespaces as part of a 'using' declaration?
             * We have typed 'using' and now expect to see a list of available
             * namespaces (as determined by the assemblies available in the
             * assembly search list). If nothing else has been typed, the root
             * namespaces will be shown; if a partial namespace has been entered
             * (i.e. the name includes one or more '.') the appropriate child
             * namespaces are shown.
             */

            lookupItemList = GetChildNamespaces(
                context.Target, context.Line);

            if (lookupItemList != null)
            {
                return(new LookupList(
                           context.Target.LookAhead, lookupItemList));
            }

            /*
             * If before the class definition give up looking.
             */

            if (!context.BeforeClass)
            {
                return(null);
            }

            /*
             * Are we looking for members of a namespace?
             * We have typed a name including at least one '.' and expect to
             * see the relevant members of this entity. This could be a
             * namespace, class or variable. First we check to see if we get
             * anything back by assuming it's a namespace.
             */

            if (context.Target.Entity != String.Empty)
            {
                List <LookupListItem> types =
                    FindNamespaceTypeLookupItems(context.Target.Entity);

                List <LookupListItem> namespaces =
                    FindNamespaceChildLookupItems(context.Target.Entity);

                if (types != null || namespaces != null)
                {
                    lookupItemList = new List <LookupListItem>();
                    if (types != null)
                    {
                        lookupItemList.AddRange(types);
                    }
                    if (namespaces != null)
                    {
                        lookupItemList.AddRange(namespaces);
                    }
                }
            }

            if (lookupItemList != null)
            {
                return(new LookupList(
                           context.Target.LookAhead, lookupItemList));
            }

            /*
             * Get the local variables declared within the scope visible from
             * the lookup location (the caret) and any variables inherited
             * from the base class.
             */

            /*
             * Are we in a static or instance method?
             * Set the context accordingly.
             */

            GetDeclarationContext(context);

            context.DeclaredVariables = GetDeclaredVariables(
                context.PreSource, true,
                context.DeclarationContext);

            context.InheritedVariables = GetInheritedVariables(
                context.PreSource,
                context.DeclarationContext,
                workspaceAssemblyList,
                fullNamespaceList,
                rootNamespaceList);

            /*
             * Are we looking for anything in scope?
             * We have typed a name without a '.' so it could be a local
             * variable, a class visible within any of the imported namespaces
             * or any root namespace in the referenced assemblies.
             */

            if (String.IsNullOrEmpty(context.Target.Entity))
            {
                lookupItemList = GetVisibleTypes(context,
                                                 true, true, true);

                if (lookupItemList != null)
                {
                    // Add the root namespaces
                    foreach (string ns in rootNamespaceList)
                    {
                        LookupListItem item = new LookupListItem();
                        item.DisplayText = ns;
                        item.InsertText  = ns;
                        item.Category    =
                            QuickSharp.CodeAssist.Constants.NAMESPACE;
                        item.ToolTipText = String.Format("namespace {0}", ns);
                        lookupItemList.Add(item);
                    }

                    return(new LookupList(
                               context.Target.LookAhead, lookupItemList));
                }
            }

            /*
             * The target cannot be blank after this point; if we haven't
             * found anything using a blank target then there's nothing
             * to find.
             */

            if (String.IsNullOrEmpty(context.Target.Entity))
            {
                return(null);
            }

            /******************************************************************
            * From this point on we must have at least one '.' in the target
            * so we're looking for members of various entities not the
            * entities themselves. These could be the current class or its
            * base; variables (instance member lookup) or classes (static
            * member lookup).
            ******************************************************************/

            /*
             * Convert C# type names to .NET type names.
             */

            context.Target.Entity =
                CSharpFormattingTools.ToCTSType(context.Target.Entity);

            /*
             * Are we looking for members of the current class?
             * This is inaccurate in that it doesn't include the members of
             * the current type (as we're not parsing the code) only the
             * inherited members and local variables. It's basically the
             * same as 'base' with variables added.
             */

            if (context.Target.Entity == "this")
            {
                lookupItemList = GetVisibleTypes(
                    context, true, false, true);

                if (lookupItemList != null)
                {
                    return(new LookupList(
                               context.Target.LookAhead, lookupItemList));
                }
            }

            /*
             * Are we looking for members of the base class?
             */

            if (context.Target.Entity == "base")
            {
                lookupItemList = GetVisibleTypes(
                    context, false, false, false);

                if (lookupItemList != null)
                {
                    return(new LookupList(
                               context.Target.LookAhead, lookupItemList));
                }
            }

            /*
             * If the target is a sub-member of 'this' or 'base' we can
             * treat this as though the 'this' or 'base' aren't there.
             */

            if (context.Target.Entity.StartsWith("this.") ||
                context.Target.Entity.StartsWith("base."))
            {
                context.Target.Entity = context.Target.Entity.Substring(5);
            }

            /*
             * Are we looking for members of a variable?
             * We will have the variable name followed by one or more
             * sub-members (e.g. sr.ReadLine().ToString().GetType()...).
             * We need to convert the variable to it's type
             * (e.g. StreamReader) and perform instance lookups
             * down the member chain getting the return type of
             * each so we can get its members, find the type of the next
             * and so on. The last type found is the type we need to
             * show in the lookup list.
             */

            Variable variable = context.DeclaredVariables.
                                GetVariable(context.Target.Entity);

            if (variable == null)
            {
                variable = context.Properties.
                           GetProperty(context.Target.Entity);
            }

            if (variable == null)
            {
                variable = context.InheritedVariables.
                           GetVariable(context.Target.Entity);
            }

            if (variable != null)
            {
                context.Target.Name = variable.Name;

                String[] split = context.Target.Entity.Split('.');

                /*
                 * An added complication is if the variable is a collection
                 * type such as an array or List<T>. If we have item operators
                 * (e.g. list[]) we need to convert the variable type to
                 * the type being collected not the collection itself. So if
                 * we have a List<String>, List. presents members of the
                 * List class but List[]. presents members of the String class.
                 */

                if (context.Target.IsIndexed)
                {
                    context.Target.Entity =
                        variable.GetVariableCollectionType(
                            context.Target.Entity);
                }
                else
                {
                    split[0] = variable.Type;
                    context.Target.Entity = String.Join(".", split);
                }

                lookupItemList = GetTypeMembers(
                    context, context.Target, DeclarationContext.Instance,
                    null, false, false);

                if (lookupItemList != null)
                {
                    return(new LookupList(
                               context.Target.LookAhead, lookupItemList));
                }
            }

            /*
             * Are we looking for members of a local
             * method's return type?
             */

            string[] methodSplit = context.Target.Name.Split('.');

            LocalMethods.MethodDefinition method =
                context.Methods.GetMethod(methodSplit[0]);

            if (method != null)
            {
                int i = context.Target.Entity.IndexOf('.');

                if (i == -1)
                {
                    context.Target.Entity = method.ReturnType;
                }
                else
                {
                    context.Target.Entity =
                        method.ReturnType +
                        context.Target.Entity.Substring(i);
                }

                lookupItemList = GetTypeMembers(
                    context, context.Target, DeclarationContext.Instance,
                    null, false, false);

                if (lookupItemList != null)
                {
                    return(new LookupList(
                               context.Target.LookAhead, lookupItemList));
                }
            }

            /*
             * Are we looking for members of a class?
             * We expect to see a list of static members of the class. This is
             * almost the same as the previous instance lookup except that we
             * already know the type of the entity and our first level member
             * lookup will be for static members.
             */

            lookupItemList = GetTypeMembers(
                context, context.Target, DeclarationContext.Static,
                null, false, false);

            return(new LookupList(
                       context.Target.LookAhead, lookupItemList));

            /*
             * If the final lookup finds nothing we will return null.
             */
        }