Using Dart Analysis Server for IntelliJ plugin development for Flutter

106 views
Skip to first unread message

Shirish Kadam

unread,
Mar 21, 2023, 11:51:55 AM3/21/23
to Dart Analyzer Discussion
Hey y'all,

I am building an IntelliJ plugin to wrap, extract and add new Flutter widgets to an existing widget tree in a Flutter project. This is a GUI plugin and hence live template is not a viable option. So, far I was trying to get the Dart PSI to identify the containing/parent widget at a cursor offset to add a selected widget from the plugin GUI, which I was not able to figure out. While doing so I came across Dart Analysis Server. 

Can I identify the parent widget at the cursor of the IntelliJ IDE editor in a project and add a new widget to its child or children? Any help is appreciated. 

Thanks.

Brian Wilkerson

unread,
Mar 21, 2023, 12:12:09 PM3/21/23
to analyzer...@dartlang.org
The analysis server communicates with clients, such as your IntelliJ plugin, using one of two protocols: either https://github.com/dart-lang/sdk/blob/main/pkg/analysis_server/doc/api.html or https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification. The Dart plugin provided by Jetbrains uses the first of those two.

Unfortunately, neither of those protocols allow the kind of fine grained querying that I think you're asking for. While it would be fairly straightforward to add the kind of support you're asking about within the analysis server, it isn't able to help you solve that problem from your plugin.

If you haven't already, you might open an issue on the Jetbrains issue tracker (https://youtrack.jetbrains.com/issues/IDEA) to get help using their PSI model.

--
You received this message because you are subscribed to the Google Groups "Dart Analyzer Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to analyzer-discu...@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/analyzer-discuss/aa8b9850-e18d-45c5-9a29-71a57cb06b92n%40dartlang.org.

Shirish Kadam

unread,
Mar 21, 2023, 12:24:13 PM3/21/23
to Dart Analyzer Discussion, brianwilkerson
Hi Brain, Thanks for the response. I agree, I just realised this after tinkering with the `DartAnalysisServerService` class. A few methods as per Server analysis API specs are missing in the class like `findMemberDeclarations` and so on. (ref: https://htmlpreview.github.io/?https://github.com/dart-lang/sdk/blob/master/pkg/analysis_server/doc/api.html#request_search.findMemberDeclarations)

I have a follow-up question. If this functionality is added how will it not help me query through the plugin? I think I am missing something. Please help so I can create an appropriate ticket or consider a contribution.

Thanks

Brian Wilkerson

unread,
Mar 21, 2023, 12:40:36 PM3/21/23
to Shirish Kadam, Dart Analyzer Discussion
If this functionality is added how will it not help me query through the plugin?

Sorry, I think I failed to communicate clearly.

If you were contributing assists to the analysis server that could "wrap, extract and add new Flutter widgets to an existing widget tree", then you would have access to our AST structure, which would easily allow you to find the "containing/parent widget at a cursor offset". What you wouldn't have is the ability to allow the user to use the GUI to provide any of the required inputs to the operation. I don't think that would support the kind of tool you're trying to build.

But if you're doing this from your plugin on the client side, then the analysis server doesn't have any protocols to access that kind of information. Nor do we have any plans to add that level of API to the protocol.

Shirish Kadam

unread,
Mar 21, 2023, 1:37:56 PM3/21/23
to Dart Analyzer Discussion, brianwilkerson, Dart Analyzer Discussion, Shirish Kadam
Oh I understand, I am only trying to replicate the functionality of Android Studio and extend it as a plugin. Android Studio currently lets you wrap a widget with a Row, Column, Padding etc. or Swap a child or move, up or down a widget. I would like to extend it and offer more widgets to be wrapped beyond the currently offered. So, I was trying to figure out how the Flutter plugin implemented this to IntelliJ as an Intention Action.

Why I am doing that? Well, just like UI designers have a design system that they use across projects to reuse components like an Email Input box. I would like to make a private widget library and make it available as an intent action in the IDE for ease of use. Any ideas on how I can go about figuring that out? 
Maybe I should dive deep into the flutter plugin source code for Android Studio? Will that help?

PFA: screenshot of Android Studio intention action to wrap a specific widget around the widget where my cursor is.

Thanks a bunch.

Screenshot 2023-03-21 at 10.57.43 PM.png

Brian Wilkerson

unread,
Mar 21, 2023, 2:26:19 PM3/21/23
to analyzer...@dartlang.org, Shirish Kadam
So, I was trying to figure out how the Flutter plugin implemented this to IntelliJ as an Intention Action.

That's a question I can answer. The intention actions you're referring to are provided by the analysis server as a response to the `edit.getAssists` request.

I would like to make a private widget library and make it available as an intent action in the IDE for ease of use.

There isn't currently any way to do that, but the idea has come up before.

We haven't had time to pursue it, but the idea I like best is to provide a way for a widget author, like yourself, to tell the analysis server about the widgets that should be offered in a "wrap" operation. There are some interesting design challenges that would need to be addressed, such as how much control to give users vs widget authors. It might prove to be too difficult, but I'm hoping we'll have the cycles to look into it after the next release.

In the meantime, a plugin ought to be able to do the same thing, but it likely won't be as easy because you'd be missing a lot of support logic that the analysis server already has.

Shirish Kadam

unread,
Mar 21, 2023, 2:58:10 PM3/21/23
to Dart Analyzer Discussion, brianwilkerson, Shirish Kadam
Interesting, meaning I'll just have to rely on Dart PSI offered by IntelliJ for now. It won't be that accurate, but it should do for now I suppose. I have filed an issue on the flutter plugin repo to get the conversation started. https://github.com/flutter/flutter-intellij/issues/6688

If there is any other ongoing discussion on this topic please share I'd like to follow it and contribute.

Thanks. G'day.
Reply all
Reply to author
Forward
0 new messages