public IEnumerable <JetTuple <IProjectFile, string, IProjectFile> > GetRelatedFiles(IProjectFile projectFile) { var typeNamesInFile = GetTypeNamesDefinedInFile(projectFile).ToList(); var candidateTypeNames = GetTypeCandidates(typeNamesInFile); // Look for the candidate types throught the solution. var solution = projectFile.GetSolution(); var candidateTypes = new List <IClrDeclaredElement>(); foreach (var candidateTypeName in candidateTypeNames) { var types = FindType(solution, candidateTypeName); candidateTypes.AddRange(types); } // Get the source files for each of the candidate types. var sourceFiles = new List <IPsiSourceFile>(); foreach (var type in candidateTypes) { var sourceFilesForCandidateType = type.GetSourceFiles(); sourceFiles.AddRange(sourceFilesForCandidateType); } var elementCollector = new RecursiveElementCollector <ITypeDeclaration>(); foreach (var psiSourceFile in sourceFiles) { foreach (var file in psiSourceFile.EnumerateDominantPsiFiles()) { elementCollector.ProcessElement(file); } } var elements = elementCollector.GetResults(); IEnumerable <IProjectFile> projectFiles = elements.Select(declaration => declaration.GetSourceFile().ToProjectFile()); var rval = new List <JetTuple <IProjectFile, string, IProjectFile> >(); foreach (var file in projectFiles.OfType <ProjectFileImpl>().Distinct(pf => pf.Location.FullPath)) { // Remove all extensions (e.g.: .xaml.cs). var fn = file.Name; var dotPos = fn.IndexOf('.'); if (dotPos != -1) { fn = fn.Substring(0, dotPos); } var display = fn.EndsWith("ViewModel") ? "ViewModel" : "View"; var tuple = JetTuple.Of((IProjectFile)file, display, projectFile); rval.Add(tuple); } return(rval); }
private IEnumerable <JetTuple <string, bool> > GetQuickSearchTexts(IDeclaredElement declaredElement) { var ctor = declaredElement as IConstructor; if (ctor != null) { var cDotName = ctor.IsStatic ? JetTuple.Of(".cctor", false) : JetTuple.Of(".ctor", false); var quickSearchTexts = new Collection <JetTuple <string, bool> > { cDotName, JetTuple.Of("new", false) }; var containingType = ctor.GetContainingType(); if (containingType != null) { quickSearchTexts.Add(JetTuple.Of(containingType.ShortName, true)); } var cName = ctor.IsStatic ? JetTuple.Of("cctor", false) : JetTuple.Of("ctor", false); quickSearchTexts.Add(cName); return(quickSearchTexts); } if (IsIndexer()) { return new[] { JetTuple.Of("this", false) } } ; if ((declaredElement as IMethod).IsOverridesObjectFinalize()) { return new[] { JetTuple.Of(declaredElement.ShortName, false), JetTuple.Of("~", false) } } ; var oper = declaredElement as IOperator; if (oper != null) { return new[] { JetTuple.Of(oper.ShortName, true), JetTuple.Of("operator", false) } } ; var evt = declaredElement as IEvent; if (evt != null) { return new[] { JetTuple.Of(evt.ShortName, true), JetTuple.Of("event", false) } } ; return(new[] { JetTuple.Of(declaredElement.ShortName, true) }); }
public static ICollection <JetTuple <MvcKind, string, IAttributeInstance> > GetMvcKinds( [NotNull] this IAttributesOwner element) { CodeAnnotationsCache codeAnnotations = element.GetPsiServices().GetCodeAnnotationsCache(); return(element .GetAttributeInstances(false) .SelectMany(attr => MvcKinds.Where(pair => codeAnnotations.IsAnnotationAttribute(attr, pair.Key)) .Select(pair => JetTuple.Of ( pair.Value, MvcKindAnonymousPropertyInitializers.ContainsKey(pair.Value) ? MvcKindAnonymousPropertyInitializers[pair.Value](attr) : null, attr ))) .ToList()); }
public static bool IsModelTypeExpression([NotNull] ITreeNode node, out IArgument argument, out IList <JetTuple <IWebFileWithCodeBehind, IDeclaredType, IType> > modelTypes) { modelTypes = null; argument = node.GetContainingNode <IArgument>(true); if ((argument == null) || (argument.Expression != node)) { return(false); } var argumentsOwner = argument.GetContainingNode <IArgumentsOwner>(); if (argumentsOwner == null) { return(false); } var possibleViewExpressions = new LocalList <ITreeNode>(); bool modelFound = false; foreach (var data in RetrieveArgumentExpressions(argumentsOwner)) { modelFound = modelFound || ((data.First == node) && data.Second.Any(_ => _.A == MvcKind.ModelType)); if (data.Second.Any(_ => _.A == MvcKind.View || _.A == MvcKind.PartialView)) { possibleViewExpressions.Add(data.First); } } if (!modelFound) { return(false); } possibleViewExpressions.Add(argumentsOwner); IPsiModule psiModule = node.GetPsiModule(); modelTypes = ( // if model argument presents that view must be somewhere from viewExpression in possibleViewExpressions.ResultingList() from viewReference in viewExpression.GetReferences().OfType <IMvcViewReference>() let viewResolveResult = viewReference.Resolve().Result from viewDeclaredElement in viewResolveResult.Candidates.Prepend(viewResolveResult.DeclaredElement) .OfType <IPathDeclaredElement>() let view = viewDeclaredElement.GetProjectItem() as IProjectFile where view != null let viewFile = view.GetPrimaryPsiFile() as IWebFileWithCodeBehind where viewFile != null from superType in viewFile.GetSuperTypes() from baseTypeName in FileSpecificUtil.GetMvcViewWithModelBaseTypes(view) #if SDK80 let baseType = TypeFactory.CreateTypeByCLRName(baseTypeName, psiModule, node.GetResolveContext()) #else let baseType = TypeFactory.CreateTypeByCLRName(baseTypeName, psiModule) #endif let modelTypeParameter = baseType.GetSubstitution().Domain.Single() #if SDK80 from concreteBaseType in superType.GetSuperType(baseType.GetTypeElement(), node.GetResolveContext()) #else from concreteBaseType in superType.GetSuperType(baseType.GetTypeElement()) #endif where !concreteBaseType.IsUnknown let modelType = concreteBaseType.GetSubstitution()[modelTypeParameter] where !modelType.IsUnknown select JetTuple.Of(viewFile, superType, modelType) ).ToList(); return(true); }
private static ICollection <JetTuple <string, string, MvcUtil.DeterminationKind, ICollection <IClass> > > GetModulesNotCached([NotNull] IArgumentsOwner argumentsOwner) { argumentsOwner.AssertIsValid("argumentsOwner is invalid"); IPsiModule psiModule = argumentsOwner.GetPsiModule(); IProject project = argumentsOwner.GetProject(); Assertion.AssertNotNull(project, "project == null"); IProjectFile projectFile = argumentsOwner.GetSourceFile().ToProjectFile(); Assertion.AssertNotNull(projectFile, "projectFile == null"); IList <string> controllerNames = ProcessArgumentsExpression(argumentsOwner, MvcKind.Controller); ICollection <JetTuple <string, string, MvcUtil.DeterminationKind, bool> > moduleNames = new List <JetTuple <string, string, MvcUtil.DeterminationKind, bool> >(); if (controllerNames.IsEmpty()) { // first, try detect implicit controller type by view var typeDeclaration = argumentsOwner.GetContainingNode <ITypeDeclaration>(); IClass declaredElement = (typeDeclaration != null) ? typeDeclaration.DeclaredElement as IClass : null; if (declaredElement == null) { return(EmptyList <JetTuple <string, string, MvcUtil.DeterminationKind, ICollection <IClass> > > .InstanceList); } JetTuple <string, string, MvcUtil.DeterminationKind, ICollection <IClass> > @default = JetTuple.Of(GetControllerArea(declaredElement), GetControllerName(declaredElement), MvcUtil.DeterminationKind.ImplicitByContainingMember, (ICollection <IClass>) new IClass[] { null }); // with inheritors if (declaredElement.IsAbstract) { // with inheritors return(GetAvailableModules(psiModule, argumentsOwner.GetResolveContext(), baseClass: declaredElement) .SelectMany(_ => _.Value) .GroupBy( @class => new { area = GetControllerArea(@class), controller = GetControllerName(@class) }) .Select(_ => JetTuple.Of(_.Key.area, _.Key.controller, MvcUtil.DeterminationKind.ImplicitByContainingMember, (ICollection <IClass>)_.ToList())) .DefaultIfEmpty(@default) .ToList()); } moduleNames = new[] { JetTuple.Of(@default.A, @default.B, MvcUtil.DeterminationKind.ImplicitByContainingMember, true) }; } return((from tuple in moduleNames let availableModules = GetAvailableModules(psiModule, argumentsOwner.GetResolveContext(), includingIntermediateControllers: tuple.D) select JetTuple.Of(tuple.A, tuple.B, tuple.C, tuple.B == null ? (ICollection <IClass>) new IClass[] { null } : availableModules.GetValuesCollection(tuple.B))) .ToList()); }
private IEnumerable <JetTuple <string, bool> > GetQuickSearchTexts(IDeclaredElement declaredElement) { return(new[] { JetTuple.Of(declaredElement.ShortName, true) }); }
public IReference[] GetReferences(ITreeNode element, IReference[] oldReferences) { if (oldReferences != null && oldReferences.Any() && oldReferences.All(reference => { if (reference is IMvcReference && reference.GetTreeNode() == element) { return(((IMvcReference)reference).IsInternalValid); } return(false); })) { return(oldReferences); } var expression1 = element as TExpression; if (expression1 != null && HasImplicitReference(expression1, GetAllMvcNames(expression1))) { return(GetImplicitReferences(expression1).ToArray()); } TExpression argumentExpression; string anonymousPropertyName; IExpression mvcLiteral = GetMvcLiteral(element, out argumentExpression, out anonymousPropertyName); if (mvcLiteral == null) { return(EmptyArray <IReference> .Instance); } IParameter parameter = mvcLiteral.GetContainingNode <IArgument>().IfNotNull(d => d.MatchingParameter).IfNotNull(p => p.Element); if (parameter == null) { return(EmptyArray <IReference> .Instance); } var jt = parameter.GetMvcKinds().FirstOrDefault(_ => StringComparer.OrdinalIgnoreCase.Equals(_.B, anonymousPropertyName)); if (jt == null) { return(EmptyArray <IReference> .Instance); } switch (jt.A) { case MvcKind.Area: return(new IReference[] { GetMvcAreaReference(mvcLiteral) }); case MvcKind.Controller: return(new IReference[] { GetMvcControllerReference(mvcLiteral, argumentExpression) }); case MvcKind.Action: return(new IReference[] { GetMvcActionReference(mvcLiteral, argumentExpression) }); case MvcKind.View: case MvcKind.PartialView: case MvcKind.Master: case MvcKind.DisplayTemplate: case MvcKind.EditorTemplate: var list = NancyUtil.GetModules(argumentExpression) .DefaultIfEmpty(JetTuple.Of((string)null, (string)null, MvcUtil.DeterminationKind.Explicit, (ICollection <IClass>)null)).ToList(); return(new IReference[] { GetMvcViewReference(mvcLiteral, list, jt.A, new Version()) }); default: return(EmptyArray <IReference> .Instance); } }