lsp-dart icon indicating copy to clipboard operation
lsp-dart copied to clipboard

[Feature Request] Wrap the widget under the point into another widget(e.g. container)

Open zw963 opened this issue 3 years ago • 11 comments

Basically, we have the similar feature in left flutter outline window, as following screenshot.

image

but, i think it not conveniently because find the widget under the point in this outline window is not so easy anyway, so i consider, we can improve this two different way.

  1. Following current editing widget in flutter outline window, that is, when we are editing a Container, we can active that Container in flutter outline async, because we are a issue as we discussed on #127, if this is not possible for performance reason, we can try 2
  2. Add a right menu sub-item to enable wrap element on the point into the following default right click menu.

image

zw963 avatar Jan 27 '22 10:01 zw963

You can do this with eglot using eglot-code-actions

PatrickWulfe avatar Jun 10 '22 19:06 PatrickWulfe

Via code actions this is possible as well using lsp-mode

ericdallo avatar Jun 10 '22 21:06 ericdallo

It's not clear to me what is the feature requested @zw963, code actions already provide wrapping widget image

ericdallo avatar Jun 10 '22 21:06 ericdallo

Wired, It still not works for me ...

image

image

image

zw963 avatar Jun 11 '22 08:06 zw963

That's weird indeed, all those cases work for me, @DanTup is there anything on the server-side that may not return "wrap widget" code actions?

ericdallo avatar Jun 11 '22 14:06 ericdallo

I'm not sure without a repro. I notice in the screenshots above there's a difference with return Foo() vs final app = Foo();.. does that change things?

If not, some exact code this repros on and a log of the LSP traffic might help figure it out.

DanTup avatar Jun 11 '22 18:06 DanTup

You are correct @DanTup ! I don't get wrap widget code actions if trying on a widget that belongs to a variable: final foo = MaterialApp(); I suppose this is expected as the server has no easy way to know this is the return of the build method or a widget

ericdallo avatar Jun 11 '22 18:06 ericdallo

I think it was just a bug. I don't see any reason to restrict them to where the widget is being returned. There was code and tests for the following:

Widget w;
w = Container();

I think it was just an oversight that variable declarations weren't also handled. I've opened a change at https://dart-review.googlesource.com/c/sdk/+/248125. Once landed, it will show up in a future Dart/Flutter SDK release.

DanTup avatar Jun 13 '22 17:06 DanTup

I try record IO in the last wrappered example.

[Trace - 10:17:59 AM] Sending request 'textDocument/codeAction - (40)'.
Params: {
  "textDocument": {
    "uri": "file:///home/zw963/test_project/lib/app.dart"
  },
  "range": {
    "start": {
      "line": 199,
      "character": 16
    },
    "end": {
      "line": 199,
      "character": 16
    }
  },
  "context": {
    "diagnostics": []
  }
}


[Trace - 10:17:59 AM] Received response 'textDocument/codeAction - (40)' in 17ms.
Result: [
  {
    "title": "Sort Members",
    "kind": "source.sortMembers",
    "command": {
      "title": "Sort Members",
      "command": "edit.sortMembers",
      "arguments": [
        "/home/zw963/test_project/lib/app.dart"
      ]
    }
  },
  {
    "title": "Organize Imports",
    "kind": "source.organizeImports",
    "command": {
      "title": "Organize Imports",
      "command": "edit.organizeImports",
      "arguments": [
        "/home/zw963/test_project/lib/app.dart"
      ]
    }
  },
  {
    "title": "Fix All",
    "kind": "source.fixAll",
    "command": {
      "title": "Fix All",
      "command": "edit.fixAll",
      "arguments": [
        "/home/zw963/test_project/lib/app.dart"
      ]
    }
  },
  {
    "title": "Extract Method",
    "kind": "refactor.extract",
    "command": {
      "title": "Extract Method",
      "command": "refactor.perform",
      "arguments": [
        "EXTRACT_METHOD",
        "/home/zw963/test_project/lib/app.dart",
        0,
        6228,
        0,
        null
      ]
    }
  },
  {
    "title": "Extract Local Variable",
    "kind": "refactor.extract",
    "command": {
      "title": "Extract Local Variable",
      "command": "refactor.perform",
      "arguments": [
        "EXTRACT_LOCAL_VARIABLE",
        "/home/zw963/test_project/lib/app.dart",
        0,
        6228,
        0,
        null
      ]
    }
  },
  {
    "title": "Extract Widget",
    "kind": "refactor.extract",
    "command": {
      "title": "Extract Widget",
      "command": "refactor.perform",
      "arguments": [
        "EXTRACT_WIDGET",
        "/home/zw963/test_project/lib/app.dart",
        0,
        6228,
        0,
        null
      ]
    }
  }
]

zw963 avatar Jun 14 '22 02:06 zw963

Yep, i can make it works when i try to directly show action on following returned widget instead.

image

Following is lsp output log

http://dpaste.com/E78YBYJTY

Another strange things is, when i select widget use region, then show action, it get different result.

image

zw963 avatar Jun 14 '22 02:06 zw963

Awesome @DanTup thank you! @zw963 we can keep this open until the fix on dart-sdk is merged

ericdallo avatar Jun 14 '22 13:06 ericdallo