diff --git a/service/dap/command.go b/service/dap/command.go index 0b08c569..a9690d2f 100644 --- a/service/dap/command.go +++ b/service/dap/command.go @@ -4,6 +4,7 @@ import ( "bytes" "errors" "fmt" + "sort" "strings" "github.com/go-delve/delve/pkg/config" @@ -37,24 +38,29 @@ type command struct { const ( msgHelp = `Prints the help message. -help [command] +dlv help [command] Type "help" followed by the name of a command for more information about it.` msgConfig = `Changes configuration parameters. - config -list + dlv config -list Show all configuration parameters. - config + dlv config Changes the value of a configuration parameter. - config substitutePath - config substitutePath + dlv config substitutePath + dlv config substitutePath Adds or removes a path substitution rule.` + msgSources = `Print list of source files. + + dlv sources [] + +If regex is specified only the source files matching it will be returned.` ) // debugCommands returns a list of commands with default commands defined. @@ -62,6 +68,7 @@ func debugCommands(s *Session) []command { return []command{ {aliases: []string{"help", "h"}, cmdFn: s.helpMessage, helpMsg: msgHelp}, {aliases: []string{"config"}, cmdFn: s.evaluateConfig, helpMsg: msgConfig}, + {aliases: []string{"sources", "s"}, cmdFn: s.sources, helpMsg: msgSources}, } } @@ -88,14 +95,14 @@ func (s *Session) helpMessage(_, _ int, args string) (string, error) { h = h[:idx] } if len(cmd.aliases) > 1 { - fmt.Fprintf(&buf, " %s (alias: %s) \t %s\n", cmd.aliases[0], strings.Join(cmd.aliases[1:], " | "), h) + fmt.Fprintf(&buf, " dlv %s (alias: %s) \t %s\n", cmd.aliases[0], strings.Join(cmd.aliases[1:], " | "), h) } else { - fmt.Fprintf(&buf, " %s \t %s\n", cmd.aliases[0], h) + fmt.Fprintf(&buf, " dlv %s \t %s\n", cmd.aliases[0], h) } } fmt.Fprintln(&buf) - fmt.Fprintln(&buf, "Type help followed by a command for full documentation.") + fmt.Fprintln(&buf, "Type 'dlv help' followed by a command for full documentation.") return buf.String(), nil } @@ -113,3 +120,12 @@ func (s *Session) evaluateConfig(_, _ int, expr string) (string, error) { return res, nil } } + +func (s *Session) sources(_, _ int, filter string) (string, error) { + sources, err := s.debugger.Sources(filter) + if err != nil { + return "", err + } + sort.Strings(sources) + return strings.Join(sources, "\n"), nil +} diff --git a/service/dap/server_test.go b/service/dap/server_test.go index cbd75b3a..24e1591c 100644 --- a/service/dap/server_test.go +++ b/service/dap/server_test.go @@ -3846,10 +3846,11 @@ func TestEvaluateCommandRequest(t *testing.T) { // Request help. const dlvHelp = `The following commands are available: - help (alias: h) Prints the help message. - config Changes configuration parameters. + dlv help (alias: h) Prints the help message. + dlv config Changes configuration parameters. + dlv sources (alias: s) Print list of source files. -Type help followed by a command for full documentation. +Type 'dlv help' followed by a command for full documentation. ` client.EvaluateRequest("dlv help", 1000, "repl") got := client.ExpectEvaluateResponse(t) @@ -3909,6 +3910,25 @@ Type help followed by a command for full documentation. got = client.ExpectEvaluateResponse(t) checkEval(t, got, "substitutePath\t[]\n\nUpdated", noChildren) + // Test sources. + client.EvaluateRequest("dlv sources", 1000, "repl") + got = client.ExpectEvaluateResponse(t) + if !strings.Contains(got.Body.Result, fixture.Source) { + t.Errorf("\ngot: %#v, want sources contains %s", got, fixture.Source) + } + + client.EvaluateRequest(fmt.Sprintf("dlv sources .*%s", strings.ReplaceAll(filepath.Base(fixture.Source), ".", "\\.")), 1000, "repl") + got = client.ExpectEvaluateResponse(t) + if got.Body.Result != fixture.Source { + t.Errorf("\ngot: %#v, want sources=%q", got, fixture.Source) + } + + client.EvaluateRequest("dlv sources nonexistentsource", 1000, "repl") + got = client.ExpectEvaluateResponse(t) + if got.Body.Result != "" { + t.Errorf("\ngot: %#v, want sources=\"\"", got) + } + // Test bad inputs. client.EvaluateRequest("dlv help bad", 1000, "repl") client.ExpectErrorResponse(t)