Codemaid does not organize all items into regions correctly
Environment
- Visual Studio version: Microsoft Visual Studio Professional 2019 (16.2.3)
- CodeMaid version: 11.0.183
- Code language: C#
Description
When I attempt to clean up the active document, it will pick up some items and put them into the correct region, but there are always a few that do not get recognized and are organized alphabetically at the end without a region. This has happened with multiple files and on multiple computers.
Extra information: I am using ghostdoc for documentation comments, so the documentation contains some xml.
Below are some code snippits (examples):
before reorganization
using log4net;
using MoreLinq;
using Reader.API;
using Reader.Config;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
namespace Reader.Study
{
public class CommandProcessor
{
/// <summary>
/// The log
/// </summary>
private static readonly ILog Log = LogManager.GetLogger(typeof(CommandProcessor));
/// <summary>
/// Gets or sets the study API.
/// </summary>
/// <value>
/// The study API.
/// </value>
private StudyApi _studyApi { get; set; }
private SiteConfig _siteConfig;
private HttpClient _httpClient;
/// <summary>
/// Gets or sets the site configuration.
/// </summary>
/// <value>
/// The site configuration.
/// </value>
public SiteConfig SiteConfig
{
get
{
return _siteConfig;
}
set
{
_siteConfig = value;
if (_siteConfig != null && _httpClient != null)
{
_studyApi = new StudyApi(_siteConfig, _httpClient);
}
}
}
/// <summary>
/// Gets or sets the client.
/// </summary>
/// <value>
/// The client.
/// </value>
public HttpClient HttpClient
{
get
{
return _httpClient;
}
set
{
_httpClient = value;
if (_siteConfig != null && _httpClient != null)
{
_studyApi = new StudyApi(_siteConfig, _httpClient);
}
}
}
/// <summary>
/// Gets or sets a value indicating whether the command list should remove duplicate commands.
/// </summary>
/// <value>
/// <c>true</c> if we do not want duplicate commands; otherwise, <c>false</c>.
/// </value>
public bool Dedupe { get; private set; } = true;
/// <summary>
/// Gets or sets a value indicating whether we continue processing the
/// command queue when a command fails.
/// </summary>
/// <value>
/// <c>true</c> if [continue on fail]; otherwise, <c>false</c>.
/// </value>
public bool ContinueOnFail { get; private set; } = true;
/// <summary>
/// Gets or sets a value indicating whether the failure will be logged
/// or an exception will be thrown.
/// </summary>
/// <value>
/// <c>true</c> if an error should be thrown; otherwise, <c>false</c>.
/// </value>
public bool ErrorOnFail { get; private set; } = false;
/// <summary>
/// Gets the command queue.
/// </summary>
/// <value>
/// The commands.
/// </value>
public List<Command> Commands { get; private set; } = new List<Command>();
/// <summary>
/// Gets the failed commands.
/// </summary>
/// <value>
/// The failed commands.
/// </value>
public List<Command> FailedCommands
{
get
{
// Create an empty list of commands
List<Command> tempFailed = new List<Command>();
// If there are commands
if (Commands.Count > 0)
{
/*
* Simulate the following:
* tempFailed = Commands.Where(x => x.Result.Attempts > 0 && !x.Result.Success)
*/
Commands.ForEach(x =>
{
// If the command has been executed and the command failed
if (x.Result.Attempts > 0 && !x.Result.Success)
{
// Add the command to the list
tempFailed.Add(x);
}
});
}
// Return the list of failed commands
return tempFailed;
}
}
/// <summary>
/// Initializes a new instance of the <see cref="CommandProcessor"/> class.
/// </summary>
/// <param name="dedupe">if set to <c>true</c>, remove all duplicate commands.</param>
/// <param name="continueOnFail">if set to <c>true</c>, continue executing the rest of the commands.</param>
/// <param name="errorOnFail">if set to <c>true</c>, throw an exception and log the error.</param>
public CommandProcessor(
SiteConfig config,
HttpClient client,
bool dedupe = true,
bool continueOnFail = true,
bool errorOnFail = false)
{
// Defaults to true
Dedupe = dedupe;
// Defaults to true
ContinueOnFail = continueOnFail;
// Defaults to false
ErrorOnFail = errorOnFail;
// Generate an empty list of commands
Commands = new List<Command>();
// Get the information to connect to the API
SiteConfig = config;
HttpClient = client;
}
/// <summary>
/// Processes the commands to ensure that there are no duplicates.
/// </summary>
public void ProcessCommands()
{
if (Dedupe)
{
// Store the current commands as a separate list
List<Command> deDupedList = Commands;
// Reverse the list to ensure that the most recent occurrence of any duplicate gets retained
deDupedList.Reverse();
// Returns all distinct elements by the specified field. Returns only the first occurrance
deDupedList = deDupedList.GroupBy(x => x.Result.Id)
.Select(x => x.First())
.ToList();
// Reverse the list to get it back to the original order
deDupedList.Reverse();
// Assign the changed list to Commands
Commands = deDupedList;
}
}
/// <summary>
/// Adds the command.
/// </summary>
/// <param name="command">The command.</param>
public void AddCommand(Command command)
{
Commands.Add(command);
ProcessCommands();
}
/// <summary>
/// Removes the command.
/// </summary>
/// <param name="command">The command.</param>
public void RemoveCommand(Command command)
{
Commands.Remove(command);
ProcessCommands();
}
/// <summary>
/// Clears all commands in the command list.
/// </summary>
public void ClearAllCommands()
{
Commands = new List<Command>();
}
/// <summary>
/// Executes the list of commands.
/// </summary>
/// <exception cref="Exception">Command Failed (if set to throw errors)</exception>
public async Task<List<CommandResult>> ExecuteCommands()
{
// The list of results to return
List<CommandResult> results = new List<CommandResult>();
List<Task<CommandResult>> tasks = new List<Task<CommandResult>>();
// Execute the command for each set of command arguments
foreach (var command in Commands)
{
tasks.Add(command.Execute());
}
var taskArray = tasks.ToArray();
var taskResults = await Task.WhenAll(taskArray);
// TODO: store all failed tasks
foreach (CommandResult res in taskResults)
{
results.Add(res);
// If the command failed
if (!res.Success)
{
// If it is supposed to throw an exception
if (ErrorOnFail)
{
// Log the error and throw the exception
Log.Error(res.Message);
throw new Exception(res.Message);
}
else
{
// Write to the log as an error if the command was not successful.
Log.Error(res.Message);
}
}
else
{
Log.Info(res.Message);
}
}
// Return the results
return results;
}
}
}
after reorganization:
using log4net;
using MoreLinq;
using Reader.API;
using Reader.Config;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
namespace Reader.Study
{
public class CommandProcessor
{
#region Fields
/// <summary>
/// The log
/// </summary>
private static readonly ILog Log = LogManager.GetLogger(typeof(CommandProcessor));
private HttpClient _httpClient;
private SiteConfig _siteConfig;
#endregion Fields
#region Properties
/// <summary>
/// Gets or sets the study API.
/// </summary>
/// <value>
/// The study API.
/// </value>
private StudyApi _studyApi { get; set; }
#endregion Properties
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="CommandProcessor"/> class.
/// </summary>
/// <param name="dedupe">if set to <c>true</c>, remove all duplicate commands.</param>
/// <param name="continueOnFail">if set to <c>true</c>, continue executing the rest of the commands.</param>
/// <param name="errorOnFail">if set to <c>true</c>, throw an exception and log the error.</param>
public CommandProcessor(
SiteConfig config,
HttpClient client,
bool dedupe = true,
bool continueOnFail = true,
bool errorOnFail = false)
{
// Defaults to true
Dedupe = dedupe;
// Defaults to true
ContinueOnFail = continueOnFail;
// Defaults to false
ErrorOnFail = errorOnFail;
// Generate an empty list of commands
Commands = new List<Command>();
// Get the information to connect to the API
SiteConfig = config;
HttpClient = client;
}
#endregion Constructors
#region Methods
#endregion Methods
/// <summary>
/// Gets the command queue.
/// </summary>
/// <value>
/// The commands.
/// </value>
public List<Command> Commands { get; private set; } = new List<Command>();
/// <summary>
/// Gets or sets a value indicating whether we continue processing the
/// command queue when a command fails.
/// </summary>
/// <value>
/// <c>true</c> if [continue on fail]; otherwise, <c>false</c>.
/// </value>
public bool ContinueOnFail { get; private set; } = true;
/// <summary>
/// Gets or sets a value indicating whether the command list should remove duplicate commands.
/// </summary>
/// <value>
/// <c>true</c> if we do not want duplicate commands; otherwise, <c>false</c>.
/// </value>
public bool Dedupe { get; private set; } = true;
/// <summary>
/// Gets or sets a value indicating whether the failure will be logged
/// or an exception will be thrown.
/// </summary>
/// <value>
/// <c>true</c> if an error should be thrown; otherwise, <c>false</c>.
/// </value>
public bool ErrorOnFail { get; private set; } = false;
/// <summary>
/// Gets the failed commands.
/// </summary>
/// <value>
/// The failed commands.
/// </value>
public List<Command> FailedCommands
{
get
{
// Create an empty list of commands
List<Command> tempFailed = new List<Command>();
// If there are commands
if (Commands.Count > 0)
{
/*
* Simulate the following:
* tempFailed = Commands.Where(x => x.Result.Attempts > 0 && !x.Result.Success)
*/
Commands.ForEach(x =>
{
// If the command has been executed and the command failed
if (x.Result.Attempts > 0 && !x.Result.Success)
{
// Add the command to the list
tempFailed.Add(x);
}
});
}
// Return the list of failed commands
return tempFailed;
}
}
/// <summary>
/// Gets or sets the client.
/// </summary>
/// <value>
/// The client.
/// </value>
public HttpClient HttpClient
{
get
{
return _httpClient;
}
set
{
_httpClient = value;
if (_siteConfig != null && _httpClient != null)
{
_studyApi = new StudyApi(_siteConfig, _httpClient);
}
}
}
/// <summary>
/// Gets or sets the site configuration.
/// </summary>
/// <value>
/// The site configuration.
/// </value>
public SiteConfig SiteConfig
{
get
{
return _siteConfig;
}
set
{
_siteConfig = value;
if (_siteConfig != null && _httpClient != null)
{
_studyApi = new StudyApi(_siteConfig, _httpClient);
}
}
}
/// <summary>
/// Adds the command.
/// </summary>
/// <param name="command">The command.</param>
public void AddCommand(Command command)
{
Commands.Add(command);
ProcessCommands();
}
/// <summary>
/// Clears all commands in the command list.
/// </summary>
public void ClearAllCommands()
{
Commands = new List<Command>();
}
/// <summary>
/// Executes the list of commands.
/// </summary>
/// <exception cref="Exception">Command Failed (if set to throw errors)</exception>
public async Task<List<CommandResult>> ExecuteCommands()
{
// The list of results to return
List<CommandResult> results = new List<CommandResult>();
List<Task<CommandResult>> tasks = new List<Task<CommandResult>>();
// Execute the command for each set of command arguments
foreach (var command in Commands)
{
tasks.Add(command.Execute());
}
var taskArray = tasks.ToArray();
var taskResults = await Task.WhenAll(taskArray);
// TODO: store all failed tasks
foreach (CommandResult res in taskResults)
{
results.Add(res);
// If the command failed
if (!res.Success)
{
// If it is supposed to throw an exception
if (ErrorOnFail)
{
// Log the error and throw the exception
Log.Error(res.Message);
throw new Exception(res.Message);
}
else
{
// Write to the log as an error if the command was not successful.
Log.Error(res.Message);
}
}
else
{
Log.Info(res.Message);
}
}
// Return the results
return results;
}
/// <summary>
/// Processes the commands to ensure that there are no duplicates.
/// </summary>
public void ProcessCommands()
{
if (Dedupe)
{
// Store the current commands as a separate list
List<Command> deDupedList = Commands;
// Reverse the list to ensure that the most recent occurrence of any duplicate gets retained
deDupedList.Reverse();
// Returns all distinct elements by the specified field. Returns only the first occurrance
deDupedList = deDupedList.GroupBy(x => x.Result.Id)
.Select(x => x.First())
.ToList();
// Reverse the list to get it back to the original order
deDupedList.Reverse();
// Assign the changed list to Commands
Commands = deDupedList;
}
}
/// <summary>
/// Removes the command.
/// </summary>
/// <param name="command">The command.</param>
public void RemoveCommand(Command command)
{
Commands.Remove(command);
ProcessCommands();
}
}
}
Steps to recreate
- Set cleanup to organize before cleanup
- Create a class with constructor(s), Fields, Properties, and Methods with little to no organization (with or without regions).
- Document all methods, fields, properties, and constructors using
///and XML - Cleanup active document using codemaid
Current behavior
If you look at the "after reorganization" code, you will see that there are methods and properties that are not in the expected regions.
Expected behavior
All code within the snippit should have been placed into a region (Fields, Properties, Constructors, Methods, etc)
Thanks for reporting the issue. Since it seemed to stop processing at
public List<Command> Commands { get; private set; } = new List<Command>();
I'm curious if that auto-initialized property syntax is causing something to trip up. Are you able to reproduce the issue with/without those elements? If it's still reproducible without those elements, are there certain elements once removed that cause it to behave as expected?