repochecker/grammalecte: New plugin to check french grammar
This commit is contained in:
parent
721908ee18
commit
edde9f885d
6 changed files with 431 additions and 5 deletions
86
repochecker/grammalecte/errors.go
Normal file
86
repochecker/grammalecte/errors.go
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const LOG_PREFIX_LEN = 20
|
||||
|
||||
type SpellingError struct {
|
||||
Prefix string
|
||||
Source string
|
||||
NSource int
|
||||
Start int
|
||||
End int
|
||||
Type string
|
||||
Value string
|
||||
Suggestions []string
|
||||
}
|
||||
|
||||
func (e SpellingError) Error() string {
|
||||
suggestions := ""
|
||||
if len(e.Suggestions) > 0 {
|
||||
suggestions = "\nSuggestions : " + strings.Join(e.Suggestions, ", ")
|
||||
}
|
||||
|
||||
return fmt.Sprintf(
|
||||
"%sspelling error %s %s\n%q\n%s%s",
|
||||
e.Prefix,
|
||||
e.Type,
|
||||
e.Value,
|
||||
e.Source,
|
||||
underline(1, e.Start, e.End),
|
||||
suggestions,
|
||||
)
|
||||
}
|
||||
|
||||
type GrammarError struct {
|
||||
Prefix string
|
||||
Source string
|
||||
NSource int
|
||||
Start int
|
||||
End int
|
||||
RuleId string
|
||||
Type string
|
||||
Message string
|
||||
Suggestions []string
|
||||
URL string
|
||||
}
|
||||
|
||||
func (e GrammarError) Error() string {
|
||||
sornot := ""
|
||||
if len(e.Suggestions) > 1 {
|
||||
sornot = "s"
|
||||
}
|
||||
|
||||
suggestions := ""
|
||||
if len(e.Suggestions) > 0 {
|
||||
suggestions = "\nSuggestion" + sornot + " : " + strings.Join(e.Suggestions, ", ")
|
||||
}
|
||||
|
||||
return fmt.Sprintf(
|
||||
"%s%s (%s)\n%q\n%s%s",
|
||||
e.Prefix,
|
||||
e.Message,
|
||||
e.RuleId,
|
||||
e.Source,
|
||||
underline(1, e.Start, e.End),
|
||||
suggestions,
|
||||
)
|
||||
}
|
||||
|
||||
func underline(prefix, start, end int) string {
|
||||
var b bytes.Buffer
|
||||
|
||||
for i := 0; i < prefix+start; i++ {
|
||||
b.Write([]byte{' '})
|
||||
}
|
||||
|
||||
for i := 0; i < end-start; i++ {
|
||||
b.Write([]byte{'^'})
|
||||
}
|
||||
|
||||
return b.String()
|
||||
}
|
||||
Reference in a new issue