|
| 1 | +// Parses code bookmarks from project source code's annotations |
| 2 | +package bookmarkparser |
| 3 | + |
| 4 | +import ( |
| 5 | + "context" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "regexp" |
| 10 | + |
| 11 | + "github.com/function61/gokit/encoding/jsonfile" |
| 12 | + "github.com/function61/turbobob/pkg/annotationparser" |
| 13 | +) |
| 14 | + |
| 15 | +type ID string |
| 16 | + |
| 17 | +var ( |
| 18 | + // starting with URL safe characters (to easily represent in URLs) restrictive set is always safer to widen than the other way around. |
| 19 | + // subset of https://stackoverflow.com/a/695469 |
| 20 | + idRe = regexp.MustCompile(`^[a-zA-Z0-9\-_]+$`) |
| 21 | +) |
| 22 | + |
| 23 | +func (i ID) Validate() error { |
| 24 | + if !idRe.MatchString(string(i)) { |
| 25 | + return fmt.Errorf("bookmark ID '%s' does not match regex '%s'", i, idRe.String()) |
| 26 | + } |
| 27 | + |
| 28 | + return nil |
| 29 | +} |
| 30 | + |
| 31 | +type Bookmark struct { |
| 32 | + ID ID // (hopefully) permanent ID, semantic "symbol" of the bookmark. i.e. favor naming by the concept rather than directly e.g. function name. |
| 33 | + File string // path relative to repository root |
| 34 | + Line int // line being referenced |
| 35 | +} |
| 36 | + |
| 37 | +// @:{"bookmark": "ParseBookmarks"} |
| 38 | +func ParseBookmarks(ctx context.Context, dir string, output io.Writer) error { |
| 39 | + allAnnotations := []annotationparser.Location{} |
| 40 | + |
| 41 | + // parses annotations, but not yet mapped to bookmarks |
| 42 | + if err := annotationparser.ScanDirectoryFilesRecursively(ctx, dir, func(annotation annotationparser.Location) error { |
| 43 | + allAnnotations = append(allAnnotations, annotation) |
| 44 | + return nil |
| 45 | + }, annotationparser.DefaultParsersForFileTypes); err != nil { |
| 46 | + return err |
| 47 | + } |
| 48 | + |
| 49 | + bookmarks, err := annotationsToBookmarks(allAnnotations) |
| 50 | + if err != nil { |
| 51 | + return err |
| 52 | + } |
| 53 | + |
| 54 | + return jsonfile.Marshal(output, bookmarks) |
| 55 | +} |
| 56 | + |
| 57 | +func annotationsToBookmarks(annotations []annotationparser.Location) ([]Bookmark, error) { |
| 58 | + withErr := func(err error) ([]Bookmark, error) { return nil, fmt.Errorf("annotationsToBookmarks: %w", err) } |
| 59 | + |
| 60 | + bookmarks := []Bookmark{} |
| 61 | + for _, annotation := range annotations { |
| 62 | + bookmarkID, hasBookmark := annotation.Annotations["bookmark"] |
| 63 | + if !hasBookmark { |
| 64 | + continue |
| 65 | + } |
| 66 | + |
| 67 | + id_, ok := bookmarkID.(string) |
| 68 | + if !ok { |
| 69 | + return withErr(errors.New("bookmark ID not string")) |
| 70 | + } |
| 71 | + |
| 72 | + id := ID(id_) |
| 73 | + |
| 74 | + if err := id.Validate(); err != nil { |
| 75 | + return withErr(err) |
| 76 | + } |
| 77 | + |
| 78 | + bookmarks = append(bookmarks, Bookmark{ |
| 79 | + ID: id, |
| 80 | + File: annotation.File, |
| 81 | + Line: annotation.LineTarget(), |
| 82 | + }) |
| 83 | + } |
| 84 | + |
| 85 | + return bookmarks, nil |
| 86 | +} |
0 commit comments