// Return a context object for the item being searched for, or null if the element isn't interesting. // CanContainReferences isn't called if we return null. Do the work once here, then use it multiple times for // each file in CanContainReferences public object GetElementId(IDeclaredElement element) { if (myUnityApi.IsUnityType(element as IClass)) { var sourceFile = element.GetSourceFiles().FirstOrDefault(); if (sourceFile == null) { return(null); } var guid = myMetaFileGuidCache.GetAssetGuid(sourceFile); if (guid == null) { return(null); } // Class usage is in the form: "m_Script: {fileID: 11500000, guid: $guid, ... }" // Get the set of files that contain ALL of these terms return(GetElementId("m_Script", "11500000", guid)); } if (myUnityApi.IsPotentialEventHandler(element as IMethod) || myUnityApi.IsPotentialEventHandler(element as IProperty)) { // Get all files that contain GUID, m_MethodName, short name and 11500000 var sourceFile = element.GetSourceFiles().FirstOrDefault(); if (sourceFile == null) { return(null); } var shortName = element is IMethod ? element.ShortName : (element as IProperty)?.Setter?.ShortName; if (shortName == null) { return(null); } var guid = myMetaFileGuidCache.GetAssetGuid(sourceFile); if (guid == null) { return(null); } // Searching for an event handler method requires matching something like // Event handlers are in the form: // OnSomeEvent: // m_PersistentCalls: // m_Calls: // - m_Target: {fileID: 878035745} // m_MethodName: $shortName // and // --- !u!114 &878035745 // MonoBehaviour: // ... // m_Script: {fileID: 11500000, guid: $guid, type: 3} return(GetElementId("m_PersistentCalls", "m_MethodName", "11500000", guid, shortName)); } return(new UnityYamlSearchGuruId(JetHashSet <IPsiSourceFile> .Empty)); }
// Used to filter files before searching for references. Files must contain ANY of these search terms. An // ISearchGuru implementation can narrow the search domain further (e.g. checking for files that contain ALL of // the terms). Method references require the element short name, while class references require the class's // file's asset guid public override IEnumerable <string> GetAllPossibleWordsInFile(IDeclaredElement element) { if (IsInterestingElement(element)) { var words = new List <string>(); // If it's a class, we only need the asset GUID if (element is IClass) { var metaFileGuidCache = element.GetSolution().GetComponent <MetaFileGuidCache>(); foreach (var sourceFile in element.GetSourceFiles()) { // If the element doesn't have the same name as the file it's in, Unity doesn't recognise it if (!sourceFile.Name.StartsWith(element.ShortName)) { continue; } var guid = metaFileGuidCache.GetAssetGuid(sourceFile); if (guid != null) { words.Add(guid); } } } else { words.Add(element.ShortName); } return(words); } return(EmptyList <string> .Instance); }
public override ISearchDomain GetDeclaredElementSearchDomain(IDeclaredElement declaredElement) { if (!(declaredElement is IMethod methodDeclaration)) { return(base.GetDeclaredElementSearchDomain(declaredElement)); } var specflowStepsUsagesCache = declaredElement.GetPsiServices().GetComponent <SpecflowStepsUsagesCache>(); var specflowStepsDefinitionsCache = declaredElement.GetPsiServices().GetComponent <SpecflowStepsDefinitionsCache>(); var files = new List <IPsiSourceFile>(); foreach (var sourceFile in declaredElement.GetSourceFiles()) { var stepsInFile = specflowStepsDefinitionsCache.AllStepsPerFiles[sourceFile]; foreach (var step in stepsInFile.Where(x => x.MethodName == declaredElement.ShortName).Where(x => x.ClassFullName == methodDeclaration.GetContainingType()?.GetClrName().FullName)) { foreach (var(stepSourceFileUsage, stepsTexts) in specflowStepsUsagesCache.StepUsages[step.StepKind]) { foreach (var stepText in stepsTexts) { if (step.Regex?.IsMatch(stepText) == true) { files.Add(stepSourceFileUsage); } } } } } return(SearchDomainFactory.Instance.CreateSearchDomain(files)); }
public static bool IsFromUnityProject(this IDeclaredElement element) { return(element.GetSourceFiles().Any(sf => { var project = sf.GetProject(); return project != null && project.IsUnityProject(); })); }
public ISearchDomain GetDeclaredElementSearchDomain(IDeclaredElement declaredElement) { if (!(declaredElement is IShaderLabDeclaredElement)) { return(EmptySearchDomain.Instance); } return(mySearchDomainFactory.CreateSearchDomain(declaredElement.GetSourceFiles())); }
static IProjectFile GetProjectFile(IDeclaredElement field) { var sourceFile = field.GetSourceFiles(); if (sourceFile.Count > 0) { return sourceFile[0].ToProjectFile(); } return null; }
public override ISearchDomain GetDeclaredElementSearchDomain(IDeclaredElement declaredElement) { if (declaredElement is PascalVariableDeclared || declaredElement is PascalParameterDeclared) { return(_searchDomainFactory.CreateSearchDomain(declaredElement.GetSourceFiles())); } return(base.GetDeclaredElementSearchDomain(declaredElement)); }
static IProjectFile GetProjectFile(IDeclaredElement field) { var sourceFile = field.GetSourceFiles(); if (sourceFile.Count > 0) { return(sourceFile[0].ToProjectFile()); } return(null); }
private static bool B(IDeclaredElement element1, IDeclaredElement element2) { IPsiSourceFile element1SourceFile = element1.GetSourceFiles().FirstOrDefault(); IPsiSourceFile element2SourceFile = element2.GetSourceFiles().FirstOrDefault(); if (element1SourceFile == null || element2SourceFile == null) { return(element1.ToString() == element2.ToString()); } return(element1SourceFile.DisplayName == element2SourceFile.DisplayName); }
public ISearchDomain GetDeclaredElementSearchDomain(IDeclaredElement declaredElement) { HybridCollection <IPsiSourceFile> files = declaredElement.GetSourceFiles(); if (!(declaredElement is RuleDeclaration)) { if (files.Count > 0) { return(mySearchDomainFactory.CreateSearchDomain(files[0])); } } return(mySearchDomainFactory.CreateSearchDomain(declaredElement.GetSolution(), false)); }
public IEnumerable <FileRename> GetFileRenames(IDeclaredElement declaredElement, string name) { if (declaredElement is TDeclaredElement) { var sourceFile = declaredElement.GetSourceFiles().FirstOrDefault(); if (sourceFile != null) { var psiServices = declaredElement.GetPsiServices(); var projectFile = sourceFile.ToProjectFile(); return(new[] { new FileRename(psiServices, projectFile, name) }); } } return(EmptyList <FileRename> .Enumerable); }
public override IEnumerable <string> GetAllPossibleWordsInFile(IDeclaredElement declaredElement) { if (!(declaredElement is IMethod methodDeclaration)) { return(base.GetAllPossibleWordsInFile(declaredElement)); } var specflowStepsDefinitionsCache = declaredElement.GetPsiServices().GetComponent <SpecflowStepsDefinitionsCache>(); var words = new HashSet <string>(); foreach (var sourceFile in declaredElement.GetSourceFiles()) { var stepsInFile = specflowStepsDefinitionsCache.AllStepsPerFiles[sourceFile]; foreach (var step in stepsInFile.Where(x => x.MethodName == declaredElement.ShortName).Where(x => x.ClassFullName == methodDeclaration.GetContainingType()?.GetClrName().FullName)) { words.AddRange(step.Pattern.SplitByWords()); } } return(words); }
public static bool IsRenameShouldBeSilent(IDeclaredElement declaredElement) { var project = declaredElement.GetSourceFiles().FirstOrDefault()?.GetProject(); if (project == null) { return(false); } if (project.IsPlayerProject()) { return(true); } if (project.IsMiscProjectItem()) { return(true); } return(false); }
public ISearchDomain GetDeclaredElementSearchDomain(IDeclaredElement declaredElement) { var uriIdentifier = declaredElement as IUriIdentifierDeclaredElement; if (uriIdentifier != null) { var cache = declaredElement.GetSolution().GetComponent <NTriplesCache>(); var files = cache.GetFilesContainingUri(uriIdentifier.GetNamespace(), uriIdentifier.GetLocalName()); return(this.mySearchDomainFactory.CreateSearchDomain(files)); } if (declaredElement is PrefixDeclaration) { var files = declaredElement.GetSourceFiles(); if (files.Count > 0) { return(this.mySearchDomainFactory.CreateSearchDomain(files[0])); } } return(this.mySearchDomainFactory.CreateSearchDomain(declaredElement.GetSolution(), false)); }
private static (string guid, string propertyName)? GetAssetGuidAndPropertyName(ISolution solution, IDeclaredElement declaredElement) { Assertion.Assert(solution.Locks.IsReadAccessAllowed(), "ReadLock required"); var containingType = (declaredElement as IClrDeclaredElement)?.GetContainingType(); if (containingType == null) { return(null); } var sourceFile = declaredElement.GetSourceFiles().FirstOrDefault(); if (sourceFile == null) { return(null); } if (!sourceFile.IsValid()) { return(null); } var guid = solution.GetComponent <MetaFileGuidCache>().GetAssetGuid(sourceFile); if (guid == null) { return(null); } // TODO [19.3] support several names for field // var formerlySerializedAs = AttributeUtil.GetAttribute(fieldDeclaration, KnownTypes.FormerlySerializedAsAttribute); // var oldName = formerlySerializedAs?.Arguments.FirstOrDefault()?.Value?.ConstantValue?.Value as string; return(guid, declaredElement.ShortName); }
public ISearchDomain GetDeclaredElementSearchDomain(IDeclaredElement declaredElement) { HybridCollection<IPsiSourceFile> files = declaredElement.GetSourceFiles(); if (!(declaredElement is RuleDeclaration)) { if (files.Count > 0) { return mySearchDomainFactory.CreateSearchDomain(files[0]); } } return mySearchDomainFactory.CreateSearchDomain(declaredElement.GetSolution(), false); }
public IEnumerable <FileRename> GetFileRenames(IDeclaredElement declaredElement, string newName) { if (!Settings.SupportRenameRefactor) { yield break; } var typeElement = declaredElement as ITypeElement; var clrDeclaredElement = declaredElement as IClrDeclaredElement; if (clrDeclaredElement == null) { yield break; } var psiModule = clrDeclaredElement.Module as IProjectPsiModule; if (psiModule == null) { yield break; } if (typeElement != null)//only support renaming of 'types' { var classNameBeingRenamed = declaredElement.ShortName; var project = psiModule.Project; var solution = project.GetSolution(); if (!project.IsTestProject()) { IList <TestCopProjectItem> targetProjects = new List <TestCopProjectItem>(); foreach (var sourceFile in declaredElement.GetSourceFiles()) { var projectFile = sourceFile.ToProjectFile(); targetProjects.AddRange(project.GetAssociatedProjects(projectFile, classNameBeingRenamed)); } //var targetProjects = project.GetAssociatedProjects(projectFiles); if (targetProjects.IsNullOrEmpty()) { yield break; } //now look for expected file names that are also in correct locations foreach (var targetProject in targetProjects) { var projectTestFilesWithMatchingName = new List <ProjectFileFinder.Match>(); targetProject.Project.Accept(new ProjectFileFinder(projectTestFilesWithMatchingName, targetProject.FilePattern)); foreach (var projectFileMatch in projectTestFilesWithMatchingName) { string expectedNameSpace = projectFileMatch.ProjectFile.CalculateExpectedNamespace(projectFileMatch.ProjectFile.GetPrimaryPsiFile().Language); if (expectedNameSpace == targetProject.FullNamespace()) { string currentName = projectFileMatch.ProjectFile.Location.NameWithoutExtension; var newTestClassName = newName + currentName.Substring(classNameBeingRenamed.Length); ResharperHelper.AppendLineToOutputWindow(project.Locks, "Renaming {0} to {1}".FormatEx(projectFileMatch.ProjectFile.Location.FullPath, newTestClassName)); if (projectFileMatch.ProjectFile.Location.NameWithoutExtension == newTestClassName) { ResharperHelper.AppendLineToOutputWindow(project.Locks, "# skip as same name"); continue; } //TODO EditorManager.GetInstance(solution).OpenProjectFile(projectFileMatch.ProjectFile, new OpenFileOptions(false)); EditorManager.GetInstance(solution).OpenProjectFileAsync(projectFileMatch.ProjectFile, new OpenFileOptions(false)); //need to ensure class within file is renamed tooo yield return (new FileRename(psiModule.GetPsiServices(), projectFileMatch.ProjectFile, newTestClassName)); } } } } } }
// Return a context object for the item being searched for, or null if the element isn't interesting. // CanContainReferences isn't called if we return null. Do the work once here, then use it multiple times for // each file in CanContainReferences public object GetElementId(IDeclaredElement element) { if (myUnityApi.IsUnityType(element as IClass)) { var sourceFile = element.GetSourceFiles().FirstOrDefault(); if (sourceFile == null) { return(null); } var guid = myMetaFileGuidCache.GetAssetGuid(sourceFile); if (guid == null) { return(null); } // Class usage is in the form: "m_Script: {fileID: 11500000, guid: $guid, ... }" // Get the set of files that contain ALL of these terms return(GetElementId("m_Script", "11500000", guid)); } // See RIDER-27684. This allows Unity to use private methods as event handlers if (myUnityApi.IsPotentialEventHandler(element as IMethod) || myUnityApi.IsPotentialEventHandler(element as IProperty)) { // Get all files that contain GUID, m_MethodName, short name and 11500000 var sourceFile = element.GetSourceFiles().FirstOrDefault(); if (sourceFile == null) { return(null); } var shortName = element is IMethod ? element.ShortName : (element as IProperty)?.Setter?.ShortName; if (shortName == null) { return(null); } // In previous version we check that file should contain guid which relates to file // where method is located. It is not true, consider an example: // class A : B {} - guid_1 // class B : MonoBehaviour - guid_2 // { public void Test(){}} // Let's add button, which will use script A and method Test // In previous version we require that yaml file with button should contain guid_2, // but it contains only guid_1 // [TODO] We could find derived classes' guids and add them to filter // Searching for an event handler method requires matching something like // Event handlers are in the form: // OnSomeEvent: // m_PersistentCalls: // m_Calls: // - m_Target: {fileID: 878035745} // m_MethodName: $shortName // and // --- !u!114 &878035745 // MonoBehaviour: // ... // m_Script: {fileID: 11500000, guid: $guid, type: 3} // In some cases 1150000 is not presented... // m_Script: {fileID: 1392445389, guid: f70555f144d8491a825f0804e09c671c, type: 3} Why?? Do not know return(GetElementId("m_PersistentCalls", "m_MethodName", shortName)); } return(new UnityYamlSearchGuruId(JetHashSet <IPsiSourceFile> .Empty)); }
public static bool IsFromUnityProject(this IDeclaredElement element) { return(element.GetSourceFiles().Any(sf => sf.GetProject().IsUnityProject())); }
private IModule GetModule(IDeclaredElement declaredElement) { #if RESHARPER_31 || RESHARPER_40 || RESHARPER_41 return declaredElement.Module; #elif RESHARPER_60_OR_NEWER var sourceFiles = declaredElement.GetSourceFiles(); IPsiModule psiModule; if (sourceFiles.Count == 0) { // HACK: there must be a better way of doing this var propertyInfo = declaredElement.GetType().GetProperty("Module"); psiModule = (IPsiModule)propertyInfo.GetValue(declaredElement, null); } else { psiModule = sourceFiles[0].GetPsiModule(); } return psiModule.ContainingProjectModule; #else return declaredElement.Module.ContainingProjectModule; #endif }