SearchFile searches a file for comments. It infers the language
(filePath string, reader io.Reader, cb func(*Comment))
| 26 | |
| 27 | // SearchFile searches a file for comments. It infers the language |
| 28 | func SearchFile(filePath string, reader io.Reader, cb func(*Comment)) error { |
| 29 | // create a preview reader that reads in some of the file for enry to better identify the language |
| 30 | var buf bytes.Buffer |
| 31 | tee := io.TeeReader(reader, &buf) |
| 32 | previewReader := io.LimitReader(tee, 1000) |
| 33 | preview, err := ioutil.ReadAll(previewReader) |
| 34 | if err != nil { |
| 35 | return err |
| 36 | } |
| 37 | |
| 38 | // create a new reader concatenating the preview and the original reader (which has now been read from) |
| 39 | fullReader := io.MultiReader(strings.NewReader(buf.String()), reader) |
| 40 | |
| 41 | lang := Language(enry.GetLanguage(filepath.Base(filePath), preview)) |
| 42 | if enry.IsVendor(filePath) { |
| 43 | return nil |
| 44 | } |
| 45 | options, ok := LanguageParseOptions[lang] |
| 46 | if !ok { // TODO provide a default parse option for when we don't know how to handle a language? I.e. default to CStyle comments say |
| 47 | return nil |
| 48 | } |
| 49 | commentParser, err := lege.NewParser(options) |
| 50 | if err != nil { |
| 51 | return err |
| 52 | } |
| 53 | |
| 54 | collections, err := commentParser.Parse(fullReader) |
| 55 | if err != nil { |
| 56 | return err |
| 57 | } |
| 58 | |
| 59 | for _, c := range collections { |
| 60 | comment := Comment{*c, filePath} |
| 61 | cb(&comment) |
| 62 | } |
| 63 | |
| 64 | return nil |
| 65 | } |
| 66 | |
| 67 | // SearchDir searches a directory for comments |
| 68 | func SearchDir(dirPath string, cb func(comment *Comment)) error { |
no test coverage detected