@@ -869,18 +869,57 @@ require('opencode').setup({
869869})
870870```
871871
872- For WSL or complex mappings, use a function:
872+ ### Auto-Spawning with WSL
873+
874+ Run opencode server inside WSL while using Neovim on Windows:
873875
874876``` lua
875877require (' opencode' ).setup ({
876878 server = {
877879 url = ' localhost' ,
878- port = 8080 ,
880+ port = ' auto' , -- Random port for project isolation
881+
882+ -- Spawn opencode server inside WSL
883+ spawn_command = function (port , url )
884+ local cmd = string.format (
885+ ' wsl.exe -e bash -c "opencode serve --hostname 127.0.0.1 --port %d"' ,
886+ port
887+ )
888+ print (string.format (' [opencode.nvim] Starting WSL server on port %d' , port ))
889+ return vim .fn .jobstart (cmd , { detach = 1 })
890+ end ,
891+
892+ -- Kill WSL opencode process
893+ kill_command = function (port , url )
894+ print (string.format (' [opencode.nvim] Stopping WSL server on port %d' , port ))
895+ vim .fn .jobstart (' wsl.exe -e pkill -f "opencode serve.*--port ' .. port .. ' "' )
896+ end ,
897+
898+ -- Windows → WSL path translation (for requests)
879899 path_map = function (host_path )
880- -- Convert Windows path to WSL path
881- local wsl_path = host_path :gsub (' ^C:' , ' /mnt/c' )
882- return wsl_path
900+ if vim .fn .has (' win32' ) == 1 then
901+ -- Convert C:\Users\... → /mnt/c/Users/...
902+ local drive , rest = host_path :match (' ^([A-Za-z]):(.*)$' )
903+ if drive then
904+ local wsl_path = ' /mnt/' .. drive :lower () .. rest :gsub (' \\ ' , ' /' )
905+ return wsl_path
906+ end
907+ end
908+ return host_path
909+ end ,
910+
911+ -- WSL → Windows path translation (for responses)
912+ reverse_path_map = function (server_path )
913+ -- Convert /mnt/c/Users/... → C:\Users\...
914+ local drive , rest = server_path :match (' ^/mnt/([a-z])(.*)$' )
915+ if drive then
916+ local windows_path = drive :upper () .. ' :' .. rest :gsub (' /' , ' \\ ' )
917+ return windows_path
918+ end
919+ return server_path
883920 end ,
921+
922+ auto_kill = true , -- Kill server when last nvim instance exits
884923 },
885924})
886925```
@@ -893,7 +932,8 @@ require('opencode').setup({
893932- ` spawn_command ` (function | nil): Optional function to start server: ` function(port, url) ... end `
894933- ` kill_command ` (function | nil): Optional function to stop server when ` auto_kill ` triggers: ` function(port, url) ... end `
895934- ` auto_kill ` (boolean): Kill spawned servers when last nvim instance exits (default: true)
896- - ` path_map ` (string | function | nil): Transform host paths to server paths
935+ - ` path_map ` (string | function | nil): Transform host paths to server paths (for outgoing requests)
936+ - ` reverse_path_map ` (function | nil): Transform server paths back to host paths (for incoming responses/events)
897937
898938### Multi-Instance Support
899939
0 commit comments