Skip to content

Added Windows support #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 29 additions & 19 deletions autoload/arduino.vim
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ endif
let g:loaded_arduino_autoload = 1
let s:has_cli = executable('arduino-cli') == 1
if has('win64') || has('win32') || has('win16')
echoerr 'vim-arduino does not support windows :('
finish
let s:OS = 'Windows'
else
let s:OS = substitute(system('uname'), '\n', '', '')
endif
let s:HERE = resolve(expand('<sfile>:p:h:h'))
let s:OS = substitute(system('uname'), '\n', '', '')
" In neovim, run the shell commands using :terminal to preserve interactivity
if has('nvim')
let s:TERM = 'botright split | terminal! '
Expand Down Expand Up @@ -51,7 +51,6 @@ function! arduino#InitializeConfig() abort
if !exists('g:arduino_build_path')
let g:arduino_build_path = '{project_dir}/build'
endif

if !exists('g:arduino_serial_baud')
let g:arduino_serial_baud = 9600
endif
Expand All @@ -64,11 +63,9 @@ function! arduino#InitializeConfig() abort
if !exists('g:arduino_use_vimux') || !exists('$TMUX')
let g:arduino_use_vimux = 0
endif

if !exists('g:arduino_run_headless')
let g:arduino_run_headless = executable('Xvfb') == 1
endif

if !exists('g:arduino_serial_port_globs')
let g:arduino_serial_port_globs = ['/dev/ttyACM*',
\'/dev/ttyUSB*',
Expand All @@ -78,6 +75,9 @@ function! arduino#InitializeConfig() abort
endif
if !exists('g:arduino_use_cli')
let g:arduino_use_cli = s:has_cli
if g:arduino_use_cli
let g:arduino_serial_cmd = 'arduino-cli monitor -p {port} --config baudrate={baud}'
endif
Comment on lines +78 to +80
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would move this into the if !exists('g:arduino_serial_cmd') above (line 49). Move that check below this one, and then set it to be the default serial command if g:arduino_use_cli

elseif g:arduino_use_cli && !s:has_cli
echoerr 'arduino-cli: command not found'
endif
Expand Down Expand Up @@ -195,8 +195,8 @@ function! arduino#GetBuildPath() abort
return ''
endif
let l:path = g:arduino_build_path
let l:path = substitute(l:path, '{file}', expand('%:p'), 'g')
let l:path = substitute(l:path, '{project_dir}', expand('%:p:h'), 'g')
let l:path = substitute(l:path, '{file}', substitute(expand('%:p'), '\', '/', 'g'), 'g')
let l:path = substitute(l:path, '{project_dir}', substitute(expand('%:p:h'), '\', '/', 'g'), 'g')
Comment on lines +198 to +199
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this what you mean when you say the nested substitutes are not working? What specifically isn't working about them?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I apologize for the confusion. Nested substitutions work fine. I will write another message about what is working as I not expecting.

I used this hack to make it work on Windows. I don't know why any path on Windows is composed using backslashes. Without changing slashes topmost substitute interpreted '\U' sequence as a trigger to make the next characters UPPERCASE. But on Windows path quite often starts with the C:/User/<username> prefix. That broke compiling and uploading.

return l:path
endfunction

Expand Down Expand Up @@ -578,6 +578,13 @@ function! arduino#Verify() abort
endfunction

function! arduino#Upload() abort
let cmd = arduino#UploadGetCmd()

call arduino#RunCmd(cmd)
return v:shell_error
endfunction

function! arduino#UploadGetCmd() abort
if g:arduino_use_cli
let cmd = arduino#GetCLICompileCommand('-u')
else
Expand All @@ -588,9 +595,7 @@ function! arduino#Upload() abort
endif
let cmd = arduino#GetArduinoCommand(cmd_options)
endif

call arduino#RunCmd(cmd)
return v:shell_error
return cmd
endfunction

function! arduino#Serial() abort
Expand All @@ -601,14 +606,11 @@ function! arduino#Serial() abort
endfunction

function! arduino#UploadAndSerial()
" Since 'terminal!' is non-blocking '!' must be used to provide this functionality
let termBackup = s:TERM
let s:TERM = '!'
let ret = arduino#Upload()
if ret == 0
call arduino#Serial()
endif
let s:TERM = termBackup
let upload = arduino#UploadGetCmd()
let serial = arduino#GetSerialCmd()
if empty(serial) | return | endif

call arduino#RunCmd(upload . " && " . serial)
endfunction

" Serial helpers {{{2
Expand Down Expand Up @@ -648,6 +650,12 @@ function! arduino#GetPorts() abort
call add(ports, port)
endfor
endfor
if s:OS ==? 'Windows'
let boards_data = json_decode(system('arduino-cli board list --format json'))
for board in boards_data
call add(ports, board['port']['address'])
endfor
endif
Comment on lines +653 to +658
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this have to be windows-only? It seems like this API should work on all platforms. We should put it behind a if g:arduino_use_cli check, though. And also deduplicate any entries in the list.

return ports
endfunction

Expand Down Expand Up @@ -741,6 +749,8 @@ function! arduino#GetArduinoHomeDir() abort
endif
if s:OS ==? 'Darwin'
return $HOME . '/Library/Arduino15'
elseif s:OS ==? 'Windows'
return $HOME . '/AppData/Local/Arduino15'
endif

return $HOME . '/.arduino15'
Expand Down