style: apply code formatting and linting fixes

- Remove unnecessary blank lines and improve spacing
- Fix linting warnings (remove redundant nil checks)
- Standardize indentation and formatting
- No functional changes, only style improvements
This commit is contained in:
carraes
2025-07-06 12:01:32 -03:00
parent ea1a50ba33
commit fae70c6b2b
5 changed files with 32 additions and 81 deletions
+9 -30
View File
@@ -8,12 +8,10 @@ import (
"github.com/BurntSushi/toml"
)
type Config struct {
Preserve []string `toml:"preserve"`
}
func Default() *Config {
return &Config{
Preserve: []string{
@@ -32,7 +30,6 @@ func Default() *Config {
}
}
func Load() (*Config, string, error) {
if configPath, found := findProjectConfig(); found {
@@ -43,7 +40,6 @@ func Load() (*Config, string, error) {
return config, configPath, nil
}
if configPath, found := findGlobalConfig(); found {
config, err := loadFromFile(configPath)
if err != nil {
@@ -52,11 +48,9 @@ func Load() (*Config, string, error) {
return config, configPath, nil
}
return Default(), "", nil
}
func findProjectConfig() (string, bool) {
cwd, err := os.Getwd()
@@ -69,7 +63,6 @@ func findProjectConfig() (string, bool) {
return localConfig, true
}
if gitRoot := findGitRoot(cwd); gitRoot != "" {
gitConfig := filepath.Join(gitRoot, ".shush.toml")
if fileExists(gitConfig) && gitConfig != localConfig {
@@ -80,7 +73,6 @@ func findProjectConfig() (string, bool) {
return "", false
}
func findGlobalConfig() (string, bool) {
home, err := os.UserHomeDir()
if err != nil {
@@ -95,7 +87,6 @@ func findGlobalConfig() (string, bool) {
return "", false
}
func findGitRoot(startDir string) string {
dir := startDir
for {
@@ -106,63 +97,55 @@ func findGitRoot(startDir string) string {
parent := filepath.Dir(dir)
if parent == dir {
break
break
}
dir = parent
}
return ""
}
func loadFromFile(path string) (*Config, error) {
config := &Config{}
if _, err := toml.DecodeFile(path, config); err != nil {
return nil, err
}
if len(config.Preserve) == 0 {
config.Preserve = Default().Preserve
}
return config, nil
}
func fileExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
func (c *Config) ShouldPreserveComment(comment string) bool {
comment = strings.TrimSpace(comment)
for _, pattern := range c.Preserve {
if matchesPattern(comment, pattern) {
return true
}
}
return false
}
func matchesPattern(text, pattern string) bool {
if strings.Contains(pattern, "*") {
return matchesWildcard(text, pattern)
}
return strings.Contains(text, pattern)
}
func matchesWildcard(text, pattern string) bool {
parts := strings.Split(pattern, "*")
if !strings.HasPrefix(pattern, "*") {
if !strings.HasPrefix(text, parts[0]) {
@@ -171,9 +154,8 @@ func matchesWildcard(text, pattern string) bool {
text = text[len(parts[0]):]
parts = parts[1:]
} else {
parts = parts[1:]
parts = parts[1:]
}
if !strings.HasSuffix(pattern, "*") && len(parts) > 0 {
lastPart := parts[len(parts)-1]
@@ -183,26 +165,23 @@ parts = parts[1:]
text = text[:len(text)-len(lastPart)]
parts = parts[:len(parts)-1]
}
for _, part := range parts {
if part == "" {
continue
continue
}
index := strings.Index(text, part)
if index == -1 {
return false
}
text = text[index+len(part):]
}
return true
}
func CreateExampleConfig() error {
content := `# Shush Configuration
# Patterns to preserve in comments (supports wildcards with *)
@@ -222,6 +201,6 @@ preserve = [
"*DEBUG*", # Example wildcard: preserves any comment containing DEBUG
]
`
return os.WriteFile(".shush.toml", []byte(content), 0644)
}
-1
View File
@@ -117,7 +117,6 @@ func getLineRangesFromDiff(file string, staged bool) ([]LineRange, error) {
output, err := cmd.Output()
if err != nil {
return []LineRange{}, nil
}
-24
View File
@@ -9,12 +9,6 @@ import (
"strings"
)
type HookScope int
const (
@@ -22,26 +16,21 @@ const (
ScopeProject
)
type ClaudeSettings struct {
Hooks map[string][]EventConfig `json:"hooks,omitempty"`
}
type EventConfig struct {
Matcher string `json:"matcher"`
Hooks []HookEntry `json:"hooks"`
}
type HookEntry struct {
Type string `json:"type"`
Command string `json:"command"`
Timeout int `json:"timeout,omitempty"`
}
func GetSettingsPath(scope HookScope) (string, error) {
switch scope {
case ScopeUser:
@@ -61,7 +50,6 @@ func GetSettingsPath(scope HookScope) (string, error) {
}
}
func EnsureSettingsDirectory(settingsPath string) error {
dir := filepath.Dir(settingsPath)
if err := os.MkdirAll(dir, 0755); err != nil {
@@ -70,7 +58,6 @@ func EnsureSettingsDirectory(settingsPath string) error {
return nil
}
func LoadSettings(path string) (*ClaudeSettings, error) {
data, err := os.ReadFile(path)
if err != nil {
@@ -88,7 +75,6 @@ func LoadSettings(path string) (*ClaudeSettings, error) {
return nil, fmt.Errorf("failed to parse settings file %s: %w", path, err)
}
if settings.Hooks == nil {
settings.Hooks = make(map[string][]EventConfig)
}
@@ -96,7 +82,6 @@ func LoadSettings(path string) (*ClaudeSettings, error) {
return &settings, nil
}
func SaveSettings(path string, settings *ClaudeSettings) error {
if err := EnsureSettingsDirectory(path); err != nil {
return err
@@ -114,7 +99,6 @@ func SaveSettings(path string, settings *ClaudeSettings) error {
return nil
}
func CreateShushHookEntry() HookEntry {
return HookEntry{
Type: "command",
@@ -123,7 +107,6 @@ func CreateShushHookEntry() HookEntry {
}
}
func CreateShushEventConfig() EventConfig {
return EventConfig{
Matcher: "Write|Edit|MultiEdit",
@@ -131,7 +114,6 @@ func CreateShushEventConfig() EventConfig {
}
}
func HasShushHook(settings *ClaudeSettings) bool {
if settings.Hooks == nil {
return false
@@ -153,7 +135,6 @@ func HasShushHook(settings *ClaudeSettings) bool {
return false
}
func AddShushHook(settings *ClaudeSettings) error {
if settings.Hooks == nil {
settings.Hooks = make(map[string][]EventConfig)
@@ -163,7 +144,6 @@ func AddShushHook(settings *ClaudeSettings) error {
return errors.New("shush hook already installed")
}
postToolUseConfigs, exists := settings.Hooks["PostToolUse"]
if !exists {
@@ -171,7 +151,6 @@ func AddShushHook(settings *ClaudeSettings) error {
return nil
}
for i, config := range postToolUseConfigs {
if config.Matcher == "Write|Edit|MultiEdit" || config.Matcher == "" {
@@ -180,12 +159,10 @@ func AddShushHook(settings *ClaudeSettings) error {
}
}
settings.Hooks["PostToolUse"] = append(postToolUseConfigs, CreateShushEventConfig())
return nil
}
func RemoveShushHook(settings *ClaudeSettings) error {
if settings.Hooks == nil {
return errors.New("no hooks configuration found")
@@ -213,7 +190,6 @@ func RemoveShushHook(settings *ClaudeSettings) error {
return errors.New("shush hook not found")
}
filteredConfigs := make([]EventConfig, 0, len(postToolUseConfigs))
for _, config := range postToolUseConfigs {
if len(config.Hooks) > 0 {
+3 -6
View File
@@ -117,7 +117,6 @@ func (p *Processor) processFile(filename string) error {
return p.showPreview(filename, language)
}
return p.processFileInMemory(filename, language)
}
@@ -156,7 +155,7 @@ func (p *Processor) processFileInMemory(filename string, language types.Language
modified := false
var processedLines []string
for _, line := range lines {
newLine := p.removeCommentsFromLine(line, language, cfg)
if newLine != line {
@@ -194,8 +193,6 @@ func (p *Processor) processFileInMemory(filename string, language types.Language
return nil
}
func (p *Processor) createBackup(filename string) error {
backupName := filename + ".bak"
@@ -260,9 +257,9 @@ func (p *Processor) showPreview(filename string, language types.Language) error
lineNum++
line := scanner.Text()
newLine := p.removeCommentsFromLine(line, language, cfg)
lineNumStr := gray.Sprintf("%4d", lineNum)
if newLine != line {
changedCount++
if newLine == "" {
+20 -20
View File
@@ -3,26 +3,26 @@ package types
import "github.com/alecthomas/kong"
type CLI struct {
Path string `arg:"" name:"path" help:"Source code file or directory to process" optional:""`
Inline bool `help:"Remove only line comments"`
Block bool `help:"Remove only block comments"`
Recursive bool `short:"r" help:"Process directories recursively"`
DryRun bool `help:"Show what would be removed without making changes"`
Backup bool `help:"Create backup files before modification"`
Verbose bool `help:"Show detailed output"`
PreserveLines bool `help:"Remove comments but preserve empty lines"`
LLM bool `help:"Show LLM-friendly usage guide"`
ChangesOnly bool `help:"Remove comments only from git changes (staged + unstaged + untracked)"`
Staged bool `help:"Remove comments only from staged git changes"`
Unstaged bool `help:"Remove comments only from unstaged git changes"`
InstallHook bool `help:"Install Claude Code hooks for automatic comment cleanup"`
UninstallHook bool `help:"Uninstall Claude Code hooks"`
ListHooks bool `help:"List current Claude Code hooks configuration"`
HookStatus bool `help:"Check if shush hooks are installed"`
HookScope string `short:"s" help:"Hook scope: 'project' for local, default for user-wide"`
Config bool `help:"Show current configuration and location"`
CreateConfig bool `help:"Create example .shush.toml configuration file"`
Version kong.VersionFlag `help:"Show version information"`
Path string `arg:"" name:"path" help:"Source code file or directory to process" optional:""`
Inline bool `help:"Remove only line comments"`
Block bool `help:"Remove only block comments"`
Recursive bool `short:"r" help:"Process directories recursively"`
DryRun bool `help:"Show what would be removed without making changes"`
Backup bool `help:"Create backup files before modification"`
Verbose bool `help:"Show detailed output"`
PreserveLines bool `help:"Remove comments but preserve empty lines"`
LLM bool `help:"Show LLM-friendly usage guide"`
ChangesOnly bool `help:"Remove comments only from git changes (staged + unstaged + untracked)"`
Staged bool `help:"Remove comments only from staged git changes"`
Unstaged bool `help:"Remove comments only from unstaged git changes"`
InstallHook bool `help:"Install Claude Code hooks for automatic comment cleanup"`
UninstallHook bool `help:"Uninstall Claude Code hooks"`
ListHooks bool `help:"List current Claude Code hooks configuration"`
HookStatus bool `help:"Check if shush hooks are installed"`
HookScope string `short:"s" help:"Hook scope: 'project' for local, default for user-wide"`
Config bool `help:"Show current configuration and location"`
CreateConfig bool `help:"Create example .shush.toml configuration file"`
Version kong.VersionFlag `help:"Show version information"`
}
type Language struct {