From 491a32144022a0fa3ac174fea60dfb133779383e Mon Sep 17 00:00:00 2001
From: Yegappan Lakshmanan <yegappan@yahoo.com>
Date: Wed, 24 Jan 2024 07:51:32 -0800
Subject: [PATCH] Creating a file using workspaceedit is not supported

---
 autoload/lsp/capabilities.vim |  3 ++
 autoload/lsp/textedit.vim     | 56 ++++++++++++++++++++++++++++++++++-
 2 files changed, 58 insertions(+), 1 deletion(-)

diff --git a/autoload/lsp/capabilities.vim b/autoload/lsp/capabilities.vim
index 4fa5804..46f5973 100644
--- a/autoload/lsp/capabilities.vim
+++ b/autoload/lsp/capabilities.vim
@@ -476,6 +476,9 @@ export def GetClientCaps(): dict<any>
     workspace: {
       workspaceFolders: true,
       applyEdit: true,
+      workspaceEdit: {
+	resourceOperations: ['rename', 'create', 'delete']
+      },
       configuration: true,
       symbol: {
 	dynamicRegistration: false
diff --git a/autoload/lsp/textedit.vim b/autoload/lsp/textedit.vim
index abea145..5c09c81 100644
--- a/autoload/lsp/textedit.vim
+++ b/autoload/lsp/textedit.vim
@@ -219,12 +219,66 @@ def ApplyTextDocumentEdit(textDocEdit: dict<any>)
   ApplyTextEdits(bnr, textDocEdit.edits)
 enddef
 
+# interface CreateFile
+# Create the "createFile.uri" file
+def FileCreate(createFile: dict<any>)
+  var fname: string = util.LspUriToFile(createFile.uri)
+  var opts: dict<bool> = createFile->get('options', {ignoreIfExists: false, overwrite: true})
+
+  # LSP Spec: Overwrite wins over `ignoreIfExists`
+  if (opts->has_key('ignoreIfExists') && !opts.ignoreIfExists)
+      || (opts->has_key('overwrite') && opts.overwrite)
+    fnamemodify(fname, ':p:h')->mkdir('p')
+    []->writefile(fname)
+  endif
+  fname->bufadd()
+enddef
+
+# interface DeleteFile
+# Delete the "deleteFile.uri" file
+def FileDelete(deleteFile: dict<any>)
+  util.ErrMsg($'Deleting files not supported')
+
+  # var fname: string = util.LspUriToFile(createFile.uri)
+  # var opts: dict<bool> = createFile->get('options', {recursive: false, ignoreIfNotExists: true})
+
+  # if !filereadable(fname) && opts.ignoreIfNotExists
+  #   return
+  # endif
+
+  # var flags: string = ''
+  # if opts.recursive
+  #   # NOTE: is this a dangerous operation?  The LSP server can send a
+  #   # DeleteFile message to recursively delete all the files in the disk.
+  #   flags = 'rf'
+  # elseif isdirectory(fname)
+  #   flags = 'd'
+  # endif
+  # var bnr: number = fname->bufadd()
+  # delete(fname, flags)
+  # exe $'{bnr}bwipe!'
+enddef
+
+# interface RenameFile
+# Delete the "renameFile.uri" file
+def FileRename(renameFile: dict<any>)
+  util.ErrMsg($'Renaming files not supported')
+enddef
+
 # interface WorkspaceEdit
 export def ApplyWorkspaceEdit(workspaceEdit: dict<any>)
   if workspaceEdit->has_key('documentChanges')
     for change in workspaceEdit.documentChanges
       if change->has_key('kind')
-	util.ErrMsg($'Unsupported change in workspace edit [{change.kind}]')
+	if change.kind == 'create'
+	  FileCreate(change)
+	elseif change.kind == 'delete'
+	  FileDelete(change)
+	elseif change.kind == 'rename'
+	  FileRename(change)
+	else
+	  util.ErrMsg($'Unsupported change in workspace edit [{change.kind}]')
+	endif
       else
 	ApplyTextDocumentEdit(change)
       endif
-- 
2.51.0