Skip to content

Commit 4747800

Browse files
Merge pull request #42 from augmentable-dev/add-file-preview
preview file content when identifying language
2 parents d3c5761 + e86cf31 commit 4747800

File tree

4 files changed

+39
-5
lines changed

4 files changed

+39
-5
lines changed

pkg/comments/comments.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package comments
22

33
import (
4+
"bytes"
45
"io"
6+
"io/ioutil"
57
"os"
68
"path/filepath"
79
"strings"
@@ -24,9 +26,19 @@ type Comment struct {
2426

2527
// SearchFile searches a file for comments. It infers the language
2628
func SearchFile(filePath string, reader io.Reader, cb func(*Comment)) error {
27-
// TODO right now, enry only infers the language based on the file extension
28-
// we should add some "preview" bytes from the file so that it has some sample content to examine
29-
lang := Language(enry.GetLanguage(filepath.Base(filePath), nil))
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))
3042
if enry.IsVendor(filePath) {
3143
return nil
3244
}
@@ -39,7 +51,7 @@ func SearchFile(filePath string, reader io.Reader, cb func(*Comment)) error {
3951
return err
4052
}
4153

42-
collections, err := commentParser.Parse(reader)
54+
collections, err := commentParser.Parse(fullReader)
4355
if err != nil {
4456
return err
4557
}

pkg/comments/comments_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,17 @@ func TestRustFiles(t *testing.T) {
4747
t.Fail()
4848
}
4949
}
50+
51+
func TestPHPFiles(t *testing.T) {
52+
var comments Comments
53+
err := SearchDir("testdata/php", func(comment *Comment) {
54+
comments = append(comments, comment)
55+
})
56+
if err != nil {
57+
t.Fatal(err)
58+
}
59+
60+
if len(comments) != 3 {
61+
t.Fail()
62+
}
63+
}

pkg/comments/languages.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ var LanguageParseOptions map[Language]*lege.ParseOptions = map[Language]*lege.Pa
4949
"JavaScript": CStyleCommentOptions,
5050
"Python": HashStyleCommentOptions,
5151
"Ruby": HashStyleCommentOptions,
52-
"PHP": CStyleCommentOptions,
52+
"PHP": {Boundaries: append(CStyleCommentOptions.Boundaries, HashStyleCommentOptions.Boundaries...)},
5353
"Shell": HashStyleCommentOptions,
5454
"Visual Basic": {Boundaries: []lege.Boundary{{Start: "'", End: "\n"}}},
5555
"TypeScript": CStyleCommentOptions,

pkg/comments/testdata/php/test.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
// this is a comment
3+
4+
/* this is another comment */
5+
6+
# and one last one
7+
?>
8+

0 commit comments

Comments
 (0)