Repository navigation
Add a refactoring between reference and struct tuples #20548
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
Open
xperiandri
wants to merge
12
commits into
dotnet:main
Choose a base branch
from
xperiandri:feature/tuple-struct-refactoring
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
eca584a
Add a refactoring between reference and struct tuples
xperiandri c4880aa
Build the two-file refactoring test project as a synthetic project
xperiandri c80c1e5
Check the two-file tuple result as a new project
xperiandri a751847
Leave a method's parameter list alone and convert curried tuple argum…
xperiandri f470248
Link the release note to the pull request and move it to a random lin…
xperiandri 89ac5ae
Tell argument lists by the called symbol and follow values through tr…
xperiandri 9620d95
Follow tuples through lambdas, function aliases, named arguments and …
xperiandri 8c36108
Compact the tuple refactoring sources
xperiandri 7c9a82d
Share the cross-file refactoring test setup
xperiandri 5260227
Bring the rebased branch to what it had before the rebase
xperiandri b77aaa7
Convert both sides of an as pattern with the tuple it takes apart
xperiandri 4d1d452
Withhold the tuple conversion when the value passes a tuple abbreviation
xperiandri 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,88 @@ | ||
| // Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information. | ||
|
|
||
| namespace Microsoft.VisualStudio.FSharp.Editor | ||
|
|
||
| open System | ||
| open System.Composition | ||
| open System.Threading | ||
| open System.Threading.Tasks | ||
|
|
||
| open Microsoft.CodeAnalysis | ||
| open Microsoft.CodeAnalysis.CodeActions | ||
| open Microsoft.CodeAnalysis.CodeRefactorings | ||
|
|
||
| open FSharp.Compiler.Syntax | ||
| open FSharp.Compiler.Text | ||
|
|
||
| open CancellableTasks | ||
| open TupleConversion | ||
|
|
||
| [<ExportCodeRefactoringProvider(FSharpConstants.FSharpLanguageName, Name = "ConvertTuple"); Shared>] | ||
| type internal FSharpConvertTupleRefactoring [<ImportingConstructor>] () = | ||
| inherit CodeRefactoringProvider() | ||
|
|
||
| static let hasSignatureFile (document: Document) = | ||
| let signaturePath = document.FilePath + "i" | ||
|
|
||
| document.Project.Documents | ||
| |> Seq.exists (fun d -> String.Equals(d.FilePath, signaturePath, StringComparison.OrdinalIgnoreCase)) | ||
|
|
||
| static let isInQuotation (caretNode: CaretNode) = | ||
| let path = | ||
| match caretNode with | ||
| | CaretNode.Expr(path = path) | ||
| | CaretNode.Pat(path = path) | ||
| | CaretNode.Type(annotated = Annotated.Pattern(path = path)) | ||
| | CaretNode.Type(annotated = Annotated.Expression(path = path)) -> path | ||
| | CaretNode.Type _ -> [] | ||
|
|
||
| path | ||
| |> List.exists (function | ||
| | SyntaxNode.SynExpr(SynExpr.Quote _) -> true | ||
| | _ -> false) | ||
|
|
||
| override _.ComputeRefactoringsAsync context = | ||
| cancellableTask { | ||
| let document = context.Document | ||
|
|
||
| if not (document.IsFSharpSignatureFile || hasSignatureFile document) then | ||
| let! cancellationToken = CancellableTask.getCancellationToken () | ||
| let! sourceText = document.GetTextAsync cancellationToken | ||
| let! parseResults = document.GetFSharpParseResultsAsync(nameof FSharpConvertTupleRefactoring) | ||
|
|
||
| let caret = | ||
| let linePosition = sourceText.Lines.GetLinePosition context.Span.Start | ||
| Position.mkPos (Line.fromZ linePosition.Line) linePosition.Character | ||
|
|
||
| let hasTupleCandidate = | ||
| (tryCaretNode (fun _ -> false) caret parseResults.ParseTree).IsSome | ||
|
|
||
| let! found = | ||
| if hasTupleCandidate then | ||
| cancellableTask { | ||
| let! _, checkResults = document.GetFSharpParseAndCheckResultsAsync(nameof FSharpConvertTupleRefactoring) | ||
| return tryCaretNode (callsMethod sourceText checkResults) caret parseResults.ParseTree | ||
| } | ||
| else | ||
| CancellableTask.singleton ValueNone | ||
|
|
||
| match found with | ||
| | ValueSome caretNode when not (isInQuotation caretNode) && canChange sourceText caretNode -> | ||
| let! converted = TuplePropagation.tryConvert document caretNode (nameof FSharpConvertTupleRefactoring) | ||
|
|
||
| match converted with | ||
| | ValueSome solution -> | ||
| let title = | ||
| if isStructNode caretNode then | ||
| SR.ConvertToReferenceTuple() | ||
| else | ||
| SR.ConvertToStructTuple() | ||
|
|
||
| let action = | ||
| CodeAction.Create(title, Func<CancellationToken, Task<Solution>>(fun _ -> Task.FromResult solution), title) | ||
|
|
||
| context.RegisterRefactoring action | ||
| | ValueNone -> () | ||
| | _ -> () | ||
| } | ||
| |> CancellableTask.startAsTask context.CancellationToken | ||
Oops, something went wrong.
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.
🤖🕵️ Runs a cross-solution reference search on every caret stop on a tuple; move it to apply time (as Roslyn does) and report problems then.