config-lsp/doc-values/value-regex.go
2024-09-28 12:07:18 +02:00

49 lines
1.1 KiB
Go

package docvalues
import (
"fmt"
"regexp"
protocol "github.com/tliron/glsp/protocol_3_16"
)
type RegexInvalidError struct {
Regex string
}
func (e RegexInvalidError) Error() string {
return fmt.Sprintf("This value does not match the regular expression (Pattern: `%s`)", e.Regex)
}
type RegexValue struct {
Regex regexp.Regexp
}
func (v RegexValue) GetTypeDescription() []string {
return []string{
fmt.Sprintf("String matching the regular expression (Pattern: `%s`)", v.Regex.String()),
}
}
func (v RegexValue) DeprecatedCheckIsValid(value string) []*InvalidValue {
if !v.Regex.MatchString(value) {
return []*InvalidValue{{
Err: RegexInvalidError{Regex: v.Regex.String()},
Start: 0,
End: uint32(len(value)),
}}
}
return []*InvalidValue{}
}
func (v RegexValue) DeprecatedFetchCompletions(line string, cursor uint32) []protocol.CompletionItem {
return []protocol.CompletionItem{}
}
func (v RegexValue) DeprecatedFetchHoverInfo(line string, cursor uint32) []string {
return []string{
fmt.Sprintf("Pattern: `%s`", v.Regex.String()),
}
}