@@ -76,43 +76,49 @@ type internal AddReturnType [<ImportingConstructor>] () =
7676
7777 override _.ComputeRefactoringsAsync context =
7878 cancellableTask {
79- let document = context.Document
80- let position = context.Span.Start
81- let! sourceText = document.GetTextAsync()
82- let textLine = sourceText.Lines.GetLineFromPosition position
83- let textLinePos = sourceText.Lines.GetLinePosition position
84- let fcsTextLineNumber = Line.fromZ textLinePos.Line
85-
86- let! lexerSymbol =
87- document.TryFindFSharpLexerSymbolAsync( position, SymbolLookupKind.Greedy, false , false , nameof ( AddReturnType))
88-
89- let! ( parseFileResults , checkFileResults ) = document.GetFSharpParseAndCheckResultsAsync( nameof ( AddReturnType))
90-
91- let symbolUseOpt =
92- lexerSymbol
93- |> Option.bind ( fun lexer ->
94- checkFileResults.GetSymbolUseAtLocation(
95- fcsTextLineNumber,
96- lexer.Ident.idRange.EndColumn,
97- textLine.ToString(),
98- lexer.FullIsland
99- ))
100-
101- let memberFuncOpt =
102- symbolUseOpt
103- |> Option.bind ( fun sym -> sym.Symbol |> AddReturnType.ofFSharpMemberOrFunctionOrValue)
104-
105- match ( symbolUseOpt, memberFuncOpt) with
106- | ( Some symbolUse, Some memberFunc) ->
107- let isValidMethod =
108- memberFunc
109- |> AddReturnType.isValidMethodWithoutTypeAnnotation symbolUse parseFileResults
110-
111- match isValidMethod with
112- | Some( memberFunc, typeRange) -> do AddReturnType.refactor context ( memberFunc, typeRange, symbolUse)
113- | None -> ()
114- | _ -> ()
115-
116- return ()
79+ try
80+ let document = context.Document
81+ let position = context.Span.Start
82+ let! sourceText = document.GetTextAsync()
83+ let textLine = sourceText.Lines.GetLineFromPosition position
84+ let textLinePos = sourceText.Lines.GetLinePosition position
85+ let fcsTextLineNumber = Line.fromZ textLinePos.Line
86+
87+ let! lexerSymbol =
88+ document.TryFindFSharpLexerSymbolAsync( position, SymbolLookupKind.Greedy, false , false , nameof ( AddReturnType))
89+
90+ let! ( parseFileResults , checkFileResults ) = document.GetFSharpParseAndCheckResultsAsync( nameof ( AddReturnType))
91+
92+ let symbolUseOpt =
93+ lexerSymbol
94+ |> Option.bind ( fun lexer ->
95+ checkFileResults.GetSymbolUseAtLocation(
96+ fcsTextLineNumber,
97+ lexer.Ident.idRange.EndColumn,
98+ textLine.ToString(),
99+ lexer.FullIsland
100+ ))
101+
102+ let memberFuncOpt =
103+ symbolUseOpt
104+ |> Option.bind ( fun sym -> sym.Symbol |> AddReturnType.ofFSharpMemberOrFunctionOrValue)
105+
106+ match ( symbolUseOpt, memberFuncOpt) with
107+ | ( Some symbolUse, Some memberFunc) ->
108+ let isValidMethod =
109+ memberFunc
110+ |> AddReturnType.isValidMethodWithoutTypeAnnotation symbolUse parseFileResults
111+
112+ match isValidMethod with
113+ | Some( memberFunc, typeRange) -> do AddReturnType.refactor context ( memberFunc, typeRange, symbolUse)
114+ | None -> ()
115+ | _ -> ()
116+
117+ return ()
118+ with _ ->
119+ // File is not part of the project yet, or project options are not ready.
120+ // This can happen when files are added/copied before the project system updates.
121+ // Just return without offering any refactorings.
122+ return ()
117123 }
118124 |> CancellableTask.startAsTask context.CancellationToken
0 commit comments