internal/commentpolicy/support.go
1
package commentpolicy
3
import (
4
"path/filepath"
5
"sort"
6
"strings"
7
)
9
// BlockDelimiter is one pair of markers that open and close a block comment.
10
type BlockDelimiter struct {
11
Open string
12
Close string
13
}
15
// FiletypeSupport describes how the marker scan finds comments in one
16
// filetype. It is the shape the generated language reference prints, so a
17
// filetype koment gains cannot appear in the code without appearing in the
18
// manual.
19
type FiletypeSupport struct {
20
Extension string
21
Line []string
22
Block []BlockDelimiter
23
Directives []string
24
}
26
// DetectedFiletypes reports every extension the syntax table names, sorted by
27
// extension.
28
func DetectedFiletypes() []FiletypeSupport {
29
supported := make([]FiletypeSupport, 0, len(syntaxByExtension))
30
for extension, syntax := range syntaxByExtension {
31
supported = append(supported, describe(extension, syntax))
32
}
33
sort.Slice(supported, func(earlier, later int) bool {
34
return supported[earlier].Extension < supported[later].Extension
35
})
36
return supported
37
}
39
func describe(extension string, syntax commentSyntax) FiletypeSupport {
40
described := FiletypeSupport{
41
Extension: extension,
42
Line: append([]string(nil), syntax.line...),
43
Directives: append([]string(nil), syntax.directives...),
44
}
45
for _, delimiter := range syntax.block {
46
described.Block = append(described.Block, BlockDelimiter{Open: delimiter.open, Close: delimiter.close})
47
}
48
return described
49
}
51
// FallbackMarkers reports the line markers koment assumes for an extension the
52
// syntax table does not name.
53
func FallbackMarkers() []string {
54
return append([]string(nil), fallbackSyntax.line...)
55
}
57
// UndetectedExtensions reports the prose and data formats koment never scans,
58
// sorted.
59
func UndetectedExtensions() []string {
60
extensions := make([]string, 0, len(uncommentableExtensions))
61
for extension := range uncommentableExtensions {
62
extensions = append(extensions, extension)
63
}
64
sort.Strings(extensions)
65
return extensions
66
}
68
// ScriptFilenames reports the extensionless filenames koment reads as shell,
69
// sorted.
70
func ScriptFilenames() []string {
71
names := append([]string(nil), scriptFilenames...)
72
sort.Strings(names)
73
return names
74
}
76
// DetectorName reports which detector claims a path, or an empty string when
77
// no detector does.
78
func DetectorName(file string) string {
79
detector := detectorFor(file)
80
if detector == nil {
81
return ""
82
}
83
return detector.Name()
84
}
86
const goExtension = ".go"
88
func hasScriptFilename(file string) bool {
89
base := strings.ToLower(filepath.Base(file))
90
for _, known := range scriptFilenames {
91
if base == known || strings.HasPrefix(base, known+".") {
92
return true
93
}
94
}
95
return false
96
}