Browse Source

Context actions: fixed logical bug when resolving class declarations on current line by name - more classes with same name and different type arguments can exist, of course.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@6201 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
pull/1/head
Martin Koníček 16 years ago
parent
commit
9cc04d536e
  1. 2
      src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/AddUsing.cs
  2. 13
      src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/Extensions.cs
  3. 2
      src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/GenerateMember.cs
  4. 4
      src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/ImplementAbstractClass.cs
  5. 4
      src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/ImplementInterface.cs
  6. 2
      src/Main/Base/Project/ICSharpCode.SharpDevelop.csproj
  7. 4
      src/Main/Base/Project/Src/Services/RefactoringService/ContextActions/ContextActionsService.cs
  8. 21
      src/Main/Base/Project/Src/Services/RefactoringService/ContextActions/EditorContext.cs
  9. 2
      src/Main/Base/Project/Src/Services/RefactoringService/ContextActions/IContextActionsProvider.cs

2
src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/AddUsing.cs

@ -18,7 +18,7 @@ namespace SharpRefactoring.ContextActions @@ -18,7 +18,7 @@ namespace SharpRefactoring.ContextActions
/// </summary>
public class AddUsingProvider : IContextActionsProvider
{
public IEnumerable<IContextAction> GetAvailableActions(EditorASTProvider editorAST)
public IEnumerable<IContextAction> GetAvailableActions(EditorContext editorAST)
{
var currentLineAST = editorAST.CurrentLineAST;
if (currentLineAST == null)

13
src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/Extensions.cs

@ -27,15 +27,18 @@ namespace SharpRefactoring.ContextActions @@ -27,15 +27,18 @@ namespace SharpRefactoring.ContextActions
return findVisitor.Declarations;
}
public static IEnumerable<IClass> GetClassesOnCurrentLine(this EditorASTProvider editorAST)
public static IEnumerable<IClass> GetClassDeclarationsOnCurrentLine(this EditorContext editorContext)
{
var currentLineAST = editorAST.CurrentLineAST;
var currentLineAST = editorContext.CurrentLineAST;
if (currentLineAST == null)
yield break;
var editor = editorAST.Editor;
var editor = editorContext.Editor;
foreach (var declaration in currentLineAST.FindTypeDeclarations()) {
var rr = ParserService.Resolve(new ExpressionResult(declaration.Name), editor.Caret.Line, editor.Caret.Column, editor.FileName, editor.Document.Text);
int indexOfClassNameOnTheLine = editorContext.CurrentLine.Text.IndexOf(declaration.Name, declaration.StartLocation.Column/*, declaration.EndLocation.Column + 1 - declaration.StartLocation.Column*/);
if (indexOfClassNameOnTheLine == -1)
continue;
int declarationOffset = editorContext.CurrentLine.Offset + indexOfClassNameOnTheLine;
var rr = ParserService.Resolve(declarationOffset + 1, editor.Document, editor.FileName);
if (rr != null && rr.ResolvedType != null) {
var foundClass = rr.ResolvedType.GetUnderlyingClass();
if (foundClass != null) {

2
src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/GenerateMember.cs

@ -17,7 +17,7 @@ namespace SharpRefactoring.ContextActions @@ -17,7 +17,7 @@ namespace SharpRefactoring.ContextActions
/// </summary>
public class GenerateMemberProvider : IContextActionsProvider
{
public IEnumerable<IContextAction> GetAvailableActions(EditorASTProvider editorAST)
public IEnumerable<IContextAction> GetAvailableActions(EditorContext editorAST)
{
yield break;
}

4
src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/ImplementAbstractClass.cs

@ -21,11 +21,11 @@ namespace SharpRefactoring.ContextActions @@ -21,11 +21,11 @@ namespace SharpRefactoring.ContextActions
/// </summary>
public class ImplementAbstractClassProvider : IContextActionsProvider
{
public IEnumerable<IContextAction> GetAvailableActions(EditorASTProvider editorAST)
public IEnumerable<IContextAction> GetAvailableActions(EditorContext editorContext)
{
var ambience = AmbienceService.GetCurrentAmbience();
foreach (var targetClass in editorAST.GetClassesOnCurrentLine().Where(c => c.ClassType == ClassType.Class)) {
foreach (var targetClass in editorContext.GetClassDeclarationsOnCurrentLine().Where(c => c.ClassType == ClassType.Class)) {
foreach (var implementAction in RefactoringService.GetImplementAbstractClassActions(targetClass)) {
var implementActionCopy = implementAction;

4
src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/ImplementInterface.cs

@ -24,14 +24,14 @@ namespace SharpRefactoring.ContextActions @@ -24,14 +24,14 @@ namespace SharpRefactoring.ContextActions
/// </summary>
public class ImplementInterfaceProvider : IContextActionsProvider
{
public IEnumerable<IContextAction> GetAvailableActions(EditorASTProvider editorAST)
public IEnumerable<IContextAction> GetAvailableActions(EditorContext editorContext)
{
// Using CurrentLineAST is basically OK, but when the "class" keyword is on different line than class name,
// parsing only one line never tells us that we are looking at TypeDeclaration
// Alternative solution could be to try to resolve also IdentifierExpression to see if it is class declaration.
var ambience = AmbienceService.GetCurrentAmbience();
foreach (var targetClass in editorAST.GetClassesOnCurrentLine().
foreach (var targetClass in editorContext.GetClassDeclarationsOnCurrentLine().
Where(c => c.ClassType == ClassType.Class || c.ClassType == ClassType.Interface)) {
foreach (var implementAction in RefactoringService.GetImplementInterfaceActions(targetClass, false)) {

2
src/Main/Base/Project/ICSharpCode.SharpDevelop.csproj

@ -333,7 +333,7 @@ @@ -333,7 +333,7 @@
<SubType>Code</SubType>
</Compile>
<Compile Include="Src\Services\RefactoringService\ContextActions\ContextActionsService.cs" />
<Compile Include="Src\Services\RefactoringService\ContextActions\EditorASTProvider.cs" />
<Compile Include="Src\Services\RefactoringService\ContextActions\EditorContext.cs" />
<Compile Include="Src\Services\RefactoringService\ContextActions\IContextAction.cs" />
<Compile Include="Src\Services\RefactoringService\ContextActions\ContextActionCommand.cs" />
<Compile Include="Src\Services\RefactoringService\ContextActions\ContextActionsControl.xaml.cs">

4
src/Main/Base/Project/Src/Services/RefactoringService/ContextActions/ContextActionsService.cs

@ -40,10 +40,10 @@ namespace ICSharpCode.SharpDevelop.Refactoring @@ -40,10 +40,10 @@ namespace ICSharpCode.SharpDevelop.Refactoring
public IEnumerable<IContextAction> GetAvailableActions(ITextEditor editor)
{
yield break;
var editorAST = new EditorASTProvider(editor);
var editorContext = new EditorContext(editor);
// could run providers in parallel
foreach (var provider in this.providers) {
foreach (var action in provider.GetAvailableActions(editorAST)) {
foreach (var action in provider.GetAvailableActions(editorContext)) {
yield return action;
}
}

21
src/Main/Base/Project/Src/Services/RefactoringService/ContextActions/EditorASTProvider.cs → src/Main/Base/Project/Src/Services/RefactoringService/ContextActions/EditorContext.cs

@ -18,13 +18,13 @@ namespace ICSharpCode.SharpDevelop.Refactoring @@ -18,13 +18,13 @@ namespace ICSharpCode.SharpDevelop.Refactoring
/// Never keep long-lived references to this class
/// - the AST serves as one-time cache and does not get updated when editor text changes.
/// </summary>
public class EditorASTProvider
public class EditorContext
{
public ITextEditor Editor { get; private set; }
public SnippetParser snippetParser { get; private set; }
public NRefactoryResolver resolver { get; private set; }
public EditorASTProvider(ITextEditor editor)
public EditorContext(ITextEditor editor)
{
if (editor == null)
throw new ArgumentNullException("editor");
@ -49,21 +49,20 @@ namespace ICSharpCode.SharpDevelop.Refactoring @@ -49,21 +49,20 @@ namespace ICSharpCode.SharpDevelop.Refactoring
}
}
string currentLineText;
public string CurrentLineText
IDocumentLine currentLine;
public IDocumentLine CurrentLine
{
get
{
if (currentLineText != null)
return currentLineText;
if (currentLine != null)
return currentLine;
try
{
var currentLine = Editor.Document.GetLine(Editor.Caret.Line);
return (currentLineText = currentLine.Text);
return (currentLine = Editor.Document.GetLine(Editor.Caret.Line));
}
catch
{
return string.Empty;
return null;
}
}
}
@ -75,10 +74,10 @@ namespace ICSharpCode.SharpDevelop.Refactoring @@ -75,10 +74,10 @@ namespace ICSharpCode.SharpDevelop.Refactoring
{
if (currentLineAST != null)
return currentLineAST;
if (snippetParser == null)
if (this.snippetParser == null || this.CurrentLine == null)
return null;
try {
return (currentLineAST = snippetParser.Parse(this.CurrentLineText));
return (currentLineAST = snippetParser.Parse(this.CurrentLine.Text));
}
catch {
return null;

2
src/Main/Base/Project/Src/Services/RefactoringService/ContextActions/IContextActionsProvider.cs

@ -19,6 +19,6 @@ namespace ICSharpCode.SharpDevelop.Refactoring @@ -19,6 +19,6 @@ namespace ICSharpCode.SharpDevelop.Refactoring
/// <summary>
/// Gets actions available for current line of the editor.
/// </summary>
IEnumerable<IContextAction> GetAvailableActions(EditorASTProvider editorAST);
IEnumerable<IContextAction> GetAvailableActions(EditorContext editorAST);
}
}

Loading…
Cancel
Save