heavenshell / vim-jsdoc

Generate JSDoc to your JavaScript code.
BSD 3-Clause "New" or "Revised" License
452 stars 44 forks source link

Autodetect params type #66

Closed kopach closed 7 years ago

kopach commented 7 years ago

There is code-analysis engine for JavaScript named Tern which actually analyses js code very well. One of the feature - it can automatically, based on the code, determine the type of variable/parameter. You can try it out online on this demo page. Also, there is vim plugin for this.

So, my question is: is there any chase vim-jsdoc plugin could be integrated with the Tern engine to automatically generate/suggest types for @parameters/@return statements?

heavenshell commented 7 years ago

@kopach Thx for comment. Sorry if my understanding was wrong.

Currently no. JSDoc.vim can only generate from method/function signature. I don't want depend on any specific plugin.

Flow/TypeScript is supported, but Flow/TypeScript have typed params. So, JSDoc.vim can generate type.

kopach commented 7 years ago

To make it clear what Tern could do - take a look on image below: ezgif com-optimize

And it can analyze much more complex code. In my opinion, it would be super cool to have such suggestions of arguments types based on code analysis when possible. So, type of @parameters could be automatically generated for me.

davidosomething commented 7 years ago

@kopach that would be more appropriate to add into the tern_for_vim plugin, you should ask there https://github.com/ternjs/tern_for_vim

related https://github.com/ternjs/tern_for_vim/issues/124

kopach commented 7 years ago

@davidosomething, but the idea is not to create TypeScript from JavaScript. But to generate JsDoc with some pre filled information when possible. tern_for_vim it's just a port for Tern in vim, it's not for documentation generating.

davidosomething commented 7 years ago

yes I know exactly what you're looking for

tern_for_vim is not a port for tern in vim. It is an interface and wrapper around the tern.js server and tern.js API. It starts a tern server and makes calls to that local tern server's API from within vim.

tern_for_vim already has the methods to get the static types from tern server, all it needs to do is return it as a string instead of :echoing it to vim's message area; then you can make a mapping to insert it into the file in the appropriate jsdoc format.

kopach commented 7 years ago

yes, exactly. So, maybe, there is a chance to implement this communication with tern_for_vim plugin in JSDoc.vim? ;-) Maybe with some option which could be even disabled by default. Of course after tern_for_vim will be able to return a string instead of :echo.

heavenshell commented 7 years ago

@kopach @davidosomething Thx for your information. I understood your requirement.

There are few problems.

  1. Can tern_for_vim get type at once? for example, screenchast get string and number by :TernType command. move cursol to param1 and type :TernType, and move cursol to param2 and type TernType Is there any command or api to get param1 and param2 type at once? It's much better to get return type at the same time. If not, someone should write get each type and concat(format?) types code. (I don't use tern_for_vim)
  2. I don't want to depend on any library, even if it is optional I want keep jsdoc.vim simple. No dependencies.

So I'm sorry, currently I don't have any plan to communicate with tern_for_vim or any other library.

Following is a just design idea. No plan to implement

What about user can inject params/return type? Create new api to accept injectred user defined function.

" your .vimrc or whatever
function! s:inject_type()
  " I don't know tern_for_vim has following api.
  let currnet_types = tern_for_vim#get_current_types()
  " format current_types to
  " [{'val': 'param1', 'type': 'string'}, {'val': 'param2', 'type': number}]
   let current_types = s:format(currnet_types)

  " inject to https://github.com/heavenshell/vim-jsdoc/blob/master/autoload/jsdoc.vim#L124 
   return current_types
endfunction
call jsdoc#inject_type(s:currnet_types)

when :jsdoc was executed, injected function s:inject_type() also executed. and override @params line by injected data.

JSDoc.vim only have injection api and user should write code self and it's user's responsibilty.

Anyway I think this is a big change and of course tern_for_vim implements above requirements.

Maybe It's time to refactor(rewrite?) all jsdoc.vim? :-p