fix(server): Improvements

Signed-off-by: Myzel394 <github.7a2op@simplelogin.co>
This commit is contained in:
Myzel394 2025-05-29 16:16:45 +02:00
parent 9a1686a7d8
commit 429c2cd4be
No known key found for this signature in database
GPG Key ID: 3B955307C2FC2F11
8 changed files with 81 additions and 43 deletions

6
flake.lock generated
View File

@ -41,11 +41,11 @@
}, },
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1748026106, "lastModified": 1748370509,
"narHash": "sha256-6m1Y3/4pVw1RWTsrkAK2VMYSzG4MMIj7sqUy7o8th1o=", "narHash": "sha256-QlL8slIgc16W5UaI3w7xHQEP+Qmv/6vSNTpoZrrSlbk=",
"owner": "nixos", "owner": "nixos",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "063f43f2dbdef86376cc29ad646c45c46e93234c", "rev": "4faa5f5321320e49a78ae7848582f684d64783e9",
"type": "github" "type": "github"
}, },
"original": { "original": {

View File

@ -2,22 +2,12 @@ package docvalues
import ( import (
"config-lsp/utils" "config-lsp/utils"
protocol "github.com/tliron/glsp/protocol_3_16" "errors"
"strings" "strings"
protocol "github.com/tliron/glsp/protocol_3_16"
) )
type PathDoesNotExistError struct{}
func (e PathDoesNotExistError) Error() string {
return "This path does not exist"
}
type PathInvalidError struct{}
func (e PathInvalidError) Error() string {
return "This path is invalid"
}
type PathType uint8 type PathType uint8
const ( const (
@ -53,29 +43,66 @@ func (v PathValue) DeprecatedCheckIsValid(value string) []*InvalidValue {
return nil return nil
} else { } else {
return []*InvalidValue{{ return []*InvalidValue{{
Err: PathDoesNotExistError{}, Err: errors.New("This path does not exist"),
Start: 0, Start: 0,
End: uint32(len(value)), End: uint32(len(value)),
}} }}
} }
} }
isValid := false fileRequired := (v.RequiredType & PathTypeFile) == PathTypeFile
directoryRequired := (v.RequiredType & PathTypeDirectory) == PathTypeDirectory
if (v.RequiredType & PathTypeFile) == PathTypeFile { isValid := true
// If file is expected
if fileRequired {
// and exists
isValid = isValid && utils.IsPathFile(value) isValid = isValid && utils.IsPathFile(value)
// file not expected
} else {
// and should not exist
isValid = isValid && !utils.IsPathFile(value)
} }
if (v.RequiredType & PathTypeDirectory) == PathTypeDirectory { // if directory
if directoryRequired {
// and exists
isValid = isValid && utils.IsPathDirectory(value) isValid = isValid && utils.IsPathDirectory(value)
// directory not expected
} else {
// and should not exist
isValid = isValid && !utils.IsPathDirectory(value)
} }
if isValid { if isValid {
return nil return nil
} }
if fileRequired && directoryRequired {
return []*InvalidValue{{ return []*InvalidValue{{
Err: PathInvalidError{}, 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, Start: 0,
End: uint32(len(value)), End: uint32(len(value)),
}} }}

View File

@ -370,7 +370,7 @@ See PATTERNS in ssh_config(5) for more information on patterns. This keyword may
Note that sshd(8) will refuse to use a file if it is group/world-accessible and that the HostKeyAlgorithms option restricts which of the keys are actually used by sshd(8). Note that sshd(8) will refuse to use a file if it is group/world-accessible and that the HostKeyAlgorithms option restricts which of the keys are actually used by sshd(8).
It is possible to have multiple host key files. It is also possible to specify public host key files instead. In this case operations on the private key will be delegated to an ssh-agent(1).`, It is possible to have multiple host key files. It is also possible to specify public host key files instead. In this case operations on the private key will be delegated to an ssh-agent(1).`,
Value: docvalues.PathValue{ Value: docvalues.PathValue{
IsOptional: true, IsOptional: false,
RequiredType: docvalues.PathTypeFile, RequiredType: docvalues.PathTypeFile,
}, },
}, },

View File

@ -24,7 +24,6 @@ func TextDocumentCompletion(context *glsp.Context, params *protocol.CompletionPa
entry, matchBlock := d.Config.FindOption(line) entry, matchBlock := d.Config.FindOption(line)
if entry == nil || if entry == nil ||
entry.Separator == nil || entry.Separator == nil ||
entry.Key == nil || entry.Key == nil ||

View File

@ -1,6 +1,8 @@
package utils package utils
import ( import (
"errors"
"fmt"
"os" "os"
) )
@ -13,11 +15,21 @@ func DoesPathExist(path string) bool {
func IsPathDirectory(path string) bool { func IsPathDirectory(path string) bool {
info, err := os.Stat(path) info, err := os.Stat(path)
return err == nil && info.IsDir() if err != nil {
return false
}
return info.IsDir()
} }
func IsPathFile(path string) bool { func IsPathFile(path string) bool {
info, err := os.Stat(path) _, err := os.Stat(path)
return err == nil && !info.IsDir() print(fmt.Sprintf("Checking if path %s is a file: %v\n", path, err))
if errors.Is(err, os.ErrNotExist) {
return false
}
return true
} }