Skip to content

Commit

Permalink
Preserve indentation of expression following inserted declaration (an…
Browse files Browse the repository at this point in the history
…d associated tests)
  • Loading branch information
tommy9 committed Apr 28, 2018
1 parent 1b1fe5c commit 4edaa1e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
Expand Up @@ -168,9 +168,12 @@ private void InsertNewDeclaration()
}

var insertionIndex = (expression as ParserRuleContext).Start.TokenIndex;
var firstReferenceLine = _vbe.ActiveCodePane.CodeModule.GetLines((expression as ParserRuleContext).Start.Line, 1);
var indentLength = firstReferenceLine.Length - firstReferenceLine.TrimStart().Length;
var padding = new string(' ', indentLength);

var rewriter = _state.GetRewriter(firstReference.QualifiedModuleName);
rewriter.InsertBefore(insertionIndex, newVariable);
rewriter.InsertBefore(insertionIndex, newVariable + padding);

_rewriters.Add(rewriter);
}
Expand Down
Expand Up @@ -25,7 +25,7 @@ public void MoveFieldCloserToUsage_QuickFixWorks()
const string expectedCode =
@"Public Sub Foo()
Dim bar As String
bar = ""test""
bar = ""test""
End Sub";

var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out var component);
Expand Down
32 changes: 16 additions & 16 deletions RubberduckTests/Refactoring/MoveCloserToUsageTests.cs
Expand Up @@ -32,7 +32,7 @@ public void MoveCloserToUsageRefactoring_Field()
const string expectedCode =
@"Private Sub Foo()
Dim bar As Boolean
bar = True
bar = True
End Sub";

var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out var component, selection);
Expand Down Expand Up @@ -69,7 +69,7 @@ As _
const string expectedCode =
@"Private Sub Foo()
Dim bar As Boolean
bar = True
bar = True
End Sub";

var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out var component, selection);
Expand Down Expand Up @@ -155,7 +155,7 @@ public void MoveCloserToUsageRefactoring_Variable()
@"Private Sub Foo()
Dim bat As Integer
Dim bar As Boolean
bar = True
bar = True
End Sub";

var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out var component, selection);
Expand Down Expand Up @@ -194,7 +194,7 @@ As _
@"Private Sub Foo()
Dim bat As Integer
Dim bar As Boolean
bar = True
bar = True
End Sub";

var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out var component, selection);
Expand Down Expand Up @@ -234,7 +234,7 @@ public void MoveCloserToUsageRefactoring_MultipleFields_MoveSecond()
Private Sub Foo()
Dim bat As Boolean
bat = True
bat = True
End Sub";

var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out var component, selection);
Expand Down Expand Up @@ -274,7 +274,7 @@ public void MoveCloserToUsageRefactoring_MultipleFieldsOneStatement_MoveFirst()
Private Sub Foo()
Dim bar As Integer
bar = 3
bar = 3
End Sub";

var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out var component, selection);
Expand Down Expand Up @@ -314,7 +314,7 @@ public void MoveCloserToUsageRefactoring_MultipleFieldsOneStatement_MoveSecond()
Private Sub Foo()
Dim bat As Boolean
bat = True
bat = True
End Sub";

var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out var component, selection);
Expand Down Expand Up @@ -354,7 +354,7 @@ public void MoveCloserToUsageRefactoring_MultipleFieldsOneStatement_MoveLast()
Private Sub Foo()
Dim bay As Date
bay = #1/13/2004#
bay = #1/13/2004#
End Sub";

var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out var component, selection);
Expand Down Expand Up @@ -396,7 +396,7 @@ public void MoveCloserToUsageRefactoring_MultipleVariablesOneStatement_MoveFirst
bat = True
Dim bar As Integer
bar = 3
bar = 3
End Sub";

var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out var component, selection);
Expand Down Expand Up @@ -438,7 +438,7 @@ public void MoveCloserToUsageRefactoring_MultipleVariablesOneStatement_MoveSecon
bar = 1
Dim bat As Boolean
bat = True
bat = True
End Sub";

var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out var component, selection);
Expand Down Expand Up @@ -480,7 +480,7 @@ public void MoveCloserToUsageRefactoring_MultipleVariablesOneStatement_MoveLast(
bar = 4
Dim bay As Date
bay = #1/13/2004#
bay = #1/13/2004#
End Sub";

var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out var component, selection);
Expand Down Expand Up @@ -583,7 +583,7 @@ public void MoveCloserToUsageRefactoring_ReferenceIsNotBeginningOfStatement_Assi
const string expectedCode =
@"Private Sub Foo(ByRef bat As Boolean)
Dim bar As Boolean
bat = bar
bat = bar
End Sub";
var selection = new Selection(1, 1);

Expand Down Expand Up @@ -618,7 +618,7 @@ Sub Baz(ByVal bat As Boolean)
const string expectedCode =
@"Private Sub Foo()
Dim bar As Boolean
Baz bar
Baz bar
End Sub
Sub Baz(ByVal bat As Boolean)
End Sub";
Expand Down Expand Up @@ -657,7 +657,7 @@ Sub Baz(ByVal bat As Boolean, ByVal bas As Boolean, ByVal bac As Boolean)
const string expectedCode =
@"Private Sub Foo()
Dim bar As Boolean
Baz True, _
Baz True, _
True, _
bar
End Sub
Expand Down Expand Up @@ -735,7 +735,7 @@ Debug.Print someParam
@"
Public Sub Test()
Dim foo As Long
SomeSub someParam:=foo
SomeSub someParam:=foo
End Sub
Public Sub SomeSub(ByVal someParam As Long)
Expand Down Expand Up @@ -906,7 +906,7 @@ End Property
Debug.Print ""Some statements between""
Debug.Print ""Declaration and first usage!""
Dim foo As Class1
Set foo = new Class1
Set foo = new Class1
foo.Name = ""FooName""
foo.OtherProperty = 1626
End Sub";
Expand Down

0 comments on commit 4edaa1e

Please sign in to comment.