@@ -30,21 +30,33 @@ open Microsoft.FSharp.Compiler.SourceCodeServices
3030[<ExportLanguageService( typeof< ISynchronousIndentationService>, FSharpCommonConstants.FSharpLanguageName) >]
3131type internal FSharpIndentationService () =
3232
33- static member GetDesiredIndentation ( sourceText : SourceText , lineNumber : int , tabSize : int ): Option < int > =
33+ static member GetDesiredIndentation ( sourceText : SourceText , lineNumber : int , tabSize : int ): Option < int > =
34+ // Match indentation with previous line
35+ let rec tryFindPreviousNonEmptyLine l =
36+ if l <= 0 then
37+ None
38+ else
39+ let previousLine = sourceText.Lines.[ l - 1 ]
40+ if not ( String.IsNullOrEmpty( previousLine.ToString())) then
41+ Some previousLine
42+ else
43+ tryFindPreviousNonEmptyLine ( l - 1 )
44+ // No indentation on the first line of a document
3445 if lineNumber = 0 then
35- // No indentation on the first line of a document
3646 None
3747 else
38- // Match indentation with previous line
39- let previousLine = sourceText.Lines.[ lineNumber - 1 ]
40- let rec loop column spaces =
41- if previousLine.Start + column >= previousLine.End then
42- spaces
43- else match previousLine.Text.[ previousLine.Start + column] with
44- | ' ' -> loop ( column + 1 ) ( spaces + 1 )
45- | '\t' -> loop ( column + 1 ) ((( spaces / tabSize) + 1 ) * tabSize)
46- | _ -> spaces
47- Some( loop 0 0 )
48+ match tryFindPreviousNonEmptyLine lineNumber with
49+ | None -> Some 0
50+ | Some previousLine ->
51+ let rec loop column spaces =
52+ if previousLine.Start + column >= previousLine.End then
53+ spaces
54+ else
55+ match previousLine.Text.[ previousLine.Start + column] with
56+ | ' ' -> loop ( column + 1 ) ( spaces + 1 )
57+ | '\t' -> loop ( column + 1 ) ((( spaces / tabSize) + 1 ) * tabSize)
58+ | _ -> spaces
59+ Some ( loop 0 0 )
4860
4961 interface ISynchronousIndentationService with
5062 member this.GetDesiredIndentation ( document : Document , lineNumber : int , cancellationToken : CancellationToken ): Nullable < IndentationResult > =
0 commit comments