Skip to content
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

Generate signature file #378

Draft
wants to merge 3 commits into
base: net232
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<Compile Include="src\Intentions\SetNameAction.fs" />
<Compile Include="src\Intentions\LetToUseAction.fs" />
<Compile Include="src\Intentions\RenameFileToMatchTypeNameAction.fs" />
<Compile Include="src\Intentions\GenerateSignatureFileAction.fs" />
<Compile Include="src\QuickFixes\FSharpQuickFixBase.fs" />
<Compile Include="src\QuickFixes\RemoveUnusedOpensFix.fs" />
<Compile Include="src\QuickFixes\ReplaceUseWithLetFix.fs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
namespace JetBrains.ReSharper.Plugins.FSharp.Psi.Features.ContextActions

open System.IO
open JetBrains.ProjectModel.ProjectsHost
open JetBrains.RdBackend.Common.Features.ProjectModel
open JetBrains.RdBackend.Common.Features.ProjectModel.View.Ordering
open JetBrains.ReSharper.Feature.Services.ContextActions
open JetBrains.ReSharper.Plugins.FSharp.Psi.Features.Intentions
open type JetBrains.ReSharper.Psi.PsiSourceFileExtensions

[<ContextAction(Group = "F#", Name = "Generate signature file for current file", Priority = 1s,
Description = "Generate signature file for current file.")>]
type GenerateSignatureFileAction(dataProvider: FSharpContextActionDataProvider) =
inherit FSharpContextActionBase(dataProvider)

override this.Text = "Generate signature file for current file"

override this.IsAvailable _ =
let currentFSharpFile = dataProvider.PsiFile
let fcsService = currentFSharpFile.FcsCheckerService
let hasSignature = fcsService.FcsProjectProvider.HasPairFile dataProvider.SourceFile
not hasSignature

override this.ExecutePsiTransaction(solution, _) =
let projectFile = dataProvider.SourceFile.ToProjectFile()
let physicalPath = dataProvider.SourceFile.ToProjectFile().Location.FileAccessPath
let fsiFile = Path.ChangeExtension(physicalPath, ".fsi")

try
let currentFSharpFile = dataProvider.PsiFile
let fcsService = currentFSharpFile.FcsCheckerService
let checkResult = fcsService.ParseAndCheckFile(currentFSharpFile.GetSourceFile(), "for signature file", true)
do
match checkResult with
| None -> ()
| Some { CheckResults = checkResult } ->

match checkResult.GenerateSignature() with
| None -> ()
| Some signatureSourceText ->
let content = string signatureSourceText
File.WriteAllText(fsiFile, content)
with ex ->
// TODO: show some balloon thing?
()

solution.InvokeUnderTransaction(fun transactionCookie ->
let virtualPath = FileSystemPath.TryParse(fsiFile).ToVirtualFileSystemPath()
let relativeTo = RelativeTo(projectFile, RelativeToType.Before)
transactionCookie.AddFile(projectFile.ParentFolder, virtualPath, context = OrderingContext(relativeTo))
|> ignore)

null