mirror of
https://github.com/Myzel394/config-lsp.git
synced 2025-06-18 23:15:26 +02:00
feat(ssh_config): Do not handle includes if they contain tokens or environmental variables
This commit is contained in:
parent
fdb005a75d
commit
4cdc916d51
@ -12,17 +12,44 @@ import (
|
|||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
|
||||||
protocol "github.com/tliron/glsp/protocol_3_16"
|
protocol "github.com/tliron/glsp/protocol_3_16"
|
||||||
)
|
)
|
||||||
|
|
||||||
var whitespacePattern = regexp.MustCompile(`\S+`)
|
var whitespacePattern = regexp.MustCompile(`\S+`)
|
||||||
|
var environmtalVariablePattern = regexp.MustCompile(`\${.+?}`)
|
||||||
|
var availableTokens = []string{
|
||||||
|
"%%",
|
||||||
|
"%C",
|
||||||
|
"%d",
|
||||||
|
"%f",
|
||||||
|
"%H",
|
||||||
|
"%h",
|
||||||
|
"%l",
|
||||||
|
"%i",
|
||||||
|
"%j",
|
||||||
|
"%K",
|
||||||
|
"%k",
|
||||||
|
"%L",
|
||||||
|
"%l",
|
||||||
|
"%n",
|
||||||
|
"%p",
|
||||||
|
"%r",
|
||||||
|
"%T",
|
||||||
|
"%t",
|
||||||
|
"%u",
|
||||||
|
}
|
||||||
|
|
||||||
func analyzeIncludeValues(
|
func analyzeIncludeValues(
|
||||||
ctx *analyzerContext,
|
ctx *analyzerContext,
|
||||||
) {
|
) {
|
||||||
for _, include := range ctx.document.Indexes.Includes {
|
for _, include := range ctx.document.Indexes.Includes {
|
||||||
for _, value := range include.Values {
|
for _, value := range include.Values {
|
||||||
|
if isImpossibleToVerify(value.Value) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
validPaths, err := createIncludePaths(value.Value)
|
validPaths, err := createIncludePaths(value.Value)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -38,6 +65,24 @@ func analyzeIncludeValues(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// We can't evaluate environmental variables or tokens as we don't know the actual
|
||||||
|
// values
|
||||||
|
func isImpossibleToVerify(
|
||||||
|
path string,
|
||||||
|
) bool {
|
||||||
|
if environmtalVariablePattern.MatchString(path) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, token := range availableTokens {
|
||||||
|
if strings.Contains(path, token) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
func createIncludePaths(
|
func createIncludePaths(
|
||||||
suggestedPath string,
|
suggestedPath string,
|
||||||
) ([]indexes.ValidPath, error) {
|
) ([]indexes.ValidPath, error) {
|
||||||
@ -45,6 +90,14 @@ func createIncludePaths(
|
|||||||
|
|
||||||
if path.IsAbs(suggestedPath) {
|
if path.IsAbs(suggestedPath) {
|
||||||
absolutePath = suggestedPath
|
absolutePath = suggestedPath
|
||||||
|
} else if strings.HasPrefix(suggestedPath, "~") {
|
||||||
|
homeFolder, err := os.UserHomeDir()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.New(fmt.Sprintf("Could not find home folder (error: %s)", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
absolutePath = path.Join(homeFolder, suggestedPath[1:])
|
||||||
} else {
|
} else {
|
||||||
homeFolder, err := os.UserHomeDir()
|
homeFolder, err := os.UserHomeDir()
|
||||||
|
|
||||||
|
43
server/handlers/ssh_config/handlers/definition.go
Normal file
43
server/handlers/ssh_config/handlers/definition.go
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"config-lsp/common"
|
||||||
|
"config-lsp/handlers/ssh_config/indexes"
|
||||||
|
"config-lsp/utils"
|
||||||
|
"slices"
|
||||||
|
|
||||||
|
protocol "github.com/tliron/glsp/protocol_3_16"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetIncludeOptionLocation(
|
||||||
|
include *indexes.SSHIndexIncludeLine,
|
||||||
|
index common.IndexPosition,
|
||||||
|
) []protocol.Location {
|
||||||
|
foundIndex, found := slices.BinarySearchFunc(
|
||||||
|
include.Values,
|
||||||
|
index,
|
||||||
|
func(current *indexes.SSHIndexIncludeValue, target common.IndexPosition) int {
|
||||||
|
if current.IsPositionAfterEnd(target) {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
|
if current.IsPositionBeforeStart(target) {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
if !found {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
path := include.Values[foundIndex]
|
||||||
|
|
||||||
|
return utils.Map(path.Paths, func(path indexes.ValidPath) protocol.Location {
|
||||||
|
return protocol.Location{
|
||||||
|
URI: path.AsURI(),
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
@ -1,8 +1,10 @@
|
|||||||
package lsp
|
package lsp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"config-lsp/common"
|
||||||
sshconfig "config-lsp/handlers/ssh_config"
|
sshconfig "config-lsp/handlers/ssh_config"
|
||||||
"config-lsp/handlers/ssh_config/fields"
|
"config-lsp/handlers/ssh_config/fields"
|
||||||
|
"config-lsp/handlers/ssh_config/handlers"
|
||||||
|
|
||||||
"github.com/tliron/glsp"
|
"github.com/tliron/glsp"
|
||||||
protocol "github.com/tliron/glsp/protocol_3_16"
|
protocol "github.com/tliron/glsp/protocol_3_16"
|
||||||
@ -12,9 +14,16 @@ var tagOption = fields.CreateNormalizedName("Tag")
|
|||||||
|
|
||||||
func TextDocumentDefinition(context *glsp.Context, params *protocol.DefinitionParams) ([]protocol.Location, error) {
|
func TextDocumentDefinition(context *glsp.Context, params *protocol.DefinitionParams) ([]protocol.Location, error) {
|
||||||
line := params.Position.Line
|
line := params.Position.Line
|
||||||
|
index := common.LSPCharacterAsIndexPosition(params.Position.Character)
|
||||||
d := sshconfig.DocumentParserMap[params.TextDocument.URI]
|
d := sshconfig.DocumentParserMap[params.TextDocument.URI]
|
||||||
|
|
||||||
|
if include, found := d.Indexes.Includes[line]; found {
|
||||||
|
return handlers.GetIncludeOptionLocation(
|
||||||
|
include,
|
||||||
|
index,
|
||||||
|
), nil
|
||||||
|
}
|
||||||
|
|
||||||
option, _ := d.Config.FindOption(line)
|
option, _ := d.Config.FindOption(line)
|
||||||
|
|
||||||
if option != nil && option.Key.Key == tagOption && option.OptionValue != nil {
|
if option != nil && option.Key.Key == tagOption && option.OptionValue != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user