config-lsp/server/doc-values/value-path.go
Myzel394 429c2cd4be
fix(server): Improvements
Signed-off-by: Myzel394 <github.7a2op@simplelogin.co>
2025-05-29 20:10:35 +02:00

118 lines
2.3 KiB
Go

package docvalues
import (
"config-lsp/utils"
"errors"
"strings"
protocol "github.com/tliron/glsp/protocol_3_16"
)
type PathType uint8
const (
PathTypeFile PathType = 1
PathTypeDirectory PathType = 2
)
type PathValue struct {
IsOptional bool
RequiredType PathType
}
func (v PathValue) GetTypeDescription() []string {
hints := make([]string, 0)
switch v.RequiredType {
case PathTypeFile:
hints = append(hints, "File")
case PathTypeDirectory:
hints = append(hints, "Directory")
}
if v.IsOptional {
hints = append(hints, "Optional")
}
return []string{strings.Join(hints, ", ")}
}
func (v PathValue) DeprecatedCheckIsValid(value string) []*InvalidValue {
if !utils.DoesPathExist(value) {
if v.IsOptional {
return nil
} else {
return []*InvalidValue{{
Err: errors.New("This path does not exist"),
Start: 0,
End: uint32(len(value)),
}}
}
}
fileRequired := (v.RequiredType & PathTypeFile) == PathTypeFile
directoryRequired := (v.RequiredType & PathTypeDirectory) == PathTypeDirectory
isValid := true
// If file is expected
if fileRequired {
// and exists
isValid = isValid && utils.IsPathFile(value)
// file not expected
} else {
// and should not exist
isValid = isValid && !utils.IsPathFile(value)
}
// if directory
if directoryRequired {
// and exists
isValid = isValid && utils.IsPathDirectory(value)
// directory not expected
} else {
// and should not exist
isValid = isValid && !utils.IsPathDirectory(value)
}
if isValid {
return nil
}
if fileRequired && directoryRequired {
return []*InvalidValue{{
Err: errors.New("This must be either a file or a directory"),
Start: 0,
End: uint32(len(value)),
}}
}
if fileRequired {
return []*InvalidValue{{
Err: errors.New("This must be a file"),
Start: 0,
End: uint32(len(value)),
}}
}
if directoryRequired {
return []*InvalidValue{{
Err: errors.New("This must be a directory"),
Start: 0,
End: uint32(len(value)),
}}
}
return []*InvalidValue{{
Err: errors.New("This path is invalid"),
Start: 0,
End: uint32(len(value)),
}}
}
func (v PathValue) DeprecatedFetchCompletions(line string, cursor uint32) []protocol.CompletionItem {
return []protocol.CompletionItem{}
}
func (v PathValue) DeprecatedFetchHoverInfo(line string, cursor uint32) []string {
return []string{}
}