From dd9e0233eeb8efef9a9543485497e4c1ad70c017 Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Sun, 29 Aug 2010 20:42:00 +0200 Subject: [PATCH] Add CurrentParseInformation to EditorContext. This allows retrieving the ProjectContent without requiring CallingClass!=null (fixes http://community.sharpdevelop.net/forums/t/11756.aspx) Also removed some try-catch-ignore constructs. --- .../Project/Src/ContextActions/AddUsing.cs | 36 ++++++++----------- .../ContextActions/EditorContext.cs | 34 ++++++++++++------ 2 files changed, 38 insertions(+), 32 deletions(-) diff --git a/src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/AddUsing.cs b/src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/AddUsing.cs index 989d85164f..7ef72bd701 100644 --- a/src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/AddUsing.cs +++ b/src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/AddUsing.cs @@ -21,50 +21,41 @@ namespace SharpRefactoring.ContextActions public class AddUsingProvider : ContextActionsProvider { public override IEnumerable GetAvailableActions(EditorContext context) - { - foreach (var contextAction in GetAddUsingContextActions(context.CurrentSymbol, context.Editor, context.CurrentExpression.Context)) { - yield return contextAction; - } - } - - IEnumerable GetAddUsingContextActions(ResolveResult symbol, ITextEditor editor, ExpressionContext exprContext) { // class - foreach (var addUsingAction in RefactoringService.GetAddUsingActions(symbol,editor)) { + foreach (var addUsingAction in RefactoringService.GetAddUsingActions(context.CurrentSymbol, context.Editor)) { yield return addUsingAction; } // extension method - if (exprContext != ExpressionContext.Attribute) { - foreach (var addUsingAction in GetAddUsingExtensionMethodActions(symbol, editor)) { + if (context.CurrentExpression.Context != ExpressionContext.Attribute) { + foreach (var addUsingAction in GetAddUsingExtensionMethodActions(context)) { yield return addUsingAction; } } // attribute - if (exprContext == ExpressionContext.Attribute) { - foreach (var addUsingAction in GetAddUsingAttributeActions(symbol, editor)) { + if (context.CurrentExpression.Context == ExpressionContext.Attribute) { + foreach (var addUsingAction in GetAddUsingAttributeActions(context)) { yield return addUsingAction; } } } #region Extension method - IEnumerable GetAddUsingExtensionMethodActions(ResolveResult symbol, ITextEditor editor) + IEnumerable GetAddUsingExtensionMethodActions(EditorContext context) { - if (!(symbol is UnknownMethodResolveResult)) + UnknownMethodResolveResult rr = context.CurrentSymbol as UnknownMethodResolveResult; + if (rr == null) yield break; - UnknownMethodResolveResult rr = symbol as UnknownMethodResolveResult; List results = new List(); - IProjectContent pc = rr.CallingClass.ProjectContent; + IProjectContent pc = context.ProjectContent; SearchAllExtensionMethodsWithName(results, pc, rr.CallName); foreach (IProjectContent content in pc.ReferencedContents) SearchAllExtensionMethodsWithName(results, content, rr.CallName); - if (!results.Any()) - yield break; foreach (IClass c in results) { - yield return new RefactoringService.AddUsingAction(rr.CallingClass.CompilationUnit, editor, c.Namespace); + yield return new RefactoringService.AddUsingAction(context.CurrentParseInformation.CompilationUnit, context.Editor, c.Namespace); } } @@ -79,14 +70,15 @@ namespace SharpRefactoring.ContextActions #endregion #region Attribute - IEnumerable GetAddUsingAttributeActions(ResolveResult symbol, ITextEditor editor) + IEnumerable GetAddUsingAttributeActions(EditorContext context) { + ResolveResult symbol = context.CurrentSymbol; if (!(symbol is UnknownIdentifierResolveResult || symbol is UnknownMethodResolveResult)) yield break; List results = new List(); - ParseInformation info = ParserService.GetParseInformation(editor.FileName); + ParseInformation info = context.CurrentParseInformation; if (info == null || info.CompilationUnit == null || info.CompilationUnit.ProjectContent == null) yield break; ICompilationUnit unit = info.CompilationUnit; @@ -103,7 +95,7 @@ namespace SharpRefactoring.ContextActions } foreach (IClass c in results) { - yield return new RefactoringService.AddUsingAction(unit, editor, c.Namespace); + yield return new RefactoringService.AddUsingAction(unit, context.Editor, c.Namespace); } } diff --git a/src/Main/Base/Project/Src/Services/RefactoringService/ContextActions/EditorContext.cs b/src/Main/Base/Project/Src/Services/RefactoringService/ContextActions/EditorContext.cs index a5431f5e31..59dd543d0a 100644 --- a/src/Main/Base/Project/Src/Services/RefactoringService/ContextActions/EditorContext.cs +++ b/src/Main/Base/Project/Src/Services/RefactoringService/ContextActions/EditorContext.cs @@ -38,6 +38,20 @@ namespace ICSharpCode.SharpDevelop.Refactoring /// public ResolveResult CurrentSymbol { get; private set; } + /// + /// Language independent. + /// + public ParseInformation CurrentParseInformation { get; private set; } + + public IProjectContent ProjectContent { + get { + if (CurrentParseInformation != null) + return CurrentParseInformation.CompilationUnit.ProjectContent; + else + return null; + } + } + public IDocumentLine CurrentLine { get; private set; } /// /// Only available for C# and VB. @@ -70,6 +84,7 @@ namespace ICSharpCode.SharpDevelop.Refactoring this.CurrentExpression = GetExpressionAtCaret(editor); this.CurrentSymbol = ResolveExpression(CurrentExpression, editor, CaretLine, CaretColumn); + this.CurrentParseInformation = ParserService.GetExistingParseInformation(editor.FileName); this.CurrentLine = editor.Document.GetLine(CaretLine); this.CurrentLineAST = GetCurrentLineAst(this.CurrentLine, editor); @@ -202,12 +217,12 @@ namespace ICSharpCode.SharpDevelop.Refactoring var snippetParser = GetSnippetParser(editor); if (snippetParser == null) return null; - try { + //try { return snippetParser.Parse(currentLine.Text); - } - catch { - return null; - } + //} + //catch { + // return null; + //} } SnippetParser GetSnippetParser(ITextEditor editor) @@ -233,13 +248,12 @@ namespace ICSharpCode.SharpDevelop.Refactoring INode GetCurrentMemberAST(ITextEditor editor) { - try { + //try { var resolver = GetInitializedNRefactoryResolver(editor, this.CaretLine, this.CaretColumn); return resolver.ParseCurrentMember(editor.Document.Text); - } - catch { - return null; - } + //} catch { + // return null; + //} } NRefactoryResolver GetInitializedNRefactoryResolver(ITextEditor editor, int caretLine, int caretColumn)