Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite42cef3

Browse files
committed
Using maybe to make TryCreateSession a bit cleaner
1 parent02af98e commite42cef3

File tree

1 file changed

+66
-72
lines changed

1 file changed

+66
-72
lines changed

‎vsintegration/src/FSharp.Editor/AutomaticCompletion/BraceCompletionSessionProvider.fs‎

Lines changed: 66 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,23 @@ open Microsoft.CodeAnalysis.Classification
2222
[<AutoOpen>]
2323
moduleBraceCompletionSessionProviderHelpers=
2424

25-
letgetLanguageService<'Twhen'T:>ILanguageServiceand'T:null>(document:Document)=
25+
lettryGetLanguageService<'Twhen'T:>ILanguageServiceand'T:null>(document:Document)=
2626
match document.Projectwith
27-
|null->null
27+
|null->None
2828
| project->
2929
match project.LanguageServiceswith
30-
|null->null
30+
|null->None
3131
| languageServices->
3232
languageServices.GetService<'T>()
33+
|> Some
3334

34-
letgetCaretPoint(buffer:ITextBuffer)(session:IBraceCompletionSession)=
35-
session.TextView.Caret.Position.Point.GetPoint(buffer, PositionAffinity.Predecessor)
35+
lettryGetCaretPoint(buffer:ITextBuffer)(session:IBraceCompletionSession)=
36+
letpoint= session.TextView.Caret.Position.Point.GetPoint(buffer, PositionAffinity.Predecessor)
37+
if point.HasValuethen Some point.Value
38+
else None
3639

37-
letgetCaretPosition session=
38-
session|>getCaretPoint session.SubjectBuffer
40+
lettryGetCaretPosition session=
41+
session|>tryGetCaretPoint session.SubjectBuffer
3942

4043
lettryInsertAdditionalBracePair(session:IBraceCompletionSession)openingChar closingChar=
4144
letsourceCode= session.TextView.TextSnapshot
@@ -170,12 +173,9 @@ type BraceCompletionSession
170173
letclosingSnapshotPoint= closingPoint.GetPoint(subjectBuffer.CurrentSnapshot)
171174

172175
if closingSnapshotPoint.Position>0then
173-
letcaretPos= 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 caretPoswhennot(this.HasNoForwardTyping(caretPos, closingSnapshotPoint.Subtract(1)))->true
178+
|_->false
179179
else
180180
false
181181

@@ -200,31 +200,33 @@ type BraceCompletionSession
200200
memberthis.PreBackspace handledCommand=
201201
handledCommand<-false
202202

203-
letcaretPos= getCaretPosition this
204-
letsnapshot= subjectBuffer.CurrentSnapshot
203+
match tryGetCaretPosition thiswith
204+
| Some caretPos->
205+
letsnapshot= 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+
ifcaretPos.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-
letspan= 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+
letspan= 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

229231
member__.PostBackspace()=()
230232

@@ -239,16 +241,16 @@ type BraceCompletionSession
239241

240242
letclosingSnapshotPoint= closingPoint.GetPoint(snapshot)
241243
ifnot this.HasForwardTyping&& session.AllowOverType(this, cancellationToken)then
242-
letcaretPos=getCaretPosition this
244+
letcaretPosOpt=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>0then
248-
250+
| Some caretPoswhen caretPos.Position< closingSnapshotPoint.Position&& closingSnapshotPoint.Position>0->
249251
use undo= this.CreateUndoTransaction()
250252

251-
letspan= SnapshotSpan(caretPos.Value, closingSnapshotPoint.Subtract(1))
253+
letspan= SnapshotSpan(caretPos, closingSnapshotPoint.Subtract(1))
252254

253255
use edit= subjectBuffer.CreateEdit()
254256

@@ -264,6 +266,7 @@ type BraceCompletionSession
264266
this.MoveCaretToClosingPoint()
265267
editorOperations.AddAfterTextBufferChangePrimitive()
266268
undo.Complete()
269+
|_->()
267270

268271
member__.PostOverType()=()
269272

@@ -284,13 +287,13 @@ type BraceCompletionSession
284287
handledCommand<-false
285288

286289
memberthis.PostReturn()=
287-
letcaretPos= getCaretPosition this
288-
289-
if caretPos.HasValuethen
290+
match tryGetCaretPosition thiswith
291+
| Some caretPos->
290292
letclosingSnapshotPoint= 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

295298
member__.Finish()=()
296299

@@ -528,33 +531,24 @@ type BraceCompletionSessionProvider
528531
interface IBraceCompletionSessionProviderwith
529532

530533
member__.TryCreateSession(textView,openingPoint,openingBrace,closingBrace,session)=
531-
lettextSnapshot= openingPoint.Snapshot
532-
533-
letnewSession=
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-
letcancellationToken= CancellationToken.None
542-
543-
match editorSessionFactory.TryCreateSession(document, openingPoint.Position, openingBrace, cancellationToken)with
544-
|null->null
545-
| editorSession->
546-
letundoHistory= 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+
letundoHistory= 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

559553
match sessionwith
560554
|null->false

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp