cuelang.org/go@v0.10.1/internal/golangorgx/gopls/golang/util.go (about) 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 golang 6 7 import ( 8 "context" 9 "regexp" 10 11 "cuelang.org/go/internal/golangorgx/gopls/cache" 12 "cuelang.org/go/internal/golangorgx/gopls/protocol" 13 "cuelang.org/go/internal/golangorgx/gopls/util/safetoken" 14 ) 15 16 // IsGenerated gets and reads the file denoted by uri and reports 17 // whether it contains a "generated file" comment as described at 18 // https://golang.org/s/generatedcode. 19 // 20 // TODO(adonovan): opt: this function does too much. 21 // Move snapshot.ReadFile into the caller (most of which have already done it). 22 func IsGenerated(ctx context.Context, snapshot *cache.Snapshot, uri protocol.DocumentURI) bool { 23 fh, err := snapshot.ReadFile(ctx, uri) 24 if err != nil { 25 return false 26 } 27 pgf, err := snapshot.ParseGo(ctx, fh, ParseHeader) 28 if err != nil { 29 return false 30 } 31 for _, commentGroup := range pgf.File.Comments { 32 for _, comment := range commentGroup.List { 33 if matched := generatedRx.MatchString(comment.Text); matched { 34 // Check if comment is at the beginning of the line in source. 35 if safetoken.Position(pgf.Tok, comment.Slash).Column == 1 { 36 return true 37 } 38 } 39 } 40 } 41 return false 42 } 43 44 // Matches cgo generated comment as well as the proposed standard: 45 // 46 // https://golang.org/s/generatedcode 47 var generatedRx = regexp.MustCompile(`// .*DO NOT EDIT\.?`)