feat: Improvements; Add wireguard

This commit is contained in:
Myzel394 2024-08-14 19:52:40 +02:00
parent ffe1451526
commit eab2d8ad29
No known key found for this signature in database
GPG Key ID: DEC4AAB876F73185
8 changed files with 75 additions and 3 deletions

View File

@ -25,6 +25,7 @@ func SetUpRootHandler() {
TextDocumentDidChange: TextDocumentDidChange, TextDocumentDidChange: TextDocumentDidChange,
TextDocumentCompletion: TextDocumentCompletion, TextDocumentCompletion: TextDocumentCompletion,
TextDocumentHover: TextDocumentHover, TextDocumentHover: TextDocumentHover,
TextDocumentDidClose: TextDocumentDidClose,
} }
server := server.NewServer(&lspHandler, lsName, false) server := server.NewServer(&lspHandler, lsName, false)

View File

@ -15,11 +15,13 @@ type SupportedLanguage string
const ( const (
LanguageSSHDConfig SupportedLanguage = "sshd_config" LanguageSSHDConfig SupportedLanguage = "sshd_config"
LanguageFstab SupportedLanguage = "fstab" LanguageFstab SupportedLanguage = "fstab"
LanguageWireguard SupportedLanguage = "languagewireguard"
) )
var AllSupportedLanguages = []string{ var AllSupportedLanguages = []string{
string(LanguageSSHDConfig), string(LanguageSSHDConfig),
string(LanguageFstab), string(LanguageFstab),
string(LanguageWireguard),
} }
type FatalFileNotReadableError struct { type FatalFileNotReadableError struct {
@ -53,9 +55,14 @@ var valueToLanguageMap = map[string]SupportedLanguage{
"fstab": LanguageFstab, "fstab": LanguageFstab,
"etc/fstab": LanguageFstab, "etc/fstab": LanguageFstab,
"wireguard": LanguageWireguard,
"wg": LanguageWireguard,
"languagewireguard": LanguageWireguard,
} }
var typeOverwriteRegex = regexp.MustCompile(`^#\?\s*lsp\.language\s*=\s*(\w+)\s*$`) var typeOverwriteRegex = regexp.MustCompile(`^#\?\s*lsp\.language\s*=\s*(\w+)\s*$`)
var wireguardPattern = regexp.MustCompile(`/wg\d+\.conf$`)
func DetectLanguage( func DetectLanguage(
content string, content string,
@ -94,6 +101,10 @@ func DetectLanguage(
return LanguageFstab, nil return LanguageFstab, nil
} }
if strings.HasPrefix(uri, "file:///etc/wireguard/") || wireguardPattern.MatchString(uri) {
return LanguageWireguard, nil
}
return "", common.ParseError{ return "", common.ParseError{
Line: 0, Line: 0,
Err: LanguageUndetectableError{}, Err: LanguageUndetectableError{},

5
root-handler/shared.go Normal file
View File

@ -0,0 +1,5 @@
package roothandler
import protocol "github.com/tliron/glsp/protocol_3_16"
var openedFiles = make(map[protocol.DocumentUri]struct{})

View File

@ -2,6 +2,7 @@ package roothandler
import ( import (
"config-lsp/handlers/fstab" "config-lsp/handlers/fstab"
"config-lsp/handlers/wireguard"
"github.com/tliron/glsp" "github.com/tliron/glsp"
protocol "github.com/tliron/glsp/protocol_3_16" protocol "github.com/tliron/glsp/protocol_3_16"
@ -15,6 +16,8 @@ func TextDocumentCompletion(context *glsp.Context, params *protocol.CompletionPa
return fstab.TextDocumentCompletion(context, params) return fstab.TextDocumentCompletion(context, params)
case LanguageSSHDConfig: case LanguageSSHDConfig:
return nil, nil return nil, nil
case LanguageWireguard:
return wireguard.TextDocumentCompletion(context, params)
} }
panic("root-handler/TextDocumentCompletion: unexpected language" + language) panic("root-handler/TextDocumentCompletion: unexpected language" + language)

View File

@ -2,6 +2,7 @@ package roothandler
import ( import (
"config-lsp/handlers/fstab" "config-lsp/handlers/fstab"
"config-lsp/handlers/wireguard"
"github.com/tliron/glsp" "github.com/tliron/glsp"
protocol "github.com/tliron/glsp/protocol_3_16" protocol "github.com/tliron/glsp/protocol_3_16"
@ -15,6 +16,8 @@ func TextDocumentDidChange(context *glsp.Context, params *protocol.DidChangeText
return fstab.TextDocumentDidChange(context, params) return fstab.TextDocumentDidChange(context, params)
case LanguageSSHDConfig: case LanguageSSHDConfig:
return nil return nil
case LanguageWireguard:
return wireguard.TextDocumentDidChange(context, params)
} }
panic("root-handler/TextDocumentDidChange: unexpected language" + language) panic("root-handler/TextDocumentDidChange: unexpected language" + language)

View File

@ -0,0 +1,25 @@
package roothandler
import (
"config-lsp/handlers/wireguard"
"github.com/tliron/glsp"
protocol "github.com/tliron/glsp/protocol_3_16"
)
func TextDocumentDidClose(context *glsp.Context, params *protocol.DidCloseTextDocumentParams) error {
language := rootHandler.GetLanguageForDocument(params.TextDocument.URI)
delete(openedFiles, params.TextDocument.URI)
rootHandler.RemoveDocument(params.TextDocument.URI)
switch language {
case LanguageFstab:
case LanguageSSHDConfig:
case LanguageWireguard:
return wireguard.TextDocumentDidClose(context, params)
default:
}
return nil
}

View File

@ -3,6 +3,7 @@ package roothandler
import ( import (
"config-lsp/common" "config-lsp/common"
fstab "config-lsp/handlers/fstab" fstab "config-lsp/handlers/fstab"
"config-lsp/handlers/wireguard"
"fmt" "fmt"
"github.com/tliron/glsp" "github.com/tliron/glsp"
@ -27,6 +28,8 @@ func TextDocumentDidOpen(context *glsp.Context, params *protocol.DidOpenTextDocu
return parseError.Err return parseError.Err
} }
openedFiles[params.TextDocument.URI] = struct{}{}
// Everything okay, now we can handle the file // Everything okay, now we can handle the file
rootHandler.AddDocument(params.TextDocument.URI, language) rootHandler.AddDocument(params.TextDocument.URI, language)
@ -34,11 +37,12 @@ func TextDocumentDidOpen(context *glsp.Context, params *protocol.DidOpenTextDocu
case LanguageFstab: case LanguageFstab:
return fstab.TextDocumentDidOpen(context, params) return fstab.TextDocumentDidOpen(context, params)
case LanguageSSHDConfig: case LanguageSSHDConfig:
default: break
panic(fmt.Sprintf("unexpected roothandler.SupportedLanguage: %#v", language)) case LanguageWireguard:
return wireguard.TextDocumentDidOpen(context, params)
} }
return nil panic(fmt.Sprintf("unexpected roothandler.SupportedLanguage: %#v", language))
} }
func showParseError( func showParseError(

View File

@ -29,6 +29,26 @@ func Map[T any, O any](values []T, f func(T) O) []O {
return result return result
} }
func MapMap[T comparable, O any, P any](values map[T]O, f func(T, O) P) map[T]P {
result := make(map[T]P)
for key, value := range values {
result[key] = f(key, value)
}
return result
}
func MapMapToSlice[T comparable, O any, P any](values map[T]O, f func(T, O) P) []P {
result := make([]P, 0)
for key, value := range values {
result = append(result, f(key, value))
}
return result
}
func SliceToSet[T comparable](values []T) map[T]struct{} { func SliceToSet[T comparable](values []T) map[T]struct{} {
set := make(map[T]struct{}) set := make(map[T]struct{})