@@ -22,20 +22,23 @@ open Microsoft.CodeAnalysis.Classification
2222[<AutoOpen>]
2323module BraceCompletionSessionProviderHelpers =
2424
25- let getLanguageService < 'T when 'T :> ILanguageService and 'T : null > ( document : Document ) =
25+ let tryGetLanguageService < 'T when 'T :> ILanguageService and 'T : null > ( document : Document ) =
2626match document.Projectwith
27- | null -> null
27+ | null -> None
2828| project->
2929match project.LanguageServiceswith
30- | null -> null
30+ | null -> None
3131| languageServices->
3232 languageServices.GetService< 'T>()
33+ |> Some
3334
34- let getCaretPoint ( buffer : ITextBuffer ) ( session : IBraceCompletionSession ) =
35- session.TextView.Caret.Position.Point.GetPoint( buffer, PositionAffinity.Predecessor)
35+ let tryGetCaretPoint ( buffer : ITextBuffer ) ( session : IBraceCompletionSession ) =
36+ let point = session.TextView.Caret.Position.Point.GetPoint( buffer, PositionAffinity.Predecessor)
37+ if point.HasValuethen Some point.Value
38+ else None
3639
37- let getCaretPosition session =
38- session|> getCaretPoint session.SubjectBuffer
40+ let tryGetCaretPosition session =
41+ session|> tryGetCaretPoint session.SubjectBuffer
3942
4043let tryInsertAdditionalBracePair ( session : IBraceCompletionSession ) openingChar closingChar =
4144let sourceCode = session.TextView.TextSnapshot
@@ -170,12 +173,9 @@ type BraceCompletionSession
170173let closingSnapshotPoint = closingPoint.GetPoint( subjectBuffer.CurrentSnapshot)
171174
172175if closingSnapshotPoint.Position> 0 then
173- let caretPos = getCaretPosition this
174-
175- if caretPos.HasValue&& not ( this.HasNoForwardTyping( caretPos.Value, closingSnapshotPoint.Subtract( 1 ))) then
176- true
177- else
178- false
176+ match tryGetCaretPosition thiswith
177+ | Some caretPoswhen not ( this.HasNoForwardTyping( caretPos, closingSnapshotPoint.Subtract( 1 ))) -> true
178+ | _ -> false
179179else
180180false
181181
@@ -200,31 +200,33 @@ type BraceCompletionSession
200200member this.PreBackspace handledCommand =
201201 handledCommand<- false
202202
203- let caretPos = getCaretPosition this
204- let snapshot = subjectBuffer.CurrentSnapshot
203+ match tryGetCaretPosition thiswith
204+ | Some caretPos->
205+ let snapshot = subjectBuffer.CurrentSnapshot
205206
206- if caretPos.HasValue && caretPos.Value .Position> 0 &&
207- caretPos.Value .Position- 1 = openingPoint.GetPoint( snapshot) .Position&&
208- not this.HasForwardTypingthen
207+ if caretPos.Position> 0 &&
208+ caretPos.Position- 1 = openingPoint.GetPoint( snapshot) .Position&&
209+ not this.HasForwardTypingthen
209210
210- use undo= this.CreateUndoTransaction()
211- use edit= subjectBuffer.CreateEdit()
212-
213- let span = SnapshotSpan( openingPoint.GetPoint( snapshot), closingPoint.GetPoint( snapshot))
214-
215- edit.Delete( span.Span) |> ignore
216-
217- if edit.HasFailedChangesthen
218- edit.Cancel()
219- undo.Cancel()
220- Debug.Fail( " Unable to clear braces" )
221- else
222- // handle the command so the backspace does
223- // not go through since we've already cleared the braces
224- handledCommand<- true
225- edit.Apply() |> ignore// FIXME: ApplyAndLogExceptions()
226- undo.Complete()
227- this.EndSession()
211+ use undo= this.CreateUndoTransaction()
212+ use edit= subjectBuffer.CreateEdit()
213+
214+ let span = SnapshotSpan( openingPoint.GetPoint( snapshot), closingPoint.GetPoint( snapshot))
215+
216+ edit.Delete( span.Span) |> ignore
217+
218+ if edit.HasFailedChangesthen
219+ edit.Cancel()
220+ undo.Cancel()
221+ Debug.Fail( " Unable to clear braces" )
222+ else
223+ // handle the command so the backspace does
224+ // not go through since we've already cleared the braces
225+ handledCommand<- true
226+ edit.Apply() |> ignore// FIXME: ApplyAndLogExceptions()
227+ undo.Complete()
228+ this.EndSession()
229+ | _ -> ()
228230
229231member __.PostBackspace () = ()
230232
@@ -239,16 +241,16 @@ type BraceCompletionSession
239241
240242let closingSnapshotPoint = closingPoint.GetPoint( snapshot)
241243if not this.HasForwardTyping&& session.AllowOverType( this, cancellationToken) then
242- let caretPos = getCaretPosition this
244+ let caretPosOpt = tryGetCaretPosition this
243245
244- Debug.Assert( caretPos.HasValue && caretPos .Value.Position< closingSnapshotPoint.Position)
246+ Debug.Assert( caretPosOpt.IsSome && caretPosOpt .Value.Position< closingSnapshotPoint.Position)
245247
248+ match caretPosOptwith
246249// ensure that we are within the session before clearing
247- if caretPos.HasValue&& caretPos.Value.Position< closingSnapshotPoint.Position&& closingSnapshotPoint.Position> 0 then
248-
250+ | Some caretPoswhen caretPos.Position< closingSnapshotPoint.Position&& closingSnapshotPoint.Position> 0 ->
249251use undo= this.CreateUndoTransaction()
250252
251- let span = SnapshotSpan( caretPos.Value , closingSnapshotPoint.Subtract( 1 ))
253+ let span = SnapshotSpan( caretPos, closingSnapshotPoint.Subtract( 1 ))
252254
253255use edit= subjectBuffer.CreateEdit()
254256
@@ -264,6 +266,7 @@ type BraceCompletionSession
264266 this.MoveCaretToClosingPoint()
265267 editorOperations.AddAfterTextBufferChangePrimitive()
266268 undo.Complete()
269+ | _ -> ()
267270
268271member __.PostOverType () = ()
269272
@@ -284,13 +287,13 @@ type BraceCompletionSession
284287 handledCommand<- false
285288
286289member this.PostReturn () =
287- let caretPos = getCaretPosition this
288-
289- if caretPos.HasValuethen
290+ match tryGetCaretPosition thiswith
291+ | Some caretPos->
290292let closingSnapshotPoint = closingPoint.GetPoint( subjectBuffer.CurrentSnapshot)
291293
292- if closingSnapshotPoint.Position> 0 && this.HasNoForwardTyping( caretPos.Value , closingSnapshotPoint.Subtract( 1 )) then
294+ if closingSnapshotPoint.Position> 0 && this.HasNoForwardTyping( caretPos, closingSnapshotPoint.Subtract( 1 )) then
293295 session.AfterReturn( this, CancellationToken.None)
296+ | _ -> ()
294297
295298member __.Finish () = ()
296299
@@ -528,33 +531,24 @@ type BraceCompletionSessionProvider
528531interface IBraceCompletionSessionProviderwith
529532
530533member __.TryCreateSession ( textView , openingPoint , openingBrace , closingBrace , session ) =
531- let textSnapshot = openingPoint.Snapshot
532-
533- let newSession =
534- match textSnapshot.GetOpenDocumentInCurrentContextWithChanges() with
535- | null -> null
536- | document->
537- match getLanguageService< IEditorBraceCompletionSessionFactory> documentwith
538- | null -> null
539- | editorSessionFactory->
540- // Brace completion is (currently) not cancellable.
541- let cancellationToken = CancellationToken.None
542-
543- match editorSessionFactory.TryCreateSession( document, openingPoint.Position, openingBrace, cancellationToken) with
544- | null -> null
545- | editorSession->
546- let undoHistory = undoManager.GetTextBufferUndoManager( textView.TextBuffer) .TextBufferUndoHistory
547- BraceCompletionSession(
548- textView,
549- openingPoint.Snapshot.TextBuffer,
550- openingPoint,
551- openingBrace,
552- closingBrace,
553- undoHistory,
554- editorOperationsFactoryService,
555- editorSession) :> IBraceCompletionSession
556-
557- session<- newSession
534+ session<-
535+ maybe {
536+ let! document = openingPoint.Snapshot.GetOpenDocumentInCurrentContextWithChanges() |> Option.ofObj
537+ let! sessionFactory = tryGetLanguageService< IEditorBraceCompletionSessionFactory> document
538+ let! session = sessionFactory.TryCreateSession( document, openingPoint.Position, openingBrace, CancellationToken.None) |> Option.ofObj
539+
540+ let undoHistory = undoManager.GetTextBufferUndoManager( textView.TextBuffer). TextBufferUndoHistory
541+ return BraceCompletionSession(
542+ textView,
543+ openingPoint.Snapshot.TextBuffer,
544+ openingPoint,
545+ openingBrace,
546+ closingBrace,
547+ undoHistory,
548+ editorOperationsFactoryService,
549+ session) :> IBraceCompletionSession
550+ }
551+ |> Option.toObj
558552
559553match sessionwith
560554| null -> false