Skip to content
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
36 changes: 24 additions & 12 deletions vsintegration/src/FSharp.Editor/IndentationService.fs
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,33 @@ open Microsoft.FSharp.Compiler.SourceCodeServices
[<ExportLanguageService(typeof<ISynchronousIndentationService>, FSharpCommonConstants.FSharpLanguageName)>]
type internal FSharpIndentationService() =

static member GetDesiredIndentation(sourceText: SourceText, lineNumber: int, tabSize: int): Option<int> =
static member GetDesiredIndentation(sourceText: SourceText, lineNumber: int, tabSize: int): Option<int> =
// Match indentation with previous line
let rec tryFindPreviousNonEmptyLine l =
if l <= 0 then
None
else
let previousLine = sourceText.Lines.[l - 1]
if not (String.IsNullOrEmpty(previousLine.ToString())) then
Some previousLine
else
tryFindPreviousNonEmptyLine (l - 1)
// No indentation on the first line of a document
if lineNumber = 0 then
// No indentation on the first line of a document
None
else
// Match indentation with previous line
let previousLine = sourceText.Lines.[lineNumber - 1]
let rec loop column spaces =
if previousLine.Start + column >= previousLine.End then
spaces
else match previousLine.Text.[previousLine.Start + column] with
| ' ' -> loop (column + 1) (spaces + 1)
| '\t' -> loop (column + 1) (((spaces / tabSize) + 1) * tabSize)
| _ -> spaces
Some(loop 0 0)
match tryFindPreviousNonEmptyLine lineNumber with
| None -> Some 0
| Some previousLine ->
let rec loop column spaces =
if previousLine.Start + column >= previousLine.End then
spaces
else
match previousLine.Text.[previousLine.Start + column] with
| ' ' -> loop (column + 1) (spaces + 1)
| '\t' -> loop (column + 1) (((spaces / tabSize) + 1) * tabSize)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we allow \t to appear in an F# source file in the editor? is it not immediately turned into spaces?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm perhaps we do in the not light mode. never mind.

| _ -> spaces
Some (loop 0 0)

interface ISynchronousIndentationService with
member this.GetDesiredIndentation(document: Document, lineNumber: int, cancellationToken: CancellationToken): Nullable<IndentationResult> =
Expand Down
5 changes: 4 additions & 1 deletion vsintegration/tests/unittests/IndentationServiceTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ type Class1() =
static let nestedTypesTemplate = "
namespace testspace
type testtype
static member testmember = 1"
static member testmember = 1

"

static member private testCases: Object[][] = [|
[| None; 0; consoleProjectTemplate |]
Expand All @@ -59,6 +61,7 @@ namespace testspace
[| Some(0); 2; nestedTypesTemplate |]
[| Some(4); 3; nestedTypesTemplate |]
[| Some(8); 4; nestedTypesTemplate |]
[| Some(8); 5; nestedTypesTemplate |]
|]

[<TestCaseSource("testCases")>]
Expand Down