/// <summary> /// Returns replace span of the reference (what should be replaced) and also updates it to fit the new result item /// </summary> public override TextSpan GetInlineReplaceSpan(CodeReferenceResultItem item, out int absoluteStartIndex, out int absoluteLength) { TextSpan current = item.GetInlineReplaceSpan(true, out absoluteStartIndex, out absoluteLength); item.UpdateReplaceSpan(); return(current); }
/// <summary> /// Returns new undo unit for the item /// </summary> public override AbstractUndoUnit GetUndoUnit(CodeReferenceResultItem item, bool externalChange) { return(new InlineUndoUnit(item.FullReferenceText, externalChange)); }
/// <summary> /// Returns replace span of the reference (what should be replaced) /// </summary> public override TextSpan GetInlineReplaceSpan(CodeReferenceResultItem item, out int absoluteStartIndex, out int absoluteLength) { return(item.GetInlineReplaceSpan(false, out absoluteStartIndex, out absoluteLength)); }
/// <summary> /// Returns text that replaces current reference /// </summary> public override string GetReplaceString(CodeReferenceResultItem item) { return(item.GetInlineValue()); // this includes escaping sequences ( " -> \" ) }
/// <summary> /// Returns new undo unit for the item /// </summary> public override AbstractUndoUnit GetUndoUnit(CodeReferenceResultItem item, bool externalChange) { return(new GridRenameKeyInCodeUndoUnit(item.Key, item.KeyAfterRename)); }
/// <summary> /// Returns text that replaces current reference /// </summary> public override string GetReplaceString(CodeReferenceResultItem item) { return(item.GetReferenceAfterRename(item.KeyAfterRename)); }
/// <summary> /// Compares expected and actual result item /// </summary> internal static void ValidateItems(AbstractResultItem a, AbstractResultItem b) { if (b is NetStringResultItem) { NetStringResultItem an = a as NetStringResultItem; NetStringResultItem bn = b as NetStringResultItem; Assert.AreEqual(an.VariableElementName, bn.VariableElementName, "Variable names are not equal, " + a.Value); Assert.AreEqual(an.MethodElementName, bn.MethodElementName, "Method names are not equal, " + a.Value); } if (b is CSharpStringResultItem) { TestCSharpStringResultItem an = a as TestCSharpStringResultItem; CSharpStringResultItem bn = b as CSharpStringResultItem; if (an.NamespaceElementName == null) { Assert.IsNull(bn.NamespaceElement, "Namespace null, " + a.Value); } else { Assert.AreEqual(an.NamespaceElementName, bn.NamespaceElement.FullName, "Namespace names are not equal, " + a.Value); } } if (b is VBStringResultItem) { TestVBStringResultItem an = a as TestVBStringResultItem; VBStringResultItem bn = b as VBStringResultItem; if (an.NamespaceElementName == null) { Assert.IsNull(bn.NamespaceElement, "Namespace null, " + a.Value); } else { Assert.AreEqual(an.NamespaceElementName, bn.NamespaceElement.FullName, "Namespace names are not equal, " + a.Value); } } if (b is AspNetStringResultItem) { TestAspNetStringResultItem an = a as TestAspNetStringResultItem; AspNetStringResultItem bn = b as AspNetStringResultItem; Assert.AreEqual(an.ComesFromClientComment, bn.ComesFromClientComment, "ComesFromClientComment are not equal, " + a.Value + " on line " + a.ReplaceSpan.iStartLine); Assert.AreEqual(an.ComesFromCodeBlock, bn.ComesFromCodeBlock, "ComesFromCodeBlock are not equal, " + a.Value + " on line " + a.ReplaceSpan.iStartLine); Assert.AreEqual(an.ComesFromDirective, bn.ComesFromDirective, "ComesFromDirective are not equal, " + a.Value + " on line " + a.ReplaceSpan.iStartLine); Assert.AreEqual(an.ComesFromElement, bn.ComesFromElement, "ComesFromElement are not equal, " + a.Value + " on line " + a.ReplaceSpan.iStartLine); Assert.AreEqual(an.ComesFromInlineExpression, bn.ComesFromInlineExpression, "ComesFromInlineExpression are not equal, " + a.Value + " on line " + a.ReplaceSpan.iStartLine); Assert.AreEqual(an.ComesFromPlainText, bn.ComesFromPlainText, "ComesFromPlainText are not equal, " + a.Value + " on line " + a.ReplaceSpan.iStartLine); Assert.AreEqual(an.ElementName, bn.ElementName, "ElementName are not equal, " + a.Value + " on line " + a.ReplaceSpan.iStartLine); Assert.AreEqual(an.ElementPrefix, bn.ElementPrefix, "ElementPrefix are not equal, " + a.Value + " on line " + a.ReplaceSpan.iStartLine); Assert.AreEqual(an.LocalizabilityProved, bn.LocalizabilityProved, "LocalizabilityProved are not equal, " + a.Value + " on line " + a.ReplaceSpan.iStartLine); Assert.AreEqual(an.Language, bn.Language, "Language are not equal, " + a.Value + " on line " + a.ReplaceSpan.iStartLine); } if (b is CodeStringResultItem) { CodeStringResultItem an = a as CodeStringResultItem; CodeStringResultItem bn = b as CodeStringResultItem; Assert.AreEqual(an.WasVerbatim, bn.WasVerbatim, "Verbatim options are not equal, " + a.Value + " on line " + a.ReplaceSpan.iStartLine); Assert.AreEqual(an.ClassOrStructElementName, bn.ClassOrStructElementName, "Class names are not equal, " + a.Value + " on line " + a.ReplaceSpan.iStartLine); } if (b is CodeReferenceResultItem) { CodeReferenceResultItem an = a as CodeReferenceResultItem; CodeReferenceResultItem bn = b as CodeReferenceResultItem; Assert.AreEqual(an.FullReferenceText, bn.FullReferenceText, "FullReferenceText are not equal" + " on line " + a.ReplaceSpan.iStartLine); Assert.AreEqual(an.OriginalReferenceText, bn.OriginalReferenceText, "OriginalReferenceText are not equal" + " on line " + a.ReplaceSpan.iStartLine); } if (b is AspNetCodeReferenceResultItem) { AspNetCodeReferenceResultItem an = a as AspNetCodeReferenceResultItem; AspNetCodeReferenceResultItem bn = b as AspNetCodeReferenceResultItem; Assert.AreEqual(an.ComesFromInlineExpression, bn.ComesFromInlineExpression, "ComesFromInlineExpression are not equal" + " on line " + a.ReplaceSpan.iStartLine); Assert.AreEqual(an.ComesFromWebSiteResourceReference, bn.ComesFromWebSiteResourceReference, "ComesFromWebSiteResourceReference are not equal" + " on line " + a.ReplaceSpan.iStartLine); Assert.AreEqual(an.Language, bn.Language, "Language are not equal" + " on line " + a.ReplaceSpan.iStartLine); if (an.ComesFromInlineExpression) { Assert.IsNotNull(an.InlineReplaceSpan); Assert.IsNotNull(bn.InlineReplaceSpan); Assert.AreEqual(an.InlineReplaceSpan.StartLine, bn.InlineReplaceSpan.StartLine, "StartLine are not equal, " + a.Value + " on line " + a.ReplaceSpan.iStartLine); Assert.AreEqual(an.InlineReplaceSpan.StartIndex, bn.InlineReplaceSpan.StartIndex, "StartIndex are not equal, " + a.Value + " on line " + a.ReplaceSpan.iStartLine); Assert.AreEqual(an.InlineReplaceSpan.EndLine, bn.InlineReplaceSpan.EndLine, "EndLine are not equal, " + a.Value + " on line " + a.ReplaceSpan.iStartLine); Assert.AreEqual(an.InlineReplaceSpan.EndIndex, bn.InlineReplaceSpan.EndIndex, "EndIndex are not equal, " + a.Value + " on line " + a.ReplaceSpan.iStartLine); Assert.AreEqual(an.InlineReplaceSpan.AbsoluteCharOffset, bn.InlineReplaceSpan.AbsoluteCharOffset, "Offsets are not equal, " + a.Value + " on line " + a.ReplaceSpan.iStartLine); Assert.AreEqual(an.InlineReplaceSpan.AbsoluteCharLength, bn.InlineReplaceSpan.AbsoluteCharLength, "AbsoluteLengths are not equal, " + a.Value + " on line " + a.ReplaceSpan.iStartLine); } else { Assert.IsNull(an.InlineReplaceSpan); Assert.IsNull(bn.InlineReplaceSpan); } } Assert.AreEqual(a.Value, b.Value, "Values are not equal on line " + a.ReplaceSpan.iStartLine); Assert.AreEqual(a.SourceItem, b.SourceItem, "Source items are not equal, " + a.Value + " on line " + a.ReplaceSpan.iStartLine); Assert.AreEqual(a.IsWithinLocalizableFalse, b.IsWithinLocalizableFalse, "[Localizable(false)] options are not equal, " + a.Value + " on line " + a.ReplaceSpan.iStartLine); Assert.AreEqual(a.IsMarkedWithUnlocalizableComment, b.IsMarkedWithUnlocalizableComment, "/*VL_NO_LOC*/ options are not equal, " + a.Value + " on line " + a.ReplaceSpan.iStartLine); Assert.AreEqual(a.ComesFromDesignerFile, b.ComesFromDesignerFile, "Designer file options are not equal, " + a.Value + " on line " + a.ReplaceSpan.iStartLine); Assert.IsNotNull(a.ReplaceSpan); Assert.IsNotNull(b.ReplaceSpan); Assert.AreEqual(a.ReplaceSpan.iStartLine, b.ReplaceSpan.iStartLine, "iStartLine are not equal, " + a.Value + " on line " + a.ReplaceSpan.iStartLine); Assert.AreEqual(a.ReplaceSpan.iStartIndex, b.ReplaceSpan.iStartIndex, "iStartIndex are not equal, " + a.Value + " on line " + a.ReplaceSpan.iStartLine); Assert.AreEqual(a.ReplaceSpan.iEndLine, b.ReplaceSpan.iEndLine, "iEndLine are not equal, " + a.Value + " on line " + a.ReplaceSpan.iStartLine); Assert.AreEqual(a.ReplaceSpan.iEndIndex, b.ReplaceSpan.iEndIndex, "iEndIndex are not equal, " + a.Value + " on line " + a.ReplaceSpan.iStartLine); Assert.AreEqual(a.AbsoluteCharOffset, b.AbsoluteCharOffset, "Offsets are not equal, " + a.Value + " on line " + a.ReplaceSpan.iStartLine); Assert.AreEqual(a.AbsoluteCharLength, b.AbsoluteCharLength, "AbsoluteLengths are not equal, " + a.Value + " on line " + a.ReplaceSpan.iStartLine); Assert.AreEqual(a.IsConst, b.IsConst, "IsConst are not equal on line " + a.ReplaceSpan.iStartLine); }
public void Inline(List <CodeReferenceResultItem> dataList, bool externalChange, ref int errorRows) { // sort according to position dataList.Sort(new ResultItemsPositionCompararer <CodeReferenceResultItem>()); // start with the last items - not necessary to adjust position of many items after replace for (int i = dataList.Count - 1; i >= 0; i--) { try { CodeReferenceResultItem resultItem = dataList[i]; if (resultItem.MoveThisItem) // the item was checked in the toolwindow grid { int absoluteStartIndex, absoluteLength; // get text that replaces the result item string text = GetReplaceString(resultItem); // get position information about block to replace TextSpan inlineSpan = GetInlineReplaceSpan(resultItem, out absoluteStartIndex, out absoluteLength); string path = resultItem.SourceItem.GetFullPath(); if (RDTManager.IsFileOpen(path) && RDTManager.IsFileVisible(path)) // file is open { if (!buffersCache.ContainsKey(path)) // file's buffer is not yet loaded // load buffer { IVsTextLines textLines = DocumentViewsManager.GetTextLinesForFile(path, false); buffersCache.Add(path, textLines); IOleUndoManager m; // load undo manager int hr = textLines.GetUndoManager(out m); Marshal.ThrowExceptionForHR(hr); undoManagersCache.Add(path, m); } // replace the result item with the new text int h = buffersCache[path].ReplaceLines(inlineSpan.iStartLine, inlineSpan.iStartIndex, inlineSpan.iEndLine, inlineSpan.iEndIndex, Marshal.StringToBSTR(text), text.Length, new TextSpan[] { inlineSpan }); Marshal.ThrowExceptionForHR(h); // previous step caused undo unit to be added - remove it List <IOleUndoUnit> units = undoManagersCache[path].RemoveTopFromUndoStack(1); // and add custom undo unit which includes whole operation AbstractUndoUnit newUnit = GetUndoUnit(resultItem, externalChange); newUnit.AppendUnits.AddRange(units); undoManagersCache[path].Add(newUnit); } else { if (!filesCache.ContainsKey(path)) // file is not yet loaded // load the file and save it in cache { filesCache.Add(path, new StringBuilder(File.ReadAllText(path))); } StringBuilder b = filesCache[path]; // replace the text b = b.Remove(absoluteStartIndex, absoluteLength); b = b.Insert(absoluteStartIndex, text); filesCache[path] = b; } } } catch (Exception ex) { errorRows++; VLOutputWindow.VisualLocalizerPane.WriteException(ex); } } foreach (var pair in filesCache) { if (RDTManager.IsFileOpen(pair.Key)) { RDTManager.SetIgnoreFileChanges(pair.Key, true); File.WriteAllText(pair.Key, pair.Value.ToString()); RDTManager.SetIgnoreFileChanges(pair.Key, false); RDTManager.SilentlyReloadFile(pair.Key); } else { File.WriteAllText(pair.Key, pair.Value.ToString()); } } if (errorRows > 0) { throw new Exception("Error occured while processing some rows - see Output window for details."); } }
/// <summary> /// Returns new undo unit for the item /// </summary> public abstract AbstractUndoUnit GetUndoUnit(CodeReferenceResultItem item, bool externalChange);
/// <summary> /// Returns replace span of the reference (what should be replaced) /// </summary> public abstract TextSpan GetInlineReplaceSpan(CodeReferenceResultItem item, out int absoluteStartIndex, out int absoluteLength);
/// <summary> /// Returns text that replaces current reference /// </summary> public abstract string GetReplaceString(CodeReferenceResultItem item);