public int Insert(ITextDocument editor, string text) { int offset = editor.LocationToOffset(Location); using (var undo = editor.OpenUndoGroup()) { // TODO: Run formatter !!! // text = editor.FormatString (Location, text); var line = editor.GetLineByOffset(offset); int insertionOffset = line.Offset + Location.Column - 1; offset = insertionOffset; InsertNewLine(editor, LineBefore, ref offset); int result = offset - insertionOffset; editor.InsertText(offset, text); offset += text.Length; InsertNewLine(editor, LineAfter, ref offset); return(result); } }
public int Insert (ITextDocument editor, string text) { int offset = editor.LocationToOffset (Location); using (var undo = editor.OpenUndoGroup ()) { // TODO: Run formatter !!! // text = editor.FormatString (Location, text); var line = editor.GetLineByOffset (offset); int insertionOffset = line.Offset + Location.Column - 1; offset = insertionOffset; InsertNewLine (editor, LineBefore, ref offset); int result = offset - insertionOffset; editor.InsertText (offset, text); offset += text.Length; InsertNewLine (editor, LineAfter, ref offset); return result; } }
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 int PositionToOffset(this ITextDocument editor, Position position) { Runtime.AssertMainThread(); return(editor.LocationToOffset(position.Line + 1, position.Character + 1)); }
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()); }