Skip to content

Vimscript support #117

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

Closed
SamSaffron opened this issue Jul 21, 2022 · 5 comments
Closed

Vimscript support #117

SamSaffron opened this issue Jul 21, 2022 · 5 comments

Comments

@SamSaffron
Copy link

SamSaffron commented Jul 21, 2022

Not really an issue, please close. But in case anyone is looking for vimscript support ... I kind of cobbled this hack up.

I guess the next thing will be for someone to cleanly package this up in a plugin (and clean the messy bits).

" with help from https://github.com/prettier/vim-prettier/blob/master/autoload/prettier/utils/buffer.vim
function SyntaxTreeRubyFormat()

  let l:contents = join(getline(1, '$'), "\n")
  let l:path = fnamemodify(expand('%:p'), ':h')
  let l:found = 0

  while len(l:path) > 2
    if filereadable(l:path . '/Gemfile')

      let l:gemfile = join(readfile(l:path . '/Gemfile'), "\n")

      if stridx(l:gemfile, 'syntax_tree') > 0
        let l:found = 1
        break
      endif
    endif

    let l:path = fnamemodify(l:path, ':h')
  endwhile

  if l:found == 0
    return
  endif

  let l:command = "bundle exec stree format"


  let l:old_path = chdir(l:path)

  let l:old_ruby_opt = $RUBYOPT
  let $RUBYOPT = "-W0"
  let l:formatted = system(l:command, l:contents)

  let $RUBYOPT = l:old_ruby_opt
  call chdir(l:old_path)

  let l:winview = winsaveview()

  " https://vim.fandom.com/wiki/Restore_the_cursor_position_after_undoing_text_change_made_by_a_script
  " create a fake change entry and merge with undo stack prior to do formating
  execute "normal! i "
  execute "normal! a\<BS>"
  try | silent undojoin | catch | endtry


   " delete all lines on the current buffer
  silent! execute 'lockmarks %delete _'

  " replace all lines from the current buffer with output from prettier
  let l:idx = 0
  for l:line in split(l:formatted, "\n")
    silent! lockmarks call append(l:idx, l:line)
    let l:idx += 1
  endfor

  " delete trailing newline introduced by the above append procedure
  silent! lockmarks execute '$delete _'

  " Restore view
  call winrestview(l:winview)

endfunction

nmap <silent> <leader>s :call SyntaxTreeRubyFormat()<CR>
@kddnewton
Copy link
Member

Thanks @SamSaffron this is great!

I'm not familiar with vimscript at all, but from what I understand, would it be useful to have a syntax tree option to format with a specific cursor position? I think I could build that in (it's already built into prettier, so I could port that here).

@SamSaffron
Copy link
Author

SamSaffron commented Jul 21, 2022

I see, as in retain cursor position after you perform the prettification. Yes that sound very handy.

I guess syntax tree would need to take in "buffer" and "current cursor position" vis STDIN somehow and return it in STDOUT....

I think implementation wise the easiest thing to do would be to support a comment like this at the top of the file:

# syntax-tree-cursor: 80,22

Then if syntax-tree sees it, it converts it to

# syntax-tree-cursor: 88,10

So programs integrating would ship in this "magic" comment at the head of STDIN?

I'm not familiar with vimscript at all,

:) that makes two of us, I was just fumbling my way around here

@kddnewton
Copy link
Member

I was thinking more you would pass it in as a command-line option, like bundle exec stree format --cursor-position=88,10.

@SamSaffron
Copy link
Author

I was thinking about this Kevin, how would you feed back the result to the user?

I was thinking many integrations go STDIN -> syntax tree -> STDOUT ... so it makes sense just to have this in STDIN for symmetry ,

@kddnewton
Copy link
Member

Going to close this for now. Folks can find this through search or using https://github.com/dense-analysis/ale, which has stree now preconfigured.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants