fortran-language-server icon indicating copy to clipboard operation
fortran-language-server copied to clipboard

Automatically add use statements from autocomplete

Open michaelkonecny opened this issue 6 years ago • 5 comments

It would be great if autocomplete could also suggest to me names from modules I haven't yet used. On selecting this suggestion, the use-only statement for this name would then be added automatically.

Preferred order in the autocomplete should be:

  • names from current scope
  • names from already used modules
  • names from other modules

I'm not sure off the top of my head if this is possible with LSP, I will try to do some research when I have a bit of time. Just putting the idea here in case anyone wants to think about it.

michaelkonecny avatar Oct 24 '18 12:10 michaelkonecny

This could be done using the additionalTextEdits property of the response object to a textDocument/completion request (see https://microsoft.github.io/language-server-protocol/specification#textDocument_completion).

What do you think @hansec, how hard would this be to implement?

michaelkonecny avatar Nov 18 '18 13:11 michaelkonecny

I think this wouldn't be too hard to add. The hardest part is providing useful suggestions in enough situations. Currently, I don't parse all statements in the code just definitions, use statements, and a few other things crucial to extracting defined objects and scopes. As a result, the language server can only currently identify objects that could be use'd in a few specific cases.

For example, currently I can tell if a user-defined type is used in a variable definition without being defined and search for it in available modules, but I cannot do the same for a variable/subroutine/function used in a general statement since those are not parsed at the moment.

Usage of an object without useing it is the main situation I can think of where this would be useful, but identifying those cases is limited at the moment without parsing more general statements. Can you think of other cases where suggestions might be made?

Even the limited cases available now might be useful though so I will look into this more when I get some time.

hansec avatar Nov 19 '18 18:11 hansec

I'm not sure I understand. The most useful case from my point of view is this:

module A
  contains
  subroutine subrInA()
  end subroutine
end module 

module B
  contains
  subroutine subrInB()
    ! use statements go here
    implicit none

    ! I'm typing here
  end subroutine
end module

When I type subr, I would like to get a suggestion for subrInA and for the use statement to be automatically added if I select it.

I thought this should be possible because if I do this:

  subroutine subrInB()
    use A, only: ! I'm typing here

I do get a suggestion for subrInA() if I type subr.

I might try to do this next month when I have time, if I manage to understand how the server works.

michaelkonecny avatar Nov 23 '18 16:11 michaelkonecny

Ok, I understand what you mean now. I will have to look in to how to do this type of action on inserting the suggestion.

The only problem I see with this is that it would require including all objects in the project in autocomplete results, which of course are then filtered by what you type. This may produce a lot of unwanted noise or could slow things down due to a large number of results, but of course it can always be an option not the default behavior.

hansec avatar Nov 25 '18 22:11 hansec

Regarding noise: that's why I suggested ordering the results by where they come from in my first message.

Regarding time: Yeah, I can see how this could get time consuming. Might be a good idea for the list you're looking through to be indexed, but it's probably worth testing even without indexing (might work fine), with an enable/disable option as you suggest.

michaelkonecny avatar Nov 27 '18 23:11 michaelkonecny