public void SearchClassDifferentNamespacePreferVisible() { ICompilationUnit ref1 = Prepare(LanguageProperties.CSharp); ref1.ProjectContent.AddClassToNamespaceList(new DefaultClass(ref1, "NS1.ClassName") { Modifiers = ModifierEnum.Internal }); ICompilationUnit ref2 = Prepare(LanguageProperties.CSharp); ref2.ProjectContent.AddClassToNamespaceList(new DefaultClass(ref2, "NS2.ClassName") { Modifiers = ModifierEnum.Public }); ICompilationUnit cu = Prepare(LanguageProperties.CSharp); cu.ProjectContent.ReferencedContents.Add(ref1.ProjectContent); cu.ProjectContent.ReferencedContents.Add(ref2.ProjectContent); cu.UsingScope.Usings.Add(new DefaultUsing(cu.ProjectContent) { Usings = { "NS1", "NS2" } }); SearchTypeResult r = cu.ProjectContent.SearchType(new SearchTypeRequest("ClassName", 0, null, cu, 1, 1)); Assert.AreEqual(ModifierEnum.Public, r.Result.GetUnderlyingClass().Modifiers); }
public override IList <IUsing> FindUnusedUsingDeclarations(string fileName, string fileContent, ICompilationUnit cu) { IClass @class = cu.Classes.Count == 0 ? null : cu.Classes[0]; Dictionary <PossibleTypeReference, object> references = FindPossibleTypeReferences(fileContent); if (references == null) { return(new IUsing[0]); } Dictionary <IUsing, object> dict = new Dictionary <IUsing, object>(); foreach (PossibleTypeReference tr in references.Keys) { SearchTypeRequest request = new SearchTypeRequest(tr.Name, tr.TypeParameterCount, @class, cu, 1, 1); SearchTypeResult response = cu.ProjectContent.SearchType(request); if (response.UsedUsing != null) { dict[response.UsedUsing] = null; } } List <IUsing> list = new List <IUsing>(); foreach (IUsing import in cu.Usings) { if (!dict.ContainsKey(import)) { if (import.HasAliases) { foreach (string key in import.Aliases.Keys) { if (references.ContainsKey(new PossibleTypeReference(key))) { goto checkNextImport; } } } list.Add(import); // this using is unused } checkNextImport :; } return(list); }
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); }
public override IList <IUsing> FindUnusedUsingDeclarations(IDomProgressMonitor progressMonitor, string fileName, string fileContent, ICompilationUnit cu) { IClass @class = cu.Classes.Count == 0 ? null : cu.Classes[0]; HashSet <PossibleTypeReference> references = FindPossibleTypeReferences(progressMonitor, fileContent, new ParseInformation(cu)); if (references == null) { return(new IUsing[0]); } HashSet <IUsing> usedUsings = new HashSet <IUsing>(); foreach (PossibleTypeReference tr in references) { if (tr.ExtensionMethod != null) { // the invocation of an extension method can implicitly use a using StringComparer nameComparer = cu.ProjectContent.Language.NameComparer; // go through all usings in all nested child scopes foreach (IUsing import in cu.GetAllUsings()) { foreach (string i in import.Usings) { if (nameComparer.Equals(tr.ExtensionMethod.DeclaringType.Namespace, i)) { usedUsings.Add(import); } } } } else { // normal possible type reference SearchTypeRequest request = new SearchTypeRequest(tr.Name, tr.TypeParameterCount, @class, cu, 1, 1); SearchTypeResult response = cu.ProjectContent.SearchType(request); if (response.UsedUsing != null) { usedUsings.Add(response.UsedUsing); } } } List <IUsing> unusedUsings = new List <IUsing>(); foreach (IUsing import in cu.GetAllUsings()) { if (!usedUsings.Contains(import)) { if (import.HasAliases) { foreach (string key in import.Aliases.Keys) { if (references.Contains(new PossibleTypeReference(key))) { goto checkNextImport; } } } unusedUsings.Add(import); // this using is unused } checkNextImport :; } return(unusedUsings); }
bool ResolveIdentifier(string identifier) { IField local; ClearResult(); if (resolver.CallingMember != null) { local = resolver.FindLocalVariable(identifier, false); if (local != null) { MakeResult(local); return(true); } IMethodOrProperty method = resolver.CallingMember; if (method != null) { foreach (IParameter p in method.Parameters) { if (IsSameName(p.Name, identifier)) { MakeResult(new DefaultField.ParameterField(p.ReturnType, p.Name, p.Region, callingClass)); return(true); } } if (method is IProperty && IsSameName(identifier, "value")) { if (((IProperty)method).SetterRegion.IsInside(resolver.CaretLine, resolver.CaretColumn)) { MakeResult(new DefaultField.ParameterField(method.ReturnType, "value", method.Region, callingClass)); return(true); } } } } { // Find members of this class or enclosing classes IClass tmp = callingClass; while (tmp != null) { if (ResolveMember(tmp.DefaultReturnType, identifier)) { return(true); } tmp = tmp.DeclaringType; } } SearchTypeResult searchTypeResult = projectContent.SearchType(new SearchTypeRequest(identifier, 0, callingClass, cu, resolver.CaretLine, resolver.CaretColumn)); if (!string.IsNullOrEmpty(searchTypeResult.NamespaceResult)) { MakeNamespaceResult(searchTypeResult.NamespaceResult); return(true); } // Boo can import classes+modules: foreach (object o in resolver.GetImportedNamespaceContents()) { IClass c = o as IClass; if (c != null && IsSameName(identifier, c.Name)) { MakeTypeResult(c); return(true); } IMember member = o as IMember; if (member != null && IsSameName(identifier, member.Name)) { if (member is IMethod) { MakeMethodResult(member.DeclaringType.DefaultReturnType, member.Name); } else { MakeResult(member); } return(true); } } local = resolver.FindLocalVariable(identifier, true); if (local != null) { MakeResult(local); return(true); } return(false); }