Пример #1
0
        static void ResortImports(List <ImportStatement> importsToSort, ITextDocument editor, List <DAttribute> attributesNotToWrite, bool separatePackageRoots)
        {
            if (importsToSort.Count < 2)
            {
                return;
            }

            int    firstOffset = int.MaxValue;
            string indent      = "";

            // Remove all of them from the document; Memorize where the first import was
            for (int i = importsToSort.Count - 1; i >= 0; i--)
            {
                var ss            = importsToSort[i];
                var ssLocation    = ss.Location;
                var ssEndLocation = ss.EndLocation;

                DAttribute attr;
                if (ss.Attributes != null && ss.Attributes.Length > 0)
                {
                    attr = ss.Attributes.FirstOrDefault((e) => !attributesNotToWrite.Contains(e));
                    if (attr != null && attr.Location < ssLocation)
                    {
                        ssLocation = attr.Location;
                    }

                    attr = ss.Attributes.LastOrDefault((e) => !attributesNotToWrite.Contains(e));
                    if (attr != null && attr.EndLocation > ssEndLocation)
                    {
                        ssEndLocation = attr.EndLocation;
                    }
                }

                var l1 = editor.LocationToOffset(ssLocation.Line, ssLocation.Column);
                var l2 = editor.LocationToOffset(ssEndLocation.Line, ssEndLocation.Column);
                var n  = editor.Length - 1;

                // Remove indents and trailing semicolon.
                for (char c; l1 > 0 && ((c = editor.GetCharAt(l1 - 1)) == ' ' || c == '\t'); l1--)
                {
                    ;
                }
                for (char c; l2 < n && ((c = editor.GetCharAt(l2 + 1)) == ' ' || c == '\t' || c == ';'); l2++)
                {
                    ;
                }
                for (char c; l2 < n && ((c = editor.GetCharAt(l2 + 1)) == '\n' || c == '\r'); l2++)
                {
                    ;
                }

                l1 = Math.Max(0, l1);
                l2 = Math.Max(0, l2);

                firstOffset = Math.Min(l1, firstOffset);
                indent      = editor.GetLineIndent(editor.OffsetToLineNumber(firstOffset));
                editor.Remove(l1, l2 - l1);
            }

            // Sort
            importsToSort.Sort(new ImportComparer());

            // Write all imports beneath each other.
            var eol = editor.EolMarker;
            var sb  = new StringBuilder();
            ITypeDeclaration prevId = null;

            foreach (var i in importsToSort)
            {
                sb.Append(indent);

                if (i.Attributes != null)
                {
                    foreach (var attr in i.Attributes)
                    {
                        if (attributesNotToWrite.Contains(attr))
                        {
                            continue;
                        }

                        sb.Append(attr.ToString()).Append(' ');
                    }
                }

                sb.Append(i.ToCode(false)).Append(";").Append(eol);

                if (separatePackageRoots)
                {
                    var iid = ImportComparer.ExtractPrimaryId(i);
                    if (prevId != null && iid != null &&
                        (iid.InnerDeclaration ?? iid).ToString(true) != (prevId.InnerDeclaration ?? prevId).ToString(true))
                    {
                        sb.Append(eol);
                    }

                    prevId = iid;
                }
            }

            editor.Insert(firstOffset, sb.ToString());
        }