Chromium Code Reviews| Index: tools/ddbg_service/lib/debugger.dart |
| diff --git a/tools/ddbg_service/lib/debugger.dart b/tools/ddbg_service/lib/debugger.dart |
| index a5dbb6d9cb64db557729aaa6e66c220c0ba10f31..10d0a517319cfeddb0a0f6508ee11647903bde5d 100644 |
| --- a/tools/ddbg_service/lib/debugger.dart |
| +++ b/tools/ddbg_service/lib/debugger.dart |
| @@ -14,6 +14,8 @@ class Debugger { |
| Commando cmdo; |
| var _cmdoSubscription; |
| + CommandList _commands; |
| + |
| VM _vm; |
| VM get vm => _vm; |
| set vm(VM vm) { |
| @@ -91,10 +93,16 @@ class Debugger { |
| } |
| Debugger() { |
| - cmdo = new Commando(completer: completeCommand); |
| + cmdo = new Commando(completer: _completeCommand); |
| _cmdoSubscription = cmdo.commands.listen(_processCommand, |
| onError: _cmdoError, |
| onDone: _cmdoDone); |
| + _commands = new CommandList(); |
| + _commands.register(new AttachCommand()); |
| + _commands.register(new DetachCommand()); |
| + _commands.register(new HelpCommand(_commands)); |
| + _commands.register(new IsolateCommand()); |
| + _commands.register(new QuitCommand()); |
|
turnidge
2014/09/17 16:06:24
CommandList is now an object and owned by the Debu
|
| } |
| Future _closeCmdo() { |
| @@ -116,7 +124,7 @@ class Debugger { |
| }); |
| } |
| - void _cmdoError(self, parent, zone, error, StackTrace trace) { |
| + void _cmdoError(error, StackTrace trace) { |
| cmdo.print('\n--------\nExiting due to unexpected error:\n' |
| ' $error\n$trace\n'); |
| quit(); |
| @@ -126,6 +134,10 @@ class Debugger { |
| quit(); |
| } |
| + List<String> _completeCommand(List<String> commandParts) { |
| + return _commands.complete(commandParts); |
| + } |
| + |
| void _processCommand(String cmdLine) { |
| void huh() { |
| cmdo.print("'$cmdLine' not understood, try 'help' for help."); |
| @@ -138,7 +150,7 @@ class Debugger { |
| return; |
| } |
| var command = args[0]; |
| - var matches = matchCommand(command, true); |
| + var matches = _commands.match(command, true); |
| if (matches.length == 0) { |
| huh(); |
| cmdo.show(); |
| @@ -161,6 +173,10 @@ abstract class Command { |
| String get helpShort; |
| void printHelp(Debugger debugger, List<String> args); |
| Future run(Debugger debugger, List<String> args); |
| + List<String> complete(List<String> commandParts) { |
| + return ["$name ${commandParts.join(' ')}"]; |
|
turnidge
2014/09/17 16:06:24
Commands can now implement command-specific comple
Cutch
2014/09/17 20:07:26
Nice!
|
| + |
| + } |
| } |
| class AttachCommand extends Command { |
| @@ -203,56 +219,44 @@ Usage: |
| } |
| } |
| -class DetachCommand extends Command { |
| - final name = 'detach'; |
| - final helpShort = 'Detach from a running Dart VM'; |
| - void printHelp(Debugger debugger, List<String> args) { |
| - cmdo.print(''' |
| ------ detach ----- |
| - |
| -Detach from the Dart VM. |
| +class CommandList { |
| + List _commands = new List<Command>(); |
| -Usage: |
| - detach |
| -'''); |
| + void register(Command cmd) { |
| + _commands.add(cmd); |
| } |
| - Future run(Debugger debugger, List<String> args) { |
| - var cmdo = debugger.cmdo; |
| - if (args.length > 1) { |
| - cmdo.print('$name expects no arguments'); |
| - return new Future.value(); |
| - } |
| - if (debugger.vm == null) { |
| - cmdo.print('No VM is attached'); |
| - } else { |
| - debugger.vm = null; |
| + List<Command> match(String commandName, bool exactMatchWins) { |
| + var matches = []; |
| + for (var command in _commands) { |
| + if (command.name.startsWith(commandName)) { |
| + if (exactMatchWins && command.name == commandName) { |
| + // Exact match |
| + return [command]; |
| + } else { |
| + matches.add(command); |
| + } |
| + } |
| } |
| - return new Future.value(); |
| + return matches; |
| } |
| -} |
| - |
| -class HelpCommand extends Command { |
| - final name = 'help'; |
| - final helpShort = 'Show a list of debugger commands'; |
| - void printHelp(Debugger debugger, List<String> args) { |
| - debugger.cmdo.print(''' |
| ------ help ----- |
| -Show a list of debugger commands or get more information about a |
| -particular command. |
| - |
| -Usage: |
| - help |
| - help <command> |
| -'''); |
| + List<String> complete(List<String> commandParts) { |
| + var completions = new List<String>(); |
| + String prefix = commandParts[0]; |
| + for (var command in _commands) { |
| + if (command.name.startsWith(prefix)) { |
| + completions.addAll(command.complete(commandParts.sublist(1))); |
| + } |
| + } |
| + return completions; |
| } |
| - Future run(Debugger debugger, List<String> args) { |
| + void printHelp(Debugger debugger, List<String> args) { |
| var cmdo = debugger.cmdo; |
| if (args.length <= 1) { |
| cmdo.print("\nDebugger commands:\n"); |
| - for (var command in commandList) { |
| + for (var command in _commands) { |
| cmdo.print(' ${command.name.padRight(11)} ${command.helpShort}'); |
| } |
| cmdo.print("For more information about a particular command, type:\n\n" |
| @@ -261,7 +265,7 @@ Usage: |
| cmdo.print("Commands may be abbreviated: e.g. type 'h' for 'help.\n"); |
| } else { |
| var commandName = args[1]; |
| - var matches = matchCommand(commandName, true); |
| + var matches =match(commandName, true); |
| if (matches.length == 0) { |
| cmdo.print("Command '$commandName' not recognized. " |
| "Try 'help' for a list of commands."); |
| @@ -271,7 +275,34 @@ Usage: |
| } |
| } |
| } |
| + } |
| +} |
| +class DetachCommand extends Command { |
| + final name = 'detach'; |
| + final helpShort = 'Detach from a running Dart VM'; |
| + void printHelp(Debugger debugger, List<String> args) { |
| + cmdo.print(''' |
| +----- detach ----- |
| + |
| +Detach from the Dart VM. |
| + |
| +Usage: |
| + detach |
| +'''); |
| + } |
| + |
| + Future run(Debugger debugger, List<String> args) { |
| + var cmdo = debugger.cmdo; |
| + if (args.length > 1) { |
| + cmdo.print('$name expects no arguments'); |
| + return new Future.value(); |
| + } |
| + if (debugger.vm == null) { |
| + cmdo.print('No VM is attached'); |
| + } else { |
| + debugger.vm = null; |
| + } |
| return new Future.value(); |
| } |
| } |
| @@ -375,6 +406,17 @@ Usage: |
| }); |
| return new Future.value(); |
| } |
| + |
| + List<String> complete(List<String> commandParts) { |
| + if (commandParts.isEmpty) { |
| + return ['$name ${commandParts.join(" ")}']; |
| + } else { |
| + var completions = _commands.complete(commandParts); |
| + return completions.map((completion) { |
| + return '$name $completion'; |
| + }); |
| + } |
| + } |
| } |
| class QuitCommand extends Command { |
| @@ -400,38 +442,3 @@ Usage: |
| return debugger.quit(); |
| } |
| } |
| - |
| -List<Command> commandList = |
| - [ new AttachCommand(), |
| - new DetachCommand(), |
| - new HelpCommand(), |
| - new IsolateCommand(), |
| - new QuitCommand() ]; |
| - |
| -List<Command> matchCommand(String commandName, bool exactMatchWins) { |
| - var matches = []; |
| - for (var command in commandList) { |
| - if (command.name.startsWith(commandName)) { |
| - if (exactMatchWins && command.name == commandName) { |
| - // Exact match |
| - return [command]; |
| - } else { |
| - matches.add(command); |
| - } |
| - } |
| - } |
| - return matches; |
| -} |
| - |
| -List<String> completeCommand(List<String> commandParts) { |
| - var completions = new List<String>(); |
| - if (commandParts.length == 1) { |
| - String prefix = commandParts[0]; |
| - for (var command in commandList) { |
| - if (command.name.startsWith(prefix)) { |
| - completions.add(command.name); |
| - } |
| - } |
| - } |
| - return completions; |
| -} |