-
Notifications
You must be signed in to change notification settings - Fork 57
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
Fantomas: support cursor api #484
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4288b90
wip
DedSec256 00ee256
wip
DedSec256 640ac37
cleanup
DedSec256 fe0ef02
fix
DedSec256 c81f67b
simplify test data
DedSec256 cafb480
fix
DedSec256 6244744
add comment
DedSec256 9877c89
fix
DedSec256 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
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 |
---|---|---|
|
@@ -4,6 +4,7 @@ open JetBrains.Application.Infra | |
open JetBrains.Diagnostics | ||
open JetBrains.DocumentModel | ||
open JetBrains.DocumentModel.Impl | ||
open JetBrains.Lifetimes | ||
open JetBrains.ProjectModel | ||
open JetBrains.ReSharper.Feature.Services.CodeCleanup | ||
open JetBrains.ReSharper.Plugins.FSharp.Psi | ||
|
@@ -12,10 +13,11 @@ open JetBrains.ReSharper.Psi | |
open JetBrains.ReSharper.Psi.Tree | ||
open JetBrains.ReSharper.Psi.Util | ||
open JetBrains.ReSharper.Resources.Shell | ||
open JetBrains.TextControl | ||
open JetBrains.Util.Text | ||
|
||
[<CodeCleanupModule>] | ||
type FSharpReformatCode() = | ||
type FSharpReformatCode(textControlManager: ITextControlManager) = | ||
let REFORMAT_CODE_DESCRIPTOR = CodeCleanupOptionDescriptor<bool>( | ||
"FSReformatCode", | ||
CodeCleanupLanguage("F#", 2), | ||
|
@@ -33,7 +35,7 @@ type FSharpReformatCode() = | |
| CodeCleanupService.DefaultProfileType.REFORMAT | ||
| CodeCleanupService.DefaultProfileType.CODE_STYLE -> | ||
profile.SetSetting<bool>(REFORMAT_CODE_DESCRIPTOR, true) | ||
| _ -> | ||
| _ -> | ||
Assertion.Fail($"Unexpected cleanup profile type: {nameof(profileType)}") | ||
|
||
member x.IsAvailable(sourceFile: IPsiSourceFile) = | ||
|
@@ -65,23 +67,33 @@ type FSharpReformatCode() = | |
let newLineText = sourceFile.DetectLineEnding().GetPresentation() | ||
let parsingOptions = fsFile.CheckerService.FcsProjectProvider.GetParsingOptions(sourceFile) | ||
|
||
let change = | ||
if isNotNull rangeMarker then | ||
try | ||
let range = ofDocumentRange rangeMarker.DocumentRange | ||
let formatted = | ||
fantomasHost.FormatSelection(filePath, range, text, settings, parsingOptions, newLineText) | ||
let offset = rangeMarker.DocumentRange.StartOffset.Offset | ||
let oldLength = rangeMarker.DocumentRange.Length | ||
Some(DocumentChange(document, offset, oldLength, formatted, stamp, modificationSide)) | ||
with _ -> None | ||
else | ||
let formatted = fantomasHost.FormatDocument(filePath, text, settings, parsingOptions, newLineText) | ||
Some(DocumentChange(document, 0, text.Length, formatted, stamp, modificationSide)) | ||
if isNotNull rangeMarker then | ||
try | ||
let range = ofDocumentRange rangeMarker.DocumentRange | ||
let formatted = fantomasHost.FormatSelection(filePath, range, text, settings, parsingOptions, newLineText) | ||
let offset = rangeMarker.DocumentRange.StartOffset.Offset | ||
let oldLength = rangeMarker.DocumentRange.Length | ||
let documentChange = DocumentChange(document, offset, oldLength, formatted, stamp, modificationSide) | ||
use _ = WriteLockCookie.Create() | ||
document.ChangeDocument(documentChange, TimeStamp.NextValue) | ||
sourceFile.GetPsiServices().Files.CommitAllDocuments() | ||
with _ -> () | ||
else | ||
let textControl = textControlManager.VisibleTextControls |> Seq.tryFind (fun c -> c.Document == document) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if there are multiple editors with the same document? This check seems to ignore which editor was actually focused before the reformat. Please try to use data contexts or other ways to get the proper text control. |
||
let cursorPosition = textControl |> Option.map (fun c -> c.Caret.Position.Value.ToDocLineColumn()) | ||
let formatResult = fantomasHost.FormatDocument(filePath, text, settings, parsingOptions, newLineText, cursorPosition) | ||
let newCursorPosition = formatResult.CursorPosition | ||
|
||
match change with | ||
| Some(change) -> | ||
use cookie = WriteLockCookie.Create() | ||
document.ChangeDocument(change, TimeStamp.NextValue) | ||
document.ReplaceText(document.DocumentRange, formatResult.Code) | ||
sourceFile.GetPsiServices().Files.CommitAllDocuments() | ||
| _ -> () | ||
|
||
if isNull newCursorPosition then () else | ||
|
||
// move cursor after current document transaction | ||
let moveCursorLifetime = new LifetimeDefinition() | ||
let codeCleanupService = solution.GetComponent<CodeCleanupService>() | ||
codeCleanupService.WholeFileCleanupCompletedAfterSave.Advise(moveCursorLifetime.Lifetime, fun _ -> | ||
moveCursorLifetime.Terminate() | ||
textControl.Value.Caret.MoveTo(docLine newCursorPosition.Row, | ||
docColumn newCursorPosition.Column, | ||
CaretVisualPlacement.Generic)) |
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
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
29 changes: 29 additions & 0 deletions
29
...as/FantomasRunOptionsTest/local tool 6_0 with cursor/gold/local tool 6_0 with cursor.gold
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,29 @@ | ||
namespace JetBrains.ReSharper.Plugins.FSharp.Services.Formatter | ||
|
||
[<CodeCleanupModule>] | ||
type FSharpReformatCode(textControlManager: ITextControlManager) = | ||
member x.Process(sourceFile, rangeMarker, _, _, _) = | ||
if isNotNull rangeMarker then | ||
try<caret> | ||
let range = ofDocumentRange rangeMarker.DocumentRange | ||
|
||
let formatted = | ||
fantomasHost.FormatSelection(filePath, range, text, settings, parsingOptions, newLineText) | ||
|
||
let offset = rangeMarker.DocumentRange.StartOffset.Offset | ||
let oldLength = rangeMarker.DocumentRange.Length | ||
|
||
let documentChange = | ||
DocumentChange(document, offset, oldLength, formatted, stamp, modificationSide) | ||
|
||
use _ = WriteLockCookie.Create() | ||
document.ChangeDocument(documentChange, TimeStamp.NextValue) | ||
sourceFile.GetPsiServices().Files.CommitAllDocuments() | ||
with _ -> | ||
() | ||
else | ||
let textControl = | ||
textControlManager.VisibleTextControls | ||
|> Seq.find (fun c -> c.Document == document) | ||
|
||
cursorPosition = textControl.Caret.Position.Value.ToDocLineColumn() |
19 changes: 19 additions & 0 deletions
19
...p/testData/fantomas/FantomasRunOptionsTest/local tool 6_0 with cursor/source/LargeFile.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,19 @@ | ||
namespace JetBrains.ReSharper.Plugins.FSharp.Services.Formatter | ||
|
||
[<CodeCleanupModule>] | ||
type FSharpReformatCode(textControlManager: ITextControlManager) = | ||
member x.Process(sourceFile, rangeMarker, _, _, _) = | ||
if isNotNull rangeMarker then | ||
try<caret> | ||
let range = ofDocumentRange rangeMarker.DocumentRange | ||
let formatted = fantomasHost.FormatSelection(filePath, range, text, settings, parsingOptions, newLineText) | ||
let offset = rangeMarker.DocumentRange.StartOffset.Offset | ||
let oldLength = rangeMarker.DocumentRange.Length | ||
let documentChange = DocumentChange(document, offset, oldLength, formatted, stamp, modificationSide) | ||
use _ = WriteLockCookie.Create() | ||
document.ChangeDocument(documentChange, TimeStamp.NextValue) | ||
sourceFile.GetPsiServices().Files.CommitAllDocuments() | ||
with _ -> () | ||
else | ||
let textControl = textControlManager.VisibleTextControls |> Seq.find (fun c -> c.Document == document) | ||
cursorPosition = textControl.Caret.Position.Value.ToDocLineColumn(); |
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.
Could you add a comment why this is needed, please?
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.
You should be able to pass in
lf
as part of the configuration btw.(And override whatever the user had)
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.
This is a relic of the past. We can try to rewrite it in a separate PR.
resharper-fsharp/ReSharper.FSharp/src/FSharp.Psi.Features/src/Formatter/FSharpReformatCode.fs
Line 98 in 80620e2
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.
I think a comment about the version check would stil be good here. With a link to a PR or a commit.