Skip to content

Commit

Permalink
Prevent user from submitting if the file already exist in the project
Browse files Browse the repository at this point in the history
Fix #1771

This PR covers:
- "Add file" from project level
- "Add file below"
- "Add file above"
  • Loading branch information
MangelMaxime authored and baronfel committed Sep 5, 2022
1 parent aef716d commit ebefc3a
Showing 1 changed file with 67 additions and 36 deletions.
103 changes: 67 additions & 36 deletions src/Components/SolutionExplorer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,44 @@ module SolutionExplorer =
else
(fn + ".fs")

let private createNewFileDialg (proj : string) (existingFiles : list<Model>) (prompt : string) =
let opts = createEmpty<InputBoxOptions>
opts.placeHolder <- Some "new.fs"
opts.prompt <- Some prompt

opts.validateInput <-
Some(fun userInput ->
let fileExist =
existingFiles
|> List.tryFind (fun file ->
match file with
| Workspace _
| Solution _
| WorkspaceFolder _
| PackageReferenceList _
| ProjectReferencesList _
| ProjectNotLoaded _
| ProjectLoading _
| ProjectFailedToLoad _
| ProjectNotRestored _
| ProjectLanguageNotSupported _
| Project _
| Folder _
| PackageReference _
| ProjectReference _ -> false
| File (_, filePath, _, _, _) ->
let projDir = node.path.dirname proj
// Need to compute the relative path from the project in order to match the user input
let relativeFilePathFromProject = node.path.relative (projDir, filePath)
// Sanitize the path for comparison
userInput.Replace("\\", "/") = relativeFilePathFromProject.Replace("\\", "/"))

match fileExist with
| Some _ -> U2.Case1 "File already exists"
| None -> undefined)

window.showInputBox opts

let newProject () =
promise {
let! templates = LanguageService.dotnetNewList ()
Expand Down Expand Up @@ -763,21 +801,19 @@ module SolutionExplorer =
"fsharp.explorer.addAbove",
objfy2 (fun m ->
match unbox m with
| File (_, _, name, Some virtPath, proj) ->
let opts = createEmpty<InputBoxOptions>
opts.placeHolder <- Some "new.fs"
opts.prompt <- Some "New file name, relative to selected file"
opts.value <- Some "new.fs"

window.showInputBox (opts)
|> Promise.ofThenable
|> Promise.bind (fun file ->
match file with
| Some file ->
let file' = handleUntitled file
FsProjEdit.addFileAbove proj virtPath file'
| None -> Promise.empty)
|> unbox
| File (parent, _, name, Some virtPath, proj) ->
match parent.Value with
| Some (Project (_, proj, _, files, _, _, _, _)) ->
createNewFileDialg proj files "New file name, relative to selected file"
|> Promise.ofThenable
|> Promise.bind (fun file ->
match file with
| Some file ->
let file' = handleUntitled file
FsProjEdit.addFileAbove proj virtPath file'
| None -> Promise.empty)
|> unbox
| _ -> undefined
| _ -> undefined)
)
|> context.Subscribe
Expand All @@ -786,21 +822,21 @@ module SolutionExplorer =
"fsharp.explorer.addBelow",
objfy2 (fun m ->
match unbox m with
| File (_, fr_om, name, Some virtPath, proj) ->
let opts = createEmpty<InputBoxOptions>
opts.placeHolder <- Some "new.fs"
opts.prompt <- Some "New file name, relative to selected file"
opts.value <- Some "new.fs"
| File (parent, fr_om, name, Some virtPath, proj) ->
match parent.Value with
| Some (Project (_, proj, _, files, _, _, _, _)) ->
createNewFileDialg proj files "New file name, relative to selected file"
|> Promise.ofThenable
|> Promise.map (fun file ->
match file with
| Some file ->
let file' = handleUntitled file
FsProjEdit.addFileBelow proj virtPath file'
| None -> Promise.empty)
|> unbox
| _ ->
undefined

window.showInputBox (opts)
|> Promise.ofThenable
|> Promise.map (fun file ->
match file with
| Some file ->
let file' = handleUntitled file
FsProjEdit.addFileBelow proj virtPath file'
| None -> Promise.empty)
|> unbox
| _ -> undefined)
)
|> context.Subscribe
Expand All @@ -809,13 +845,8 @@ module SolutionExplorer =
"fsharp.explorer.addFile",
objfy2 (fun m ->
match unbox m with
| Project (_, proj, _, _, _, _, _, _) ->
let opts = createEmpty<InputBoxOptions>
opts.placeHolder <- Some "new.fs"
opts.prompt <- Some "New file name, relative to project file"
opts.value <- Some "new.fs"

window.showInputBox (opts)
| Project (_, proj, _, files, _, _, _, _) ->
createNewFileDialg proj files "New file name, relative to project file"
|> Promise.ofThenable
|> Promise.map (fun file ->
match file with
Expand Down

0 comments on commit ebefc3a

Please sign in to comment.