added the ability to get comments and priority
This commit is contained in:
parent
f01ec4ca94
commit
1722e3128f
1 changed files with 47 additions and 84 deletions
131
cmd/root.go
131
cmd/root.go
|
|
@ -12,6 +12,7 @@ import (
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"strings"
|
"strings"
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"regexp"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -60,96 +61,58 @@ func init() {
|
||||||
rootCmd.PersistentFlags().StringP("path", "p", "","assign the path to get comments from.")
|
rootCmd.PersistentFlags().StringP("path", "p", "","assign the path to get comments from.")
|
||||||
}
|
}
|
||||||
func readFileTree(path string) {
|
func readFileTree(path string) {
|
||||||
var count int
|
fsys := os.DirFS(path)
|
||||||
fsys := os.DirFS(path)
|
|
||||||
|
|
||||||
// Walk through all files in the directory
|
// Walk through all files in the directory
|
||||||
fs.WalkDir(fsys, ".", func(p string, d fs.DirEntry, err error) error {
|
fs.WalkDir(fsys, ".", func(p string, d fs.DirEntry, err error) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if file has .go extension
|
// Check if file has .go extension
|
||||||
if filepath.Ext(p) == ".go" {
|
if filepath.Ext(p) == ".go" {
|
||||||
// Read the file and check for comments
|
// Read the file and check for comments
|
||||||
file, err := os.Open(filepath.Join(path, p))
|
file, err := os.Open(filepath.Join(path, p))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
// Process each line in the file
|
// Process each line in the file
|
||||||
scanner := bufio.NewScanner(file)
|
scanner := bufio.NewScanner(file)
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
line := scanner.Text()
|
line := scanner.Text()
|
||||||
|
|
||||||
// Check for C-style comments (// and /* */)
|
//TODO: P:0 add support for TODO's not at start of line
|
||||||
if strings.Contains(strings.TrimSpace(line), "//") {
|
// Check for C-style comments (// and /* */)
|
||||||
count++
|
if strings.HasPrefix(strings.TrimSpace(line), "//TODO:") {
|
||||||
fmt.Printf("comment: %s | current comment count:%d\n",strings.TrimSpace(line),count)
|
// Regular expression to match "P:" followed by a digit
|
||||||
}
|
re := regexp.MustCompile(`P:(\d)`)
|
||||||
if strings.Contains(line, "/*") && strings.Contains(line, "*/") {
|
|
||||||
count++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if there was any error reading the file
|
// Trim spaces and remove the "//TODO:" prefix
|
||||||
if err := scanner.Err(); err != nil {
|
trimmedLine := strings.TrimSpace(line)
|
||||||
return err
|
trimmedLine = strings.TrimPrefix(trimmedLine, "//TODO: ")
|
||||||
}
|
|
||||||
}
|
|
||||||
if filepath.Ext(p) == ".lua" {
|
|
||||||
// Read the file and check for comments
|
|
||||||
file, err := os.Open(filepath.Join(path, p))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
// Process each line in the file
|
// Try to find the priority
|
||||||
scanner := bufio.NewScanner(file)
|
matches := re.FindStringSubmatch(trimmedLine)
|
||||||
for scanner.Scan() {
|
|
||||||
line := scanner.Text()
|
// If a priority is found, remove it and print the comment and priority
|
||||||
|
if len(matches) > 1 {
|
||||||
|
// Remove the "P:<digit>" from the beginning of the line
|
||||||
|
trimmedLine = strings.Replace(trimmedLine, matches[0], "", 1) // Replace only the first occurrence
|
||||||
|
fmt.Printf("comment: %s | priority: %s \n", strings.TrimSpace(trimmedLine), matches[1])
|
||||||
|
} else {
|
||||||
|
fmt.Printf("comment: %s | priority: 9 \n", trimmedLine)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Check for Lua-style comments (--)
|
}
|
||||||
if strings.Contains(strings.TrimSpace(line), "--") {
|
|
||||||
count++
|
|
||||||
fmt.Printf("comment: %s | current comment count:%d\n",strings.TrimSpace(line),count)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Check if there was any error reading the file
|
|
||||||
if err := scanner.Err(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if filepath.Ext(p) == ".py" {
|
|
||||||
// Read the file and check for comments
|
|
||||||
file, err := os.Open(filepath.Join(path, p))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
// Process each line in the file
|
|
||||||
scanner := bufio.NewScanner(file)
|
|
||||||
for scanner.Scan() {
|
|
||||||
line := scanner.Text()
|
|
||||||
|
|
||||||
// Check for Python-style comments (#)
|
// Check if there was any error reading the file
|
||||||
if strings.Contains(strings.TrimSpace(line), "#") {
|
if err := scanner.Err(); err != nil {
|
||||||
count++
|
return err
|
||||||
fmt.Printf("comment: %s | current comment count:%d\n",strings.TrimSpace(line),count)
|
}
|
||||||
}
|
}
|
||||||
}
|
return nil
|
||||||
// Check if there was any error reading the file
|
})
|
||||||
if err := scanner.Err(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
// Print the number of comments found
|
|
||||||
fmt.Println("Comment count:", count)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue