Skip to content

Commit ca76cee

Browse files
CodeRefactor.OrganizeImports: sort imports by package then by name. ASCompletion.ASGenerator: sort imports by package then by name.
1 parent 32aeaf9 commit ca76cee

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

External/Plugins/ASCompletion/Completion/ASGenerator.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4326,7 +4326,7 @@ public static int InsertImport(MemberModel member, bool fixScrolling)
43264326
// insert in alphabetical order
43274327
mImport = ASFileParserRegexes.Import.Match(txt);
43284328
if (mImport.Success &&
4329-
String.Compare(mImport.Groups["package"].Value, fullPath) > 0)
4329+
ComparePackages(mImport.Groups["package"].Value, fullPath) > 0)
43304330
{
43314331
line--;
43324332
break;
@@ -4427,6 +4427,23 @@ private static void AddLookupPosition()
44274427
ASContext.Panel.SetLastLookupPosition(ASContext.Context.CurrentFile, lookupLine, lookupCol);
44284428
}
44294429
}
4430+
4431+
private static Int32 ComparePackages(String package1, String package2)
4432+
{
4433+
IComparer cmp = new CaseInsensitiveComparer();
4434+
String[] parts1 = package1.Split('.');
4435+
String[] parts2 = package2.Split('.');
4436+
int pkgLen1 = parts1.Length - 1;
4437+
int pkgLen2 = parts2.Length - 1;
4438+
int commonLen = (pkgLen1 <= pkgLen2) ? pkgLen1 : pkgLen2;
4439+
for (int i = 0; i < commonLen; ++i)
4440+
{
4441+
int cmpResult = cmp.Compare(parts1[i], parts2[i]);
4442+
if (cmpResult != 0)
4443+
return cmpResult;
4444+
}
4445+
return pkgLen1 - pkgLen2;
4446+
}
44304447
#endregion
44314448
}
44324449

External/Plugins/CodeRefactor/Commands/OrganizeImports.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,7 @@ private void InsertImports(List<MemberModel> imports, String searchInText, Scint
149149
{
150150
String eol = LineEndDetector.GetNewLineMarker(sci.EOLMode);
151151
Int32 line = imports[0].LineFrom - DeletedImportsCompensation;
152-
ImportsComparerType comparerType = new ImportsComparerType();
153-
imports.Sort(comparerType);
152+
imports.Sort(new ImportsComparerType());
154153
sci.GotoLine(line);
155154
Int32 curLine = 0;
156155
List<String> uniques = this.GetUniqueImports(imports, searchInText, sci.FileName);
@@ -226,7 +225,19 @@ class ImportsComparerType : IComparer<MemberModel>
226225
{
227226
public Int32 Compare(MemberModel item1, MemberModel item2)
228227
{
229-
return new CaseInsensitiveComparer().Compare(item1.Type, item2.Type);
228+
IComparer cmp = new CaseInsensitiveComparer();
229+
String[] parts1 = item1.Type.Split('.');
230+
String[] parts2 = item2.Type.Split('.');
231+
int pkgLen1 = parts1.Length - 1;
232+
int pkgLen2 = parts2.Length - 1;
233+
int commonLen = (pkgLen1 <= pkgLen2) ? pkgLen1 : pkgLen2;
234+
for (int i = 0; i < commonLen; ++i)
235+
{
236+
int cmpResult = cmp.Compare(parts1[i], parts2[i]);
237+
if (cmpResult != 0)
238+
return cmpResult;
239+
}
240+
return pkgLen1 - pkgLen2;
230241
}
231242
}
232243

0 commit comments

Comments
 (0)