/// <summary>Finds all instances of a text in the document object for the provided item.</summary> /// <param name="item">Project item to search texts</param> /// <param name="text">Text to look for</param> /// <returns>A collection of BaseHardCodedString objects that references to text</returns> public static ReadOnlyCollection <BaseHardCodedString> FindAllInstancesInDocument(ProjectItem item, string text) { TextDocument doc = GetDocumentForItem(item); Collection <BaseHardCodedString> instances = new Collection <BaseHardCodedString>(); EditPoint start = doc.StartPoint.CreateEditPoint(); start.MoveToAbsoluteOffset(1); EditPoint end = null; // Unused object for FindPattern method. TextRanges ranges = null; System.Collections.IEnumerator comments = null; Match currentMatch = null; BaseHardCodedString instance = BaseHardCodedString.GetHardCodedString(item.Document); while (start.FindPattern(text, (int)(vsFindOptions.vsFindOptionsMatchCase), ref end, ref ranges)) { if (instance != null) { if (comments == null) { comments = instance.FindCommentsInDocument(item).GetEnumerator(); } bool inComment = false; while (currentMatch != null || (comments.MoveNext())) { currentMatch = (Match)(comments.Current); // If this match appears earlier then current text skip if (currentMatch.Index + currentMatch.Length <= (start.AbsoluteCharOffset - 1)) { currentMatch = null; } // If this comment is later then current text stop processing comments else if (currentMatch.Index >= (end.AbsoluteCharOffset - 1)) { break; } // At this point current text must be part of a comment block else { inComment = true; break; } } if (!inComment) { instance = instance.CreateInstance(item, start.AbsoluteCharOffset - 1, end.AbsoluteCharOffset - 1); instances.Add(instance); } } start = end; } return(new ReadOnlyCollection <BaseHardCodedString>(instances)); }
private int toPosition(int absolute) { TextDocument doc = App.ActiveDocument.Object("TextDocument") as TextDocument; TextSelection selection = doc.Selection as TextSelection; EditPoint point = selection.ActivePoint.CreateEditPoint(); point.MoveToAbsoluteOffset(absolute); string text = doc.StartPoint.CreateEditPoint().GetText(point); return((absolute - 1) + number(text, "\r\n", text.Length - 1)); }
private int locate(int off) { TextSelection selection = _dte.ActiveDocument.Selection as TextSelection; VirtualPoint active = selection.ActivePoint; EditPoint point0 = active.CreateEditPoint(); EditPoint point1 = active.CreateEditPoint(); point0.StartOfDocument(); point1.MoveToAbsoluteOffset(off); string text = point0.GetText(point1); return (off - 1) + length(text, "\r\n", text.Length - 1); }
/// <summary> /// /// </summary> /// <param name="beg"></param> /// <param name="end"></param> /// <param name="value"></param> /// <returns></returns> public bool replace(int beg, int end, string value) { TextSelection selection = _dte.ActiveDocument.Selection as TextSelection; VirtualPoint active = selection.ActivePoint; EditPoint point0 = active.CreateEditPoint(); EditPoint point1 = active.CreateEditPoint(); point0.MoveToAbsoluteOffset(offset(beg)); point1.MoveToAbsoluteOffset(offset(end)); point0.ReplaceText(point1, value, 1); return true; }
private void ReorderMembers(CodeElement2 type, IEnumerable <CodeMember> members, string orderedCode, TextPoint startPoint) { ThreadHelper.ThrowIfNotOnUIThread(); // Removing members will shift the startPoint back one line. // So we'll use the absolute offset to jump back to that insert point. int startPointOffset = 0; if (startPoint != null) { startPointOffset = startPoint.AbsoluteCharOffset; } FileCodeModel2 codeModel = this.textHandler.Document.ProjectItem.FileCodeModel as FileCodeModel2; codeModel.BeginBatch(); try { foreach (CodeMember member in members) { member.Remove(); } } finally { codeModel.EndBatch(); } if (startPoint != null) { EditPoint startEdit = startPoint.CreateEditPoint(); startEdit.MoveToAbsoluteOffset(startPointOffset); startEdit.StartOfLine(); // If the line above startEdit isn't empty and isn't the start of the class/struct/interface/enum, // then insert a blank line so the sortedCode will be separated from the code above it. EditPoint lineAboveEdit = startEdit.CreateEditPoint(); lineAboveEdit.LineUp(); lineAboveEdit.StartOfLine(); string lineText = lineAboveEdit.GetText(lineAboveEdit.LineLength).Trim(); if (lineText.Length != 0 && lineAboveEdit.Line > type.StartPoint.Line && (this.language != Language.CSharp || lineText != "{")) { startEdit.Insert(Environment.NewLine); } startEdit.Insert(orderedCode); // If the line after startEdit isn't empty and isn't the end of the class/struct/interface/enum, // then insert a blank line so the sortedCode will be separated from the code below it. startEdit.StartOfLine(); lineText = startEdit.GetText(startEdit.LineLength).Trim(); if (lineText.Length != 0 && startEdit.Line < type.EndPoint.Line && (this.language != Language.CSharp || lineText != "}")) { startEdit.Insert(Environment.NewLine); } } }