-
Notifications
You must be signed in to change notification settings - Fork 375
Expand file tree
/
Copy pathcodeview_controller.dart
More file actions
561 lines (480 loc) · 17.6 KB
/
codeview_controller.dart
File metadata and controls
561 lines (480 loc) · 17.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
// Copyright 2022 The Flutter Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file or at https://developers.google.com/open-source/licenses/bsd.
/// @docImport 'codeview.dart';
library;
import 'dart:async';
import 'dart:convert';
import 'package:devtools_app_shared/utils.dart';
import 'package:flutter/foundation.dart';
import 'package:logging/logging.dart';
import 'package:vm_service/vm_service.dart';
import '../../shared/diagnostics/primitives/source_location.dart';
import '../../shared/framework/app_error_handling.dart';
import '../../shared/framework/routing.dart';
import '../../shared/globals.dart';
import '../../shared/managers/notifications.dart';
import '../../shared/primitives/history_manager.dart';
import '../../shared/ui/search.dart';
import '../../shared/utils/utils.dart';
import '../vm_developer/vm_service_private_extensions.dart';
import 'debugger_model.dart';
import 'program_explorer_controller.dart';
import 'syntax_highlighter.dart';
final _log = Logger('codeview_controller');
class CodeViewController extends DisposableController
with
AutoDisposeControllerMixin,
SearchControllerMixin<SourceToken>,
RouteStateHandlerMixin {
CodeViewController() {
init();
}
@override
void init() {
super.init();
_scriptHistoryListener = () async {
final currentScriptValue = scriptsHistory.current.value;
if (currentScriptValue != null) {
await _showScriptLocation(ScriptLocation(currentScriptValue));
}
};
scriptsHistory.current.addListener(_scriptHistoryListener);
}
@override
void dispose() {
scriptsHistory.current.removeListener(_scriptHistoryListener);
_scriptLocation.dispose();
_currentScriptRef.dispose();
parsedScript.dispose();
_showSearchInFileField.dispose();
_showFileOpener.dispose();
_librariesVisible.dispose();
programExplorerController.dispose();
scriptsHistory.dispose();
_showCodeCoverage.dispose();
_showProfileInformation.dispose();
_focusLine.dispose();
super.dispose();
}
/// Perform operations based on changes in navigation state.
///
/// This method is only invoked if [subscribeToRouterEvents] has been called on
/// this instance with a valid [DevToolsRouterDelegate].
@override
Future<void> onRouteStateUpdate(DevToolsNavigationState state) async {
switch (state.kind) {
case CodeViewSourceLocationNavigationState.type:
await _handleNavigationEvent(state);
break;
}
}
Future<void> _handleNavigationEvent(DevToolsNavigationState state) async {
final processedState = CodeViewSourceLocationNavigationState._fromState(
state,
);
final object = processedState.object;
_navigationInProgress = true;
await showScriptLocation(processedState.location, focusLine: true);
if (programExplorerController.initialized.value) {
if (object != null) {
final node = programExplorerController.findOutlineNode(object);
if (node != null) {
programExplorerController.selectOutlineNode(node);
} else {
// If the object isn't associated with an outline node, clear
// the current outline selection.
programExplorerController.clearOutlineSelection();
}
} else {
programExplorerController.clearOutlineSelection();
}
}
_navigationInProgress = false;
}
/// Whether there is a [CodeViewSourceLocationNavigationState] currently being
/// processed and handled.
bool get navigationInProgress => _navigationInProgress;
bool _navigationInProgress = false;
ValueListenable<ScriptLocation?> get scriptLocation => _scriptLocation;
final _scriptLocation = ValueNotifier<ScriptLocation?>(null);
ValueListenable<ScriptRef?> get currentScriptRef => _currentScriptRef;
final _currentScriptRef = ValueNotifier<ScriptRef?>(null);
ValueListenable<ParsedScript?> get currentParsedScript => parsedScript;
@visibleForTesting
final parsedScript = ValueNotifier<ParsedScript?>(null);
ValueListenable<bool> get showSearchInFileField => _showSearchInFileField;
final _showSearchInFileField = ValueNotifier<bool>(false);
ValueListenable<bool> get showFileOpener => _showFileOpener;
final _showFileOpener = ValueNotifier<bool>(false);
ValueListenable<bool> get fileExplorerVisible => _librariesVisible;
final _librariesVisible = ValueNotifier(false);
final programExplorerController = ProgramExplorerController();
final scriptsHistory = ScriptsHistory();
late VoidCallback _scriptHistoryListener;
ValueListenable<bool> get showCodeCoverage => _showCodeCoverage;
final _showCodeCoverage = ValueNotifier<bool>(false);
ValueListenable<bool> get showProfileInformation => _showProfileInformation;
final _showProfileInformation = ValueNotifier<bool>(false);
/// Specifies which line should have focus applied in [CodeView].
///
/// A line can be focused by invoking `showScriptLocation` with `focusLine`
/// set to true.
ValueListenable<int> get focusLine => _focusLine;
final _focusLine = ValueNotifier<int>(-1);
void toggleShowCodeCoverage() {
_showCodeCoverage.value = !_showCodeCoverage.value;
}
void toggleShowProfileInformation() {
_showProfileInformation.value = !_showProfileInformation.value;
}
void clearState() {
// It would be nice to not clear the script history but it is currently
// coupled to ScriptRef objects so that is unsafe.
scriptsHistory.clear();
parsedScript.value = null;
_currentScriptRef.value = null;
_scriptLocation.value = null;
_librariesVisible.value = false;
}
void clearScriptHistory() {
scriptsHistory.clear();
}
/// Callback to be called when the debugger screen is first loaded.
///
/// We delay calling this method until the debugger screen is first loaded
/// for performance reasons. None of the code here needs to be called when
/// DevTools first connects to an app, and doing so inhibits DevTools from
/// connecting to low-end devices.
Future<void> maybeSetupProgramExplorer() async {
await _maybeSetUpProgramExplorer();
addAutoDisposeListener(
currentScriptRef,
() => unawaited(_maybeSetUpProgramExplorer()),
);
}
Future<void> _maybeSetUpProgramExplorer() async {
if (!programExplorerController.initialized.value) {
programExplorerController.initListeners();
unawaited(programExplorerController.initialize());
}
if (currentScriptRef.value != null) {
await programExplorerController.selectScriptNode(currentScriptRef.value);
programExplorerController.resetOutline();
}
}
Future<Script?> getScriptForRef(ScriptRef ref) async {
final cachedScript = scriptManager.getScriptCached(ref);
if (cachedScript == null) {
return await scriptManager.getScript(ref);
}
return cachedScript;
}
/// Jump to the given ScriptRef and optional SourcePosition.
Future<void> showScriptLocation(
ScriptLocation scriptLocation, {
bool focusLine = false,
}) async {
// TODO(elliette): This is here so that when a program is selected in the
// program explorer, the file opener will close (if it was open). Instead,
// give the program explorer focus so that the focus changes so the file
// opener will close automatically when its focus is lost.
toggleFileOpenerVisibility(false);
final succeeded = await _showScriptLocation(
scriptLocation,
focusLine: focusLine,
);
if (succeeded) {
// Update the scripts history (and make sure we don't react to the
// subsequent event).
scriptsHistory.current.removeListener(_scriptHistoryListener);
scriptsHistory.pushEntry(scriptLocation.scriptRef);
scriptsHistory.current.addListener(_scriptHistoryListener);
}
}
Future<void> refreshCodeStatistics() async {
final current = parsedScript.value;
if (current == null) {
return;
}
final isolateRef =
serviceConnection.serviceManager.isolateManager.selectedIsolate.value!;
final processedReport = await _getSourceReport(isolateRef, current.script);
parsedScript.value = ParsedScript(
script: current.script,
highlighter: current.highlighter,
executableLines: current.executableLines,
sourceReport: processedReport,
);
}
/// Resets the current script information before invoking [showScriptLocation].
Future<void> resetScriptLocation(ScriptLocation scriptLocation) async {
_scriptLocation.value = null;
_currentScriptRef.value = null;
parsedScript.value = null;
await showScriptLocation(scriptLocation);
}
/// Show the given script location (without updating the script navigation
/// history).
///
/// Returns a boolean value representing success or failure.
Future<bool> _showScriptLocation(
ScriptLocation scriptLocation, {
bool focusLine = false,
}) async {
final scriptRef = scriptLocation.scriptRef;
if (scriptRef.id != parsedScript.value?.script.id) {
// Try to parse the script if it isn't the currently parsed script:
final script = await _parseScript(scriptRef);
if (script == null) {
// Return early and indicate failure if parsing fails.
reportError(
'Failed to parse ${scriptRef.uri}.',
stack: StackTrace.current,
);
return false;
}
parsedScript.value = script;
}
_currentScriptRef.value = scriptRef;
if (focusLine) {
_focusLine.value = scriptLocation.location?.line ?? -1;
}
// We want to notify regardless of the previous scriptLocation, temporarily
// set to null to ensure that happens.
_scriptLocation.value = null;
_scriptLocation.value = scriptLocation;
return true;
}
Future<ProcessedSourceReport> _getSourceReport(
IsolateRef isolateRef,
Script script,
) async {
final hitLines = <int>{};
final missedLines = <int>{};
try {
final report = await serviceConnection.serviceManager.service!
.getSourceReport(
isolateRef.id!,
// TODO(bkonyi): make _Profile a public report type.
// See https://github.com/dart-lang/sdk/issues/50641
const [SourceReportKind.kCoverage, '_Profile'],
scriptId: script.id!,
reportLines: true,
);
for (final range in report.ranges!) {
final coverage = range.coverage!;
hitLines.addAll(coverage.hits!);
missedLines.addAll(coverage.misses!);
}
final profileReport = report.asProfileReport(script);
return ProcessedSourceReport(
coverageHitLines: hitLines,
coverageMissedLines: missedLines,
profilerEntries: profileReport.profileRanges
.fold<Map<int, ProfileReportEntry>>(
{},
(last, e) => last..addAll(e.entries),
),
);
} catch (e, st) {
// Ignore - not supported for all vm service implementations.
_log.warning(e, e, st);
}
return const ProcessedSourceReport.empty();
}
/// Parses the given script into executable lines and prepares the script
/// for syntax highlighting.
Future<ParsedScript?> _parseScript(ScriptRef scriptRef) async {
final isolateRef =
serviceConnection.serviceManager.isolateManager.selectedIsolate.value;
if (isolateRef == null) return null;
final script = await getScriptForRef(scriptRef);
if (script == null || script.source == null) return null;
// Create a new SyntaxHighlighter with the script's source in preparation
// for building the code view.
final highlighter = SyntaxHighlighter(source: script.source);
// Gather the data to display breakable lines.
var executableLines = <int>{};
try {
final positions = await breakpointManager.getBreakablePositions(
isolateRef,
script,
);
executableLines = Set.from(
positions.where((p) => p.line != null).map((p) => p.line),
);
if (executableLines.isEmpty) {
_maybeShowSourceMapsWarning();
}
} catch (e, st) {
// Ignore - not supported for all vm service implementations.
_log.warning(e, e, st);
}
final processedReport = await _getSourceReport(isolateRef, script);
return ParsedScript(
script: script,
highlighter: highlighter,
executableLines: executableLines,
sourceReport: processedReport,
);
}
/// Make the 'Libraries' view on the right-hand side of the screen visible or
/// hidden.
void toggleLibrariesVisible() {
toggleFileOpenerVisibility(false);
_librariesVisible.value = !_librariesVisible.value;
}
void toggleSearchInFileVisibility(bool visible) {
final fileExists = _currentScriptRef.value != null;
_showSearchInFileField.value = visible && fileExists;
if (!visible) {
resetSearch();
}
}
void toggleFileOpenerVisibility(bool visible) {
_showFileOpener.value = visible;
}
void _maybeShowSourceMapsWarning() {
final isWebApp =
serviceConnection.serviceManager.connectedApp?.isDartWebAppNow ?? false;
final enableSourceMapsLink = devToolsEnvironmentParameters
.enableSourceMapsLink();
if (isWebApp && enableSourceMapsLink != null) {
final enableSourceMapsAction = NotificationAction(
label: 'Enable sourcemaps',
onPressed: () =>
unawaited(launchUrlWithErrorHandling(enableSourceMapsLink.url)),
);
notificationService.pushNotification(
NotificationMessage(
'Cannot debug when sourcemaps are disabled.',
isError: true,
isDismissible: true,
actions: [enableSourceMapsAction],
),
);
}
}
// TODO(kenz): search through previous matches when possible.
@override
List<SourceToken> matchesForSearch(
String search, {
bool searchPreviousMatches = false,
}) {
if (search.isEmpty || parsedScript.value == null) {
return [];
}
final matches = <SourceToken>[];
final caseInsensitiveSearch = search.toLowerCase();
final currentScript = parsedScript.value!;
for (int i = 0; i < currentScript.lines.length; i++) {
final line = currentScript.lines[i].toLowerCase();
final matchesForLine = caseInsensitiveSearch.allMatches(line);
if (matchesForLine.isNotEmpty) {
matches.addAll(
matchesForLine.map(
(m) => SourceToken(
position: SourcePosition(line: i, column: m.start),
length: m.end - m.start,
),
),
);
}
}
return matches;
}
}
class ProcessedSourceReport {
ProcessedSourceReport({
required this.coverageHitLines,
required this.coverageMissedLines,
required this.profilerEntries,
});
const ProcessedSourceReport.empty()
: coverageHitLines = const <int>{},
coverageMissedLines = const <int>{},
profilerEntries = const <int, ProfileReportEntry>{};
final Set<int> coverageHitLines;
final Set<int> coverageMissedLines;
final Map<int, ProfileReportEntry> profilerEntries;
}
/// Maintains the navigation history of the debugger's code area - which files
/// were opened, whether it's possible to navigate forwards and backwards in the
/// history, ...
class ScriptsHistory extends HistoryManager<ScriptRef> {
// TODO(devoncarew): This class should also record and restore scroll
// positions.
final _openedScripts = <ScriptRef>{};
bool get hasScripts => _openedScripts.isNotEmpty;
void pushEntry(ScriptRef ref) {
if (ref == current.value) return;
while (hasNext) {
pop();
}
_openedScripts.remove(ref);
_openedScripts.add(ref);
push(ref);
}
Iterable<ScriptRef> get openedScripts => _openedScripts.toList().reversed;
}
class ParsedScript {
ParsedScript({
required this.script,
required this.highlighter,
required this.executableLines,
required this.sourceReport,
}) : lines = (script.source?.split('\n') ?? const []).toList();
final Script script;
final SyntaxHighlighter highlighter;
final Set<int> executableLines;
final ProcessedSourceReport? sourceReport;
final List<String> lines;
int get lineCount => lines.length;
}
/// State used to inform [CodeViewController]s listening for
/// [DevToolsNavigationState] changes to display a specific source location.
class CodeViewSourceLocationNavigationState extends DevToolsNavigationState {
CodeViewSourceLocationNavigationState({
required ScriptRef script,
required int line,
ObjRef? object,
}) : super(
kind: type,
state: <String, String?>{
_kScriptId: script.id,
_kUri: script.uri,
_kLine: line.toString(),
if (object != null) _kObject: json.encode(object.json),
},
);
CodeViewSourceLocationNavigationState._fromState(
DevToolsNavigationState state,
) : super(kind: type, state: state.state);
static CodeViewSourceLocationNavigationState? fromState(
DevToolsNavigationState? state,
) {
if (state?.kind != type) return null;
return CodeViewSourceLocationNavigationState._fromState(state!);
}
static const _kScriptId = 'scriptId';
static const _kUri = 'uri';
static const _kLine = 'line';
static const _kObject = 'object';
static const type = 'codeViewSourceLocation';
ScriptRef get script => ScriptRef(id: state[_kScriptId]!, uri: state[_kUri]);
int get line => int.parse(state[_kLine]!);
ObjRef? get object {
final obj = state[_kObject];
if (obj == null) {
return null;
}
return createServiceObject(json.decode(obj), const []) as ObjRef?;
}
ScriptLocation get location =>
ScriptLocation(script, location: SourcePosition(line: line, column: 1));
@override
String toString() {
return 'kind: $kind script: ${script.uri} line: $line object: ${object?.id}';
}
}