Skip to content

Commit

Permalink
Merge branch 'dungpa-indentation'
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinRansom committed Dec 14, 2016
2 parents 0fe9edd + cab8c4f commit fe7f859
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
36 changes: 24 additions & 12 deletions vsintegration/src/FSharp.Editor/Utilities/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)
| _ -> 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

0 comments on commit fe7f859

Please sign in to comment.