]> Sergey Matveev's repositories - vim-lsp.git/commitdiff
Adapt LspFileTorUri for Cygwin
authorPeter Jankuliak <p.jankuliak@gmail.com>
Mon, 8 May 2023 19:40:03 +0000 (20:40 +0100)
committerPeter Jankuliak <p.jankuliak@gmail.com>
Mon, 8 May 2023 20:02:53 +0000 (21:02 +0100)
Convert paths of the form "/cygdrive/c/foo/bar" to "c:/foo/bar" and paths of
the form "/home/pete/foo" to "C:/cygwin64/home/pete/foo"

autoload/lsp/util.vim

index 548856131ff8471f9db0ababb8a24ac9cf3b43aa..172704d2e05c688bc37f6d8719ec39744fc03096 100644 (file)
@@ -111,6 +111,11 @@ enddef
 export def LspFileToUri(fname: string): string
   var uri: string = fname->fnamemodify(':p')
 
+  if has("win32unix")
+    # We're in Cygwin
+    uri = CygwinToWindowsPath(uri)
+  endif
+
   var on_windows: bool = false
   if uri =~? '^\a:'
     on_windows = true
@@ -133,6 +138,27 @@ export def LspFileToUri(fname: string): string
   return uri
 enddef
 
+# Convert POSIX paths as used in Cygwin to native Windows paths
+def CygwinToWindowsPath(path: string): string
+  if path =~? '^\/cygdrive\/'
+    # Convert paths of the form "/cygdrive/c/foo/bar" to "C:/foo/bar"
+    return substitute(path, '^\/cygdrive\/\(\a\)\/', '\=submatch(1) .. ":/"', "")
+  elseif path =~? '^\/'
+    # Convert paths of the form "/home/pete/foo" to "C:/cygwin64/home/pete/foo"
+    if len(g:cygwinroot) == 0
+      # https://stackoverflow.com/a/7449029/273348
+      g:cygwinroot = substitute(system("reg query HKEY_LOCAL_MACHINE\\\\SOFTWARE\\\\Cygwin\\\\setup /v rootdir | grep rootdir"),
+            \ '^\s*\S\+\s\+\S\+\s\+\(\p\+\).*$',
+            \ '\=submatch(1)',
+            \ "")
+    endif
+
+    return $'{g:cygwinroot}{path}'
+  else
+    return path
+  endif
+enddef
+
 # Convert a Vim buffer number to an LSP URI (file://<absolute_path>)
 export def LspBufnrToUri(bnr: number): string
   return LspFileToUri(bnr->bufname())