public UnknownCodeContext(ICompilation compilation, IUnresolvedFile file, TextLocation location)
            : this(compilation)
        {
            var curDef = file.GetInnermostTypeDefinition (location);
            if (curDef != null) {
                var resolvedDef = curDef.Resolve (context).GetDefinition ();
                if (resolvedDef != null) {
                    context = context.WithCurrentTypeDefinition (resolvedDef);

                    var curMember = resolvedDef.Members.FirstOrDefault (m => m.Region.FileName == file.FileName && m.Region.Begin <= location && location < m.BodyRegion.End);
                    if (curMember != null)
                        context = context.WithCurrentMember (curMember);
                }
            }
        }
        /// <summary>
        /// Gets the next member after the specified caret position.
        /// </summary>
        IUnresolvedEntity GetMemberAfter(ITextEditor editor, int caretLine)
        {
            FileName          fileName    = editor.FileName;
            IUnresolvedEntity nextElement = null;

            if (fileName != null)
            {
                IUnresolvedFile unresolvedFile = SD.ParserService.ParseFile(fileName, editor.Document);
                if (unresolvedFile != null)
                {
                    var currentClass    = unresolvedFile.GetInnermostTypeDefinition(caretLine, 0);
                    int nextElementLine = int.MaxValue;
                    if (currentClass == null)
                    {
                        foreach (var c in unresolvedFile.TopLevelTypeDefinitions)
                        {
                            if (c.Region.BeginLine < nextElementLine && c.Region.BeginLine > caretLine)
                            {
                                nextElementLine = c.Region.BeginLine;
                                nextElement     = c;
                            }
                        }
                    }
                    else
                    {
                        foreach (var c in currentClass.NestedTypes)
                        {
                            if (c.Region.BeginLine < nextElementLine && c.Region.BeginLine > caretLine)
                            {
                                nextElementLine = c.Region.BeginLine;
                                nextElement     = c;
                            }
                        }
                        foreach (var m in currentClass.Members)
                        {
                            if (m.Region.BeginLine < nextElementLine && m.Region.BeginLine > caretLine)
                            {
                                nextElementLine = m.Region.BeginLine;
                                nextElement     = m;
                            }
                        }
                    }
                }
            }
            return(nextElement);
        }
Example #3
0
        IUnresolvedTypeDefinition GetCurrentClass(SDTask item)
        {
            // Tasks are created by parsing, so the parse information for item.FileName should already be present.
            // If they aren't, that's because the file might have been deleted/renamed in the meantime.
            // We use GetExistingParseInformation to avoid trying to parse a file that might have been deleted/renamed.
            IUnresolvedFile parseInfo = SD.ParserService.GetExistingUnresolvedFile(item.FileName);

            if (parseInfo != null)
            {
                var c = parseInfo.GetInnermostTypeDefinition(item.Line, item.Column);
                if (c != null)
                {
                    return(c);
                }
            }

            return(null);
        }
        public UnknownCodeContext(ICompilation compilation, IUnresolvedFile file, TextLocation location)
            : this(compilation)
        {
            var curDef = file.GetInnermostTypeDefinition(location);
            if (curDef != null)
            {
                var resolvedDef = curDef.Resolve(context).GetDefinition();
                if (resolvedDef != null)
                {
                    context = context.WithCurrentTypeDefinition(resolvedDef);

                    var curMember = resolvedDef.Members.FirstOrDefault(m => m.Region.FileName == file.FileName && m.Region.Begin <= location && location < m.BodyRegion.End);
                    if (curMember != null)
                    {
                        context = context.WithCurrentMember(curMember);
                    }
                }
            }
        }
Example #5
0
        IUnresolvedTypeDefinition GetCurrentClass()
        {
            if (SD.Workbench.ActiveViewContent == null || SD.Workbench.ActiveViewContent.PrimaryFileName == null)
            {
                return(null);
            }

            IUnresolvedFile parseInfo = SD.ParserService.GetExistingUnresolvedFile(SD.Workbench.ActiveViewContent.PrimaryFileName);

            if (parseInfo != null)
            {
                IPositionable positionable = SD.Workbench.ActiveViewContent.GetService <IPositionable>();
                if (positionable != null)
                {
                    var c = parseInfo.GetInnermostTypeDefinition(positionable.Line, positionable.Column);
                    if (c != null)
                    {
                        return(c);
                    }
                }
            }

            return(null);
        }
Example #6
0
 /// <summary>
 /// Gets the type (potentially a nested type) defined at the specified location.
 /// Returns null if no type is defined at that location.
 /// </summary>
 public static IUnresolvedTypeDefinition GetInnermostTypeDefinition(this IUnresolvedFile file, int line, int column)
 {
     return(file.GetInnermostTypeDefinition(new TextLocation(line, column)));
 }
Example #7
0
        public IList <ICompletionItem> CreateElementList(XamlCompletionContext context, bool includeAbstract)
        {
            if (context.ParseInformation == null)
            {
                return(EmptyList <ICompletionItem> .Instance);
            }

            List <ICompletionItem> result = new List <ICompletionItem>();
            AXmlElement            last   = context.ParentElement;
            ITextEditor            editor = context.Editor;

            compilation = SD.ParserService.GetCompilationForFile(editor.FileName);
            IUnresolvedFile file = context.ParseInformation.UnresolvedFile;

            foreach (string item in XamlConst.GetAllowedItems(context))
            {
                result.Add(new XamlCompletionItem(item));
            }

            IType rt = null;

            if (last != null)
            {
                if (string.Equals(last.Prefix, context.XamlNamespacePrefix, StringComparison.OrdinalIgnoreCase))
                {
                    if (string.Equals(last.LocalName, "Members", StringComparison.OrdinalIgnoreCase))
                    {
                        return(result);
                    }
                    if (string.Equals(last.LocalName, "Code", StringComparison.OrdinalIgnoreCase))
                    {
                        return(result);
                    }
                }
                // If we have an element that is not a property or an incomplete
                // definition => interpret element as a type.
                XamlResolver resolver = new XamlResolver(compilation);
                int          dotIndex = last.LocalName.IndexOf(".", StringComparison.Ordinal) + 1;
                if (dotIndex < 1 || dotIndex == last.LocalName.Length)
                {
                    rt = resolver.ResolveType(last.Namespace, last.LocalName.Trim('.'));
                    string contentPropertyName = GetContentPropertyName(rt.GetDefinition());
                    // If the type has a content property specified, use its type for completion.
                    if (!string.IsNullOrEmpty(contentPropertyName))
                    {
                        IProperty p = rt.GetProperties(m => m.Name == contentPropertyName).FirstOrDefault();
                        if (p != null)
                        {
                            rt = p.ReturnType;
                        }
                    }
                }
                else
                {
                    string typeName   = last.LocalName.Substring(0, dotIndex - 1);
                    string memberName = last.LocalName.Substring(dotIndex);
                    rt = resolver.ResolveType(last.Namespace, typeName);
                    IMember member = rt.GetMembers(m => m.Name == memberName).FirstOrDefault();
                    if (member != null)
                    {
                        rt = member.ReturnType;
                    }
                }
            }

            bool            parentAdded    = false;
            var             utd            = file.GetInnermostTypeDefinition(editor.Caret.Location);
            ITypeDefinition currentTypeDef = null;

            if (utd != null)
            {
                currentTypeDef = utd.Resolve(new SimpleTypeResolveContext(compilation.MainAssembly)).GetDefinition();
            }
            MemberLookup memberLookup = new MemberLookup(currentTypeDef, compilation.MainAssembly);

            IList <ITypeDefinition> possibleTypesInCollection = EmptyList <ITypeDefinition> .Instance;

            if (rt != null && Extensions.IsListType(rt))
            {
                possibleTypesInCollection = rt.GetMethods(m => m.Parameters.Count == 1 && "Add".Equals(m.Name, StringComparison.Ordinal))
                                            .Select(m => m.Parameters[0].Type.GetDefinition())
                                            .Where(t => t != null)
                                            .ToList();
            }

            var items = GetClassesFromContext(context);

            foreach (var ns in items)
            {
                foreach (ITypeDefinition td in ns.Value)
                {
                    if (td.Kind != TypeKind.Class && (!includeAbstract || td.Kind != TypeKind.Interface))
                    {
                        continue;
                    }
                    if (td.IsStatic || (!includeAbstract && td.IsAbstract) || td.IsDerivedFrom(KnownTypeCode.Attribute))
                    {
                        continue;
                    }
                    if (td.Kind == TypeKind.Class && !td.GetConstructors().Any(m => memberLookup.IsAccessible(m, false)))
                    {
                        continue;
                    }
                    if (possibleTypesInCollection.Count > 0 && !possibleTypesInCollection.Any(td.IsDerivedFrom))
                    {
                        continue;
                    }
                    string fullName = td.Name;
                    if (!string.IsNullOrEmpty(ns.Key))
                    {
                        fullName = ns.Key + ":" + fullName;
                    }
                    XamlCompletionItem item = new XamlCompletionItem(fullName, td);
                    parentAdded = parentAdded || (last != null && item.Text == last.Name);
                    result.Add(item);
                }
            }

            // TODO reimplement this if it is really necessary.
//			if (!parentAdded && last != null && !last.Name.Contains(".")) {
//				IClass itemClass = cu.CreateType(last.Namespace, last.LocalName.Trim('.')).GetUnderlyingClass();
//				if (itemClass != null)
//					result.Add(new XamlCodeCompletionItem(itemClass, last.Prefix));
//			}

            return(result);
        }
Example #8
0
 public override IUnresolvedTypeDefinition GetInnermostTypeDefinition(TextLocation location)
 {
     return(parsedFile.GetInnermostTypeDefinition(location));
 }