From 62bcd0e7f051ca57c4c89406a0c962a3df8c74bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kon=C3=AD=C4=8Dek?= Date: Sun, 22 Aug 2010 15:38:07 +0000 Subject: [PATCH] Moved "Resolve attribute" to context actions. git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@6438 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61 --- .../Project/Src/ContextActions/AddUsing.cs | 92 ++++++++++++++++--- .../RefactoringService/RefactoringService.cs | 21 ++++- 2 files changed, 99 insertions(+), 14 deletions(-) diff --git a/src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/AddUsing.cs b/src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/AddUsing.cs index b2ec2f25ac..777df1b4fe 100644 --- a/src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/AddUsing.cs +++ b/src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/AddUsing.cs @@ -6,10 +6,12 @@ // using System; using System.Collections.Generic; +using ICSharpCode.SharpDevelop; using ICSharpCode.SharpDevelop.Dom; using ICSharpCode.SharpDevelop.Editor; using ICSharpCode.SharpDevelop.Editor.AvalonEdit; using ICSharpCode.SharpDevelop.Refactoring; +using System.Linq; namespace SharpRefactoring.ContextActions { @@ -24,28 +26,92 @@ namespace SharpRefactoring.ContextActions if (currentLineAST == null) yield break; var symbol = context.CurrentSymbol; - foreach (var contextAction in GetAddUsingContextActions(symbol, context.Editor)) { + foreach (var contextAction in GetAddUsingContextActions(symbol, context.Editor, context.CurrentExpression.Context)) { yield return contextAction; } } - IEnumerable GetAddUsingContextActions(ResolveResult symbol, ITextEditor editor) + IEnumerable GetAddUsingContextActions(ResolveResult symbol, ITextEditor editor, ExpressionContext exprContext) { - IEnumerable addUsingActions = null; - if (symbol is UnknownIdentifierResolveResult) { - addUsingActions = RefactoringService.GetAddUsingActions((UnknownIdentifierResolveResult)symbol, editor); - } else if (symbol is UnknownConstructorCallResolveResult) { - addUsingActions = RefactoringService.GetAddUsingActions((UnknownConstructorCallResolveResult)symbol, editor); + foreach (var addUsingAction in RefactoringService.GetAddUsingActions(symbol,editor)) { + yield return addUsingAction; + } + if (exprContext == ExpressionContext.Attribute) { + foreach (var addUsingAction in GetAddUsingAttributeActions(symbol, editor)) { + yield return addUsingAction; + } } - if (addUsingActions == null) + foreach (var addUsingAction in GetAddUsingExtensionMethodActions(symbol, editor)) { + yield return addUsingAction; + } + } + + IEnumerable GetAddUsingExtensionMethodActions(ResolveResult symbol, ITextEditor editor) + { + yield break; + } + + #region GetAddUsingAttributeActions + IEnumerable GetAddUsingAttributeActions(ResolveResult symbol, ITextEditor editor) + { + if (!(symbol is UnknownIdentifierResolveResult || symbol is UnknownMethodResolveResult)) + yield break; + + List results = new List(); + + ParseInformation info = ParserService.GetParseInformation(editor.FileName); + if (info == null || info.CompilationUnit == null || info.CompilationUnit.ProjectContent == null) yield break; - foreach (var addUsingAction in addUsingActions) { - var addUsingActionCopy = addUsingAction; - yield return new DelegateAction { - Title = "using " + addUsingActionCopy.NewNamespace, - ExecuteAction = addUsingActionCopy.Execute + ICompilationUnit unit = info.CompilationUnit; + IProjectContent pc = info.CompilationUnit.ProjectContent; + + string name = null; + if (symbol is UnknownMethodResolveResult) { + name = Search((UnknownMethodResolveResult)symbol, pc, results); + } + if (symbol is UnknownIdentifierResolveResult) { + name = Search((UnknownIdentifierResolveResult)symbol, pc, results); + } else { + yield break; + } + + foreach (IClass c in results) { + string newNamespace = c.Namespace; + yield return new DelegateAction {Title = "using " + newNamespace, + ExecuteAction = delegate { + NamespaceRefactoringService.AddUsingDeclaration(unit, editor.Document, newNamespace, true); + ParserService.BeginParse(editor.FileName, editor.Document); + } }; } } + + public string Search(UnknownMethodResolveResult rr, IProjectContent pc, List results) + { + SearchAttributesWithName(results, pc, rr.CallName); + foreach (IProjectContent content in pc.ReferencedContents) + SearchAttributesWithName(results, content, rr.CallName); + return rr.CallName; + } + + public string Search(UnknownIdentifierResolveResult rr, IProjectContent pc, List results) + { + SearchAttributesWithName(results, pc, rr.Identifier); + foreach (IProjectContent content in pc.ReferencedContents) + SearchAttributesWithName(results, content, rr.Identifier); + return rr.Identifier; + } + + IClass baseClass = null; + void SearchAttributesWithName(List results, IProjectContent pc, string name) + { + if (baseClass == null) + baseClass = pc.GetClass("System.Attribute", 0); + foreach (IClass c in pc.Classes) { + if (c.IsTypeInInheritanceTree(baseClass) && (c.Name == name || c.Name == name + "Attribute")) + results.Add(c); + } + } + #endregion } } diff --git a/src/Main/Base/Project/Src/Services/RefactoringService/RefactoringService.cs b/src/Main/Base/Project/Src/Services/RefactoringService/RefactoringService.cs index ad14c1dbb0..458d72269e 100644 --- a/src/Main/Base/Project/Src/Services/RefactoringService/RefactoringService.cs +++ b/src/Main/Base/Project/Src/Services/RefactoringService/RefactoringService.cs @@ -595,6 +595,16 @@ namespace ICSharpCode.SharpDevelop.Refactoring #endregion #region Add using + public static IEnumerable GetAddUsingActions(ResolveResult symbol, ITextEditor editor) + { + if (symbol is UnknownIdentifierResolveResult) { + return GetAddUsingActions((UnknownIdentifierResolveResult)symbol, editor); + } else if (symbol is UnknownConstructorCallResolveResult) { + return GetAddUsingActions((UnknownConstructorCallResolveResult)symbol, editor); + } + return new AddUsingAction[0]; + } + public static IEnumerable GetAddUsingActions(UnknownIdentifierResolveResult rr, ITextEditor textArea) { return GetAddUsingActionsForUnknownClass(rr.CallingClass, rr.Identifier, textArea); @@ -632,7 +642,7 @@ namespace ICSharpCode.SharpDevelop.Refactoring } } - public class AddUsingAction + public class AddUsingAction : IContextAction { public ICompilationUnit CompilationUnit { get; private set; } public ITextEditor Editor { get; private set; } @@ -656,6 +666,15 @@ namespace ICSharpCode.SharpDevelop.Refactoring NamespaceRefactoringService.AddUsingDeclaration(CompilationUnit, Editor.Document, NewNamespace, true); ParserService.BeginParse(Editor.FileName, Editor.Document); } + + public string Title { + get { + if (this.Editor.Language.Properties == LanguageProperties.VBNet) { + return "Import " + this.NewNamespace; + } + return "using " + this.NewNamespace; + } + } } #endregion }