devtools
devtools copied to clipboard
Define standard event for navigating IDEs to view a file + line + column
Currently the inspector does a lot of extra work to navigate to select a widget. We should instead dispatch an event with a file + line + column indicating IDEs should navigate to that location.
Flutter should dispatch that event directly rather than relying on IDEs listening for the dart:developer inspect event. DevTools should also dispatch the event to trigger code navigation in the IDE from all places where DevTools wants to trigger navigation in the IDE.
Fyi @bkonyi as another option would be have a first class dart:developer API for this rather than just a convention Flutter, DevTools, and IDEs follow.
That's potentially an option we could investigate. Can you file an issue against the SDK?
Created: https://github.com/dart-lang/sdk/issues/49606
@CoderDake fixed this. You can now call this tool event to navigate in IDEs https://github.com/flutter/flutter/blob/9c72f5a7e62e63c199739267f3feeee0559caf11/packages/flutter/lib/src/widgets/widget_inspector.dart#L1518
postEvent(
'navigate',
<String, Object>{
'fileUri': location.file, // URI file path of the location.
'line': location.line, // 1-based line number.
'column': location.column, // 1-based column number.
'source': 'flutter.inspector',
},
stream: 'ToolEvent',
);
```
This works great, thanks!
Just one small thing I noticed: If the fileUri is invalid, somehow the IDE seems to disconnect from the attached app
In particular, I sent instead of sending fileUri: 'file///path/to/file', I sent /path/to/file
@DanTup could this be a bug on the Dart-Code side of handling 'ToolEvent' events?
It sounds like a bug somewhere if an invalid URI is causing a disconnect. I don't know if it's in Dart-Code or the DAP, but I'll try to repro and fix.
FWIW it is required to be a URI to avoid any confusion with the format of this (for example between Windows and Linux/Mac) and to support non-paths (such as google3:// or package:) which will be resolved on the way through the debug adapter, it should always be a URI. The original expectation was that it was always a file URI (which is why it was named fileUri) but it was only afterwards we found that g3 couldn't provide local file paths. Both DAP and Dart-Code support just uri as well as fileUri, but it doesn't look like Flutter was changed. If VS Code is currently the only client of this, I wonder if we should change the Flutter inspector to uri? (@CoderDake do you recall if there was a reason we didn't do that?)
@rrousselGit do you have steps to reproduce? I tried adding this to a counter app, but it doesn't fail, it just doesn't navigate (which is what I was expecting):
void _incrementCounter() {
print('Sending navigation event!');
developer.postEvent(
'navigate',
{
// Good:
// 'uri': Platform.script.toString(),
// Bad:
'uri': Platform.script.toFilePath(),
'line': 1,
'column': 1,
'source': 'ketchup',
},
stream: 'ToolEvent',
);
setState(() {
_counter++;
});
}
It happened to me when I was sending a non-URI
Like: 'uri': '/path/to/file.dart'
It could be an issue when if the scheme is missing there's an internal exception
@rrousselGit in the example above I sent a non-URI ('uri': Platform.script.toFilePath(),) but I was unable to cause any disconnect. If you're still able to reproduce this and provide a code sample I'd be interested in debugging. Invalid data might prevent navigating, but it shouldn't cause anything else to fail.
I'll try and make a reproducible example next time I see this happen