fix(ssh_config): Fix formatter; Fetch options per line now

This commit is contained in:
Myzel394 2024-10-01 16:57:09 +02:00
parent a0c0ccce25
commit 2ea2c1eea8
No known key found for this signature in database
GPG Key ID: ED20A1D1D423AF3F
3 changed files with 123 additions and 96 deletions

View File

@ -229,3 +229,15 @@ func (c SSHConfig) GetAllOptions() []AllOptionInfo {
return options
}
func (c SSHConfig) GetOptionsInRange(startLine uint32, endLine uint32) []AllOptionInfo {
options := make([]AllOptionInfo, 0, 50)
for _, info := range c.GetAllOptions() {
if info.Option.LocationRange.Start.Line >= startLine && info.Option.LocationRange.End.Line <= endLine {
options = append(options, info)
}
}
return options
}

View File

@ -2,11 +2,14 @@ package handlers
import (
sshconfig "config-lsp/handlers/ssh_config"
"config-lsp/handlers/ssh_config/ast"
"config-lsp/handlers/ssh_config/fields"
protocol "github.com/tliron/glsp/protocol_3_16"
)
var hostOption = fields.CreateNormalizedName("Host")
var matchOption = fields.CreateNormalizedName("Match")
func FormatDocument(
d *sshconfig.SSHDocument,
textRange protocol.Range,
@ -14,33 +17,45 @@ func FormatDocument(
) ([]protocol.TextEdit, error) {
edits := make([]protocol.TextEdit, 0)
it := d.Config.Options.Iterator()
for it.Next() {
line := it.Key().(uint32)
entry := it.Value().(ast.SSHEntry)
entries := d.Config.GetOptionsInRange(textRange.Start.Line, textRange.End.Line)
if !(line >= textRange.Start.Line && line <= textRange.End.Line) {
for _, info := range entries {
option := info.Option
if option.Key == nil {
continue
}
switch entry.GetType() {
case ast.SSHTypeOption:
option := entry.(*ast.SSHOption)
edits = append(edits, formatSSHOption(
option,
options,
optionTemplate,
)...)
case ast.SSHTypeMatch:
matchBlock := entry.(*ast.SSHMatchBlock)
edits = formatSSHMatchBlock(textRange, matchBlock, options)
case ast.SSHTypeHost:
hostBlock := entry.(*ast.SSHHostBlock)
edits = formatSSHHostBlock(textRange, hostBlock, options)
}
edits = append(edits, formatOption(option, info.Block, options)...)
}
// it := d.Config.Options.Iterator()
// for it.Next() {
// line := it.Key().(uint32)
// entry := it.Value().(ast.SSHEntry)
//
// if !(line >= textRange.Start.Line && line <= textRange.End.Line) {
// continue
// }
//
// switch entry.GetType() {
// case ast.SSHTypeOption:
// option := entry.(*ast.SSHOption)
// edits = append(edits, formatSSHOption(
// option,
// options,
// optionTemplate,
// )...)
// case ast.SSHTypeMatch:
// matchBlock := entry.(*ast.SSHMatchBlock)
//
// edits = formatSSHMatchBlock(textRange, matchBlock, options)
// case ast.SSHTypeHost:
// hostBlock := entry.(*ast.SSHHostBlock)
//
// edits = formatSSHHostBlock(textRange, hostBlock, options)
// }
// }
return edits, nil
}

View File

@ -16,12 +16,82 @@ import (
var optionTemplate = formatting.FormatTemplate(
"%s /!'%s/!'",
)
var blockOptionTemplate = formatting.FormatTemplate(
" %s /!'%s/!'",
)
var matchTemplate = formatting.FormatTemplate(
"%s %s",
)
var matchOptionTemplate = formatting.FormatTemplate(
" %s /!'%s/!'",
)
func formatOption(
option *ast.SSHOption,
block ast.SSHBlock,
options protocol.FormattingOptions,
) []protocol.TextEdit {
if option.Key == nil || option.OptionValue == nil {
return nil
}
edits := make([]protocol.TextEdit, 0)
if option.Key.Key == hostOption {
edits = append(edits, formatHostBlock(block.(*ast.SSHHostBlock), options)...)
} else if option.Key.Key == matchOption {
edits = append(edits, formatMatchBlock(block.(*ast.SSHMatchBlock), options)...)
} else {
var template formatting.FormatTemplate
if block == nil {
template = optionTemplate
} else if block.GetBlockType() == ast.SSHBlockTypeMatch {
template = blockOptionTemplate
} else if block.GetBlockType() == ast.SSHBlockTypeHost {
template = blockOptionTemplate
}
edits = append(edits, formatSSHOption(option, options, template)...)
}
return edits
}
func formatHostBlock(
hostBlock *ast.SSHHostBlock,
options protocol.FormattingOptions,
) []protocol.TextEdit {
edits := make([]protocol.TextEdit, 0)
key := fields.FieldsNameFormattedMap[hostBlock.GetEntryOption().Key.Key]
edits = append(edits, protocol.TextEdit{
Range: hostBlock.GetEntryOption().ToLSPRange(),
NewText: matchTemplate.Format(
options,
key,
formatHostToString(hostBlock.HostValue),
),
})
return edits
}
func formatMatchBlock(
matchBlock *ast.SSHMatchBlock,
options protocol.FormattingOptions,
) []protocol.TextEdit {
edits := make([]protocol.TextEdit, 0)
key := fields.FieldsNameFormattedMap[matchBlock.GetEntryOption().Key.Key]
edits = append(edits, protocol.TextEdit{
Range: matchBlock.GetEntryOption().ToLSPRange(),
NewText: matchTemplate.Format(
options,
key,
formatMatchToString(matchBlock.MatchValue),
),
})
return edits
}
func formatSSHOption(
option *ast.SSHOption,
@ -52,41 +122,6 @@ func formatSSHOption(
}
}
func formatSSHMatchBlock(
textRange protocol.Range,
matchBlock *ast.SSHMatchBlock,
options protocol.FormattingOptions,
) []protocol.TextEdit {
edits := make([]protocol.TextEdit, 0)
key := fields.FieldsNameFormattedMap[matchBlock.GetEntryOption().Key.Key]
edits = append(edits, protocol.TextEdit{
Range: matchBlock.GetEntryOption().ToLSPRange(),
NewText: matchTemplate.Format(
options,
key,
formatMatchToString(matchBlock.MatchValue),
),
})
it := matchBlock.GetOptions().Iterator()
for it.Next() {
option := it.Value().(*ast.SSHOption)
if !(option.Start.Line >= textRange.Start.Line && option.End.Line <= textRange.End.Line) {
continue
}
edits = append(edits, formatSSHOption(
option,
options,
matchOptionTemplate,
)...)
}
return edits
}
func formatMatchToString(
match *matchparser.Match,
) string {
@ -112,41 +147,6 @@ func formatMatchToString(
return strings.Join(entriesAsStrings, " ")
}
func formatSSHHostBlock(
textRange protocol.Range,
hostBlock *ast.SSHHostBlock,
options protocol.FormattingOptions,
) []protocol.TextEdit {
edits := make([]protocol.TextEdit, 0)
key := fields.FieldsNameFormattedMap[hostBlock.GetEntryOption().Key.Key]
edits = append(edits, protocol.TextEdit{
Range: hostBlock.GetEntryOption().ToLSPRange(),
NewText: matchTemplate.Format(
options,
key,
formatHostToString(hostBlock.HostValue),
),
})
it := hostBlock.GetOptions().Iterator()
for it.Next() {
option := it.Value().(*ast.SSHOption)
if !(option.Start.Line >= textRange.Start.Line && option.End.Line <= textRange.End.Line) {
continue
}
edits = append(edits, formatSSHOption(
option,
options,
matchOptionTemplate,
)...)
}
return edits
}
func formatHostToString(
host *hostparser.Host,
) string {