coc-flutter icon indicating copy to clipboard operation
coc-flutter copied to clipboard

[feature] fix LSP suggestion where initState snippet from language server implies code should be added above super.initState()

Open dkbast opened this issue 3 years ago • 6 comments

when overwriting initState of a statefulWidget using the context menu / quick fix we end up with

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

which is misleading as we want to call initState first and then add our own code according to the documentation https://github.com/flutter/flutter/blob/6eb65b376d822b7aa87372b7a001d839836eeb23/packages/flutter/lib/src/widgets/framework.dart#L981

This is how it should look like:

  @override
  void initState() {
    super.initState();
    // TODO: implement initState
  }

There is an active issue in the SDK ( https://github.com/dart-lang/sdk/issues/45783 ) but wouldn't that be something we can fix here?

Basically overwriting the behaviour for overwriting initState ?

If anybody can point me to a place to start, I'm more than happy to give it a try

dkbast avatar May 18 '22 14:05 dkbast

If you want to fix here, you can do in middleware, like improve the setState snippets here https://github.com/iamcco/coc-flutter/blob/32aaad1edc5b6f51734653771ef136ccf0c181f4/src/server/lsp/resolveCompleteItem.ts#L38-L43

iamcco avatar May 19 '22 08:05 iamcco

Thanks! - thats really looking like something I can do - can you point me to a guide on how I can setup a dev environment for coc-flutter to try this on my own machine?

dkbast avatar May 19 '22 15:05 dkbast

Thanks! - thats really looking like something I can do - can you point me to a guide on how I can setup a dev environment for coc-flutter to try this on my own machine?

1 clone repo 2 in the repo directory run yarn install or npm install 3 build repo with yarn build 4 run yarn link 5 goto ~/.config/coc/extensions directory and run yarn link coc-flutter to replace extension to local extension.

iamcco avatar May 20 '22 02:05 iamcco

@iamcco I did steps 1 to 5 and can confirm that ~/.config/coc/extensions/node_modules/coc-flutter is pointing to my development directory but I don't see any change in the behaviour of the plugin in vim, tried restarting vim and ':CocInstall' - anything else I might be missing?

I tried changing the setState resolver (adding a //test comment), but I don't see it in vim.

  if (label === 'setState(() {});' && insertTextFormat !== InsertTextFormat.Snippet) {
    item.insertText = ['setState(() {//test', '\t${1}', '});${0}'].join('\n');
    item.insertTextFormat = InsertTextFormat.Snippet;
    return item;
  }

dkbast avatar May 24 '22 11:05 dkbast

Check the lsp output to see setState if is InsertTextFormat.Snippet. this fix is for old lsp version switch return is not InsertTextFormat.Snippet.

iamcco avatar May 24 '22 15:05 iamcco

This should be changed in the dart analyzer IMHO

Kavantix avatar Sep 08 '22 17:09 Kavantix