Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more signature help #22

Merged
merged 4 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions server/handlers/fstab/handlers/signature_help.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package handlers

import (
"config-lsp/common"
"config-lsp/handlers/fstab/ast"

protocol "github.com/tliron/glsp/protocol_3_16"
)

func GetEntrySignatureHelp(
entry *ast.FstabEntry,
cursor common.CursorPosition,
) *protocol.SignatureHelp {
var index uint32

if entry == nil || entry.Fields.Spec == nil || entry.Fields.Spec.ContainsPosition(cursor) {
index = 0
} else if entry.Fields.MountPoint == nil && entry.Fields.MountPoint.ContainsPosition(cursor) {
index = 1
} else if entry.Fields.FilesystemType == nil && entry.Fields.FilesystemType.ContainsPosition(cursor) {
index = 2
} else if entry.Fields.Options == nil || entry.Fields.Options.ContainsPosition(cursor) {
index = 3
} else if entry.Fields.Freq == nil || entry.Fields.Freq.ContainsPosition(cursor) {
index = 4
} else {
index = 5
}

signature := uint32(0)

return &protocol.SignatureHelp{
ActiveSignature: &signature,
Signatures: []protocol.SignatureInformation{
{
Label: "<spec> <mount point> <file system type> <options> <freq> <pass>",
ActiveParameter: &index,
Parameters: []protocol.ParameterInformation{
{
Label: []uint32{
0,
uint32(len("<spec>")),
},
Documentation: "The device or remote filesystem to mount",
},
{
Label: []uint32{
uint32(len("<spec>")),
uint32(len("<spec> ") + len("<mount point>")),
},
Documentation: "The directory to mount the device or remote filesystem",
},
{
Label: []uint32{
uint32(len("<spec> <mount point>")),
uint32(len("<spec> <mount point> ") + len("<file system type>")),
},
Documentation: "The type of filesystem",
},
{
Label: []uint32{
uint32(len("<spec> <mount point> <file system type>")),
uint32(len("<spec> <mount point> <file system type> ") + len("<options>")),
},
Documentation: "Mount options",
},
{
Label: []uint32{
uint32(len("<spec> <mount point> <file system type> <options>")),
uint32(len("<spec> <mount point> <file system type> <options> ") + len("<freq>")),
},
Documentation: "Used by dump(8) to determine which filesystems need to be dumped",
},
{
Label: []uint32{
uint32(len("<spec> <mount point> <file system type> <options> <freq>")),
uint32(len("<spec> <mount point> <file system type> <options> <freq> ") + len("<pass>")),
},
Documentation: "Used by fsck(8) to determine the order in which filesystem checks are done at boot time",
},
},
},
},
}
}
31 changes: 31 additions & 0 deletions server/handlers/fstab/lsp/text-document-signature-help.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package lsp

import (
"config-lsp/common"
"config-lsp/handlers/fstab/ast"
"config-lsp/handlers/fstab/handlers"
fstab "config-lsp/handlers/fstab/shared"

"github.com/tliron/glsp"
protocol "github.com/tliron/glsp/protocol_3_16"
)

func TextDocumentSignatureHelp(context *glsp.Context, params *protocol.SignatureHelpParams) (*protocol.SignatureHelp, error) {
document := fstab.DocumentParserMap[params.TextDocument.URI]

line := uint32(params.Position.Line)
cursor := common.LSPCharacterAsCursorPosition(params.Position.Character)

if _, found := document.Config.CommentLines[line]; found {
// Comment
return nil, nil
}

entry, found := document.Config.Entries.Get(line)

if !found {
return handlers.GetEntrySignatureHelp(nil, cursor), nil
} else {
return handlers.GetEntrySignatureHelp(entry.(*ast.FstabEntry), cursor), nil
}
}
58 changes: 58 additions & 0 deletions server/handlers/hosts/handlers/signature_help.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package handlers

import (
"config-lsp/common"
"config-lsp/handlers/hosts/ast"

protocol "github.com/tliron/glsp/protocol_3_16"
)

func GetEntrySignatureHelp(
entry *ast.HostsEntry,
cursor common.CursorPosition,
) *protocol.SignatureHelp {
var index uint32

if entry == nil || entry.IPAddress == nil || entry.IPAddress.Location.ContainsPosition(cursor) {
index = 0
} else if entry.Hostname == nil || entry.Hostname.Location.ContainsPosition(cursor) {
index = 1
} else {
index = 2
}

signature := uint32(0)

return &protocol.SignatureHelp{
ActiveSignature: &signature,
Signatures: []protocol.SignatureInformation{
{
Label: "<ip address> <hostname> [<alias>...]",
ActiveParameter: &index,
Parameters: []protocol.ParameterInformation{
{
Label: []uint32{
0,
uint32(len("<ip address>")),
},
Documentation: "The ip address to forward to",
},
{
Label: []uint32{
uint32(len("<ip address>")),
uint32(len("<ip address> ") + len("<hostname>")),
},
Documentation: "The hostname to forward to",
},
{
Label: []uint32{
uint32(len("<ip address> ") + len("<hostname>")),
uint32(len("<ip address> ") + len("<hostname> ") + len("[<alias>...]")),
},
Documentation: "An optional list of aliases that can also forward",
},
},
},
},
}
}
31 changes: 31 additions & 0 deletions server/handlers/hosts/lsp/text-document-signature-help.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package lsp

import (
"config-lsp/common"
"config-lsp/handlers/hosts"
"config-lsp/handlers/hosts/ast"
"config-lsp/handlers/hosts/handlers"

"github.com/tliron/glsp"
protocol "github.com/tliron/glsp/protocol_3_16"
)

func TextDocumentSignatureHelp(context *glsp.Context, params *protocol.SignatureHelpParams) (*protocol.SignatureHelp, error) {
document := hosts.DocumentParserMap[params.TextDocument.URI]

line := uint32(params.Position.Line)
cursor := common.LSPCharacterAsCursorPosition(params.Position.Character)

if _, found := document.Parser.CommentLines[line]; found {
// Comment
return nil, nil
}

entry, found := document.Parser.Tree.Entries.Get(line)

if !found {
return handlers.GetEntrySignatureHelp(nil, cursor), nil
} else {
return handlers.GetEntrySignatureHelp(entry.(*ast.HostsEntry), cursor), nil
}
}
9 changes: 5 additions & 4 deletions server/handlers/ssh_config/handlers/signature_help.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func GetOptionSignatureHelp(
}

signature := uint32(0)

return &protocol.SignatureHelp{
ActiveSignature: &signature,
Signatures: []protocol.SignatureInformation{
Expand All @@ -37,7 +38,7 @@ func GetOptionSignatureHelp(
{
Label: []uint32{
uint32(len("<option>")),
uint32(len("<option>") + len("<value>") + 1),
uint32(len("<option> ") + len("<value>")),
},
Documentation: "The value for the option",
},
Expand Down Expand Up @@ -90,7 +91,7 @@ func GetMatchSignatureHelp(
{
Label: []uint32{
uint32(len("Host <criteria> ")),
uint32(len("Host <criteria> ") + len("<values>") + 1),
uint32(len("Host <criteria> ") + len("<values>")),
},
Documentation: "Values for the criteria",
},
Expand Down Expand Up @@ -132,14 +133,14 @@ func GetHostSignatureHelp(
{
Label: []uint32{
uint32(len("Host ")),
uint32(len("Host ") + len("<host1>") + 1),
uint32(len("Host ") + len("<host1>")),
},
Documentation: "A host that should match",
},
{
Label: []uint32{
uint32(len("Host <host1> ")),
uint32(len("Host <host1> ") + len("[<host2> ...]") + 1),
uint32(len("Host <host1> ") + len("[<host2> ...]")),
},
Documentation: "Additional (optional) hosts that should match",
},
Expand Down
6 changes: 4 additions & 2 deletions server/root-handler/lsp/text-document-signature-help.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package lsp

import (
aliases "config-lsp/handlers/aliases/lsp"
fstab "config-lsp/handlers/fstab/lsp"
hosts "config-lsp/handlers/hosts/lsp"
sshconfig "config-lsp/handlers/ssh_config/lsp"
sshdconfig "config-lsp/handlers/sshd_config/lsp"
"config-lsp/root-handler/shared"
Expand All @@ -18,13 +20,13 @@ func TextDocumentSignatureHelp(context *glsp.Context, params *protocol.Signature

switch *language {
case shared.LanguageHosts:
return nil, nil
return hosts.TextDocumentSignatureHelp(context, params)
case shared.LanguageSSHDConfig:
return sshdconfig.TextDocumentSignatureHelp(context, params)
case shared.LanguageSSHConfig:
return sshconfig.TextDocumentSignatureHelp(context, params)
case shared.LanguageFstab:
return nil, nil
return fstab.TextDocumentSignatureHelp(context, params)
case shared.LanguageWireguard:
return nil, nil
case shared.LanguageAliases:
Expand Down
2 changes: 1 addition & 1 deletion server/root-handler/shared/indexes.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type LanguageOverwrite struct {
Language SupportedLanguage

// The start of the overwrite
Raw string
Raw string
Line uint32
Character uint32
}
Expand Down