MCPcopy
hub / github.com/go-task/task / NewSnippet

Function NewSnippet

taskfile/snippet.go:58–77  ·  view source on GitHub ↗

NewSnippet creates a new [Snippet] from a byte slice and a line and column number. The line and column numbers should be 1-indexed. For example, the first character in the file would be 1:1 (line 1, column 1). The padding determines the number of lines to include before and after the chosen line.

(b []byte, opts ...SnippetOption)

Source from the content-addressed store, hash-verified

56// first character in the file would be 1:1 (line 1, column 1). The padding
57// determines the number of lines to include before and after the chosen line.
58func NewSnippet(b []byte, opts ...SnippetOption) *Snippet {
59 snippet := &Snippet{}
60 snippet.Options(opts...)
61
62 // Syntax highlight the input and split it into lines
63 buf := &bytes.Buffer{}
64 if err := quick.Highlight(buf, string(b), "yaml", "terminal", "task"); err != nil {
65 buf.Write(b)
66 }
67 linesRaw := strings.Split(string(b), "\n")
68 linesHighlighted := strings.Split(buf.String(), "\n")
69
70 // Work out the start and end lines of the snippet
71 snippet.start = max(snippet.line-snippet.padding, 1)
72 snippet.end = min(snippet.line+snippet.padding, len(linesRaw)-1)
73 snippet.linesRaw = linesRaw[snippet.start-1 : snippet.end]
74 snippet.linesHighlighted = linesHighlighted[snippet.start-1 : snippet.end]
75
76 return snippet
77}
78
79// Options loops through the given [SnippetOption] functions and applies them
80// to the [Snippet].

Callers 3

TestNewSnippetFunction · 0.85
TestSnippetStringFunction · 0.85
readNodeMethod · 0.85

Calls 3

OptionsMethod · 0.95
WriteMethod · 0.45
StringMethod · 0.45

Tested by 2

TestNewSnippetFunction · 0.68
TestSnippetStringFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…