anchor.diff:      Debug.WriteLine("selection:\n{0}", selection)
       Debug.WriteLine("post:\n{0}", post)
   
  -    let tree = parse fsi selection
  +    let formatSelection fsi config selection =
  +        Context.create config selection
  +        |> genParsedInput (parse fsi selection)
  +        |> ifElse (s.[finish] = '\n') sepNln sepNone
  +        |> dump
  +        |> integrateComments selection
  +
  +    let reconstructSourceCode startCol formatteds pre post =
  +        // Realign results on the correct column
  +        Context.create config "" 
  +        |> str pre
  +        |> atIndentLevel startCol (col sepNln formatteds str)
  +        |> str post
  +        |> dump
   
       match patch with
       | TypeMember ->
  -        /// Get formatted selection with "type T = \n" patch
  -        let result = 
  -            Context.create config selection
  -            |> genParsedInput tree
  -            |> ifElse (s.[finish] = '\n') sepNln sepNone
  -            |> dump
  -        /// Remove the patch
  -        let contents = result.Split([|'\n'|], StringSplitOptions.None)
  +        // Get formatted selection with "type T = \n" patch
  +        let result = formatSelection fsi config selection
  +        // Remove the patch
  +        let contents = result.Replace("\r\n","\n").Split('\r', '\n')
           if contents = [||] then
               sprintf "%s%s%s" pre result post
           else
  -            /// Due to patching, the text has at least two lines
  +            // Due to patching, the text has at least two lines
               let first = contents.[1]
               let column = first.Length - first.TrimStart().Length
  -            let selections = contents.[1..] |> Seq.map (fun s -> s.[column..])
  -            /// Realign results on the correct column
  -            Context.create config "" 
  -            |> str pre
  -            |> atIndentLevel startCol (col sepNln selections str)
  -            |> str post
  -            |> dump
  -    | RecType | RecLet ->        
  -        /// Get formatted selection with "type" or "let rec" replacement for "and"
  -        let result = 
  -            Context.create config selection
  -            |> genParsedInput tree
  -            |> ifElse (s.[finish] = '\n') sepNln sepNone
  -            |> dump
  -        /// Substitute by old contents
  +            let formatteds = contents.[1..] |> Seq.map (fun s -> s.[column..])
  +            reconstructSourceCode startCol formatteds pre post
  +    | RecType 
  +    | RecLet ->        
  +        // Get formatted selection with "type" or "let rec" replacement for "and"
  +        let result = formatSelection fsi config selection
  +        // Substitute by old contents
           let pattern = if patch = RecType then Regex("type") else Regex("let rec")
  -        let selection = pattern.Replace(result, "and", 1)
  -        /// Realign results on the correct column
  -        Context.create config "" 
  -        |> str pre
  -        |> atIndentLevel startCol (str selection)
  -        |> str post
  -        |> dump
  +        let formatteds = pattern.Replace(result, "and", 1).Replace("\r\n","\n").Split('\r', '\n')
  +        reconstructSourceCode startCol formatteds pre post
       | NoPatch ->
  -        Context.create config selection 
  -        |> str pre
  -        |> atIndentLevel startCol (genParsedInput tree)
  -        |> ifElse (s.[finish] = '\n') sepNln sepNone
  -        |> str post
  -        |> dump
  +        let result = formatSelection fsi config selection
  +        let formatteds = result.Replace("\r\n","\n").Split('\r', '\n')
  +        reconstructSourceCode startCol formatteds pre post
   
   /// Format selection in range r and keep other parts unchanged; return None if failed
   let tryFormatSelectionFromString fsi (r : range) (s : string) config =
  
positive.code:     _ -> None
  
  /// Format a source string using given config and write to a text writer
  let processSourceString fsi inStr (tw : TextWriter) config =
      Context.create config inStr 
      |> genParsedInput (parse fsi inStr) 
      |> dump
      |> tw.Write
  
  /// Format a source string using given config and write to a text writer; return None if failed
  let tryProcessSourceString fsi inStr tw config =
      try
          Some (processSourceString fsi inStr tw config)
      with 
      _ -> None
  
  
negative.code:         |> Seq.toArray
  
      let start = positions.[r.StartLine-1] + r.StartColumn
      // We can't assume the range is valid, so check string boundary here
      let finish = 
          let pos = positions.[r.EndLine-1] + r.EndColumn
          if pos >= content.Length then content.Length - 1 else pos 
  