private void OpenBracket(IEditor editor, ITextDocument document, string text)
        {
            if (text[0].IsOpenBracketChar() && editor.CaretOffset <= document.TextLength && editor.CaretOffset > 0)
            {
                var nextChar = ' ';

                if (editor.CaretOffset != document.TextLength)
                {
                    nextChar = document.GetCharAt(editor.CaretOffset);
                }

                var location = document.GetLocation(editor.CaretOffset);

                if (char.IsWhiteSpace(nextChar) || nextChar.IsCloseBracketChar())
                {
                    if (text[0] == '{')
                    {
                        var offset = editor.CaretOffset;

                        document.Insert(editor.CaretOffset, " " + text[0].GetCloseBracketChar().ToString() + " ");

                        if (IndentationStrategy != null)
                        {
                            editor.IndentLine(editor.Line);
                        }

                        editor.CaretOffset = offset + 1;
                    }
                    else
                    {
                        var offset = editor.CaretOffset;

                        document.Insert(editor.CaretOffset, text[0].GetCloseBracketChar().ToString());

                        editor.CaretOffset = offset;
                    }
                }
            }
        }
Beispiel #2
0
        public static void GenerateImportStatementForNode(INode n, IEditorData ed, ITextDocument doc)
        {
            var off = doc.LocationToOffset(FindLastImportStatementEndLocation(ed.SyntaxTree, ed.ModuleCode).Line + 1, 0);

            doc.Insert(off, "import " + (n.NodeRoot as DModule).ModuleName + ";" + doc.EolMarker);
        }
		public static void GenerateImportStatementForNode(INode n, IEditorData ed, ITextDocument doc)
		{
			var off = doc.LocationToOffset(FindLastImportStatementEndLocation(ed.SyntaxTree, ed.ModuleCode).Line + 1, 0);
			doc.Insert(off, "import " + (n.NodeRoot as DModule).ModuleName + ";" + doc.EolMarker);
		}
        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());
        }