Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Bug: RemoveAndSortUsings #727

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions CodeMaid/Logic/Cleaning/UsingStatementCleanupLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,9 @@ public void RemoveAndSortUsingStatements(TextDocument textDocument)
// Capture all existing using statements that should be re-inserted if removed.
const string patternFormat = @"^[ \t]*{0}[ \t]*\r?\n";

var points = (from usingStatement in _usingStatementsToReinsertWhenRemoved.Value
from editPoint in TextDocumentHelper.FindMatches(textDocument, string.Format(patternFormat, usingStatement))
select new { editPoint, text = editPoint.GetLine() }).Reverse().ToList();

// Shift every captured point one character to the right so they will auto-advance
// during new insertions at the start of the line.
foreach (var point in points)
{
point.editPoint.CharRight();
}
var usingStatementsToReinsert = _usingStatementsToReinsertWhenRemoved.Value
Copy link
Owner

@codecadwallader codecadwallader Jun 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously I was trying to capture and re-insert text back where it was originally located. This helped with some custom sort rules that users had. I agree it may not be necessary anymore since newer versions of VS have merged remove and sort back together vs. being separate options. Thanks for simplifying the code. 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense, no problem.

.Where(usingStatement => TextDocumentHelper.FirstOrDefaultMatch(textDocument, string.Format(patternFormat, usingStatement)) != null)
.ToList();

if (_package.IDEVersion >= 15)
{
Expand All @@ -91,19 +84,26 @@ from editPoint in TextDocumentHelper.FindMatches(textDocument, string.Format(pat
else
{
_commandHelper.ExecuteCommand(textDocument, "Edit.RemoveUnusedUsings");
_commandHelper.ExecuteCommand(textDocument, "Edit.SortUsings");
}

// Check each using statement point and re-insert it if removed.
foreach (var point in points)
// Ignore any using statements that are still referenced
usingStatementsToReinsert = usingStatementsToReinsert
.Where(usingStatement => TextDocumentHelper.FirstOrDefaultMatch(textDocument, string.Format(patternFormat, usingStatement)) == null)
.ToList();

if (usingStatementsToReinsert.Count > 0)
{
string text = point.editPoint.GetLine();
if (text != point.text)
var point = textDocument.StartPoint.CreateEditPoint();

foreach (string usingStatement in usingStatementsToReinsert)
{
point.editPoint.StartOfLine();
point.editPoint.Insert(point.text);
point.editPoint.Insert(Environment.NewLine);
point.StartOfLine();
point.Insert(usingStatement);
point.Insert(Environment.NewLine);
}

// Now sort without removing to ensure correct ordering.
_commandHelper.ExecuteCommand(textDocument, "Edit.SortUsings");
}
}

Expand Down