src/Misc/EFTools/DesignXmlCore/Refactoring/RefactoringOperationBase.cs
/// <summary>
///     Apply changes to one file and possiblely create markers
/// </summary>
/// <param name="file">FileChange that contains list of ChangeProposals.</param>
/// <param name="textLines">IVsTextLines of the file content.</param>
/// <param name="createMarker">Need to create marker or not when applying changes.</param>
/// <param name="changeToHighlight">The ChangeProposal need to be highlighted if creating marker.</param>
Apply changes to one file and possiblely create markers
Apply changes to one file and possiblely create markers
[SuppressMessage("Microsoft.Usage", "CA1806:DoNotIgnoreMethodResults",
            MessageId =
                "Microsoft.VisualStudio.Shell.Interop.IVsSolution.GetProjectOfUniqueName(System.String,Microsoft.VisualStudio.Shell.Interop.IVsHierarchy@)"
            )]
        [SuppressMessage("Microsoft.Usage", "CA1806:DoNotIgnoreMethodResults",
            MessageId =
                "Microsoft.VisualStudio.TextManager.Interop.IVsTextLines.ReplaceLines(System.Int32,System.Int32,System.Int32,System.Int32,System.IntPtr,System.Int32,Microsoft.VisualStudio.TextManager.Interop.TextSpan[])"
            )]
        internal static void ApplyChangesToOneFile(
            FileChange file, IVsTextLines textLines, bool createMarker, ChangeProposal changeToHighlight)
        {
            ArgumentValidation.CheckForNullReference(file, "file");
            ArgumentValidation.CheckForNullReference(textLines, "textLines");
            var sortedChanges = SortChanges(file);
            var totalLineOffset = 0;
            var totalColumnOffset = 0;
            var pContent = IntPtr.Zero;
            var resultSpan = new[] { new TextSpan() };
            foreach (var sortedChange in sortedChanges)
            {
                var change = sortedChange.Value;
                if (change.Included)
                {
                    try
                    {
                        if (resultSpan[0].iEndLine != change.StartLine + totalLineOffset)
                        {
                            totalColumnOffset = 0;
                        }
                        pContent = Marshal.StringToHGlobalAuto(change.NewValue);
                        var startLine = change.StartLine + totalLineOffset;
                        var startColumn = change.StartColumn + totalColumnOffset;
                        var endLine = change.EndLine + totalLineOffset;
                        var endColumn = change.EndColumn + (startLine == endLine ? totalColumnOffset : 0);
                        textLines.ReplaceLines(
                            startLine, startColumn, endLine, endColumn,
                            pContent, change.NewValue.Length, resultSpan);
                        var currentLineOffset = resultSpan[0].iEndLine - endLine;
                        totalLineOffset += currentLineOffset;
                        totalColumnOffset = resultSpan[0].iEndIndex - change.EndColumn;
                    }
                    finally
                    {
                        if (pContent != IntPtr.Zero)
                        {
                            Marshal.FreeHGlobal(pContent);
                        }
                    }
                }
                if (createMarker)
                {
                    var textChangeToHighlight = changeToHighlight as TextChangeProposal;
                    var highlight = ((textChangeToHighlight != null) &&
                                     (change.StartLine == textChangeToHighlight.StartLine) &&
                                     (change.StartColumn == textChangeToHighlight.StartColumn));
                    CreateMarker(textLines, resultSpan[0], highlight);
                }
            }
        }
[ SuppressMessage ( " " , " " , MessageId = " " ) ] [ SuppressMessage ( " " , " " , MessageId = " " ) ] internal static void ApplyChangesToOneFile ( FileChange file , IVsTextLines textLines , bool createMarker , ChangeProposal changeToHighlight ) { ArgumentValidation . CheckForNullReference ( file , " " ) ; ArgumentValidation . CheckForNullReference ( textLines , " " ) ; var sortedChanges = SortChanges ( file ) ; var totalLineOffset = 0 ; var totalColumnOffset = 0 ; var pContent = IntPtr . Zero ; var resultSpan = new [ ] { new TextSpan ( ) } ; foreach ( var sortedChange in sortedChanges ) { var change = sortedChange . Value ; if ( change . Included ) { try { if ( resultSpan [ 0 ] . iEndLine != change . StartLine + totalLineOffset ) { totalColumnOffset = 0 ; } pContent = Marshal . StringToHGlobalAuto ( change . NewValue ) ; var startLine = change . StartLine + totalLineOffset ; var startColumn = change . StartColumn + totalColumnOffset ; var endLine = change . EndLine + totalLineOffset ; var endColumn = change . EndColumn + ( startLine == endLine ? totalColumnOffset : 0 ) ; textLines . ReplaceLines ( startLine , startColumn , endLine , endColumn , pContent , change . NewValue . Length , resultSpan ) ; var currentLineOffset = resultSpan [ 0 ] . iEndLine - endLine ; totalLineOffset += currentLineOffset ; totalColumnOffset = resultSpan [ 0 ] . iEndIndex - change . EndColumn ; } finally { if ( pContent != IntPtr . Zero ) { Marshal . FreeHGlobal ( pContent ) ; } } } if ( createMarker ) { var textChangeToHighlight = changeToHighlight as TextChangeProposal ; var highlight = ( ( textChangeToHighlight != null ) && ( change . StartLine == textChangeToHighlight . StartLine ) && ( change . StartColumn == textChangeToHighlight . StartColumn ) ) ; CreateMarker ( textLines , resultSpan [ 0 ] , highlight ) ; } } }
Apply changes to one file and possiblely create markers
Apply changes to one file and possiblely create markers
// Here we only work on text based ChangeProposal // Sort all ChangeProposal in one file by the offset. // After apply any change, the offset of next ChangeProposal will change. // We need to sort all the changes first, so it will be easy to recalculate  // the new offset for next change. // Changes may cause the total line number of this file to be changed. // This variable is used to keep this line changes. // In one line, changes may cause column number of that line to be changed. // This variable is used to keep this column changes. // Store result TextSpan info // If user choose to apply this change // If current change's StartLine is different from Last change's EndLine // reset the columnOffset // Calculate the current change's offset by applying lineOffset // and columnOffset caused by previous changes // Only apply columnOffset if current change's startLine is same as endLine // Add the line offset caused by this change to total lineOffset; // Calculate the ColumnOffset after this change // Create Marker for this change's textspan. // If this change is the change to be highlighted, highlight it.
file
FileChange
FileChange that contains list of ChangeProposals.
FileChange that contains list of ChangeProposals .
textLines
IVsTextLines
IVsTextLines of the file content.
IVsTextLines of the file content .
createMarker
bool
Need to create marker or not when applying changes.
Need to create marker or not when applying changes .
changeToHighlight
ChangeProposal
The ChangeProposal need to be highlighted if creating marker.
The ChangeProposal need to be highlighted if creating marker .