From: Yegappan Lakshmanan <yegappan@yahoo.com>
Date: Sun, 18 Jun 2023 15:24:27 +0000 (-0700)
Subject: Support using a particular offset encoding in LSP messages
X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=67c30fbab8ac0095de74508c729d115b6f334318;p=vim-lsp.git

Support using a particular offset encoding in LSP messages
---

diff --git a/autoload/lsp/capabilities.vim b/autoload/lsp/capabilities.vim
index 9df58de..d2719e0 100644
--- a/autoload/lsp/capabilities.vim
+++ b/autoload/lsp/capabilities.vim
@@ -14,6 +14,10 @@ export def ProcessServerCaps(lspserver: dict<any>, caps: dict<any>)
     serverEncoding = lspserver.caps['~additionalInitResult_offsetEncoding']
   endif
 
+  if lspserver.forceOffsetEncoding != ''
+    serverEncoding = lspserver.forceOffsetEncoding
+  endif
+
   # one of 'utf-8', 'utf-16' or 'utf-32'
   if serverEncoding == 'utf-8'
     lspserver.posEncoding = 8
diff --git a/autoload/lsp/lsp.vim b/autoload/lsp/lsp.vim
index 730527a..56c1b47 100644
--- a/autoload/lsp/lsp.vim
+++ b/autoload/lsp/lsp.vim
@@ -614,6 +614,14 @@ export def AddServer(serverList: list<dict<any>>)
       server.initializationOptions = {}
     endif
 
+    if !server->has_key('forceOffsetEncoding')
+	|| server.forceOffsetEncoding->type() != v:t_string
+	|| (server.forceOffsetEncoding != 'utf-8'
+	    && server.forceOffsetEncoding != 'utf-16'
+	    && server.forceOffsetEncoding != 'utf-32')
+      server.forceOffsetEncoding = ''
+    endif
+
     if !server->has_key('customNotificationHandlers')
 	|| server.customNotificationHandlers->type() != v:t_dict
       server.customNotificationHandlers = {}
diff --git a/autoload/lsp/lspserver.vim b/autoload/lsp/lspserver.vim
index 11c4afa..d05ff37 100644
--- a/autoload/lsp/lspserver.vim
+++ b/autoload/lsp/lspserver.vim
@@ -1740,12 +1740,6 @@ export def NewLspServer(serverParams: dict<any>): dict<any>
     name: serverParams.name,
     path: serverParams.path,
     args: serverParams.args->deepcopy(),
-    syncInit: serverParams.syncInit,
-    initializationOptions: serverParams.initializationOptions->deepcopy(),
-    customNotificationHandlers: serverParams.customNotificationHandlers->deepcopy(),
-    customRequestHandlers: serverParams.customRequestHandlers->deepcopy(),
-    processDiagHandler: serverParams.processDiagHandler,
-    features: serverParams.features->deepcopy(),
     running: false,
     ready: false,
     job: v:none,
@@ -1753,24 +1747,31 @@ export def NewLspServer(serverParams: dict<any>): dict<any>
     nextID: 1,
     caps: {},
     requests: {},
+    callHierarchyType: '',
+    completionTriggerChars: [],
+    customNotificationHandlers: serverParams.customNotificationHandlers->deepcopy(),
+    customRequestHandlers: serverParams.customRequestHandlers->deepcopy(),
+    debug: serverParams.debug,
+    features: serverParams.features->deepcopy(),
+    forceOffsetEncoding: serverParams.forceOffsetEncoding,
+    initializationOptions: serverParams.initializationOptions->deepcopy(),
+    messages: [],
+    omniCompletePending: false,
+    peekSymbolFilePopup: -1,
+    peekSymbolPopup: -1,
+    processDiagHandler: serverParams.processDiagHandler,
     rootSearchFiles: serverParams.rootSearch->deepcopy(),
     runIfSearchFiles: serverParams.runIfSearch->deepcopy(),
     runUnlessSearchFiles: serverParams.runUnlessSearch->deepcopy(),
-    omniCompletePending: false,
-    completionTriggerChars: [],
+    selection: {},
     signaturePopup: -1,
-    typeHierPopup: -1,
+    syncInit: serverParams.syncInit,
+    traceLevel: serverParams.traceLevel,
     typeHierFilePopup: -1,
-    workspaceSymbolPopup: -1,
-    workspaceSymbolQuery: '',
-    peekSymbolPopup: -1,
-    peekSymbolFilePopup: -1,
-    callHierarchyType: '',
-    selection: {},
+    typeHierPopup: -1,
     workspaceConfig: serverParams.workspaceConfig->deepcopy(),
-    messages: [],
-    debug: serverParams.debug,
-    traceLevel: serverParams.traceLevel
+    workspaceSymbolPopup: -1,
+    workspaceSymbolQuery: ''
   }
   lspserver.logfile = $'lsp-{lspserver.name}.log'
   lspserver.errfile = $'lsp-{lspserver.name}.err'
diff --git a/doc/lsp.txt b/doc/lsp.txt
index 4040ad4..40a9622 100644
--- a/doc/lsp.txt
+++ b/doc/lsp.txt
@@ -3,7 +3,7 @@
 
 Author: Yegappan Lakshmanan  (yegappan AT yahoo DOT com)
 For Vim version 9.0 and above
-Last change: June 7, 2023
+Last change: June 18, 2023
 
 ==============================================================================
 CONTENTS                                                     *lsp-contents*
@@ -321,17 +321,30 @@ Additionally the following configurations can be made:
 			request replies which you might want to silence or
 			handle.  The provided request handlers will be called
 			with a reference to the "lspserver" and the "request".
-						*lsp-cfg-features*
-	features
+
+	features				*lsp-cfg-features*
 			(Optional) toggle which features should be enabled for
 			a given language server. See |lsp-multiple-servers|
 			and |lsp-features| for more information.
+
+	forceOffsetEncoding			*lsp-cfg-forceOffsetEncoding*
+			(Optional) a |String| value that forces the use of a
+			specific offset encoding in LSP messages.  If this
+			option is not specified, then the UTF offset encoding
+			is negotiated with the server during initialization.
+			Supported values are 'utf-8' or 'utf-16' or 'utf-32'.
+			The Vim native offset encoding is 'utf-32'.  For the
+			'utf-8' and 'utf-16' encodings, the offsets need to be
+			encoded and decoded in every LSP message and will
+			incur some overhead.
+
 						*lsp-cfg-omnicompl*
 	omnicompl	(Optional) a boolean value that enables (true)
 			or disables (false) omni-completion for these file
 			types. By default this is set to "v:true".  This value
 			is applicable only if auto completion is disabled
 			(|lsp-opt-autoComplete|).
+
 						*lsp-cfg-processDiagHandler*
 	processDiagHandler
 			(Optional) A |Funcref| or |lambda| that takes a list
@@ -378,12 +391,14 @@ Additionally the following configurations can be made:
 			call is used to initialize the language server,
 			otherwise the server is initialized asynchronously.
 			By default this is set to "false".
+
 						*lsp-cfg-debug*
 	debug		(Optional) log the messages printed by this language
 			server in stdout and stderr to a file.  Useful for
 			debugging a language server.  By default the
 			messages are not logged.  See |lsp-debug| for more
 			information.
+
 						*lsp-cfg-traceLevel*
 	traceLevel	(Optional) set the debug trace level for this language
 			server.  Supported values are: "off", "debug" and