Browse Source

Code actions now have a start/end location instead of an ast node.

Not all actions have an ast node - but a start/end location. And the
location is what I need atm in the editor.
pull/32/merge
Mike Krüger 13 years ago
parent
commit
650d2fe644
  1. 6
      ICSharpCode.NRefactory.CSharp/Formatter/AstFormattingVisitor.cs
  2. 39
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeAction.cs
  3. 4
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ExtractMethod/ExtractMethodAction.cs
  4. 2
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/GatherVisitorBase.cs
  5. 17
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/UnreachableCodeIssue.cs

6
ICSharpCode.NRefactory.CSharp/Formatter/AstFormattingVisitor.cs

@ -625,10 +625,10 @@ namespace ICSharpCode.NRefactory.CSharp @@ -625,10 +625,10 @@ namespace ICSharpCode.NRefactory.CSharp
if (!propertyDeclaration.Setter.IsNull) {
if (!oneLine) {
if (!IsLineIsEmptyUpToEol(propertyDeclaration.Setter.StartLocation)) {
int offset = this.document.GetOffset(propertyDeclaration.Setter.StartLocation);
int offset = document.GetOffset(propertyDeclaration.Setter.StartLocation);
int start = SearchWhitespaceStart(offset);
string indentString = this.curIndent.IndentString;
AddChange(start, offset - start, this.options.EolMarker + indentString);
string indentString = curIndent.IndentString;
AddChange(start, offset - start, options.EolMarker + indentString);
} else {
FixIndentation(propertyDeclaration.Setter.StartLocation);
}

39
ICSharpCode.NRefactory.CSharp/Refactoring/CodeAction.cs

@ -49,10 +49,17 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -49,10 +49,17 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
}
/// <summary>
/// Gets the ast node the action acts upon.
/// Note: τhis value may be null if the action isn't specific to a single node.
/// Gets the action start location.
/// </summary>
public AstNode AstNode {
public TextLocation Start {
get;
private set;
}
/// <summary>
/// Gets the action end location.
/// </summary>
public TextLocation End {
get;
private set;
}
@ -67,6 +74,29 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -67,6 +74,29 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
/// The code transformation.
/// </param>
public CodeAction (string description, Action<Script> action, AstNode astNode)
{
if (action == null)
throw new ArgumentNullException ("action");
if (description == null)
throw new ArgumentNullException ("description");
if (astNode == null)
throw new ArgumentNullException("astNode");
Description = description;
Run = action;
Start = astNode.StartLocation;
End = astNode.EndLocation;
}
/// <summary>
/// Initializes a new instance of the <see cref="ICSharpCode.NRefactory.CSharp.Refactoring.CodeAction"/> class.
/// </summary>
/// <param name='description'>
/// The description.
/// </param>
/// <param name='action'>
/// The code transformation.
/// </param>
public CodeAction (string description, Action<Script> action, TextLocation start, TextLocation end)
{
if (action == null)
throw new ArgumentNullException ("action");
@ -74,7 +104,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -74,7 +104,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
throw new ArgumentNullException ("description");
Description = description;
Run = action;
AstNode = astNode;
this.Start = start;
this.End = end;
}
}
}

4
ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ExtractMethod/ExtractMethodAction.cs

@ -112,7 +112,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring.ExtractMethod @@ -112,7 +112,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring.ExtractMethod
} else {
task.ContinueWith (replaceStatements, TaskScheduler.FromCurrentSynchronizationContext ());
}
}, null);
}, expression);
}
CodeAction CreateFromStatements(RefactoringContext context, List<AstNode> statements)
@ -225,7 +225,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring.ExtractMethod @@ -225,7 +225,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring.ExtractMethod
} else {
task.ContinueWith (replaceStatements, TaskScheduler.FromCurrentSynchronizationContext ());
}
}, null);
}, statements.First ().StartLocation, statements.Last ().EndLocation);
}
}
}

2
ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/GatherVisitorBase.cs

@ -75,7 +75,7 @@ namespace ICSharpCode.NRefactory.CSharp @@ -75,7 +75,7 @@ namespace ICSharpCode.NRefactory.CSharp
protected void AddIssue(TextLocation start, TextLocation end, string title, System.Action<Script> fix = null)
{
FoundIssues.Add(new CodeIssue(title, start, end, fix != null ? new CodeAction(title, fix, null) : null));
FoundIssues.Add(new CodeIssue(title, start, end, fix != null ? new CodeAction(title, fix, start, end) : null));
}
protected void AddIssue(AstNode node, string title, CodeAction fix)

17
ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/UnreachableCodeIssue.cs

@ -27,6 +27,7 @@ @@ -27,6 +27,7 @@
using System;
using System.Collections.Generic;
using ICSharpCode.NRefactory.CSharp.Analysis;
using System.Linq;
namespace ICSharpCode.NRefactory.CSharp.Refactoring
{
@ -158,15 +159,15 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -158,15 +159,15 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
var startOffset = script.GetCurrentOffset (prevEnd);
var endOffset = script.GetCurrentOffset (end);
script.RemoveText (startOffset, endOffset - startOffset);
}, null);
var commentAction = new CodeAction (visitor.ctx.TranslateString ("Comment unreachable code"),
}, collectedStatements.First().StartLocation, collectedStatements.Last().EndLocation);
var commentAction = new CodeAction(visitor.ctx.TranslateString("Comment unreachable code"),
script =>
{
var startOffset = script.GetCurrentOffset (prevEnd);
script.InsertText (startOffset, Environment.NewLine + "/*");
var endOffset = script.GetCurrentOffset (end);
script.InsertText (endOffset, Environment.NewLine + "*/");
}, null);
{
var startOffset = script.GetCurrentOffset(prevEnd);
script.InsertText(startOffset, Environment.NewLine + "/*");
var endOffset = script.GetCurrentOffset(end);
script.InsertText(endOffset, Environment.NewLine + "*/");
}, collectedStatements.First().StartLocation, collectedStatements.Last().EndLocation);
var actions = new [] { removeAction, commentAction };
visitor.AddIssue (start, end, visitor.ctx.TranslateString ("Code is unreachable"), actions);
return true;

Loading…
Cancel
Save