/// <summary>
        /// Returns an array of the types that are imported by the
        /// current compilation unit.
        /// </summary>
        public List <ICompletionEntry> GetImportedTypes()
        {
            List <ICompletionEntry> types = new List <ICompletionEntry>();

            CtrlSpaceResolveHelper.AddImportedNamespaceContents(types, compilationUnit, callingClass);
            return(types);
        }
        /// <summary>
        /// Returns an array of the types that are imported by the
        /// current compilation unit.
        /// </summary>
        public ArrayList GetImportedTypes()
        {
            ArrayList types = new ArrayList();

            CtrlSpaceResolveHelper.AddImportedNamespaceContents(types, mostRecentCompilationUnit, callingClass);
            return(types);
        }
Beispiel #3
0
        void AddImportedNamespaceContents(ArrayList list)
        {
            IClass c;

            foreach (KeyValuePair <string, string> pair in BooAmbience.ReverseTypeConversionTable)
            {
                c = GetPrimitiveClass(pc, pair.Value, pair.Key);
                if (c != null)
                {
                    list.Add(c);
                }
            }
            list.Add(new DuckClass(cu));
            CtrlSpaceResolveHelper.AddImportedNamespaceContents(list, cu, callingClass);
        }
        public List <IMethod> SearchMethod(string memberName)
        {
            List <IMethod> methods = SearchMethod(callingClass.DefaultReturnType, memberName);

            if (methods.Count > 0)
            {
                return(methods);
            }

            if (languageProperties.CanImportClasses)
            {
                foreach (IUsing @using in cu.Usings)
                {
                    foreach (string import in @using.Usings)
                    {
                        IClass c = projectContent.GetClass(import, 0);
                        if (c != null)
                        {
                            methods = SearchMethod(c.DefaultReturnType, memberName);
                            if (methods.Count > 0)
                            {
                                return(methods);
                            }
                        }
                    }
                }
            }

            if (languageProperties.ImportModules)
            {
                ArrayList list = new ArrayList();
                CtrlSpaceResolveHelper.AddImportedNamespaceContents(list, cu, callingClass);
                foreach (object o in list)
                {
                    IMethod m = o as IMethod;
                    if (m != null && IsSameName(m.Name, memberName))
                    {
                        methods.Add(m);
                    }
                }
            }
            return(methods);
        }
        ResolveResult ResolveIdentifierInternal(string identifier)
        {
            if (callingMember != null)               // LocalResolveResult requires callingMember to be set
            {
                LocalLookupVariable var = SearchVariable(identifier);
                if (var != null)
                {
                    return(new LocalResolveResult(callingMember, CreateLocalVariableField(var, identifier)));
                }
                IParameter para = SearchMethodParameter(identifier);
                if (para != null)
                {
                    IField field = new DefaultField.ParameterField(para.ReturnType, para.Name, para.Region, callingClass);
                    return(new LocalResolveResult(callingMember, field));
                }
                if (IsSameName(identifier, "value"))
                {
                    IProperty property = callingMember as IProperty;
                    if (property != null && property.SetterRegion.IsInside(caretLine, caretColumn))
                    {
                        IField field = new DefaultField.ParameterField(property.ReturnType, "value", property.Region, callingClass);
                        return(new LocalResolveResult(callingMember, field));
                    }
                }
            }
            if (callingClass != null)
            {
                IMember member = GetMember(callingClass.DefaultReturnType, identifier);
                if (member != null)
                {
                    return(CreateMemberResolveResult(member));
                }
                ResolveResult result = ResolveMethod(callingClass.DefaultReturnType, identifier);
                if (result != null)
                {
                    return(result);
                }
            }

            // try if there exists a static member in outer classes named typeName
            List <IClass> classes = cu.GetOuterClasses(caretLine, caretColumn);

            foreach (IClass c in classes)
            {
                IMember member = GetMember(c.DefaultReturnType, identifier);
                if (member != null && member.IsStatic)
                {
                    return(new MemberResolveResult(callingClass, callingMember, member));
                }
            }

            string namespaceName = SearchNamespace(identifier);

            if (namespaceName != null && namespaceName.Length > 0)
            {
                return(new NamespaceResolveResult(callingClass, callingMember, namespaceName));
            }

            if (languageProperties.CanImportClasses)
            {
                foreach (IUsing @using in cu.Usings)
                {
                    foreach (string import in @using.Usings)
                    {
                        IClass c = GetClass(import);
                        if (c != null)
                        {
                            IMember member = GetMember(c.DefaultReturnType, identifier);
                            if (member != null)
                            {
                                return(CreateMemberResolveResult(member));
                            }
                            ResolveResult result = ResolveMethod(c.DefaultReturnType, identifier);
                            if (result != null)
                            {
                                return(result);
                            }
                        }
                    }
                }
            }

            if (languageProperties.ImportModules)
            {
                ArrayList list = new ArrayList();
                CtrlSpaceResolveHelper.AddImportedNamespaceContents(list, cu, callingClass);
                foreach (object o in list)
                {
                    IClass c = o as IClass;
                    if (c != null && IsSameName(identifier, c.Name))
                    {
                        return(new TypeResolveResult(callingClass, callingMember, c));
                    }
                    IMember member = o as IMember;
                    if (member != null && IsSameName(identifier, member.Name))
                    {
                        if (member is IMethod)
                        {
                            return(new MethodResolveResult(callingClass, callingMember, member.DeclaringType.DefaultReturnType, member.Name));
                        }
                        else
                        {
                            return(CreateMemberResolveResult(member));
                        }
                    }
                }
            }

            return(null);
        }
        public ResolveResult Resolve(ExpressionResult expressionResult,
                                     int caretLineNumber,
                                     int caretColumn,
                                     string fileName,
                                     string fileContent)
        {
            string expression = GetFixedExpression(expressionResult);

            if (!Initialize(fileName, caretLineNumber, caretColumn))
            {
                return(null);
            }

            Expression expr = null;

            if (language == NR.SupportedLanguage.VBNet)
            {
                if (expression.Length == 0 || expression[0] == '.')
                {
                    return(WithResolve(expression, fileContent));
                }
                else if ("global".Equals(expression, StringComparison.InvariantCultureIgnoreCase))
                {
                    return(new NamespaceResolveResult(null, null, ""));
                }
            }
            if (expr == null)
            {
                expr = ParseExpression(expression);
                if (expr == null)
                {
                    return(null);
                }
                if (expressionResult.Context.IsObjectCreation)
                {
                    Expression tmp = expr;
                    while (tmp != null)
                    {
                        if (tmp is IdentifierExpression)
                        {
                            return(ResolveInternal(expr, ExpressionContext.Type));
                        }
                        if (tmp is FieldReferenceExpression)
                        {
                            tmp = (tmp as FieldReferenceExpression).TargetObject;
                        }
                        else
                        {
                            break;
                        }
                    }
                    expr = ParseExpression("new " + expression);
                    if (expr == null)
                    {
                        return(null);
                    }
                }
            }

            if (expressionResult.Context.IsAttributeContext)
            {
                return(ResolveAttribute(expr));
            }

            RunLookupTableVisitor(fileContent);

            ResolveResult rr = CtrlSpaceResolveHelper.GetResultFromDeclarationLine(callingClass, callingMember as IMethodOrProperty, caretLine, caretColumn, expression);

            if (rr != null)
            {
                return(rr);
            }

            return(ResolveInternal(expr, expressionResult.Context));
        }
        public ArrayList CtrlSpace(int caretLine, int caretColumn, string fileName, string fileContent, ExpressionContext context)
        {
            ArrayList result = new ArrayList();

            if (language == NR.SupportedLanguage.VBNet)
            {
                foreach (KeyValuePair <string, string> pair in TypeReference.PrimitiveTypesVB)
                {
                    if ("System." + pair.Key != pair.Value)
                    {
                        result.Add(GetPrimitiveClass(pair.Value, pair.Key));
                    }
                }
                result.Add("Global");
                result.Add("New");
            }
            else
            {
                foreach (KeyValuePair <string, string> pair in TypeReference.PrimitiveTypesCSharp)
                {
                    result.Add(GetPrimitiveClass(pair.Value, pair.Key));
                }
            }
            ParseInformation parseInfo = HostCallback.GetParseInformation(fileName);

            if (parseInfo == null)
            {
                return(null);
            }

            this.caretLine   = caretLine;
            this.caretColumn = caretColumn;

            lookupTableVisitor = new LookupTableVisitor(languageProperties.NameComparer);

            cu = parseInfo.MostRecentCompilationUnit;

            if (cu != null)
            {
                callingClass = cu.GetInnermostClass(caretLine, caretColumn);
            }

            callingMember = GetCurrentMember();
            if (callingMember != null)
            {
                CompilationUnit parsedCu = ParseCurrentMemberAsCompilationUnit(fileContent);
                if (parsedCu != null)
                {
                    lookupTableVisitor.VisitCompilationUnit(parsedCu, null);
                }
            }

            CtrlSpaceResolveHelper.AddContentsFromCalling(result, callingClass, callingMember);

            foreach (KeyValuePair <string, List <LocalLookupVariable> > pair in lookupTableVisitor.Variables)
            {
                if (pair.Value != null && pair.Value.Count > 0)
                {
                    foreach (LocalLookupVariable v in pair.Value)
                    {
                        if (IsInside(new NR.Location(caretColumn, caretLine), v.StartPos, v.EndPos))
                        {
                            // convert to a field for display
                            result.Add(CreateLocalVariableField(v, pair.Key));
                            break;
                        }
                    }
                }
            }
            CtrlSpaceResolveHelper.AddImportedNamespaceContents(result, cu, callingClass);
            return(result);
        }
Beispiel #8
0
        public ArrayList CtrlSpace(int caretLine, int caretColumn, ParseInformation parseInfo, string fileContent, ExpressionContext context)
        {
            ArrayList result = new ArrayList();

            if (!Initialize(parseInfo, caretLine, caretColumn))
            {
                return(null);
            }
            if (context == ExpressionContext.Importable)
            {
                pc.AddNamespaceContents(result, "", pc.Language, true);
                CtrlSpaceResolveHelper.AddUsing(result, pc.DefaultImports, pc);
                return(result);
            }

            CtrlSpaceResolveHelper.AddContentsFromCalling(result, callingClass, callingMember);
            AddImportedNamespaceContents(result);

            if (BooProject.BooCompilerPC != null)
            {
                if (context == ExpressionFinder.BooAttributeContext.Instance)
                {
                    foreach (object o in BooProject.BooCompilerPC.GetNamespaceContents("Boo.Lang"))
                    {
                        IClass c = o as IClass;
                        if (c != null && c.Name.EndsWith("Attribute") && !c.IsAbstract)
                        {
                            result.Add(GetPrimitiveClass(BooProject.BooCompilerPC, c.FullyQualifiedName, c.Name.Substring(0, c.Name.Length - 9).ToLowerInvariant()));
                        }
                    }
                }
                else
                {
                    foreach (object o in BooProject.BooCompilerPC.GetNamespaceContents("Boo.Lang"))
                    {
                        IClass c = o as IClass;
                        if (c != null && c.Name.EndsWith("Macro") && !c.IsAbstract)
                        {
                            result.Add(GetPrimitiveClass(BooProject.BooCompilerPC, c.FullyQualifiedName, c.Name.Substring(0, c.Name.Length - 5).ToLowerInvariant()));
                        }
                    }
                }
            }

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

            foreach (object o in result)
            {
                IMember m = o as IMember;
                if (m != null)
                {
                    knownVariableNames.Add(m.Name);
                }
            }
            VariableListLookupVisitor vllv = new VariableListLookupVisitor(knownVariableNames, this);

            vllv.Visit(GetCurrentBooMethod());
            foreach (KeyValuePair <string, IReturnType> entry in vllv.Results)
            {
                result.Add(new DefaultField.LocalVariableField(entry.Value, entry.Key, DomRegion.Empty, callingClass));
            }

            return(result);
        }
Beispiel #9
0
        public ResolveResult Resolve(ExpressionResult expressionResult,
                                     ParseInformation parseInfo, string fileContent)
        {
            if (!Initialize(parseInfo, expressionResult.Region.BeginLine, expressionResult.Region.BeginColumn))
            {
                return(null);
            }
            LoggingService.Debug("Resolve " + expressionResult.ToString());
            if (expressionResult.Expression == "__GlobalNamespace")               // used for "import" completion
            {
                return(new NamespaceResolveResult(callingClass, callingMember, ""));
            }

            ResolveResult rr = CtrlSpaceResolveHelper.GetResultFromDeclarationLine(callingClass, callingMember as IMethodOrProperty, this.caretLine, this.caretColumn, expressionResult);

            if (rr != null)
            {
                return(rr);
            }

            AST.Expression expr;
            try {
                expr = Boo.Lang.Parser.BooParser.ParseExpression("expression", expressionResult.Expression);
            } catch (Exception ex) {
                LoggingService.Debug("Boo expression parser: " + ex.Message);
                return(null);
            }
            if (expr == null)
            {
                return(null);
            }
            if (expr is AST.IntegerLiteralExpression)
            {
                return(new IntegerLiteralResolveResult(callingClass, callingMember, pc.SystemTypes.Int32));
            }

            if (expressionResult.Context == ExpressionFinder.BooAttributeContext.Instance)
            {
                AST.MethodInvocationExpression mie = expr as AST.MethodInvocationExpression;
                if (mie != null)
                {
                    expr = mie.Target;
                }
                string           name             = expr.ToCodeString();
                SearchTypeResult searchTypeResult = pc.SearchType(new SearchTypeRequest(name, 0, callingClass, cu, caretLine, caretColumn));
                IReturnType      rt = searchTypeResult.Result;
                if (rt != null && rt.GetUnderlyingClass() != null)
                {
                    return(new TypeResolveResult(callingClass, callingMember, rt));
                }
                rt = pc.SearchType(new SearchTypeRequest(name + "Attribute", 0, callingClass, cu, caretLine, caretColumn)).Result;
                if (rt != null && rt.GetUnderlyingClass() != null)
                {
                    return(new TypeResolveResult(callingClass, callingMember, rt));
                }
                if (BooProject.BooCompilerPC != null)
                {
                    IClass c = BooProject.BooCompilerPC.GetClass("Boo.Lang." + char.ToUpper(name[0]) + name.Substring(1) + "Attribute", 0);
                    if (c != null)
                    {
                        return(new TypeResolveResult(callingClass, callingMember, c));
                    }
                }
                string namespaceName = searchTypeResult.NamespaceResult;
                if (namespaceName != null)
                {
                    return(new NamespaceResolveResult(callingClass, callingMember, namespaceName));
                }
                return(null);
            }
            else
            {
                if (expr.NodeType == AST.NodeType.ReferenceExpression)
                {
                    // this could be a macro
                    if (BooProject.BooCompilerPC != null)
                    {
                        string name = ((AST.ReferenceExpression)expr).Name;
                        IClass c    = BooProject.BooCompilerPC.GetClass("Boo.Lang." + char.ToUpper(name[0]) + name.Substring(1) + "Macro", 0);
                        if (c != null)
                        {
                            return(new TypeResolveResult(callingClass, callingMember, c));
                        }
                    }
                }
            }

            ResolveVisitor visitor = new ResolveVisitor(this);

            visitor.Visit(expr);
            ResolveResult result = visitor.ResolveResult;

            if (expressionResult.Context == ExpressionContext.Type && result is MixedResolveResult)
            {
                result = (result as MixedResolveResult).TypeResult;
            }
            return(result);
        }
Beispiel #10
0
        public static ResolveResult ResolveLowLevel(string fileName, string fileContent, int caretLine, int caretColumn, CompilationUnit compilationUnit, string expressionString, Expression expression, ExpressionContext context)
        {
            if (fileName == null)
            {
                throw new ArgumentNullException("fileName");
            }
            if (fileContent == null)
            {
                throw new ArgumentNullException("fileName");
            }
            if (expression == null)
            {
                throw new ArgumentNullException("expression");
            }

            IProject p = ProjectFileDictionaryService.GetProjectForFile(fileName);

            if (p == null)
            {
                LoggingService.Info("ResourceToolkit: NRefactoryAstCacheService: ResolveLowLevel failed. Project is null for file '" + fileName + "'");
                return(null);
            }

            IProjectContent pc = ResourceResolverService.GetProjectContent(p);

            if (pc == null)
            {
                LoggingService.Info("ResourceToolkit: NRefactoryAstCacheService: ResolveLowLevel failed. ProjectContent is null for project '" + p.ToString() + "'");
                return(null);
            }

            NRefactoryResolver resolver = ResourceResolverService.CreateResolver(fileName) as NRefactoryResolver;

            if (resolver == null)
            {
                resolver = new NRefactoryResolver(LanguageProperties.CSharp);
            }

            if (compilationUnit == null)
            {
                compilationUnit = GetFullAst(resolver.Language, fileName, fileContent);
            }
            if (compilationUnit == null)
            {
                LoggingService.Info("ResourceToolkit: NRefactoryAstCacheService: ResolveLowLevel failed due to the compilation unit being unavailable.");
                return(null);
            }

            if (!resolver.Initialize(ParserService.GetParseInformation(fileName), caretLine, caretColumn))
            {
                LoggingService.Info("ResourceToolkit: NRefactoryAstCacheService: ResolveLowLevel failed. NRefactoryResolver.Initialize returned false.");
                return(null);
            }

            if (resolver.CallingClass != null)
            {
                ResolveResult rr;
                if (expressionString == null)
                {
                    // HACK: Re-generate the code for the expression from the expression object by using the code generator.
                    // This is necessary when invoking from inside an AST visitor where the
                    // code belonging to this expression is unavailable.
                    expressionString = resolver.LanguageProperties.CodeGenerator.GenerateCode(expression, String.Empty);
                }
                if ((rr = CtrlSpaceResolveHelper.GetResultFromDeclarationLine(resolver.CallingClass, resolver.CallingMember as IMethodOrProperty, caretLine, caretColumn, new ExpressionResult(expressionString))) != null)
                {
                    return(rr);
                }
            }

            if (resolver.CallingMember != null)
            {
                // Cache member->node mappings to improve performance
                // (if cache is enabled)
                INode memberNode;
                if (!CacheEnabled || !cachedMemberMappings.TryGetValue(resolver.CallingMember, out memberNode))
                {
                    MemberFindAstVisitor visitor = new MemberFindAstVisitor(resolver.CallingMember);
                    compilationUnit.AcceptVisitor(visitor, null);
                    memberNode = visitor.MemberNode;
                    if (CacheEnabled && memberNode != null)
                    {
                        cachedMemberMappings.Add(resolver.CallingMember, memberNode);
                    }
                }

                if (memberNode == null)
                {
                    LoggingService.Info("ResourceToolkit: NRefactoryAstCacheService: Could not find member in AST: " + resolver.CallingMember.ToString());
                }
                else
                {
                    resolver.RunLookupTableVisitor(memberNode);
                }
            }

            return(resolver.ResolveInternal(expression, context));
        }