Skip to content

Commit 732ccc8

Browse files
authored
Swap maybe for option CEs (#1131)
1 parent 59b6bb1 commit 732ccc8

File tree

9 files changed

+19
-158
lines changed

9 files changed

+19
-158
lines changed

src/FsAutoComplete.Core/AbstractClassStubGenerator.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ let tryFindAbstractClassExprInBufferAtPos
9898
(pos: Position)
9999
(document: IFSACSourceText)
100100
=
101-
asyncMaybe {
101+
asyncOption {
102102
let! parseResults = codeGenService.ParseFileInProject document.FileName
103103
return! tryFindAbstractClassExprInParsedInput pos parseResults.ParseTree
104104
}

src/FsAutoComplete.Core/CodeGeneration.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ type CodeGenerationService(checker: FSharpCompilerServiceChecker, state: State)
5353
}
5454

5555
override x.GetSymbolAndUseAtPositionOfKind(fileName, pos: Position, kind) =
56-
asyncMaybe {
56+
asyncOption {
5757
let! symbol = (x :> ICodeGenerationService).GetSymbolAtPosition(fileName, pos)
5858

5959
if symbol.Kind = kind then

src/FsAutoComplete.Core/FileSystem.fs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,8 @@ module RoslynSourceText =
523523
new Object() with
524524
override _.ToString() = sourceText.ToString()
525525

526+
override _.Equals(x) = sourceText.Equals(x)
527+
526528
override _.GetHashCode() =
527529
let checksum = sourceText.GetChecksum()
528530

src/FsAutoComplete.Core/InlayHints.fs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ let private rangeOfNamedPat (text: IFSACSourceText) (pat: SynPat) =
556556
match pat with
557557
| SynPat.Named(accessibility = None) -> pat.Range
558558
| SynPat.Named(ident = SynIdent(ident = ident); accessibility = Some(access)) ->
559-
maybe {
559+
option {
560560
let start = ident.idRange.Start
561561
let! line = text.GetLine start
562562

@@ -706,7 +706,7 @@ let tryGetExplicitTypeInfo (text: IFSACSourceText, ast: ParsedInput) (pos: Posit
706706
// * `let f2 = fun (Value v) -> v + 1`
707707
// -> compiler generated `_arg1` in `args`,
708708
// and `v` is inside match expression in `body` & `parsedData` (-> `SynPat` )
709-
maybe {
709+
option {
710710
let! pat = pats |> List.tryFind (fun p -> rangeContainsPos p.Range pos)
711711

712712
let rec tryGetIdent pat =
@@ -861,7 +861,7 @@ let tryGetDetailedExplicitTypeInfo
861861
(text: IFSACSourceText, parseAndCheck: ParseAndCheckResults)
862862
(pos: Position)
863863
=
864-
maybe {
864+
option {
865865
let! line = text.GetLine pos
866866
let! symbolUse = parseAndCheck.TryGetSymbolUse pos line
867867

src/FsAutoComplete.Core/RecordStubGenerator.fs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ type RecordStubsInsertionParams =
7272
Some(fieldInfo, indentColumn, fieldLine)
7373
| _ -> None)
7474

75-
maybe {
75+
option {
7676
let! maxLineIdx =
7777
fieldAndStartColumnAndLineIdxList
7878
|> List.unzip3
@@ -212,7 +212,7 @@ let walkAndFindRecordBinding (pos, input) =
212212
SyntaxTraversal.Traverse(pos, input, walker)
213213

214214
let tryFindRecordExprInBufferAtPos (codeGenService: ICodeGenerationService) (pos: Position) (document: Document) =
215-
asyncMaybe {
215+
asyncOption {
216216
let! parseResults = codeGenService.ParseFileInProject(document.FullName)
217217

218218
let! found = walkAndFindRecordBinding (pos, parseResults.ParseTree)
@@ -270,7 +270,7 @@ let shouldGenerateRecordStub (recordExpr: RecordExpr) (entity: FSharpEntity) =
270270
fieldCount > 0 && writtenFieldCount < fieldCount
271271

272272
let tryFindRecordDefinitionFromPos (codeGenService: ICodeGenerationService) (pos: Position) (document: Document) =
273-
asyncMaybe {
273+
asyncOption {
274274
let! recordExpression, insertionPos = tryFindStubInsertionParamsAtPos codeGenService pos document
275275

276276
let! symbol, symbolUse =

src/FsAutoComplete.Core/SignatureHelp.fs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ let private getSignatureHelpForFunctionApplication
3333
endOfPreviousIdentPos: Position,
3434
lines: IFSACSourceText
3535
) : Async<SignatureHelpInfo option> =
36-
asyncMaybe {
36+
asyncOption {
3737
let! lineStr = lines.GetLine endOfPreviousIdentPos
3838

3939
let! possibleApplicationSymbolEnd =
40-
maybe {
40+
option {
4141
if tyRes.GetParseResults.IsPosContainedInApplicationPatched endOfPreviousIdentPos then
4242
let! funcRange = tyRes.GetParseResults.TryRangeOfFunctionOrMethodBeingAppliedPatched endOfPreviousIdentPos
4343
return funcRange.End
@@ -130,7 +130,7 @@ let private getSignatureHelpForMethod
130130
lines: IFSACSourceText,
131131
triggerChar
132132
) =
133-
asyncMaybe {
133+
asyncOption {
134134
let! paramLocations = tyRes.GetParseResults.FindParameterLocations caretPos
135135
let names = paramLocations.LongId
136136
let lidEnd = paramLocations.LongIdEndLocation

src/FsAutoComplete.Core/UnionPatternMatchCaseGenerator.fs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ open FSharp.Compiler.Text
88
open FSharp.Compiler.Syntax
99
open FsAutoComplete.CodeGenerationUtils
1010
open FSharp.Compiler.Symbols
11+
open FsToolkit.ErrorHandling
1112

1213
[<NoEquality; NoComparison>]
1314
type PatternMatchExpr =
@@ -375,7 +376,7 @@ let shouldGenerateUnionPatternMatchCases (patMatchExpr: PatternMatchExpr) (entit
375376
caseCount > 0 && writtenCaseCount < caseCount
376377

377378
let tryFindPatternMatchExprInBufferAtPos (codeGenService: ICodeGenerationService) (pos: Position) (document: Document) =
378-
asyncMaybe {
379+
asyncOption {
379380
let! parseResults = codeGenService.ParseFileInProject(document.FullName)
380381
let input = parseResults.ParseTree
381382
return! tryFindPatternMatchExprInParsedInput pos input
@@ -494,7 +495,7 @@ let checkThatPatternMatchExprEndsWithCompleteClause (expr: PatternMatchExpr) =
494495

495496

496497
let tryFindCaseInsertionParamsAtPos (codeGenService: ICodeGenerationService) pos document =
497-
asyncMaybe {
498+
asyncOption {
498499
let! patMatchExpr = tryFindPatternMatchExprInBufferAtPos codeGenService pos document
499500

500501
if checkThatPatternMatchExprEndsWithCompleteClause patMatchExpr then
@@ -505,13 +506,13 @@ let tryFindCaseInsertionParamsAtPos (codeGenService: ICodeGenerationService) pos
505506
}
506507

507508
let tryFindUnionDefinitionFromPos (codeGenService: ICodeGenerationService) pos document =
508-
asyncMaybe {
509+
asyncOption {
509510
let! patMatchExpr, insertionParams = tryFindCaseInsertionParamsAtPos codeGenService pos document
510511
let! symbol, symbolUse = codeGenService.GetSymbolAndUseAtPositionOfKind(document.FullName, pos, SymbolKind.Ident)
511512

512513

513514
let! superficialTypeDefinition =
514-
asyncMaybe {
515+
asyncOption {
515516
let! symbolUse = symbolUse
516517

517518
match symbolUse.Symbol with

src/FsAutoComplete.Core/Utils.fs

Lines changed: 0 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,6 @@ module ProcessHelper =
7676
()
7777
}
7878

79-
80-
8179
type ResultOrString<'a> = Result<'a, string>
8280

8381
type Serializer = obj -> string
@@ -251,141 +249,6 @@ module AsyncResult =
251249
let inline bimap okF errF r = Async.map (Result.bimap okF errF) r
252250
let inline ofOption recover o = Async.map (Result.ofOption recover) o
253251

254-
// Maybe computation expression builder, copied from ExtCore library
255-
/// https://github.com/jack-pappas/ExtCore/blob/master/ExtCore/Control.fs
256-
[<Sealed>]
257-
type MaybeBuilder() =
258-
// 'T -> M<'T>
259-
[<DebuggerStepThrough>]
260-
member inline __.Return value : 'T option = Some value
261-
262-
// M<'T> -> M<'T>
263-
[<DebuggerStepThrough>]
264-
member inline __.ReturnFrom value : 'T option = value
265-
266-
// unit -> M<'T>
267-
[<DebuggerStepThrough>]
268-
member inline __.Zero() : unit option = Some() // TODO: Should this be None?
269-
270-
// (unit -> M<'T>) -> M<'T>
271-
[<DebuggerStepThrough>]
272-
member __.Delay(f: unit -> 'T option) : 'T option = f ()
273-
274-
// M<'T> -> M<'T> -> M<'T>
275-
// or
276-
// M<unit> -> M<'T> -> M<'T>
277-
[<DebuggerStepThrough>]
278-
member inline __.Combine(r1, r2: 'T option) : 'T option =
279-
match r1 with
280-
| None -> None
281-
| Some() -> r2
282-
283-
// M<'T> * ('T -> M<'U>) -> M<'U>
284-
[<DebuggerStepThrough>]
285-
member inline __.Bind(value, f: 'T -> 'U option) : 'U option = Option.bind f value
286-
287-
// 'T * ('T -> M<'U>) -> M<'U> when 'U :> IDisposable
288-
[<DebuggerStepThrough>]
289-
member __.Using(resource: ('T :> IDisposable), body: _ -> _ option) : _ option =
290-
try
291-
body resource
292-
finally
293-
if not <| obj.ReferenceEquals(null, box resource) then
294-
resource.Dispose()
295-
296-
// (unit -> bool) * M<'T> -> M<'T>
297-
[<DebuggerStepThrough>]
298-
member x.While(guard, body: _ option) : _ option =
299-
if guard () then
300-
// OPTIMIZE: This could be simplified so we don't need to make calls to Bind and While.
301-
x.Bind(body, (fun () -> x.While(guard, body)))
302-
else
303-
x.Zero()
304-
305-
// seq<'T> * ('T -> M<'U>) -> M<'U>
306-
// or
307-
// seq<'T> * ('T -> M<'U>) -> seq<M<'U>>
308-
[<DebuggerStepThrough>]
309-
member x.For(sequence: seq<_>, body: 'T -> unit option) : _ option =
310-
// OPTIMIZE: This could be simplified so we don't need to make calls to Using, While, Delay.
311-
x.Using(sequence.GetEnumerator(), (fun enum -> x.While(enum.MoveNext, x.Delay(fun () -> body enum.Current))))
312-
313-
[<Sealed>]
314-
type AsyncMaybeBuilder() =
315-
[<DebuggerStepThrough>]
316-
member __.Return value : Async<'T option> = Some value |> async.Return
317-
318-
[<DebuggerStepThrough>]
319-
member __.ReturnFrom value : Async<'T option> = value
320-
321-
[<DebuggerStepThrough>]
322-
member __.ReturnFrom(value: 'T option) : Async<'T option> = async.Return value
323-
324-
[<DebuggerStepThrough>]
325-
member __.Zero() : Async<unit option> = Some() |> async.Return
326-
327-
[<DebuggerStepThrough>]
328-
member __.Delay(f: unit -> Async<'T option>) : Async<'T option> = f ()
329-
330-
[<DebuggerStepThrough>]
331-
member __.Combine(r1, r2: Async<'T option>) : Async<'T option> =
332-
async {
333-
let! r1' = r1
334-
335-
match r1' with
336-
| None -> return None
337-
| Some() -> return! r2
338-
}
339-
340-
[<DebuggerStepThrough>]
341-
member __.Bind(value: Async<'T option>, f: 'T -> Async<'U option>) : Async<'U option> =
342-
async {
343-
let! value' = value
344-
345-
match value' with
346-
| None -> return None
347-
| Some result -> return! f result
348-
}
349-
350-
[<DebuggerStepThrough>]
351-
member __.Bind(value: 'T option, f: 'T -> Async<'U option>) : Async<'U option> =
352-
async {
353-
match value with
354-
| None -> return None
355-
| Some result -> return! f result
356-
}
357-
358-
[<DebuggerStepThrough>]
359-
member __.Using(resource: ('T :> IDisposable), body: _ -> Async<_ option>) : Async<_ option> =
360-
try
361-
body resource
362-
finally
363-
if not << isNull <| resource then
364-
resource.Dispose()
365-
366-
[<DebuggerStepThrough>]
367-
member x.While(guard, body: Async<_ option>) : Async<_ option> =
368-
if guard () then
369-
x.Bind(body, (fun () -> x.While(guard, body)))
370-
else
371-
x.Zero()
372-
373-
[<DebuggerStepThrough>]
374-
member x.For(sequence: seq<_>, body: 'T -> Async<unit option>) : Async<_ option> =
375-
x.Using(sequence.GetEnumerator(), (fun enum -> x.While(enum.MoveNext, x.Delay(fun () -> body enum.Current))))
376-
377-
[<DebuggerStepThrough>]
378-
member inline __.TryWith(computation: Async<'T option>, catchHandler: exn -> Async<'T option>) : Async<'T option> =
379-
async.TryWith(computation, catchHandler)
380-
381-
[<DebuggerStepThrough>]
382-
member inline __.TryFinally(computation: Async<'T option>, compensation: unit -> unit) : Async<'T option> =
383-
async.TryFinally(computation, compensation)
384-
385-
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
386-
module AsyncMaybe =
387-
let inline liftAsync (async: Async<'T>) : Async<_ option> = async |> Async.map Some
388-
389252

390253
[<RequireQualifiedAccess>]
391254
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
@@ -533,9 +396,6 @@ module List =
533396
|> List.groupBy (fst)
534397
|> List.map (fun (key, list) -> key, list |> List.map snd)
535398

536-
537-
538-
539399
[<RequireQualifiedAccess>]
540400
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
541401
module String =
@@ -731,8 +591,6 @@ type Path with
731591

732592
let inline debug msg = Printf.kprintf Debug.WriteLine msg
733593
let inline fail msg = Printf.kprintf Debug.Fail msg
734-
let asyncMaybe = AsyncMaybeBuilder()
735-
let maybe = MaybeBuilder()
736594

737595

738596
let chooseByPrefix (prefix: string) (s: string) =

src/FsAutoComplete/CodeFixes/AddExplicitTypeAnnotation.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ let private isPositionContainedInUntypedImplicitCtorParameter input pos =
3030
member _.VisitModuleDecl(_, defaultTraverse, decl) =
3131
match decl with
3232
| SynModuleDecl.Types(typeDefns = typeDefns) ->
33-
maybe {
33+
option {
3434
let! ctorArgs =
3535
typeDefns
3636
|> List.tryPick (function

0 commit comments

Comments
 (0)