static void ShowSourceCodeErrors(IDomProgressMonitor progressMonitor, string errors)
		{
			if (progressMonitor != null)
				progressMonitor.ShowingDialog = true;
			HostCallback.ShowMessage("${res:SharpDevelop.Refactoring.CannotPerformOperationBecauseOfSyntaxErrors}\n" + errors);
			if (progressMonitor != null)
				progressMonitor.ShowingDialog = false;
		}
 static void ShowSourceCodeErrors(IDomProgressMonitor progressMonitor, string errors)
 {
     if (progressMonitor != null)
     {
         progressMonitor.ShowingDialog = true;
     }
     HostCallback.ShowMessage("${res:SharpDevelop.Refactoring.CannotPerformOperationBecauseOfSyntaxErrors}\n" + errors);
     if (progressMonitor != null)
     {
         progressMonitor.ShowingDialog = false;
     }
 }
		NR.IParser ParseFile(IDomProgressMonitor progressMonitor, string fileContent)
		{
			NR.IParser parser = NR.ParserFactory.CreateParser(language, new StringReader(fileContent));
			parser.Parse();
			if (parser.Errors.Count > 0) {
				ShowSourceCodeErrors(progressMonitor, parser.Errors.ErrorOutput);
				parser.Dispose();
				return null;
			} else {
				return parser;
			}
		}
 NR.IParser ParseFile(IDomProgressMonitor progressMonitor, string fileContent)
 {
     NR.IParser parser = NR.ParserFactory.CreateParser(language, new StringReader(fileContent));
     parser.Parse();
     if (parser.Errors.Count > 0)
     {
         ShowSourceCodeErrors(progressMonitor, parser.Errors.ErrorOutput);
         parser.Dispose();
         return(null);
     }
     else
     {
         return(parser);
     }
 }
 protected virtual HashSet <PossibleTypeReference> FindPossibleTypeReferences(IDomProgressMonitor progressMonitor, string fileContent, ParseInformation parseInfo)
 {
     NR.IParser parser = ParseFile(progressMonitor, fileContent);
     if (parser == null)
     {
         return(null);
     }
     else
     {
         FindPossibleTypeReferencesVisitor visitor = new FindPossibleTypeReferencesVisitor(parseInfo);
         parser.CompilationUnit.AcceptVisitor(visitor, null);
         parser.Dispose();
         return(visitor.list);
     }
 }
예제 #6
0
 public virtual IList <IUsing> FindUnusedUsingDeclarations(IDomProgressMonitor progressMonitor, string fileName, string fileContent, ICompilationUnit compilationUnit)
 {
     throw new NotSupportedException();
 }
        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);
        }
		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;
		}
		protected virtual HashSet<PossibleTypeReference> FindPossibleTypeReferences(IDomProgressMonitor progressMonitor, string fileContent, ParseInformation parseInfo)
		{
			NR.IParser parser = ParseFile(progressMonitor, fileContent);
			if (parser == null) {
				return null;
			} else {
				FindPossibleTypeReferencesVisitor visitor = new FindPossibleTypeReferencesVisitor(parseInfo);
				parser.CompilationUnit.AcceptVisitor(visitor, null);
				parser.Dispose();
				return visitor.list;
			}
		}
		public virtual IList<IUsing> FindUnusedUsingDeclarations(IDomProgressMonitor progressMonitor, string fileName, string fileContent, ICompilationUnit compilationUnit)
		{
			throw new NotSupportedException();
		}