anchor.diff:          View.LayoutChanged.AddHandler (new EventHandler<TextViewLayoutChangedEventArgs>(ViewLayoutChanged))
   
   
  -    let FindMatchingCloseChar (start : SnapshotPoint, openChar : char, closeChar : char, maxLines : int) = 
  -        let currentPosition = start.Position //shows the number of chars before the cursor position
  -        let isParenthesisToken t =
  -            match t with
  -            | LPAREN x 
  -            | RPAREN x -> x.Start.AbsoluteOffset > currentPosition
  +    let FindMatchForOpen (parentheses, openParenthesisPosition) = 
  +        let isFurtherThanCurrent x =
  +            match x with
  +            |LPAREN r when r.Start.AbsoluteOffset > openParenthesisPosition -> true
  +            |RPAREN r when r.Start.AbsoluteOffset > openParenthesisPosition -> true
               | _ -> false
  +        let furtherParentheses = List.filter isFurtherThanCurrent parentheses
  +        let closeParenthesisPosition = ref -1
  +        let got = ref false
           let count = ref 0
  -        let parentheses = List.ofSeq (LexString (SourceBuffer.CurrentSnapshot.GetText()) |> Seq.filter isParenthesisToken) //got all parentheses after the first
  -        let w = ref -1
  +        let counter x =
  +            match x with
  +            |LPAREN _ -> count := !count + 1
  +            |RPAREN _ when not (!count = 0 ) -> count:= !count - 1
  +            |RPAREN r when (!count = 0) && not !got -> closeParenthesisPosition := r.Start.AbsoluteOffset
  +            | _ -> ()
  +            if not (!closeParenthesisPosition = -1) then got := true
  +        List.iter counter furtherParentheses
  +        let result = ref (new SnapshotSpan())
  +        if !got then result := new SnapshotSpan (new SnapshotPoint (SourceBuffer.CurrentSnapshot, !closeParenthesisPosition), 1)
  +        !result
  +//        let currentPosition = start.Position //shows the number of chars before the cursor position
  +//        let isParenthesisToken t =
  +//            match t with
  +//            | LPAREN x 
  +//            | RPAREN x -> x.Start.AbsoluteOffset > currentPosition
  +//            | _ -> false
  +//        let count = ref 0
  +////        let tmp = List.ofSeq ((LexString (SourceBuffer.CurrentSnapshot.GetText())) |> Seq.filter (fun x ->
  +////                                                                                                match x with
  +////                                                                                                | COMMUT -> true
  +////                                                                                                | _ -> false))
  +//        let parentheses = List.ofSeq (LexString (SourceBuffer.CurrentSnapshot.GetText()) |> Seq.filter isParenthesisToken) //got all parentheses after the first
  +//        let w = ref -1
  +//        let got = ref false
  +//        let s x =
  +//            match x with
  +//            | LPAREN _ -> count:= !count + 1
  +//            | RPAREN _ when not (!count = 0) -> count := !count - 1
  +//            | RPAREN r when (!count = 0) && (!got = false) -> w := r.Start.AbsoluteOffset 
  +//            | _ -> ()
  +//            if not (!w = -1) then got := true
  +//
  +//        List.iter s parentheses
  +//        let resultPoint = ref (new SnapshotPoint () )
  +//        if !got
  +//            then resultPoint := SnapshotPoint (start.Snapshot, !w)
  +//            else resultPoint := SnapshotPoint ()
  +//        let pairSpan = new SnapshotSpan(!resultPoint, 1)
  +//        pairSpan
  +
  +    let FindMatchForClose (parentheses, closeParenthesisPosition) = 
  +        let isFurtherThanCurrent x =
  +            match x with
  +            |LPAREN r when r.Start.AbsoluteOffset < closeParenthesisPosition -> true
  +            |RPAREN r when r.Start.AbsoluteOffset < closeParenthesisPosition -> true
  +            | _ -> false
  +        let furtherParentheses = List.rev <| List.filter isFurtherThanCurrent parentheses
  +        let openParenthesisPosition = ref -1
           let got = ref false
  -        let s x =
  +        let count = ref 0
  +        let counter x =
               match x with
  -            | LPAREN _ -> count:= !count + 1
  -            | RPAREN _ when not (!count = 0) -> count := !count - 1
  -            | RPAREN r when (!count = 0) && (!got = false) -> w := r.Start.AbsoluteOffset 
  +            |RPAREN _ -> count := !count + 1
  +            |LPAREN _ when not (!count = 0) -> count := !count - 1
  +            |LPAREN r when (!count = 0) && not !got -> openParenthesisPosition := r.Start.AbsoluteOffset
               | _ -> ()
  -            if not (!w = -1) then got := true
  -
  -        List.iter s parentheses
  -        let resultPoint = new SnapshotPoint (start.Snapshot, !w)
  -        let pairSpan = new SnapshotSpan(resultPoint, 1)
  -        pairSpan
  -
  -    let FindMatchingOpenChar (finish : SnapshotPoint, openChar : char, closeChar : char, maxLines : int) = 
  -        let currentPosition = finish.Position //shows the number of chars before the cursor position
  -        let isParenthesisToken t =
  -            match t with
  -            | LPAREN x 
  -            | RPAREN x -> x.Start.AbsoluteOffset < currentPosition
  -            | _ -> false
  -        let count = ref 0
  -        let parentheses = List.rev (List.ofSeq (LexString (SourceBuffer.CurrentSnapshot.GetText()) |> Seq.filter isParenthesisToken)) //got all parentheses before the last (from the closest)
  -        let w = ref -1
  -        let got = ref false
  -        let s x =
  -            match x with
  -            | RPAREN _ -> count:= !count + 1
  -            | LPAREN _ when not (!count = 0) -> count := !count - 1
  -            | LPAREN r when (!count = 0) && (!got = false) -> w := r.Start.AbsoluteOffset 
  -            | _ -> ()
  -            if not (!w = -1) then got := true
  -
  -        List.iter s parentheses
  -        let resultPoint = new SnapshotPoint (finish.Snapshot, !w)
  -        let pairSpan = new SnapshotSpan(resultPoint, 1)
  -        pairSpan
  +            if not (!openParenthesisPosition = -1) then got := true
  +        List.iter counter furtherParentheses
  +        let result = ref (new SnapshotSpan())
  +        if !got then result := new SnapshotSpan (new SnapshotPoint (SourceBuffer.CurrentSnapshot, !openParenthesisPosition), 1)
  +        !result
  +//        let currentPosition = finish.Position //shows the number of chars before the cursor position
  +//        let isParenthesisToken t =
  +//            match t with
  +//            | LPAREN x 
  +//            | RPAREN x -> x.Start.AbsoluteOffset < currentPosition
  +//            | _ -> false
  +//        let count = ref 0
  +//        let parentheses = List.rev (List.ofSeq (LexString (SourceBuffer.CurrentSnapshot.GetText()) |> Seq.filter isParenthesisToken)) //got all parentheses before the last (from the closest)
  +//        let w = ref -1
  +//        let got = ref false
  +//        let s x =
  +//            match x with
  +//            | RPAREN _ -> count:= !count + 1
  +//            | LPAREN _ when not (!count = 0) -> count := !count - 1
  +//            | LPAREN r when (!count = 0) && (!got = false) -> w := r.Start.AbsoluteOffset 
  +//            | _ -> ()
  +//            if not (!w = -1) then got := true
  +//
  +//        List.iter s parentheses
  +//        let resultPoint = new SnapshotPoint (finish.Snapshot, !w)
  +//        let pairSpan = new SnapshotSpan(resultPoint, 1)
  +//        pairSpan
       
       let getTags (spans : NormalizedSnapshotSpanCollection) =
  +        let isLParen x = 
  +            match x with
  +            |LPAREN _ -> true
  +            | _ -> false
  +        let isRParen x = 
  +            match x with
  +            |RPAREN _ -> true
  +            | _ -> false
  +        let isParenthesis x = isRParen x || isLParen  x
           let ok = not (spans.Count = 0) && CurrentChar.HasValue && (CurrentChar.Value.Position< CurrentChar.Value.Snapshot.Length) 
           let result = if ok
                           then
                               let currentChar = ref CurrentChar.Value
  +
                               if not (spans.ElementAt<SnapshotSpan>(0).Snapshot = (!currentChar).Snapshot) then
                                           currentChar := (!currentChar).TranslateTo(spans.ElementAt<SnapshotSpan>(0).Snapshot, PointTrackingMode.Positive)
  -                            let currentText = (!currentChar).GetChar()
  -                            if currentText = '(' || currentText = ')' then
  -                                let lastChar = if (!currentChar).Position = 0
  -                                                    then
  -                                                        !currentChar
  -                                                    else (!currentChar) - 1
  -                                let lastText = lastChar.GetChar()
  -                                let pairSpan = 
  -                                    if (currentText = '(')
  -                                        then FindMatchingCloseChar(!currentChar, currentText, ')', View.TextViewLines.Count)
  -                                        else if currentText = ')'
  -                                            then FindMatchingOpenChar(!currentChar, currentText, '(', View.TextViewLines.Count)
  -                                            else (new SnapshotSpan())
  -                                let s = (new TagSpan<TextMarkerTag>(new SnapshotSpan(!currentChar,1),new TextMarkerTag("green"))) :> ITagSpan<TextMarkerTag>
  -                                let e = (new TagSpan<TextMarkerTag>(pairSpan,new TextMarkerTag("green"))) :> ITagSpan<TextMarkerTag>
  -                                let preresult = seq { yield s; yield e}
  -                                preresult
  +                           
  +                            let currentPosition = currentChar.Value.Position
  +                            let lexeredText = List.ofSeq <| LexString ( SourceBuffer.CurrentSnapshot.GetText() )
  +                            let parentheses = List.filter isParenthesis lexeredText
  +                            let shouldBeProcessed = parentheses |> List.exists (fun x ->
  +                                                match x with
  +                                                |LPAREN r when r.Start.AbsoluteOffset = currentPosition -> true
  +                                                |RPAREN r when r.Start.AbsoluteOffset = currentPosition -> true
  +                                                | _                                                     -> false )
  +                            if shouldBeProcessed
  +                                then
  +                                    let currentToken = ref ( parentheses.ElementAt(0) )
  +                                    let f x =
  +                                        match x with
  +                                            |LPAREN r when r.Start.AbsoluteOffset = currentPosition -> currentToken := x
  +                                            |RPAREN r when r.Start.AbsoluteOffset = currentPosition -> currentToken := x 
  +                                            | _ -> () |> ignore 
  +                                    List.iter f parentheses
  +                                    let pairSpan = 
  +                                        if isLParen !currentToken
  +                                            then FindMatchForOpen (parentheses, currentPosition)
  +                                            else if isRParen !currentToken
  +                                                then FindMatchForClose(parentheses, currentPosition)
  +                                                else (new SnapshotSpan())
  +                                    let s = (new TagSpan<TextMarkerTag>(new SnapshotSpan(!currentChar,1),new TextMarkerTag("green"))) :> ITagSpan<TextMarkerTag>
  +                                    let e = (new TagSpan<TextMarkerTag>(pairSpan,new TextMarkerTag("green"))) :> ITagSpan<TextMarkerTag>
  +                                    let preresult = seq { yield s; yield e}
  +                                    preresult
                               else Seq.empty
                           else Seq.empty
           result
  
positive.code:     let UpdateAtCaretPosition (position : CaretPosition) =
          CurrentChar <- position.Point.GetPoint(SourceBuffer, position.Affinity)
          let tempEvent = TagsChanged
          //if not (tempEvent = null)
             // then 
          tempEvent.Trigger (new SnapshotSpanEventArgs(new SnapshotSpan(SourceBuffer.CurrentSnapshot,0,SourceBuffer.CurrentSnapshot.Length)))
                    
      let CaretPositionChanged (sender : obj) (e : CaretPositionChangedEventArgs) = 
  
negative.code:     let ViewLayoutChanged (sender: obj) (e : TextViewLayoutChangedEventArgs) =
          if not (e.OldSnapshot = e.NewSnapshot) then
              UpdateAtCaretPosition(View.Caret.Position)
  
      do 
          View <- view
  