Haskell-language-server: [Feature Request] Add missing fields in record

Created on 17 Aug 2020  路  7Comments  路  Source: haskell/haskell-language-server

It would be great if HLS could hint a code action to add (and import) missing fields of a record.

Say I write (using the process library)

import System.Process (CreateProcess(CreateProcess), CmdSpec(RawCommand))

proc = CreateProcess { cmdspec = RawCommand "echo" [ "hello world" ] }

many other fields are missing (see doc), and need to be imported.

good first issue enhancement

Most helpful comment

A completion provider would be great. But in Rust, it's also a diagnostic fix, which has been a very nice way to use it.

All 7 comments

I think this should be a completion provider

A completion provider would be great. But in Rust, it's also a diagnostic fix, which has been a very nice way to use it.

I plan to take a stab at this, thanks to @pepeiborra's awesome tutorial on how to write a plugin.

So, should I start exploring this as a completion provider? I was able to explore the Example.hs, so far to build a sample code snippet so that each field in the record could correspond to a placeholder.

Can one of you provide me pointers, on where I can explore the AST information in ghcide for this?

Look at how completions are done in ghcide, in particular check https://github.com/haskell/ghcide/blob/9ae5134d79972405415ce5062dbae67aeb938f21/src/Development/IDE/Plugin/Completions/Logic.hs#L232

You will need to generate new completions for records that include not only the constructor, but also the fields. For that you will need to find all the TyThings that correspond to constructors fo record types and figure out the names of all the fields.

I am testing this now, and we do get completions for records. I have added some tests to show what I found so far:
https://github.com/haskell/ghcide/pull/804

Looks, like what we need is inspection of currently used fields in the context and provide a code action for missing fields. Which kind of suggests, we should use diagnostics as the original issue comment suggested.

Now, I need to look at, how to determine if I am inside a context of record creation, inspect the currently declared fields and identify the missing fields. Some scenarios, where this will not be correct is when def is used. At that point a code-action provided in that context may not be accurate.

Of course, in terms of enhancements, creating a code-snippet, as soon as we see a CiConstructor would also be nice. That could be a different issue/task.

I observed and added tests to show that missing record fields are already being report under diagnostics by hls. https://github.com/haskell/ghcide/pull/824.

Please suggest if we would like to extend this any further as part of this issue. Perhaps, we need a code action generated for each missing field?

I have started iterating over the changes required for this feature. For now, I have support for Local completions. Please could I have some feedback on the initial approach and if I can proceed in this path to support completions that are available from other modules. #512

Was this page helpful?
0 / 5 - 0 ratings