rubberduck-vba/Rubberduck

Refactor Extract Method Will Not Extract Apparently Complete Code Block If Trailing Line End Is Included in Selection

Closed this issue · 2 comments

Rubberduck version information
Version 2.5.9.6304
OS: Microsoft Windows NT 10.0.17763.0, x64
Host Product: Microsoft Office 2016 x64
Host Version: 16.0.5095.1000
Host Executable: WINWORD.EXE

Description
I am attempting to Refactor > Extract Method with the below apparently-complete code block seclected (including, as is fairly common in selection code blocks, the trailing line end of the last line of the block--i.e., whole lines are selected) and Rubberduck in the "Ready" state, but get the following error message:

Refactoring failed.

The current selection is not valid:
Extract method must contain selection that represents a set of complete statements. It cannot extract a part of a statement.

To Reproduce
The following code is in a sub procedure (each line is at least one tab indented):

Dim oStyles As Styles
Set oStyles = oDoc.Styles

With oStyles
    With .Item("TOC 1").ParagraphFormat
        .SpaceBefore = 12
        .SpaceAfter = 0
        .LeftIndent = 0
        .FirstLineIndent = 0
        .TabStops.ClearAll
    End With
    
    With .Item("TOC 2").ParagraphFormat
        .SpaceBefore = 6
        .SpaceAfter = 0
        .LeftIndent = InchesToPoints(0.5)
        .FirstLineIndent = InchesToPoints(-0.5)
        
        With .TabStops
            .ClearAll
            .Add Position:=InchesToPoints(0.5), _
                 Alignment:=wdAlignTabLeft, _
                 Leader:=wdTabLeaderSpaces
            .Add Position:=InchesToPoints(6.5), _
                 Alignment:=wdAlignTabRight, _
                 Leader:=wdTabLeaderDots
        End With
    End With
        
    With .Item("TOC 3").ParagraphFormat
        .SpaceBefore = 0
        .SpaceAfter = 0
        .LeftIndent = InchesToPoints(1)
        .FirstLineIndent = InchesToPoints(-0.5)
        
        With .TabStops
            .ClearAll
            .Add Position:=InchesToPoints(1), _
                 Alignment:=wdAlignTabLeft, _
                 Leader:=wdTabLeaderSpaces
            .Add Position:=InchesToPoints(6.5), _
                 Alignment:=wdAlignTabRight, _
                 Leader:=wdTabLeaderDots
        End With
    End With
    
    With .Item("TOC 4").ParagraphFormat
        .SpaceBefore = 0
        .SpaceAfter = 0
        .LeftIndent = InchesToPoints(1.5)
        .FirstLineIndent = InchesToPoints(-0.5)
        
        With .TabStops
            .ClearAll
            .Add Position:=InchesToPoints(1.5), _
                 Alignment:=wdAlignTabLeft, _
                 Leader:=wdTabLeaderSpaces
            .Add Position:=InchesToPoints(1.75), _
                 Alignment:=wdAlignTabLeft, _
                 Leader:=wdTabLeaderSpaces
            .Add Position:=InchesToPoints(6.5), _
                 Alignment:=wdAlignTabRight, _
                 Leader:=wdTabLeaderDots
        End With
    End With
End With

This code executes without issue in its existing procedure, the module compiles without error, and Rubberduck is in the "Ready" state after the last edits. The code block appears to be a complete block that should be able to stand alone in a separate method.

Then :

  1. Select the above code
  2. From the context menu or the Rubberduck tab, Refactor > Extract Method
  3. See error message

Expected behavior
The Extract Method dialog box should have appeared to let the new method be named and the new method's return value, of any, to be selected, and then, on "OK", the extraction and new method creation should have occurred.

Screenshots
N/A

Logfile
Here is the logfile for this event:

2023-11-17 14:38:04.1823;TRACE-2.5.9.6304;Rubberduck.Common.LogLevelHelper;
Rubberduck version 2.5.9.6304 loading:
Operating System: Windows 10 Enterprise 1809 x64 (Microsoft Windows NT 10.0.17763.0)
Host Product: Microsoft Office 2016 x64
Host Version: 16.0.5095.1000
Host Executable: WINWORD.EXE;
2023-11-17 14:38:10.9180;DEBUG-2.5.9.6304;Rubberduck.UI.Command.MenuItems.ParentMenus.ParentMenuItemBase;(57140728) Executing click handler for menu item 'Extract &Method', hash code 64499009;
2023-11-17 14:38:12.7320;TRACE-2.5.9.6304;Rubberduck.Common.Hotkeys.Hotkey;Hotkey for the associated command Rubberduck.UI.Command.ComCommands.IndentCurrentModuleCommand was registered with id 49350;
2023-11-17 14:38:12.7320;TRACE-2.5.9.6304;Rubberduck.Common.Hotkeys.Hotkey;Hotkey for the associated command Rubberduck.UI.Command.Refactorings.CodePaneRefactorRenameCommand was registered with id 49282;
2023-11-17 14:38:12.7320;TRACE-2.5.9.6304;Rubberduck.Common.Hotkeys.Hotkey;Hotkey for the associated command Rubberduck.UI.Command.Refactorings.RefactorEncapsulateFieldCommand was registered with id 49331;
2023-11-17 14:38:12.7320;TRACE-2.5.9.6304;Rubberduck.Common.Hotkeys.Hotkey;Hotkey for the associated command Rubberduck.UI.Command.Refactorings.RefactorExtractMethodCommand was registered with id 49268;
2023-11-17 14:38:12.7320;TRACE-2.5.9.6304;Rubberduck.Common.Hotkeys.Hotkey;Hotkey for the associated command Rubberduck.UI.Command.Refactorings.RefactorMoveCloserToUsageCommand was registered with id 49337;
2023-11-17 14:38:12.7320;TRACE-2.5.9.6304;Rubberduck.Common.Hotkeys.Hotkey;Hotkey for the associated command Rubberduck.UI.Command.ComCommands.CodeExplorerCommand was registered with id 49341;
2023-11-17 14:38:12.7320;TRACE-2.5.9.6304;Rubberduck.Common.Hotkeys.Hotkey;Hotkey for the associated command Rubberduck.UI.Command.ComCommands.ExportAllCommand was registered with id 49263;
2023-11-17 14:38:12.7320;TRACE-2.5.9.6304;Rubberduck.Common.Hotkeys.Hotkey;Hotkey for the associated command Rubberduck.UI.Command.ComCommands.FindSymbolCommand was registered with id 49325;
2023-11-17 14:38:12.7320;TRACE-2.5.9.6304;Rubberduck.Common.Hotkeys.Hotkey;Hotkey for the associated command Rubberduck.UI.Command.ComCommands.IndentCurrentProcedureCommand was registered with id 49392;
2023-11-17 14:38:12.7320;TRACE-2.5.9.6304;Rubberduck.Common.Hotkeys.Hotkey;Hotkey for the associated command Rubberduck.UI.Command.ComCommands.InspectionResultsCommand was registered with id 49400;
2023-11-17 14:38:12.7320;TRACE-2.5.9.6304;Rubberduck.Common.Hotkeys.Hotkey;Hotkey for the associated command Rubberduck.UI.Command.ComCommands.ReparseCommand was registered with id 49317;
2023-11-17 14:38:12.7320;TRACE-2.5.9.6304;Rubberduck.Common.Hotkeys.Hotkey;Hotkey for the associated command Rubberduck.UI.Command.ComCommands.TestExplorerCommand was registered with id 49375;
2023-11-17 14:38:12.7320;TRACE-2.5.9.6304;Rubberduck.Common.Hotkeys.Hotkey;Hotkey for the associated command Rubberduck.UI.Command.ComCommands.PeekDefinitionCommand was registered with id 49390;
2023-11-17 14:38:12.7629;TRACE-2.5.9.6304;Rubberduck.UI.Command.MenuItems.CommandBars.AppCommandBarBase;CancellationTokenSource was already disposed for OnSelectionChange.;

Additional Information
However, with the above-code block, if the selection does not include the trailing line end of the last line (i.e., whole lines in the block except for the last line are selected), the Extract Method dialog box with proper contents appears.

Hi @NDeLancie, thanks for the clear bug report. It made it pretty easy to track down an 'off by one' issue. I've just submitted a pull request to fix it.

Thanks so much, Tommy! The funny thing was I thought there might be some code error on my part or some code interpretation error on RD's part (could it have been that the code was indented, could it have been that the With blocks were a problem, etc.) and had written a long but different orientation bug report when, quite by accident, in selecting a code block to get the error message again so I could copy its text for the bug report I was "off by one" the other was in my selection (i.e., no end line), and got the dialog box, not the error message, and then realized what the problem was. Glad it's solved and it was so easy.