From 2ea2c1eea8dbf9af0c05baf4c9d2c76307e6a330 Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Tue, 1 Oct 2024 16:57:09 +0200 Subject: [PATCH] fix(ssh_config): Fix formatter; Fetch options per line now --- handlers/ssh_config/ast/ssh_config_fields.go | 12 ++ handlers/ssh_config/handlers/formatting.go | 61 +++++--- .../ssh_config/handlers/formatting_nodes.go | 146 +++++++++--------- 3 files changed, 123 insertions(+), 96 deletions(-) diff --git a/handlers/ssh_config/ast/ssh_config_fields.go b/handlers/ssh_config/ast/ssh_config_fields.go index b5544ae..0115ed6 100644 --- a/handlers/ssh_config/ast/ssh_config_fields.go +++ b/handlers/ssh_config/ast/ssh_config_fields.go @@ -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 +} diff --git a/handlers/ssh_config/handlers/formatting.go b/handlers/ssh_config/handlers/formatting.go index 8889f33..9fcfbe4 100644 --- a/handlers/ssh_config/handlers/formatting.go +++ b/handlers/ssh_config/handlers/formatting.go @@ -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 } diff --git a/handlers/ssh_config/handlers/formatting_nodes.go b/handlers/ssh_config/handlers/formatting_nodes.go index 2f236e2..2cbd8f7 100644 --- a/handlers/ssh_config/handlers/formatting_nodes.go +++ b/handlers/ssh_config/handlers/formatting_nodes.go @@ -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 {