Rubberduck icon indicating copy to clipboard operation
Rubberduck copied to clipboard

Smart Indent runs even on modules annotated with @NoIndent

Open pflugs30 opened this issue 1 year ago • 7 comments

Rubberduck version information Version 2.5.2.6059 OS: Microsoft Windows NT 10.0.19044.0, x64 Host Product: Microsoft Office x64 Host Version: 16.0.15330.20230 Host Executable: MSACCESS.EXE

Description I added the @NoIndent attribute to the top of a module because it has hidden VB attributes (ref #5309). However, I found that Rubberduck would still indent the module if I ran a right-click --> Rubberduck --> Indent --> Current Project.

To Reproduce Steps to reproduce the behavior:

  1. Add a new module.
  2. Add some code that will be auto-indented.
  3. Add the @NoIndent attribute.
  4. Right-click --> Rubberduck --> Indent --> Current Project (or Current Module).
  5. See that the code has now been indented.

Expected behavior No matter how the Smart Indent is invoked, any code module annotated with @NoIndent should not be indented.

Screenshots Code Before Indent:

'@NoIndent
Public Sub MyTest()
Debug.Print "Hello, there!"
End Sub

Code After Indent:

'@NoIndent
Public Sub MyTest()
    Debug.Print "Hello, there!"
End Sub

Logfile I changed my log settings to TRACE prior to running the Indent, but I didn't see any logs after running the Smart Indent.

pflugs30 avatar Jul 12 '22 17:07 pflugs30

Did you reparse before requesting the indent?

Vogel612 avatar Jul 12 '22 18:07 Vogel612

Good question. If by "reparse", you mean "clicked Rubberduck --> Refresh", then. yes, I did. I pasted in the example text, saved the new Module (giving it a name), refreshed Rubberduck, and ran the indent. I also tried a few other things, including compiling the project, changing the location of the attribute (meaning, at the very top of the file), etc.

pflugs30 avatar Jul 12 '22 20:07 pflugs30

Does the same thing happen if you change that to msgbox not debug.print . And also try adding option explicit below the annotation but above the sub. Or even a few linebreaks between them. The annotation must be parsed at module level, not associated with a procedure. Sorry don't have a test machine to hand

Greedquest avatar Jul 12 '22 20:07 Greedquest

Yes, it still indents with this code:

'@NoIndent
Option Compare Database
Option Explicit

Public Sub MyTest()
MsgBox "Hello, there!"
End Sub

Prior to running indent, I saved the module, compiled the project, and refreshed Rubberduck.

pflugs30 avatar Jul 12 '22 21:07 pflugs30

Probably not a satisfactory solution, but as a work-around the hidden attributes without a corresponding annotation should trigger MissingMemberAnnotation inspection results for the member-level attributes, and MissingModuleAnnotation for the module-level ones: these inspection results provide quickfixes to automatically add the appropriate annotations, and once the annotations are present and parsed Rubberduck would be able to issue MissingAttribute inspection results for any attribute that might have been lost (with a quickfix to restore them), regardless of whether it was the Indenter or accidentally through some manual intervention.

retailcoder avatar Jul 12 '22 22:07 retailcoder

(I can repro this) Excel Windows 11 latest rd pre-release.

Also another way to manage this is to remember the (configurable) shortcut ctrl+m to indent the current module only, reducing the widespread impact of a complete project indent. That way you should be better able to anticipate when you are going to be wiping out the annotations and need to apply the fixes @retailcoder outlines. Source control also helps spot when a module's annotations change unexpectedly. This should make you less worried about wiping them out and simply forgetting, breaking your code in hard to spot ways. This is what I've been doing in my projects, RD is really fighting an uphill battle against the VBE!

Sorry not sure how much that helps.

Greedquest avatar Jul 12 '22 22:07 Greedquest

@retailcoder thanks for pointing that out. I will add those attributes to all the modules with hidden VB Attributes.

@Greedquest thanks for confirming you can reproduce it. I figured this issue would be a bug the team would worth mentioning. I appreciate the two work arounds, and I will implement them both.

pflugs30 avatar Jul 12 '22 23:07 pflugs30