mirror of
https://github.com/Myzel394/config-lsp.git
synced 2025-06-18 23:15:26 +02:00
feat(wireguard): Add code action for PersistentKeepalive
This commit is contained in:
parent
cdb9017c4d
commit
394914a271
@ -274,7 +274,7 @@ func (p wireguardProperty) analyzeProperty(
|
||||
}
|
||||
|
||||
func (p wireguardParser) checkForDuplicateProperties() []protocol.Diagnostic {
|
||||
diagnostics := []protocol.Diagnostic{}
|
||||
diagnostics := make([]protocol.Diagnostic, 0)
|
||||
|
||||
for _, section := range p.Sections {
|
||||
diagnostics = append(diagnostics, section.analyzeDuplicateProperties()...)
|
||||
@ -284,7 +284,7 @@ func (p wireguardParser) checkForDuplicateProperties() []protocol.Diagnostic {
|
||||
}
|
||||
|
||||
func (p wireguardSection) analyzeDuplicateProperties() []protocol.Diagnostic {
|
||||
diagnostics := []protocol.Diagnostic{}
|
||||
diagnostics := make([]protocol.Diagnostic, 0)
|
||||
|
||||
existingProperties := make(map[string]uint32)
|
||||
|
||||
@ -337,7 +337,7 @@ func (p wireguardSection) analyzeDuplicateProperties() []protocol.Diagnostic {
|
||||
}
|
||||
|
||||
func (p wireguardParser) analyzeAllowedIPIsInRange() []protocol.Diagnostic {
|
||||
diagnostics := []protocol.Diagnostic{}
|
||||
diagnostics := make([]protocol.Diagnostic, 0)
|
||||
|
||||
return diagnostics
|
||||
}
|
||||
|
@ -9,6 +9,7 @@ type codeActionName string
|
||||
const (
|
||||
codeActionGeneratePrivateKey codeActionName = "generatePrivateKey"
|
||||
codeActionGeneratePresharedKey codeActionName = "generatePresharedKey"
|
||||
codeActionAddKeepalive codeActionName = "addKeepalive"
|
||||
)
|
||||
|
||||
type codeActionGeneratePrivateKeyArgs struct {
|
||||
@ -35,6 +36,18 @@ func codeActionGeneratePresharedKeyArgsFromArguments(arguments map[string]any) c
|
||||
}
|
||||
}
|
||||
|
||||
type codeActionAddKeepaliveArgs struct {
|
||||
URI protocol.DocumentUri
|
||||
SectionIndex uint32
|
||||
}
|
||||
|
||||
func codeActionAddKeepaliveArgsFromArguments(arguments map[string]any) codeActionAddKeepaliveArgs {
|
||||
return codeActionAddKeepaliveArgs{
|
||||
URI: arguments["URI"].(protocol.DocumentUri),
|
||||
SectionIndex: uint32(arguments["SectionIndex"].(float64)),
|
||||
}
|
||||
}
|
||||
|
||||
func (p wireguardProperty) getInsertRange(line uint32) protocol.Range {
|
||||
var insertPosition uint32 = p.Separator.Location.End
|
||||
var length uint32 = 0
|
||||
@ -114,3 +127,31 @@ func (p *wireguardParser) runGeneratePresharedKey(args codeActionGeneratePreshar
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (p *wireguardParser) runAddKeepalive(args codeActionAddKeepaliveArgs) (*protocol.ApplyWorkspaceEditParams, error) {
|
||||
section := p.Sections[args.SectionIndex]
|
||||
|
||||
label := "Add PersistentKeepalive"
|
||||
return &protocol.ApplyWorkspaceEditParams{
|
||||
Label: &label,
|
||||
Edit: protocol.WorkspaceEdit{
|
||||
Changes: map[protocol.DocumentUri][]protocol.TextEdit{
|
||||
args.URI: {
|
||||
{
|
||||
NewText: "PersistentKeepalive = 25\n",
|
||||
Range: protocol.Range{
|
||||
Start: protocol.Position{
|
||||
Line: section.EndLine + 1,
|
||||
Character: 0,
|
||||
},
|
||||
End: protocol.Position{
|
||||
Line: section.EndLine + 1,
|
||||
Character: 0,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
100
handlers/wireguard/fetch-code-actions.go
Normal file
100
handlers/wireguard/fetch-code-actions.go
Normal file
@ -0,0 +1,100 @@
|
||||
package wireguard
|
||||
|
||||
import protocol "github.com/tliron/glsp/protocol_3_16"
|
||||
|
||||
func getKeepaliveCodeActions(
|
||||
params *protocol.CodeActionParams,
|
||||
parser *wireguardParser,
|
||||
) []protocol.CodeAction {
|
||||
line := params.Range.Start.Line
|
||||
|
||||
for index, section := range parser.Sections {
|
||||
if section.StartLine >= line && line <= section.EndLine && section.Name != nil && *section.Name == "Peer" {
|
||||
if section.fetchFirstProperty("Endpoint") != nil && section.fetchFirstProperty("PersistentKeepalive") == nil {
|
||||
commandID := "wireguard." + codeActionAddKeepalive
|
||||
command := protocol.Command{
|
||||
Title: "Add PersistentKeepalive",
|
||||
Command: string(commandID),
|
||||
Arguments: []any{
|
||||
codeActionAddKeepaliveArgs{
|
||||
URI: params.TextDocument.URI,
|
||||
SectionIndex: uint32(index),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
return []protocol.CodeAction{
|
||||
{
|
||||
Title: "Add PersistentKeepalive",
|
||||
Command: &command,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func getKeyGenerationCodeActions(
|
||||
params *protocol.CodeActionParams,
|
||||
parser *wireguardParser,
|
||||
) []protocol.CodeAction {
|
||||
line := params.Range.Start.Line
|
||||
section, property := parser.getPropertyByLine(line)
|
||||
|
||||
if section == nil || property == nil || property.Separator == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
switch property.Key.Name {
|
||||
case "PrivateKey":
|
||||
if !areWireguardToolsAvailable() {
|
||||
return nil
|
||||
}
|
||||
|
||||
commandID := "wireguard." + codeActionGeneratePrivateKey
|
||||
command := protocol.Command{
|
||||
Title: "Generate Private Key",
|
||||
Command: string(commandID),
|
||||
Arguments: []any{
|
||||
codeActionGeneratePrivateKeyArgs{
|
||||
URI: params.TextDocument.URI,
|
||||
Line: line,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
return []protocol.CodeAction{
|
||||
{
|
||||
Title: "Generate Private Key",
|
||||
Command: &command,
|
||||
},
|
||||
}
|
||||
case "PresharedKey":
|
||||
if !areWireguardToolsAvailable() {
|
||||
return nil
|
||||
}
|
||||
|
||||
commandID := "wireguard." + codeActionGeneratePresharedKey
|
||||
command := protocol.Command{
|
||||
Title: "Generate PresharedKey",
|
||||
Command: string(commandID),
|
||||
Arguments: []any{
|
||||
codeActionGeneratePresharedKeyArgs{
|
||||
URI: params.TextDocument.URI,
|
||||
Line: line,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
return []protocol.CodeAction{
|
||||
{
|
||||
Title: "Generate PresharedKey",
|
||||
Command: &command,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@ -6,62 +6,15 @@ import (
|
||||
)
|
||||
|
||||
func TextDocumentCodeAction(context *glsp.Context, params *protocol.CodeActionParams) ([]protocol.CodeAction, error) {
|
||||
line := params.Range.Start.Line
|
||||
parser := documentParserMap[params.TextDocument.URI]
|
||||
|
||||
section, property := parser.getPropertyByLine(line)
|
||||
actions := make([]protocol.CodeAction, 0, 2)
|
||||
|
||||
if section == nil || property == nil || property.Separator == nil {
|
||||
return nil, nil
|
||||
}
|
||||
actions = append(actions, getKeyGenerationCodeActions(params, parser)...)
|
||||
actions = append(actions, getKeepaliveCodeActions(params, parser)...)
|
||||
|
||||
switch property.Key.Name {
|
||||
case "PrivateKey":
|
||||
if !areWireguardToolsAvailable() {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
commandID := "wireguard." + codeActionGeneratePrivateKey
|
||||
command := protocol.Command{
|
||||
Title: "Generate Private Key",
|
||||
Command: string(commandID),
|
||||
Arguments: []any{
|
||||
codeActionGeneratePrivateKeyArgs{
|
||||
URI: params.TextDocument.URI,
|
||||
Line: line,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
return []protocol.CodeAction{
|
||||
{
|
||||
Title: "Generate Private Key",
|
||||
Command: &command,
|
||||
},
|
||||
}, nil
|
||||
case "PresharedKey":
|
||||
if !areWireguardToolsAvailable() {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
commandID := "wireguard." + codeActionGeneratePresharedKey
|
||||
command := protocol.Command{
|
||||
Title: "Generate PresharedKey",
|
||||
Command: string(commandID),
|
||||
Arguments: []any{
|
||||
codeActionGeneratePresharedKeyArgs{
|
||||
URI: params.TextDocument.URI,
|
||||
Line: line,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
return []protocol.CodeAction{
|
||||
{
|
||||
Title: "Generate PresharedKey",
|
||||
Command: &command,
|
||||
},
|
||||
}, nil
|
||||
if len(actions) > 0 {
|
||||
return actions, nil
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
|
@ -23,6 +23,12 @@ func WorkspaceExecuteCommand(context *glsp.Context, params *protocol.ExecuteComm
|
||||
parser := documentParserMap[args.URI]
|
||||
|
||||
return parser.runGeneratePresharedKey(args)
|
||||
case string(codeActionAddKeepalive):
|
||||
args := codeActionAddKeepaliveArgsFromArguments(params.Arguments[0].(map[string]any))
|
||||
|
||||
parser := documentParserMap[args.URI]
|
||||
|
||||
return parser.runAddKeepalive(args)
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
|
Loading…
x
Reference in New Issue
Block a user