Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swap maybe for option CEs #1131

Merged
merged 1 commit into from
Jul 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/FsAutoComplete.Core/AbstractClassStubGenerator.fs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ let tryFindAbstractClassExprInBufferAtPos
(pos: Position)
(document: IFSACSourceText)
=
asyncMaybe {
asyncOption {
let! parseResults = codeGenService.ParseFileInProject document.FileName
return! tryFindAbstractClassExprInParsedInput pos parseResults.ParseTree
}
Expand Down
2 changes: 1 addition & 1 deletion src/FsAutoComplete.Core/CodeGeneration.fs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type CodeGenerationService(checker: FSharpCompilerServiceChecker, state: State)
}

override x.GetSymbolAndUseAtPositionOfKind(fileName, pos: Position, kind) =
asyncMaybe {
asyncOption {
let! symbol = (x :> ICodeGenerationService).GetSymbolAtPosition(fileName, pos)

if symbol.Kind = kind then
Expand Down
2 changes: 2 additions & 0 deletions src/FsAutoComplete.Core/FileSystem.fs
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,8 @@ module RoslynSourceText =
new Object() with
override _.ToString() = sourceText.ToString()

override _.Equals(x) = sourceText.Equals(x)

override _.GetHashCode() =
let checksum = sourceText.GetChecksum()

Expand Down
6 changes: 3 additions & 3 deletions src/FsAutoComplete.Core/InlayHints.fs
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ let private rangeOfNamedPat (text: IFSACSourceText) (pat: SynPat) =
match pat with
| SynPat.Named(accessibility = None) -> pat.Range
| SynPat.Named(ident = SynIdent(ident = ident); accessibility = Some(access)) ->
maybe {
option {
let start = ident.idRange.Start
let! line = text.GetLine start

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

let rec tryGetIdent pat =
Expand Down Expand Up @@ -861,7 +861,7 @@ let tryGetDetailedExplicitTypeInfo
(text: IFSACSourceText, parseAndCheck: ParseAndCheckResults)
(pos: Position)
=
maybe {
option {
let! line = text.GetLine pos
let! symbolUse = parseAndCheck.TryGetSymbolUse pos line

Expand Down
6 changes: 3 additions & 3 deletions src/FsAutoComplete.Core/RecordStubGenerator.fs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ type RecordStubsInsertionParams =
Some(fieldInfo, indentColumn, fieldLine)
| _ -> None)

maybe {
option {
let! maxLineIdx =
fieldAndStartColumnAndLineIdxList
|> List.unzip3
Expand Down Expand Up @@ -212,7 +212,7 @@ let walkAndFindRecordBinding (pos, input) =
SyntaxTraversal.Traverse(pos, input, walker)

let tryFindRecordExprInBufferAtPos (codeGenService: ICodeGenerationService) (pos: Position) (document: Document) =
asyncMaybe {
asyncOption {
let! parseResults = codeGenService.ParseFileInProject(document.FullName)

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

let tryFindRecordDefinitionFromPos (codeGenService: ICodeGenerationService) (pos: Position) (document: Document) =
asyncMaybe {
asyncOption {
let! recordExpression, insertionPos = tryFindStubInsertionParamsAtPos codeGenService pos document

let! symbol, symbolUse =
Expand Down
6 changes: 3 additions & 3 deletions src/FsAutoComplete.Core/SignatureHelp.fs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ let private getSignatureHelpForFunctionApplication
endOfPreviousIdentPos: Position,
lines: IFSACSourceText
) : Async<SignatureHelpInfo option> =
asyncMaybe {
asyncOption {
let! lineStr = lines.GetLine endOfPreviousIdentPos

let! possibleApplicationSymbolEnd =
maybe {
option {
if tyRes.GetParseResults.IsPosContainedInApplicationPatched endOfPreviousIdentPos then
let! funcRange = tyRes.GetParseResults.TryRangeOfFunctionOrMethodBeingAppliedPatched endOfPreviousIdentPos
return funcRange.End
Expand Down Expand Up @@ -130,7 +130,7 @@ let private getSignatureHelpForMethod
lines: IFSACSourceText,
triggerChar
) =
asyncMaybe {
asyncOption {
let! paramLocations = tyRes.GetParseResults.FindParameterLocations caretPos
let names = paramLocations.LongId
let lidEnd = paramLocations.LongIdEndLocation
Expand Down
9 changes: 5 additions & 4 deletions src/FsAutoComplete.Core/UnionPatternMatchCaseGenerator.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ open FSharp.Compiler.Text
open FSharp.Compiler.Syntax
open FsAutoComplete.CodeGenerationUtils
open FSharp.Compiler.Symbols
open FsToolkit.ErrorHandling

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

let tryFindPatternMatchExprInBufferAtPos (codeGenService: ICodeGenerationService) (pos: Position) (document: Document) =
asyncMaybe {
asyncOption {
let! parseResults = codeGenService.ParseFileInProject(document.FullName)
let input = parseResults.ParseTree
return! tryFindPatternMatchExprInParsedInput pos input
Expand Down Expand Up @@ -494,7 +495,7 @@ let checkThatPatternMatchExprEndsWithCompleteClause (expr: PatternMatchExpr) =


let tryFindCaseInsertionParamsAtPos (codeGenService: ICodeGenerationService) pos document =
asyncMaybe {
asyncOption {
let! patMatchExpr = tryFindPatternMatchExprInBufferAtPos codeGenService pos document

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

let tryFindUnionDefinitionFromPos (codeGenService: ICodeGenerationService) pos document =
asyncMaybe {
asyncOption {
let! patMatchExpr, insertionParams = tryFindCaseInsertionParamsAtPos codeGenService pos document
let! symbol, symbolUse = codeGenService.GetSymbolAndUseAtPositionOfKind(document.FullName, pos, SymbolKind.Ident)


let! superficialTypeDefinition =
asyncMaybe {
asyncOption {
let! symbolUse = symbolUse

match symbolUse.Symbol with
Expand Down
142 changes: 0 additions & 142 deletions src/FsAutoComplete.Core/Utils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ module ProcessHelper =
()
}



type ResultOrString<'a> = Result<'a, string>

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

// Maybe computation expression builder, copied from ExtCore library
/// https://github.com/jack-pappas/ExtCore/blob/master/ExtCore/Control.fs
[<Sealed>]
type MaybeBuilder() =
// 'T -> M<'T>
[<DebuggerStepThrough>]
member inline __.Return value : 'T option = Some value

// M<'T> -> M<'T>
[<DebuggerStepThrough>]
member inline __.ReturnFrom value : 'T option = value

// unit -> M<'T>
[<DebuggerStepThrough>]
member inline __.Zero() : unit option = Some() // TODO: Should this be None?

// (unit -> M<'T>) -> M<'T>
[<DebuggerStepThrough>]
member __.Delay(f: unit -> 'T option) : 'T option = f ()

// M<'T> -> M<'T> -> M<'T>
// or
// M<unit> -> M<'T> -> M<'T>
[<DebuggerStepThrough>]
member inline __.Combine(r1, r2: 'T option) : 'T option =
match r1 with
| None -> None
| Some() -> r2

// M<'T> * ('T -> M<'U>) -> M<'U>
[<DebuggerStepThrough>]
member inline __.Bind(value, f: 'T -> 'U option) : 'U option = Option.bind f value

// 'T * ('T -> M<'U>) -> M<'U> when 'U :> IDisposable
[<DebuggerStepThrough>]
member __.Using(resource: ('T :> IDisposable), body: _ -> _ option) : _ option =
try
body resource
finally
if not <| obj.ReferenceEquals(null, box resource) then
resource.Dispose()

// (unit -> bool) * M<'T> -> M<'T>
[<DebuggerStepThrough>]
member x.While(guard, body: _ option) : _ option =
if guard () then
// OPTIMIZE: This could be simplified so we don't need to make calls to Bind and While.
x.Bind(body, (fun () -> x.While(guard, body)))
else
x.Zero()

// seq<'T> * ('T -> M<'U>) -> M<'U>
// or
// seq<'T> * ('T -> M<'U>) -> seq<M<'U>>
[<DebuggerStepThrough>]
member x.For(sequence: seq<_>, body: 'T -> unit option) : _ option =
// OPTIMIZE: This could be simplified so we don't need to make calls to Using, While, Delay.
x.Using(sequence.GetEnumerator(), (fun enum -> x.While(enum.MoveNext, x.Delay(fun () -> body enum.Current))))

[<Sealed>]
type AsyncMaybeBuilder() =
[<DebuggerStepThrough>]
member __.Return value : Async<'T option> = Some value |> async.Return

[<DebuggerStepThrough>]
member __.ReturnFrom value : Async<'T option> = value

[<DebuggerStepThrough>]
member __.ReturnFrom(value: 'T option) : Async<'T option> = async.Return value

[<DebuggerStepThrough>]
member __.Zero() : Async<unit option> = Some() |> async.Return

[<DebuggerStepThrough>]
member __.Delay(f: unit -> Async<'T option>) : Async<'T option> = f ()

[<DebuggerStepThrough>]
member __.Combine(r1, r2: Async<'T option>) : Async<'T option> =
async {
let! r1' = r1

match r1' with
| None -> return None
| Some() -> return! r2
}

[<DebuggerStepThrough>]
member __.Bind(value: Async<'T option>, f: 'T -> Async<'U option>) : Async<'U option> =
async {
let! value' = value

match value' with
| None -> return None
| Some result -> return! f result
}

[<DebuggerStepThrough>]
member __.Bind(value: 'T option, f: 'T -> Async<'U option>) : Async<'U option> =
async {
match value with
| None -> return None
| Some result -> return! f result
}

[<DebuggerStepThrough>]
member __.Using(resource: ('T :> IDisposable), body: _ -> Async<_ option>) : Async<_ option> =
try
body resource
finally
if not << isNull <| resource then
resource.Dispose()

[<DebuggerStepThrough>]
member x.While(guard, body: Async<_ option>) : Async<_ option> =
if guard () then
x.Bind(body, (fun () -> x.While(guard, body)))
else
x.Zero()

[<DebuggerStepThrough>]
member x.For(sequence: seq<_>, body: 'T -> Async<unit option>) : Async<_ option> =
x.Using(sequence.GetEnumerator(), (fun enum -> x.While(enum.MoveNext, x.Delay(fun () -> body enum.Current))))

[<DebuggerStepThrough>]
member inline __.TryWith(computation: Async<'T option>, catchHandler: exn -> Async<'T option>) : Async<'T option> =
async.TryWith(computation, catchHandler)

[<DebuggerStepThrough>]
member inline __.TryFinally(computation: Async<'T option>, compensation: unit -> unit) : Async<'T option> =
async.TryFinally(computation, compensation)

[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module AsyncMaybe =
let inline liftAsync (async: Async<'T>) : Async<_ option> = async |> Async.map Some


[<RequireQualifiedAccess>]
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
Expand Down Expand Up @@ -533,9 +396,6 @@ module List =
|> List.groupBy (fst)
|> List.map (fun (key, list) -> key, list |> List.map snd)




[<RequireQualifiedAccess>]
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module String =
Expand Down Expand Up @@ -731,8 +591,6 @@ type Path with

let inline debug msg = Printf.kprintf Debug.WriteLine msg
let inline fail msg = Printf.kprintf Debug.Fail msg
let asyncMaybe = AsyncMaybeBuilder()
let maybe = MaybeBuilder()


let chooseByPrefix (prefix: string) (s: string) =
Expand Down
2 changes: 1 addition & 1 deletion src/FsAutoComplete/CodeFixes/AddExplicitTypeAnnotation.fs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ let private isPositionContainedInUntypedImplicitCtorParameter input pos =
member _.VisitModuleDecl(_, defaultTraverse, decl) =
match decl with
| SynModuleDecl.Types(typeDefns = typeDefns) ->
maybe {
option {
let! ctorArgs =
typeDefns
|> List.tryPick (function
Expand Down