Debian Codegraph @

gopls/internal/cmd/folding_range.go

raw symbols SCIP
1 // Copyright 2019 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package cmd
6
7 import (
8 "context"
9 "flag"
10 "fmt"
11
12 "golang.org/x/tools/gopls/internal/protocol"
13 "golang.org/x/tools/gopls/internal/tool"
14 )
15
16 // foldingRanges implements the folding_ranges verb for gopls
17 type foldingRanges struct {
18 app *Application
19 }
20
21 func (r *foldingRanges) Name() string { return "folding_ranges" }
22 func (r *foldingRanges) Parent() string { return r.app.Name() }
23 func (r *foldingRanges) Usage() string { return "<file>" }
24 func (r *foldingRanges) ShortHelp() string { return "display selected file's folding ranges" }
25 func (r *foldingRanges) DetailedHelp(f *flag.FlagSet) {
26 fmt.Fprint(f.Output(), `
27 Example:
28
29 $ gopls folding_ranges helper/helper.go
30 `)
31 printFlagDefaults(f)
32 }
33
34 func (r *foldingRanges) Run(ctx context.Context, args ...string) error {
35 if len(args) != 1 {
36 return tool.CommandLineErrorf("folding_ranges expects 1 argument (file)")
37 }
38
39 cli, _, err := r.app.connect(ctx)
40 if err != nil {
41 return err
42 }
43 defer cli.terminate(ctx)
44
45 from := parseSpan(args[0])
46 if _, err := cli.openFile(ctx, from.URI()); err != nil {
47 return err
48 }
49
50 p := protocol.FoldingRangeParams{
51 TextDocument: protocol.TextDocumentIdentifier{
52 URI: from.URI(),
53 },
54 }
55
56 ranges, err := cli.server.FoldingRange(ctx, &p)
57 if err != nil {
58 return err
59 }
60
61 for _, r := range ranges {
62 // We assume our server always supplies these fields.
63 fmt.Printf("%v:%v-%v:%v\n",
64 *r.StartLine+1,
65 *r.StartCharacter+1,
66 *r.EndLine+1,
67 *r.EndCharacter+1,
68 )
69 }
70
71 return nil
72 }