Skip to content

Commit

Permalink
Add fsproj/renameFile command
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxime Mangel committed Mar 12, 2023
1 parent b108431 commit 80a731f
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
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
39 changes: 39 additions & 0 deletions src/FsAutoComplete.Core/Commands.fs
Expand Up @@ -157,6 +157,24 @@ 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 +1501,27 @@ 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
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
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
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
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
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>>

0 comments on commit 80a731f

Please sign in to comment.