-
Notifications
You must be signed in to change notification settings - Fork 790
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
Add editor formatting service to auto-deindent closing brackets #3313
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9588eb3
Add editor formatting service for auto-deindent
saul bbf8eae
Minor refactor of the indentation service - do not indent after 'func…
saul a7784be
Only use smart indentation if indent style is set to 'Smart'
saul 7661490
Fix broken unit test build
saul b3d1357
Merge remote-tracking branch 'upstream/master' into auto-deindent
saul 6fb0534
Implement review comments, fix build
saul 6e5debc
Fix some broken brace matching tests
saul 4259c7c
Fix failing indentation tests
saul 4cd00d5
Add formatting service tests
saul b4781b6
Add more brace matching tests
saul File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
vsintegration/src/FSharp.Editor/Formatting/EditorFormattingService.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
namespace Microsoft.VisualStudio.FSharp.Editor | ||
|
||
open System.Composition | ||
open System.Collections.Generic | ||
|
||
open Microsoft.CodeAnalysis | ||
open Microsoft.CodeAnalysis.Editor | ||
open Microsoft.CodeAnalysis.Formatting | ||
open Microsoft.CodeAnalysis.Host.Mef | ||
open Microsoft.CodeAnalysis.Text | ||
|
||
open Microsoft.FSharp.Compiler.SourceCodeServices | ||
open System.Threading | ||
|
||
[<Shared>] | ||
[<ExportLanguageService(typeof<IEditorFormattingService>, FSharpConstants.FSharpLanguageName)>] | ||
type internal FSharpEditorFormattingService | ||
[<ImportingConstructor>] | ||
( | ||
checkerProvider: FSharpCheckerProvider, | ||
projectInfoManager: FSharpProjectOptionsManager | ||
) = | ||
|
||
static member GetFormattingChanges(documentId: DocumentId, sourceText: SourceText, filePath: string, checker: FSharpChecker, indentStyle: FormattingOptions.IndentStyle, projectOptions: FSharpProjectOptions option, position: int) = | ||
// Logic for determining formatting changes: | ||
// If first token on the current line is a closing brace, | ||
// match the indent with the indent on the line that opened it | ||
|
||
asyncMaybe { | ||
|
||
// Gate formatting on whether smart indentation is enabled | ||
// (this is what C# does) | ||
do! Option.guard (indentStyle = FormattingOptions.IndentStyle.Smart) | ||
|
||
let! projectOptions = projectOptions | ||
|
||
let line = sourceText.Lines.[sourceText.Lines.IndexOf position] | ||
|
||
let defines = CompilerEnvironment.GetCompilationDefinesForEditing(filePath, projectOptions.OtherOptions |> List.ofArray) | ||
|
||
let tokens = Tokenizer.tokenizeLine(documentId, sourceText, line.Start, filePath, defines) | ||
|
||
let! firstMeaningfulToken = | ||
tokens | ||
|> List.tryFind (fun x -> | ||
x.Tag <> FSharpTokenTag.WHITESPACE && | ||
x.Tag <> FSharpTokenTag.COMMENT && | ||
x.Tag <> FSharpTokenTag.LINE_COMMENT) | ||
|
||
let! (left, right) = | ||
FSharpBraceMatchingService.GetBraceMatchingResult(checker, sourceText, filePath, projectOptions, position, "FormattingService") | ||
|
||
if right.StartColumn = firstMeaningfulToken.LeftColumn then | ||
// Replace the indentation on this line with the indentation of the left bracket | ||
let! leftSpan = RoslynHelpers.TryFSharpRangeToTextSpan(sourceText, left) | ||
|
||
let indentChars (line : TextLine) = | ||
line.ToString() | ||
|> Seq.takeWhile ((=) ' ') | ||
|> Seq.length | ||
|
||
let startIndent = indentChars sourceText.Lines.[sourceText.Lines.IndexOf leftSpan.Start] | ||
let currentIndent = indentChars line | ||
|
||
return TextChange(TextSpan(line.Start, currentIndent), String.replicate startIndent " ") | ||
else | ||
return! None | ||
} | ||
|
||
member __.GetFormattingChangesAsync (document: Document, position: int, cancellationToken: CancellationToken) = | ||
async { | ||
let! sourceText = document.GetTextAsync(cancellationToken) |> Async.AwaitTask | ||
let! options = document.GetOptionsAsync(cancellationToken) |> Async.AwaitTask | ||
let indentStyle = options.GetOption(FormattingOptions.SmartIndent, FSharpConstants.FSharpLanguageName) | ||
let projectOptionsOpt = projectInfoManager.TryGetOptionsForEditingDocumentOrProject document | ||
let! textChange = FSharpEditorFormattingService.GetFormattingChanges(document.Id, sourceText, document.FilePath, checkerProvider.Checker, indentStyle, projectOptionsOpt, position) | ||
|
||
return | ||
match textChange with | ||
| Some change -> | ||
ResizeArray([change]) :> IList<_> | ||
|
||
| None -> | ||
ResizeArray() :> IList<_> | ||
} | ||
|
||
interface IEditorFormattingService with | ||
member val SupportsFormatDocument = false | ||
member val SupportsFormatSelection = false | ||
member val SupportsFormatOnPaste = false | ||
member val SupportsFormatOnReturn = true | ||
|
||
override __.SupportsFormattingOnTypedCharacter (document, ch) = | ||
if FSharpIndentationService.IsSmartIndentEnabled document.Project.Solution.Workspace.Options then | ||
match ch with | ||
| ')' | ']' | '}' -> true | ||
| _ -> false | ||
else | ||
false | ||
|
||
override __.GetFormattingChangesAsync (_document, _span, cancellationToken) = | ||
async { return ResizeArray() :> IList<_> } | ||
|> RoslynHelpers.StartAsyncAsTask cancellationToken | ||
|
||
override __.GetFormattingChangesOnPasteAsync (_document, _span, cancellationToken) = | ||
async { return ResizeArray() :> IList<_> } | ||
|> RoslynHelpers.StartAsyncAsTask cancellationToken | ||
|
||
override this.GetFormattingChangesAsync (document, _typedChar, position, cancellationToken) = | ||
this.GetFormattingChangesAsync (document, position, cancellationToken) | ||
|> RoslynHelpers.StartAsyncAsTask cancellationToken | ||
|
||
override this.GetFormattingChangesOnReturnAsync (document, position, cancellationToken) = | ||
this.GetFormattingChangesAsync (document, position, cancellationToken) | ||
|> RoslynHelpers.StartAsyncAsTask cancellationToken |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above, or perhaps check this before hand if a
startIndent
of0
would cause trouble?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tested with startIndent 0 and everything's fine :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍