Skip to content

Commit

Permalink
feat: add footer diagnostics
Browse files Browse the repository at this point in the history
Add some basic footer diagnostics. This is galaxies away from perfect,
but it's good enough for now.
  • Loading branch information
benpueschel committed Jul 1, 2024
1 parent 2441fc1 commit 7a8ec5f
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 25 deletions.
75 changes: 50 additions & 25 deletions analysis/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,58 @@ import (
func GetDiagnostics(text string) []lsp.Diagnostic {
diagnostics := []lsp.Diagnostic{}

isHeader := true
isBody := false

for row, line := range strings.Split(text, "\n") {
if strings.Index(line, "#") == 0 {
continue
}

if isHeader {
isHeader = false
diagnostics = append(diagnostics, getHeaderDiagnostics(line, row)...)
continue
}

if !isBody {
isBody = true
if len(line) > 0 {
diagnostics = append(diagnostics, lsp.Diagnostic{
Severity: 1,
Range: LineRange(row, 0, row, len(line)),
Source: "conventional-commit-lsp",
Message: "Commit body must begin one blank line after the header. This line should be empty.",
})
sections := strings.Split(text, "\n\n")
totalSections := len(sections)
currentLine := -1
for index, section := range sections {
lines := strings.Split(section, "\n")
if index == 0 {
// header
isHeader := true
for _, line := range lines {
currentLine++
// line is a comment, skip
if strings.Index(line, "#") == 0 {
continue
}
if isHeader {
isHeader = false
diagnostics = append(diagnostics, getHeaderDiagnostics(line, currentLine)...)
} else if len(line) > 0 {
diagnostics = append(diagnostics, lsp.Diagnostic{
Severity: 1,
Range: LineRange(currentLine, 0, currentLine, len(line)),
Source: "conventional-commit-lsp",
Message: "Commit body must begin one blank line after the header. This line should be empty.",
})
}
}
} else if totalSections > 2 && index == totalSections-1 {
// footer
for _, line := range lines {
currentLine++
// line is a comment, skip
if strings.Index(line, "#") == 0 {
continue
}
diagnostics = append(diagnostics, getFooterDiagnostics(line, currentLine)...)
}
} else {
// body
for _, line := range lines {
currentLine++
if len(line) > 72 {
diagnostics = append(diagnostics, lsp.Diagnostic{
Severity: 2,
Range: LineRange(currentLine, 72, currentLine, len(line)),
Source: "conventional-commit-lsp",
Message: "Commit body shoyld be wrapped at 72 characters.",
})
}
}
}

currentLine++
}

return diagnostics
}

84 changes: 84 additions & 0 deletions analysis/footer_diagnostics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package analysis

import (
"strings"

"github.com/benpueschel/conventional-commit-lsp/lsp"
)

func getFooterDiagnostics(line string, row int) []lsp.Diagnostic {
diagnostics := []lsp.Diagnostic{}

if len(line) == 0 {
return diagnostics
}

if len(line) > 72 {
diagnostics = append(diagnostics, lsp.Diagnostic{
Severity: 2,
Range: LineRange(row, 72, row, len(line)),
Source: "conventional-commit-lsp",
Message: "Commit footer should be wrapped at 72 characters.",
})
}

token, value, found := strings.Cut(line, ": ")
if !found || len(token) == 0 || len(value) == 0 {
// INFO: Footers can contain newlines, so we cannot enforce every line
// to be in the format of 'Footer-key: string value'. Basically, we can't
// be sure if the user is intending to write a multiline footer or just isn't
// able to write a simple list of key-value pair footers correctly.
// So, we can't enforce this rule.
/*
diagnostics = append(diagnostics, lsp.Diagnostic{
Severity: 1,
Range: LineRange(row, 0, row, len(line)),
Source: "conventional-commit-lsp",
Message: "Commit footer must be in the format of 'Footer-key: string value'.",
})
*/
return diagnostics

}

if token != "BREAKING CHANGE" && token != "BREAKING-CHANGE" {
line_range := LineRange(row, 0, row, len(token))
if strings.ToUpper(token) == "BREAKING CHANGE" {
diagnostics = append(diagnostics, lsp.Diagnostic{
Severity: 1,
Range: line_range,
Source: "conventional-commit-lsp",
Message: "BREAKING CHANGE must be uppercase.",
})
} else if strings.ToUpper(token) == "BREAKING-CHANGE" {
diagnostics = append(diagnostics, lsp.Diagnostic{
Severity: 1,
Range: line_range,
Source: "conventional-commit-lsp",
Message: "BREAKING-CHANGE must be uppercase.",
})
} else if strings.Contains(token, " ") {
// INFO: see above
/*
diagnostics = append(diagnostics, lsp.Diagnostic{
Severity: 1,
Range: line_range,
Source: "conventional-commit-lsp",
Message: "Footer tokens must not contain spaces. Use hyphens instead.",
})
*/
} else if strings.Contains(token, "_") {
// INFO: see above
/*
diagnostics = append(diagnostics, lsp.Diagnostic{
Severity: 2,
Range: line_range,
Source: "conventional-commit-lsp",
Message: "Footer tokens should not contain underscores. Use hyphens instead.",
})
*/
}
}

return diagnostics
}

0 comments on commit 7a8ec5f

Please sign in to comment.