Skip to content

Commit ebefc3a

Browse files
MangelMaximebaronfel
authored andcommitted
Prevent user from submitting if the file already exist in the project
Fix ionide#1771 This PR covers: - "Add file" from project level - "Add file below" - "Add file above"
1 parent aef716d commit ebefc3a

File tree

1 file changed

+67
-36
lines changed

1 file changed

+67
-36
lines changed

src/Components/SolutionExplorer.fs

+67-36
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,44 @@ module SolutionExplorer =
623623
else
624624
(fn + ".fs")
625625

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+
626664
let newProject () =
627665
promise {
628666
let! templates = LanguageService.dotnetNewList ()
@@ -763,21 +801,19 @@ module SolutionExplorer =
763801
"fsharp.explorer.addAbove",
764802
objfy2 (fun m ->
765803
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
781817
| _ -> undefined)
782818
)
783819
|> context.Subscribe
@@ -786,21 +822,21 @@ module SolutionExplorer =
786822
"fsharp.explorer.addBelow",
787823
objfy2 (fun m ->
788824
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
794839

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
804840
| _ -> undefined)
805841
)
806842
|> context.Subscribe
@@ -809,13 +845,8 @@ module SolutionExplorer =
809845
"fsharp.explorer.addFile",
810846
objfy2 (fun m ->
811847
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"
819850
|> Promise.ofThenable
820851
|> Promise.map (fun file ->
821852
match file with

0 commit comments

Comments
 (0)