@@ -623,6 +623,44 @@ module SolutionExplorer =
623
623
else
624
624
( fn + " .fs" )
625
625
626
+ let private createNewFileDialg ( proj : string ) ( existingFiles : list < Model >) ( prompt : string ) =
627
+ let opts = createEmpty< InputBoxOptions>
628
+ opts.placeHolder <- Some " new.fs"
629
+ opts.prompt <- Some prompt
630
+
631
+ opts.validateInput <-
632
+ Some( fun userInput ->
633
+ let fileExist =
634
+ existingFiles
635
+ |> List.tryFind ( fun file ->
636
+ match file with
637
+ | Workspace _
638
+ | Solution _
639
+ | WorkspaceFolder _
640
+ | PackageReferenceList _
641
+ | ProjectReferencesList _
642
+ | ProjectNotLoaded _
643
+ | ProjectLoading _
644
+ | ProjectFailedToLoad _
645
+ | ProjectNotRestored _
646
+ | ProjectLanguageNotSupported _
647
+ | Project _
648
+ | Folder _
649
+ | PackageReference _
650
+ | ProjectReference _ -> false
651
+ | File (_, filePath, _, _, _) ->
652
+ let projDir = node.path.dirname proj
653
+ // Need to compute the relative path from the project in order to match the user input
654
+ let relativeFilePathFromProject = node.path.relative ( projDir, filePath)
655
+ // Sanitize the path for comparison
656
+ userInput.Replace( " \\ " , " /" ) = relativeFilePathFromProject.Replace( " \\ " , " /" ))
657
+
658
+ match fileExist with
659
+ | Some _ -> U2.Case1 " File already exists"
660
+ | None -> undefined)
661
+
662
+ window.showInputBox opts
663
+
626
664
let newProject () =
627
665
promise {
628
666
let! templates = LanguageService.dotnetNewList ()
@@ -763,21 +801,19 @@ module SolutionExplorer =
763
801
" fsharp.explorer.addAbove" ,
764
802
objfy2 ( fun m ->
765
803
match unbox m with
766
- | File (_, _, name, Some virtPath, proj) ->
767
- let opts = createEmpty< InputBoxOptions>
768
- opts.placeHolder <- Some " new.fs"
769
- opts.prompt <- Some " New file name, relative to selected file"
770
- opts.value <- Some " new.fs"
771
-
772
- window.showInputBox ( opts)
773
- |> Promise.ofThenable
774
- |> Promise.bind ( fun file ->
775
- match file with
776
- | Some file ->
777
- let file ' = handleUntitled file
778
- FsProjEdit.addFileAbove proj virtPath file'
779
- | None -> Promise.empty)
780
- |> unbox
804
+ | File ( parent, _, name, Some virtPath, proj) ->
805
+ match parent.Value with
806
+ | Some ( Project (_, proj, _, files, _, _, _, _)) ->
807
+ createNewFileDialg proj files " New file name, relative to selected file"
808
+ |> Promise.ofThenable
809
+ |> Promise.bind ( fun file ->
810
+ match file with
811
+ | Some file ->
812
+ let file ' = handleUntitled file
813
+ FsProjEdit.addFileAbove proj virtPath file'
814
+ | None -> Promise.empty)
815
+ |> unbox
816
+ | _ -> undefined
781
817
| _ -> undefined)
782
818
)
783
819
|> context.Subscribe
@@ -786,21 +822,21 @@ module SolutionExplorer =
786
822
" fsharp.explorer.addBelow" ,
787
823
objfy2 ( fun m ->
788
824
match unbox m with
789
- | File (_, fr_ om, name, Some virtPath, proj) ->
790
- let opts = createEmpty< InputBoxOptions>
791
- opts.placeHolder <- Some " new.fs"
792
- opts.prompt <- Some " New file name, relative to selected file"
793
- opts.value <- Some " new.fs"
825
+ | File ( parent, fr_ om, name, Some virtPath, proj) ->
826
+ match parent.Value with
827
+ | Some ( Project (_, proj, _, files, _, _, _, _)) ->
828
+ createNewFileDialg proj files " New file name, relative to selected file"
829
+ |> Promise.ofThenable
830
+ |> Promise.map ( fun file ->
831
+ match file with
832
+ | Some file ->
833
+ let file ' = handleUntitled file
834
+ FsProjEdit.addFileBelow proj virtPath file'
835
+ | None -> Promise.empty)
836
+ |> unbox
837
+ | _ ->
838
+ undefined
794
839
795
- window.showInputBox ( opts)
796
- |> Promise.ofThenable
797
- |> Promise.map ( fun file ->
798
- match file with
799
- | Some file ->
800
- let file ' = handleUntitled file
801
- FsProjEdit.addFileBelow proj virtPath file'
802
- | None -> Promise.empty)
803
- |> unbox
804
840
| _ -> undefined)
805
841
)
806
842
|> context.Subscribe
@@ -809,13 +845,8 @@ module SolutionExplorer =
809
845
" fsharp.explorer.addFile" ,
810
846
objfy2 ( fun m ->
811
847
match unbox m with
812
- | Project (_, proj, _, _, _, _, _, _) ->
813
- let opts = createEmpty< InputBoxOptions>
814
- opts.placeHolder <- Some " new.fs"
815
- opts.prompt <- Some " New file name, relative to project file"
816
- opts.value <- Some " new.fs"
817
-
818
- window.showInputBox ( opts)
848
+ | Project (_, proj, _, files, _, _, _, _) ->
849
+ createNewFileDialg proj files " New file name, relative to project file"
819
850
|> Promise.ofThenable
820
851
|> Promise.map ( fun file ->
821
852
match file with
0 commit comments