Skip to content
Permalink
Browse files

preview file content when identifying language

- clean up PHP support
  • Loading branch information
patrickdevivo committed Jan 18, 2020
1 parent d3c5761 commit e86cf31a7768b2c0510498f911d690bd32d7a440
Showing with 39 additions and 5 deletions.
  1. +16 −4 pkg/comments/comments.go
  2. +14 −0 pkg/comments/comments_test.go
  3. +1 −1 pkg/comments/languages.go
  4. +8 −0 pkg/comments/testdata/php/test.php
@@ -1,7 +1,9 @@
package comments

import (
"bytes"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
@@ -24,9 +26,19 @@ type Comment struct {

// SearchFile searches a file for comments. It infers the language
func SearchFile(filePath string, reader io.Reader, cb func(*Comment)) error {
// TODO right now, enry only infers the language based on the file extension
// we should add some "preview" bytes from the file so that it has some sample content to examine
lang := Language(enry.GetLanguage(filepath.Base(filePath), nil))
// create a preview reader that reads in some of the file for enry to better identify the language
var buf bytes.Buffer
tee := io.TeeReader(reader, &buf)
previewReader := io.LimitReader(tee, 1000)
preview, err := ioutil.ReadAll(previewReader)
if err != nil {
return err
}

// create a new reader concatenating the preview and the original reader (which has now been read from)
fullReader := io.MultiReader(strings.NewReader(buf.String()), reader)

lang := Language(enry.GetLanguage(filepath.Base(filePath), preview))
if enry.IsVendor(filePath) {
return nil
}
@@ -39,7 +51,7 @@ func SearchFile(filePath string, reader io.Reader, cb func(*Comment)) error {
return err
}

collections, err := commentParser.Parse(reader)
collections, err := commentParser.Parse(fullReader)
if err != nil {
return err
}
@@ -47,3 +47,17 @@ func TestRustFiles(t *testing.T) {
t.Fail()
}
}

func TestPHPFiles(t *testing.T) {
var comments Comments
err := SearchDir("testdata/php", func(comment *Comment) {
comments = append(comments, comment)
})
if err != nil {
t.Fatal(err)
}

if len(comments) != 3 {
t.Fail()
}
}
@@ -49,7 +49,7 @@ var LanguageParseOptions map[Language]*lege.ParseOptions = map[Language]*lege.Pa
"JavaScript": CStyleCommentOptions,
"Python": HashStyleCommentOptions,
"Ruby": HashStyleCommentOptions,
"PHP": CStyleCommentOptions,
"PHP": {Boundaries: append(CStyleCommentOptions.Boundaries, HashStyleCommentOptions.Boundaries...)},
"Shell": HashStyleCommentOptions,
"Visual Basic": {Boundaries: []lege.Boundary{{Start: "'", End: "\n"}}},
"TypeScript": CStyleCommentOptions,
@@ -0,0 +1,8 @@
<?php
// this is a comment

/* this is another comment */

# and one last one
?>

0 comments on commit e86cf31

Please sign in to comment.
You can’t perform that action at this time.