mirror of
https://github.com/Myzel394/config-lsp.git
synced 2025-06-18 23:15:26 +02:00
112 lines
2.4 KiB
Go
112 lines
2.4 KiB
Go
package docvalues
|
|
|
|
import (
|
|
"config-lsp/utils"
|
|
"fmt"
|
|
"regexp"
|
|
"strconv"
|
|
|
|
protocol "github.com/tliron/glsp/protocol_3_16"
|
|
)
|
|
|
|
var timeFormatCompletionsPattern = regexp.MustCompile(`(?i)^(\d+)([smhdw])$`)
|
|
var timeFormatCheckPattern = regexp.MustCompile(`(?i)^(\d+)([smhdw]?)$`)
|
|
var isJustDigitsPattern = regexp.MustCompile(`^\d+$`)
|
|
|
|
type InvalidTimeFormatError struct{}
|
|
|
|
func (e InvalidTimeFormatError) Error() string {
|
|
return "Time format is invalid. It must be in the form of: <number>[s|m|h|d|w]"
|
|
}
|
|
|
|
type TimeFormatValue struct{}
|
|
|
|
func (v TimeFormatValue) GetTypeDescription() []string {
|
|
return []string{"Time value"}
|
|
}
|
|
|
|
func (v TimeFormatValue) DeprecatedCheckIsValid(value string) []*InvalidValue {
|
|
if !timeFormatCheckPattern.MatchString(value) {
|
|
return []*InvalidValue{
|
|
{
|
|
Err: InvalidTimeFormatError{},
|
|
Start: 0,
|
|
End: uint32(len(value)),
|
|
},
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func calculateInSeconds(value int, unit string) int {
|
|
switch unit {
|
|
case "s":
|
|
return value
|
|
case "m":
|
|
return value * 60
|
|
case "h":
|
|
return value * 60 * 60
|
|
case "d":
|
|
return value * 60 * 60 * 24
|
|
case "w":
|
|
return value * 60 * 60 * 24 * 7
|
|
default:
|
|
return 0
|
|
}
|
|
}
|
|
|
|
func (v TimeFormatValue) DeprecatedFetchCompletions(line string, cursor uint32) []protocol.CompletionItem {
|
|
completions := make([]protocol.CompletionItem, 0)
|
|
|
|
if line != "" && !timeFormatCompletionsPattern.MatchString(line) {
|
|
completions = append(
|
|
completions,
|
|
utils.Map(
|
|
[]string{"s", "m", "h", "d", "w"},
|
|
func(unit string) protocol.CompletionItem {
|
|
kind := protocol.CompletionItemKindValue
|
|
|
|
unitName := map[string]string{
|
|
"s": "seconds",
|
|
"m": "minutes",
|
|
"h": "hours",
|
|
"d": "days",
|
|
"w": "weeks",
|
|
}[unit]
|
|
|
|
var detail string
|
|
value, err := strconv.Atoi(line)
|
|
|
|
if err == nil {
|
|
if unit == "s" {
|
|
detail = fmt.Sprintf("%d seconds", value)
|
|
} else {
|
|
detail = fmt.Sprintf("%d %s (%d seconds)", value, unitName, calculateInSeconds(value, unit))
|
|
}
|
|
}
|
|
|
|
return protocol.CompletionItem{
|
|
Label: line + unit,
|
|
Kind: &kind,
|
|
Detail: &detail,
|
|
}
|
|
},
|
|
)...,
|
|
)
|
|
}
|
|
|
|
if line == "" || isJustDigitsPattern.MatchString(line) {
|
|
completions = append(
|
|
completions,
|
|
GenerateBase10Completions(line)...,
|
|
)
|
|
}
|
|
|
|
return completions
|
|
}
|
|
|
|
func (v TimeFormatValue) DeprecatedFetchHoverInfo(line string, cursor uint32) []string {
|
|
return []string{}
|
|
}
|