From 185b3bc4372ee6b8f7af1fc5ee755422d31cc4c8 Mon Sep 17 00:00:00 2001
From: Andreas Louv <andreas@louv.dk>
Date: Sat, 11 Mar 2023 09:53:16 +0100
Subject: [PATCH] Make it possible to provide a new name directly to
 "LspRename"

---
 autoload/lsp/lsp.vim | 17 ++++++++++-------
 doc/lsp.txt          | 10 ++++++----
 plugin/lsp.vim       |  2 +-
 test/unit_tests.vim  | 19 +++++++++++++++++++
 4 files changed, 36 insertions(+), 12 deletions(-)

diff --git a/autoload/lsp/lsp.vim b/autoload/lsp/lsp.vim
index 24f41bb..990a94e 100644
--- a/autoload/lsp/lsp.vim
+++ b/autoload/lsp/lsp.vim
@@ -687,20 +687,23 @@ enddef
 
 # Rename a symbol
 # Uses LSP "textDocument/rename" request
-export def Rename()
+export def Rename(a_newName: string)
   var lspserver: dict<any> = buf.CurbufGetServerChecked()
   if lspserver->empty()
     return
   endif
 
-  var sym: string = expand('<cword>')
-  var newName: string = input($"Rename symbol '{sym}' to: ", sym)
+  var newName: string = a_newName
   if newName == ''
-    return
-  endif
+    var sym: string = expand('<cword>')
+    newName = input($"Rename symbol '{sym}' to: ", sym)
+    if newName == ''
+      return
+    endif
 
-  # clear the input prompt
-  echo "\r"
+    # clear the input prompt
+    echo "\r"
+  endif
 
   lspserver.renameSymbol(newName)
 enddef
diff --git a/doc/lsp.txt b/doc/lsp.txt
index bf4cfad..fb4718a 100644
--- a/doc/lsp.txt
+++ b/doc/lsp.txt
@@ -527,10 +527,12 @@ can map these commands to keys and make it easier to invoke them.
 			language servers support this feature.
 
 						*:LspRename*
-:LspRename		Rename the current symbol. You will be prompted to
-			enter the new name for the symbol. You can press <Esc>
-			or enter an empty string in the prompt to cancel the
-			operation.
+:LspRename [newName]	Rename the current symbol.
+
+			When [newName] is not given, then you will be prompted
+			to enter the new name for the symbol. You can press
+			<Esc> or enter an empty string in the prompt to cancel
+			the operation.
 
 						*:LspSelectionExpand*
 :LspSelectionExpand	Visually select the region of the symbol under the
diff --git a/plugin/lsp.vim b/plugin/lsp.vim
index 8bf146b..f374cfc 100644
--- a/plugin/lsp.vim
+++ b/plugin/lsp.vim
@@ -105,7 +105,7 @@ command! -nargs=0 -bar LspPeekDefinition lsp.GotoDefinition(v:true, <q-mods>)
 command! -nargs=0 -bar LspPeekImpl lsp.GotoImplementation(v:true, <q-mods>)
 command! -nargs=0 -bar LspPeekReferences lsp.ShowReferences(v:true)
 command! -nargs=0 -bar LspPeekTypeDef lsp.GotoTypedef(v:true, <q-mods>)
-command! -nargs=0 -bar LspRename lsp.Rename()
+command! -nargs=? -bar LspRename lsp.Rename(<q-args>)
 command! -nargs=0 -bar LspSelectionExpand lsp.SelectionExpand()
 command! -nargs=0 -bar LspSelectionShrink lsp.SelectionShrink()
 command! -nargs=1 -complete=customlist,LspServerDebugComplete -bar LspServerDebug lsp.ServerDebug(<q-args>)
diff --git a/test/unit_tests.vim b/test/unit_tests.vim
index 777649a..c07b273 100644
--- a/test/unit_tests.vim
+++ b/test/unit_tests.vim
@@ -445,6 +445,25 @@ def Test_LspRename()
     }
   END
   assert_equal(expected, getline(1, '$'))
+
+  cursor(1, 1)
+  search('counter')
+  LspRename countvar
+  var expected2: list<string> =<< trim END
+    void F1(int countvar)
+    {
+	countvar = 20;
+
+	++countvar;
+    }
+
+    void F2(int count)
+    {
+	count = 5;
+    }
+  END
+  assert_equal(expected2, getline(1, '$'))
+  sleep 100m
   bw!
 
   # empty file
-- 
2.51.0