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

Add fsproj/renameFilex #1075

Merged
merged 1 commit into from
Mar 13, 2023
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ Custom endpoints are using (for messages body) `PlainNotification` type and stri
* `fsproj/addFile` - accepts `DotnetFileRequest`, create the file if needed and add it to the project if not already present
* `fsproj/addExistingFile` - accepts `DotnetFileRequest`, add existing file to a project if not already present
* `fsproj/removeFile` - accepts `DotnetFileRequest`, remove the file from the project
* `fsproj/renameFile` - accepts `DotnetRenameFileRequest`, rename the file from the project

### Supported LSP notifications

Expand Down
43 changes: 43 additions & 0 deletions src/FsAutoComplete.Core/Commands.fs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,26 @@ module Commands =
return CoreResponse.ErrorRes ex.Message
}

let renameFile (fsprojPath: string) (oldFileVirtualPath: string) (newFileName: string) =
async {
try
let dir = Path.GetDirectoryName fsprojPath
let oldFilePath = Path.Combine(dir, oldFileVirtualPath)
let oldFileInfo = FileInfo(oldFilePath)

let newFilePath = Path.Combine(oldFileInfo.Directory.FullName, newFileName)

File.Move(oldFilePath, newFilePath)

let newVirtPath =
Path.Combine(Path.GetDirectoryName oldFileVirtualPath, newFileName)

FsProjEditor.renameFile fsprojPath oldFileVirtualPath newVirtPath
return CoreResponse.Res()
with ex ->
return CoreResponse.ErrorRes ex.Message
}

let removeFile fsprojPath fileVirtPath =
async {
FsProjEditor.removeFile fsprojPath fileVirtPath
Expand Down Expand Up @@ -1483,6 +1503,29 @@ type Commands(checker: FSharpCompilerServiceChecker, state: State, hasAnalyzers:
return CoreResponse.ErrorRes ex.Message
}

member _.FsProjRenameFile (fsprojPath: string) (oldFileVirtualPath: string) (newFileName: string) =
async {
try
let dir = Path.GetDirectoryName fsprojPath
let oldFilePath = Path.Combine(dir, oldFileVirtualPath)
let oldFileInfo = FileInfo(oldFilePath)

let newFilePath = Path.Combine(oldFileInfo.Directory.FullName, newFileName)

File.Move(oldFilePath, newFilePath)

let newVirtPath =
Path.Combine(Path.GetDirectoryName oldFileVirtualPath, newFileName)

FsProjEditor.renameFile fsprojPath oldFileVirtualPath newVirtPath

// Clear diagnostics for the old file
state.RemoveProjectOptions(normalizePath oldFilePath)
return CoreResponse.Res()
with ex ->
return CoreResponse.ErrorRes ex.Message
}

member _.FsProjAddFile (fsprojPath: string) (fileVirtPath: string) =
async {
try
Expand Down
8 changes: 8 additions & 0 deletions src/FsAutoComplete.Core/FsprojEdit.fs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ module FsProjEditor =
itemGroup.InsertAfter(node, aboveNode) |> ignore
xdoc.Save fsprojPath

let renameFile (fsprojPath: string) (oldFileName: string) (newFileName: string) =
let xdoc = System.Xml.XmlDocument()
xdoc.Load fsprojPath
let xpath = sprintf "//Compile[@Include='%s']" oldFileName
let node = xdoc.SelectSingleNode(xpath)
node.Attributes["Include"].InnerText <- newFileName
xdoc.Save fsprojPath

let addFile (fsprojPath: string) (newFileName: string) =
let xdoc = System.Xml.XmlDocument()
xdoc.Load fsprojPath
Expand Down
5 changes: 5 additions & 0 deletions src/FsAutoComplete/LspHelpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,11 @@ type DotnetFile2Request =
FileVirtualPath: string
NewFile: string }

type DotnetRenameFileRequest =
{ FsProj: string
OldFileVirtualPath: string
NewFileName: string }

type FSharpLiterateRequest =
{ TextDocument: TextDocumentIdentifier }

Expand Down
31 changes: 31 additions & 0 deletions src/FsAutoComplete/LspServers/AdaptiveFSharpLspServer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4530,6 +4530,36 @@ type AdaptiveFSharpLspServer(workspaceLoader: IWorkspaceLoader, lspClient: FShar
return! LspResult.internalError (string e)
}

override __.FsProjRenameFile(p: DotnetRenameFileRequest) =
asyncResult {
let tags = [ "DotnetRenameFileRequest", box p ]
use trace = fsacActivitySource.StartActivityForType(thisType, tags = tags)

try
logger.info (
Log.setMessage "FsProjRenameFile Request: {parms}"
>> Log.addContextDestructured "parms" p
)

do!
Commands.renameFile p.FsProj p.OldFileVirtualPath p.NewFileName
|> AsyncResult.ofCoreResponse
|> AsyncResult.map ignore

forceLoadProjects () |> ignore
return None
with e ->
trace.SetStatusErrorSafe(e.Message).RecordExceptions(e) |> ignore

logger.error (
Log.setMessage "FsProjRenameFile Request Errored {p}"
>> Log.addContextDestructured "p" p
>> Log.addExn e
)

return! LspResult.internalError (string e)
}


override __.FsProjAddFile(p: DotnetFileRequest) =
asyncResult {
Expand Down Expand Up @@ -4709,6 +4739,7 @@ module AdaptiveFSharpLspServer =
|> Map.add "fsproj/addFileBelow" (serverRequestHandling (fun s p -> s.FsProjAddFileBelow(p)))
|> Map.add "fsproj/addFile" (serverRequestHandling (fun s p -> s.FsProjAddFile(p)))
|> Map.add "fsproj/addExistingFile" (serverRequestHandling (fun s p -> s.FsProjAddExistingFile(p)))
|> Map.add "fsproj/renameFile" (serverRequestHandling (fun s p -> s.FsProjRenameFile(p)))
|> Map.add "fsproj/removeFile" (serverRequestHandling (fun s p -> s.FsProjRemoveFile(p)))

let adaptiveServer lspClient =
Expand Down
19 changes: 19 additions & 0 deletions src/FsAutoComplete/LspServers/FsAutoComplete.Lsp.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2527,6 +2527,24 @@ type FSharpLspServer(state: State, lspClient: FSharpLspClient) =
return res
}

override __.FsProjRenameFile(p: DotnetRenameFileRequest) =
async {
logger.info (
Log.setMessage "FsProjRenameFile Request: {parms}"
>> Log.addContextDestructured "parms" p
)

let! res = commands.FsProjRenameFile p.FsProj p.OldFileVirtualPath p.NewFileName

let res =
match res with
| CoreResponse.InfoRes msg -> success None
| CoreResponse.ErrorRes msg -> LspResult.internalError msg
| CoreResponse.Res(_) -> success None

return res
}

override __.FsProjAddFile(p: DotnetFileRequest) =
async {
logger.info (
Expand Down Expand Up @@ -2902,6 +2920,7 @@ module FSharpLspServer =
|> Map.add "fsproj/addFileBelow" (serverRequestHandling (fun s p -> s.FsProjAddFileBelow(p)))
|> Map.add "fsproj/addFile" (serverRequestHandling (fun s p -> s.FsProjAddFile(p)))
|> Map.add "fsproj/addExistingFile" (serverRequestHandling (fun s p -> s.FsProjAddExistingFile(p)))
|> Map.add "fsproj/renameFile" (serverRequestHandling (fun s p -> s.FsProjRenameFile(p)))
|> Map.add "fsproj/removeFile" (serverRequestHandling (fun s p -> s.FsProjRemoveFile(p)))

let regularServer lspClient =
Expand Down
1 change: 1 addition & 0 deletions src/FsAutoComplete/LspServers/IFSharpLspServer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type IFSharpLspServer =
abstract FsProjMoveFileDown: DotnetFileRequest -> Async<LspResult<PlainNotification option>>
abstract FsProjAddFileAbove: DotnetFile2Request -> Async<LspResult<PlainNotification option>>
abstract FsProjAddFileBelow: DotnetFile2Request -> Async<LspResult<PlainNotification option>>
abstract FsProjRenameFile: DotnetRenameFileRequest -> Async<LspResult<PlainNotification option>>
abstract FsProjAddFile: DotnetFileRequest -> Async<LspResult<PlainNotification option>>
abstract FsProjRemoveFile: DotnetFileRequest -> Async<LspResult<PlainNotification option>>
abstract FsProjAddExistingFile: DotnetFileRequest -> Async<LspResult<PlainNotification option>>