Skip to content
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

AST support : "Quick import feature" #243

Open
rundis opened this issue Sep 28, 2016 · 2 comments
Open

AST support : "Quick import feature" #243

rundis opened this issue Sep 28, 2016 · 2 comments

Comments

@rundis
Copy link
Contributor

rundis commented Sep 28, 2016

Summary:

Allow a user to quickly add an import when in the context of a defintion, so that adding the import is just a keyboard shortcut away, without loosing the current context the user is in.

Details:

  1. User is inside a function definition and writes Json.decodeString and presses magic key combo
  2. The editor is able to figure out
    1. That the only module that exposes decodeString is the Json.Decode module
    2. That this module has not been already imported
  3. With that knowledge is able to add a new import declaration node to the AST with module: "Json.Decode", alias: "Json" (and no exposing node)
  4. The user may keep on typing with the knowledge the import is in place and that the Json alias know can be safely used (and will give autocompletions etc etc upon further usage)

So you would need:

  • a way to introspect all exposeds for all modules in the project (bar the ones you have already imported for the given module, and probably default imports)
  • a way to figure out based on cursor position that you are in range of a top level definition

Discusion:

  • Might partially be out of scope as it requires knowledge of other modules exposed functions
  • For it to work, an illegal state (partially) must be supported
@avh4
Copy link
Owner

avh4 commented Oct 27, 2016

As described above, I don't think this would require parsing files with syntax errors; in this example, the file is syntactically valid, but references some variables that are not in scope.

I think the discovery of exposed modules is beyond the scope of elm-format, especially since the elm-package.json file is already in json format.

However, elm-format could be used to discover the exposed functions within a module. With a simple AST output from elm-format, three things would have to be checked:

  1. if the module explicitly lists what it exposes, use that list
  2. if the module exposes (..), check all the normal top-level definitions (things like a = ... and f x = ....
  3. if the module exposes (..), check all the destructing top-level definitions (things like (a, b, c) = ... and {x, y} = ...

My current plan for the elm-format AST is to canonicalize all the variable references (meaning, for example, if you import Json.Encode exposing (Value), then an AST node of a reference to Value would actually indicate that it was referring to Json.Encode.Value rather than just Value. I think that would be ideal for tool authors, and should be doable in elm-format, but I'm not sure yet how much work will be involved.

a way to figure out based on cursor position that you are in range of a top level definition

I'm not sure what that means; aren't you always in range of top-level defintions?

@rundis
Copy link
Contributor Author

rundis commented Oct 27, 2016

Sounds great.

Re

a way to figure out based on cursor position that you are in range of a top level definition

I guess what I meant was a way to determine that you are in range of a valid top-level declaration (function, value, port , but not an import or module declaration, probably not if you are inside a comment etc). Thinking more about it would probably be better to let the client/tool handle this, as long as location information is present for all (relevant) ast nodes that would be simple enough.

@avh4 avh4 added the discussion label Mar 8, 2017
@avh4 avh4 modified the milestone: 2.0.0 public AST Mar 8, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants