Skip to content

Commit

Permalink
Merge pull request #90 from hhatto/just-lang
Browse files Browse the repository at this point in the history
Just lang
  • Loading branch information
hhatto authored Jul 15, 2024
2 parents bb0cab6 + f42734b commit 4a7dfac
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 19 deletions.
34 changes: 25 additions & 9 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,33 @@ scannerloop:
line = trimBOM(line)
}

singleloop:
for _, singleComment := range language.lineComments {
if strings.HasPrefix(line, singleComment) {
// check if single comment is a prefix of multi comment
for _, ml := range language.multiLines {
if ml[0] != "" && strings.HasPrefix(line, ml[0]) {
break singleloop
if len(language.regexLineComments) > 0 {
singleloopRegex:
for _, singleCommentRegex := range language.regexLineComments {
if singleCommentRegex.MatchString(line) {
// check if single comment is a prefix of multi comment
for _, ml := range language.multiLines {
if ml[0] != "" && strings.HasPrefix(line, ml[0]) {
break singleloopRegex
}
}
onComment(clocFile, opts, len(inComments) > 0, line, lineOrg)
continue scannerloop
}
}
} else {
singleloop:
for _, singleComment := range language.lineComments {
if strings.HasPrefix(line, singleComment) {
// check if single comment is a prefix of multi comment
for _, ml := range language.multiLines {
if ml[0] != "" && strings.HasPrefix(line, ml[0]) {
break singleloop
}
}
onComment(clocFile, opts, len(inComments) > 0, line, lineOrg)
continue scannerloop
}
onComment(clocFile, opts, len(inComments) > 0, line, lineOrg)
continue scannerloop
}
}

Expand Down
44 changes: 44 additions & 0 deletions file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,3 +528,47 @@ const color = "blue"
t.Errorf("invalid logic. lang=%v", clocFile.Lang)
}
}

func TestAnalayzeFile4Just(t *testing.T) {
tmpfile, err := os.CreateTemp("", "tmp.go")
if err != nil {
t.Logf("os.CreateTemp() error. err=[%v]", err)
return
}
defer os.Remove(tmpfile.Name())

if _, err := tmpfile.Write([]byte(`polyglot: python js perl sh ruby nu
python:
#!/usr/bin/env python3
print('Hello from python!')
js:
#!/usr/bin/env node
console.log('Greetings from JavaScript!') # with comment
# this is comment
`),
); err != nil {
t.Fatalf("tmpfile.Write() error. err=[%v]", err)
}

language := NewLanguage("Just", []string{"#"}, [][]string{{"", ""}}).
WithRegexLineComments([]string{`^#[^!].*`})
clocOpts := NewClocOptions()
clocFile := AnalyzeFile(tmpfile.Name(), language, clocOpts)
tmpfile.Close()

if clocFile.Blanks != 3 {
t.Errorf("invalid logic. blanks=%v", clocFile.Blanks)
}
if clocFile.Comments != 1 {
t.Errorf("invalid logic. comments=%v", clocFile.Comments)
}
if clocFile.Code != 7 {
t.Errorf("invalid logic. code=%v", clocFile.Code)
}
if clocFile.Lang != "Just" {
t.Errorf("invalid logic. lang=%v", clocFile.Lang)
}
}
30 changes: 22 additions & 8 deletions language.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ type ClocLanguage struct {

// Language is a type used to definitions and store statistics for one programming language.
type Language struct {
Name string
lineComments []string
multiLines [][]string
Files []string
Code int32
Comments int32
Blanks int32
Total int32
Name string
lineComments []string
regexLineComments []*regexp.Regexp
multiLines [][]string
Files []string
Code int32
Comments int32
Blanks int32
Total int32
}

// Languages is an array representation of Language.
Expand Down Expand Up @@ -211,6 +212,7 @@ var Exts = map[string]string{
"janet": "Janet",
"json": "JSON",
"jsx": "JSX",
"just": "Just",
"kak": "KakouneScript",
"kk": "Koka",
"kt": "Kotlin",
Expand Down Expand Up @@ -430,6 +432,8 @@ func getFileType(path string, opts *ClocOptions) (ext string, ok bool) {
}

switch strings.ToLower(base) {
case "justfile":
return "just", true
case "makefile":
return "makefile", true
case "nukefile":
Expand Down Expand Up @@ -459,6 +463,15 @@ func NewLanguage(name string, lineComments []string, multiLines [][]string) *Lan
}
}

func (l *Language) WithRegexLineComments(regexLineComments []string) *Language {
var regexLineCommentsCompiled []*regexp.Regexp
for _, r := range regexLineComments {
regexLineCommentsCompiled = append(regexLineCommentsCompiled, regexp.MustCompile(r))
}
l.regexLineComments = regexLineCommentsCompiled
return l
}

func lang2exts(lang string) (exts string) {
var es []string
for ext, l := range Exts {
Expand Down Expand Up @@ -590,6 +603,7 @@ func NewDefinedLanguages() *DefinedLanguages {
"JavaScript": NewLanguage("JavaScript", []string{"//"}, [][]string{{"/*", "*/"}}),
"Julia": NewLanguage("Julia", []string{"#"}, [][]string{{"#:=", ":=#"}}),
"Jupyter Notebook": NewLanguage("Jupyter Notebook", []string{"#"}, [][]string{{"", ""}}),
"Just": NewLanguage("Just", []string{"#"}, [][]string{{"", ""}}).WithRegexLineComments([]string{`^#[^!].*`}),
"JSON": NewLanguage("JSON", []string{}, [][]string{{"", ""}}),
"JSX": NewLanguage("JSX", []string{"//"}, [][]string{{"/*", "*/"}}),
"KakouneScript": NewLanguage("KakouneScript", []string{"#"}, [][]string{{"", ""}}),
Expand Down
9 changes: 7 additions & 2 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,15 @@ func getAllFiles(paths []string, languages *DefinedLanguages, opts *ClocOptions)
}

if _, ok := result[targetExt]; !ok {
result[targetExt] = NewLanguage(
definedLang := NewLanguage(
languages.Langs[targetExt].Name,
languages.Langs[targetExt].lineComments,
languages.Langs[targetExt].multiLines)
languages.Langs[targetExt].multiLines,
)
if len(languages.Langs[targetExt].regexLineComments) > 0 {
definedLang.regexLineComments = languages.Langs[targetExt].regexLineComments
}
result[targetExt] = definedLang
}
result[targetExt].Files = append(result[targetExt].Files, path)
}
Expand Down

0 comments on commit 4a7dfac

Please sign in to comment.