本文整理汇总了Golang中go/ast.CommentGroup.Pos方法的典型用法代码示例。如果您正苦于以下问题:Golang CommentGroup.Pos方法的具体用法?Golang CommentGroup.Pos怎么用?Golang CommentGroup.Pos使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类go/ast.CommentGroup
的用法示例。
在下文中一共展示了CommentGroup.Pos方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: fixImportCheck
func fixImportCheck(body []byte, importPath string) ([]byte, error) {
fset := token.NewFileSet()
// todo: see if we can restrict the mode some more
f, err := parser.ParseFile(fset, "", body, parser.ParseComments)
if err != nil {
log.Fatal(err)
}
var after *ast.CommentGroup
var pos token.Pos = token.Pos(len(body))
for _, v := range f.Comments {
text := strings.TrimSpace(v.Text())
if v.Pos() > f.Package && v.Pos() < pos && strings.HasPrefix(text, "import") {
pos = v.Pos()
after = v
}
}
if after != nil && bytes.IndexByte(body[f.Package:pos], '\n') == -1 {
comment := fmt.Sprintf(`// import "%s"`, importPath)
buf := new(bytes.Buffer)
buf.Write(body[:after.Pos()-1])
buf.WriteString(comment)
buf.Write(body[after.End()-1:])
body = buf.Bytes()
}
return body, nil
}
示例2: parseBreakpoint
func parseBreakpoint(fset *token.FileSet, filename string, cg *ast.CommentGroup) (Breakpoint, error) {
var t Test
bp := Breakpoint{Filename: filename, Line: fset.Position(cg.Pos()).Line}
appendTest := func() {
if t.Debugger != "" {
bp.Tests = append(bp.Tests, t)
}
}
for _, comment := range cg.List[1:] {
line := strings.TrimSpace(comment.Text)
lineno := fset.Position(comment.Pos()).Line
if !strings.HasPrefix(line, "// ") {
continue
}
line = strings.TrimSpace(line[len("//"):])
// Check whether this is a new test. If so,
// save the previous test and start a new one.
switch {
case strings.HasPrefix(line, "(gdb) "):
appendTest()
t = Test{
Debugger: "gdb",
Command: strings.TrimSpace(line[len("(gdb)"):]),
Line: lineno,
}
continue
case strings.HasPrefix(line, "(lldb) "):
appendTest()
t = Test{
Debugger: "lldb",
Command: strings.TrimSpace(line[len("(lldb)"):]),
Line: lineno,
}
continue
}
// Not a new test; must be a Want from the current test.
if t.Debugger == "" {
// Oops, no current test
return bp, fmt.Errorf("%s:%d expected a (gdb) or (lldb) command", filename, lineno)
}
t.Want = append(t.Want, line)
}
// Save the last test.
appendTest()
return bp, nil
}