-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathlanguage.go
More file actions
68 lines (60 loc) · 2.13 KB
/
language.go
File metadata and controls
68 lines (60 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// SPDX-License-Identifier: MIT
package main
import (
"strings"
"github.com/boyter/scc/v3/processor"
)
// initLanguageDatabase initialises the scc language database.
// Must be called before detectLanguage or languageExtensions.
func initLanguageDatabase() {
processor.ProcessConstants()
}
// detectLanguage returns the language name for the given filename and content
// using the scc language database.
func detectLanguage(filename string, content []byte) string {
detected, _ := processor.DetectLanguage(filename)
if len(detected) >= 2 {
return processor.DetermineLanguage(filename, detected[0], detected, content)
}
if len(detected) == 1 {
return detected[0]
}
return ""
}
// fileCodeStats detects the language and computes SCC code stats for a file
// in a single call. Returns empty language and zero stats for unrecognised files.
func fileCodeStats(filename string, content []byte) (language string, lines, code, comment, blank, complexity int64, contentByteType []byte) {
language = detectLanguage(filename, content)
if language == "" {
return
}
sccJob := &processor.FileJob{
Filename: filename,
Language: language,
Content: content,
Bytes: int64(len(content)),
ClassifyContent: true,
}
processor.CountStats(sccJob)
return language, sccJob.Lines, sccJob.Code, sccJob.Comment, sccJob.Blank, sccJob.Complexity, sccJob.ContentByteType
}
// languageExtensions resolves language names to file extensions using the scc
// language database. Lookup is case-insensitive. It uses the ExtensionToLanguage
// map (extension → []languageName) built by ProcessConstants to invert the mapping.
func languageExtensions(languageNames []string) []string {
// Build set of desired language names (lowercased) for fast lookup
wanted := make(map[string]struct{}, len(languageNames))
for _, name := range languageNames {
wanted[strings.ToLower(strings.TrimSpace(name))] = struct{}{}
}
var exts []string
for ext, langs := range processor.ExtensionToLanguage {
for _, lang := range langs {
if _, ok := wanted[strings.ToLower(lang)]; ok {
exts = append(exts, ext)
break
}
}
}
return exts
}