feat(server): Detect language based on the filename

This commit is contained in:
Myzel394 2024-10-20 15:24:00 +02:00
parent 5798dd45f1
commit ec4deeb59f
No known key found for this signature in database
GPG Key ID: ED20A1D1D423AF3F

View File

@ -4,6 +4,7 @@ import (
"config-lsp/common" "config-lsp/common"
"config-lsp/utils" "config-lsp/utils"
"fmt" "fmt"
"path"
"regexp" "regexp"
"strings" "strings"
@ -78,6 +79,27 @@ var valueToLanguageMap = map[string]SupportedLanguage{
"etc/aliases": LanguageAliases, "etc/aliases": LanguageAliases,
} }
var filenameToLanguageMap = map[string]SupportedLanguage{
"sshd_config": LanguageSSHDConfig,
"sshdconfig": LanguageSSHDConfig,
"sshd": LanguageSSHDConfig,
"ssh_config": LanguageSSHConfig,
"sshconfig": LanguageSSHConfig,
"fstab": LanguageFstab,
"hosts": LanguageHosts,
"aliases": LanguageAliases,
"mailaliases": LanguageAliases,
"wg": LanguageWireguard,
"wg.conf": LanguageWireguard,
"wg0.conf": LanguageWireguard,
"wg0": 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$`) var wireguardPattern = regexp.MustCompile(`/wg\d+\.conf$`)
@ -137,6 +159,12 @@ func DetectLanguage(
return LanguageAliases, nil return LanguageAliases, nil
} }
filename := path.Base(string(uri))
if language, found := filenameToLanguageMap[filename]; found {
return language, nil
}
if strings.HasPrefix(uri, "file:///etc/wireguard/") || wireguardPattern.MatchString(uri) { if strings.HasPrefix(uri, "file:///etc/wireguard/") || wireguardPattern.MatchString(uri) {
return LanguageWireguard, nil return LanguageWireguard, nil
} }