Browse Source

Added "RemoveRegion" context action.

newNRvisualizers
mike 14 years ago
parent
commit
7451607834
  1. 1
      ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj
  2. 99
      ICSharpCode.NRefactory.CSharp/Refactoring/ContextAction/RemoveRegion.cs
  3. 14
      ICSharpCode.NRefactory.CSharp/Refactoring/RefactoringContext.cs
  4. 40
      ICSharpCode.NRefactory.CSharp/Refactoring/Script.cs
  5. 91
      ICSharpCode.NRefactory.Tests/CSharp/ContextAction/RemoveRegionTests.cs
  6. 10
      ICSharpCode.NRefactory.Tests/CSharp/ContextAction/TestRefactoringContext.cs
  7. 1
      ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj

1
ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj

@ -327,6 +327,7 @@ @@ -327,6 +327,7 @@
<Compile Include="Parser\mcs\settings.cs" />
<Compile Include="Parser\mcs\SourceMethodBuilder.cs" />
<Compile Include="Ast\TokenRole.cs" />
<Compile Include="Refactoring\ContextAction\RemoveRegion.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ICSharpCode.NRefactory\ICSharpCode.NRefactory.csproj">

99
ICSharpCode.NRefactory.CSharp/Refactoring/ContextAction/RemoveRegion.cs

@ -0,0 +1,99 @@ @@ -0,0 +1,99 @@
//
// RemoveRegion.cs
//
// Author:
// Mike Krüger <mkrueger@xamarin.com>
//
// Copyright (c) 2012 Xamarin Inc. (http://xamarin.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
namespace ICSharpCode.NRefactory.CSharp.Refactoring
{
public class RemoveRegion : IContextAction
{
public bool IsValid (RefactoringContext context)
{
return GetDirective (context) != null;
}
public void Run (RefactoringContext context)
{
var directive = GetDirective (context);
var visitor = new DirectiveVisitor (directive);
context.Unit.AcceptVisitor (visitor);
Console.WriteLine ("directive:" + directive + "/" + visitor.Endregion);
if (visitor.Endregion == null)
return;
using (var script = context.StartScript ()) {
script.Remove (directive);
script.Remove (visitor.Endregion);
}
}
class DirectiveVisitor : DepthFirstAstVisitor
{
readonly PreProcessorDirective startDirective;
bool searchDirectives = false;
int depth;
public PreProcessorDirective Endregion {
get;
set;
}
public DirectiveVisitor (PreProcessorDirective startDirective)
{
this.startDirective = startDirective;
}
public override void VisitPreProcessorDirective (PreProcessorDirective preProcessorDirective)
{
if (searchDirectives) {
if (preProcessorDirective.Type == PreProcessorDirectiveType.Region)
depth++;
if (preProcessorDirective.Type == PreProcessorDirectiveType.Endregion) {
depth--;
if (depth == 0) {
Endregion = preProcessorDirective;
searchDirectives = false;
}
}
}
if (preProcessorDirective == startDirective) {
searchDirectives = true;
depth = 1;
}
base.VisitPreProcessorDirective (preProcessorDirective);
}
}
static PreProcessorDirective GetDirective (RefactoringContext context)
{
var directive = context.GetNode<PreProcessorDirective> ();
if (directive == null || directive.Type != PreProcessorDirectiveType.Region)
return null;
return directive;
}
}
}

14
ICSharpCode.NRefactory.CSharp/Refactoring/RefactoringContext.cs

@ -29,6 +29,7 @@ using ICSharpCode.NRefactory.CSharp.Resolver; @@ -29,6 +29,7 @@ using ICSharpCode.NRefactory.CSharp.Resolver;
using ICSharpCode.NRefactory.Semantics;
using ICSharpCode.NRefactory.TypeSystem;
using ICSharpCode.NRefactory.TypeSystem.Implementation;
using ICSharpCode.NRefactory.Editor;
namespace ICSharpCode.NRefactory.CSharp.Refactoring
{
@ -93,18 +94,31 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -93,18 +94,31 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
#region Text stuff
public abstract string EolMarker { get; }
public abstract bool IsSomethingSelected { get; }
public abstract string SelectedText { get; }
public abstract int SelectionStart { get; }
public abstract int SelectionEnd { get; }
public abstract int SelectionLength { get; }
public abstract int GetOffset (TextLocation location);
public abstract IDocumentLine GetLineByOffset (int offset);
public int GetOffset (int line, int col)
{
return GetOffset (new TextLocation (line, col));
}
public abstract TextLocation GetLocation (int offset);
public abstract string GetText (int offset, int length);
public abstract string GetText (ISegment segment);
#endregion
#region Resolving

40
ICSharpCode.NRefactory.CSharp/Refactoring/Script.cs

@ -90,10 +90,27 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -90,10 +90,27 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
Queue (Context.CreateLinkAction (nodes));
}
public void Remove (AstNode node)
public void Remove (AstNode node, bool removeEmptyLine = true)
{
var startOffset = Context.GetOffset (node.StartLocation);
var endOffset = Context.GetOffset (node.EndLocation);
var startLine = Context.GetLineByOffset (startOffset);
var endLine = Context.GetLineByOffset (endOffset);
if (startLine != null && endLine != null) {
bool removeStart = string.IsNullOrWhiteSpace (Context.GetText (startLine.Offset, startOffset - startLine.Offset));
if (removeStart)
startOffset = startLine.Offset;
bool removeEnd = string.IsNullOrWhiteSpace (Context.GetText (endOffset, endLine.EndOffset - endOffset));
if (removeEnd)
endOffset = endLine.EndOffset;
// Remove delimiter if the whole line get's removed.
if (removeStart && removeEnd)
endOffset += endLine.DelimiterLength;
}
Remove (startOffset, endOffset - startOffset);
}
@ -129,7 +146,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -129,7 +146,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
Queue (Context.CreateNodeSelectionAction (node));
}
public enum InsertPosition {
public enum InsertPosition
{
Start,
Before,
After,
@ -143,10 +161,10 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -143,10 +161,10 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
var node = Context.Unit.GetNodeAt (Context.GetLocation (offset));
int level = 0;
while (node != null) {
if (node is BlockStatement || node is TypeDeclaration || node is NamespaceDeclaration)
level++;
node = node.Parent;
}
if (node is BlockStatement || node is TypeDeclaration || node is NamespaceDeclaration)
level++;
node = node.Parent;
}
return level;
}
@ -155,24 +173,24 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -155,24 +173,24 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
readonly NodeOutput result;
readonly StringWriter stringWriter;
public SegmentTrackingOutputFormatter(NodeOutput result, StringWriter stringWriter)
public SegmentTrackingOutputFormatter (NodeOutput result, StringWriter stringWriter)
: base(stringWriter)
{
this.result = result;
this.stringWriter = stringWriter;
}
public override void StartNode(AstNode node)
public override void StartNode (AstNode node)
{
base.StartNode(node);
base.StartNode (node);
result.NodeSegments [node] = new NodeOutput.Segment (stringWriter.GetStringBuilder ().Length);
}
public override void EndNode(AstNode node)
public override void EndNode (AstNode node)
{
var nodeSegment = result.NodeSegments [node];
nodeSegment.Length = stringWriter.GetStringBuilder ().Length - nodeSegment.Offset;
base.EndNode(node);
base.EndNode (node);
}
}

91
ICSharpCode.NRefactory.Tests/CSharp/ContextAction/RemoveRegionTests.cs

@ -0,0 +1,91 @@ @@ -0,0 +1,91 @@
//
// RemoveRegionTests.cs
//
// Author:
// Mike Krüger <mkrueger@xamarin.com>
//
// Copyright (c) 2012 Xamarin Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using NUnit.Framework;
using ICSharpCode.NRefactory.CSharp.Refactoring;
namespace ICSharpCode.NRefactory.CSharp.ContextActions
{
[TestFixture]
public class RemoveRegionTests : ContextActionTestBase
{
[Test()]
public void TestSimpleRegion ()
{
string result = RunContextAction (
new RemoveRegion (),
"class TestClass" + Environment.NewLine +
"{" + Environment.NewLine +
" #region$ Foo" + Environment.NewLine +
" void Test ()" + Environment.NewLine +
" {" + Environment.NewLine +
" }" + Environment.NewLine +
" #endregion" + Environment.NewLine +
"}"
);
Assert.AreEqual (
"class TestClass" + Environment.NewLine +
"{" + Environment.NewLine +
" void Test ()" + Environment.NewLine +
" {" + Environment.NewLine +
" }" + Environment.NewLine +
"}", result);
}
[Test()]
public void TestNestedRegion ()
{
string result = RunContextAction (
new RemoveRegion (),
"class TestClass" + Environment.NewLine +
"{" + Environment.NewLine +
" #region$ Foo" + Environment.NewLine +
" void Test ()" + Environment.NewLine +
" {" + Environment.NewLine +
" #region Nested" + Environment.NewLine +
" Foo ();" + Environment.NewLine +
" #endregion" + Environment.NewLine +
" }" + Environment.NewLine +
" #endregion" + Environment.NewLine +
"}"
);
Assert.AreEqual (
"class TestClass" + Environment.NewLine +
"{" + Environment.NewLine +
" void Test ()" + Environment.NewLine +
" {" + Environment.NewLine +
" #region Nested" + Environment.NewLine +
" Foo ();" + Environment.NewLine +
" #endregion" + Environment.NewLine +
" }" + Environment.NewLine +
"}", result);
}
}
}

10
ICSharpCode.NRefactory.Tests/CSharp/ContextAction/TestRefactoringContext.cs

@ -123,6 +123,16 @@ namespace ICSharpCode.NRefactory.CSharp.ContextActions @@ -123,6 +123,16 @@ namespace ICSharpCode.NRefactory.CSharp.ContextActions
{
return doc.GetText (offset, length);
}
public override string GetText (ISegment segment)
{
return doc.GetText (segment);
}
public override IDocumentLine GetLineByOffset (int offset)
{
return doc.GetLineByOffset (offset);
}
#endregion
#region Resolving

1
ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj

@ -234,6 +234,7 @@ @@ -234,6 +234,7 @@
<Compile Include="CSharp\ContextAction\CheckIfParameterIsNullTests.cs" />
<Compile Include="CSharp\ContextAction\ConvertForeachToForTests.cs" />
<Compile Include="CSharp\ContextAction\AddAnotherAccessorTests.cs" />
<Compile Include="CSharp\ContextAction\RemoveRegionTests.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Mono.Cecil\Mono.Cecil.csproj">

Loading…
Cancel
Save