mirror of
https://github.com/Myzel394/config-lsp.git
synced 2025-06-18 23:15:26 +02:00
95 lines
1.8 KiB
Go
95 lines
1.8 KiB
Go
package handlers
|
|
|
|
import (
|
|
"config-lsp/common"
|
|
"config-lsp/handlers/aliases/ast"
|
|
"config-lsp/handlers/aliases/indexes"
|
|
"config-lsp/utils"
|
|
|
|
protocol "github.com/tliron/glsp/protocol_3_16"
|
|
)
|
|
|
|
func GetDefinitionLocationForValue(
|
|
i indexes.AliasesIndexes,
|
|
value ast.AliasValueInterface,
|
|
documentURI string,
|
|
) []protocol.Location {
|
|
switch value.(type) {
|
|
case ast.AliasValueUser:
|
|
userValue := value.(ast.AliasValueUser)
|
|
|
|
// Own defined alias
|
|
if entry, found := i.Keys[indexes.NormalizeKey(userValue.Value)]; found {
|
|
return []protocol.Location{
|
|
{
|
|
URI: documentURI,
|
|
Range: entry.Key.Location.ToLSPRange(),
|
|
},
|
|
}
|
|
}
|
|
|
|
// System user
|
|
systemUsers, _ := getSystemUserMap()
|
|
if user, found := systemUsers[userValue.Value]; found {
|
|
return []protocol.Location{
|
|
{
|
|
URI: "file:///etc/passwd",
|
|
Range: protocol.Range{
|
|
Start: protocol.Position{
|
|
Line: user.Line,
|
|
Character: 0,
|
|
},
|
|
End: protocol.Position{
|
|
Line: user.Line,
|
|
Character: uint32(len(user.Name)),
|
|
},
|
|
},
|
|
},
|
|
}
|
|
}
|
|
case ast.AliasValueFile:
|
|
fileValue := value.(ast.AliasValueFile)
|
|
path := string(fileValue.Path)
|
|
|
|
if utils.DoesPathExist(path) {
|
|
return []protocol.Location{
|
|
{
|
|
URI: "file://" + path,
|
|
},
|
|
}
|
|
}
|
|
case ast.AliasValueInclude:
|
|
includeValue := value.(ast.AliasValueInclude)
|
|
|
|
if includeValue.Path != nil {
|
|
path := string(includeValue.Path.Path)
|
|
|
|
if utils.DoesPathExist(path) {
|
|
return []protocol.Location{
|
|
{
|
|
URI: "file://" + path,
|
|
},
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func getSystemUserMap() (map[string]common.PasswdInfo, error) {
|
|
users, err := common.FetchPasswdInfo()
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
userMap := make(map[string]common.PasswdInfo)
|
|
|
|
for _, user := range users {
|
|
userMap[user.Name] = user
|
|
}
|
|
|
|
return userMap, nil
|
|
}
|