chore: Apply gofmt

This commit is contained in:
Myzel394 2024-07-28 18:05:24 +02:00
parent 403cc9a336
commit 1d55158fc8
No known key found for this signature in database
GPG Key ID: DEC4AAB876F73185
12 changed files with 579 additions and 580 deletions

View File

@ -14,39 +14,43 @@ type Value interface {
type EnumValue struct { type EnumValue struct {
Values []string Values []string
} }
func (v EnumValue) getTypeDescription() []string { func (v EnumValue) getTypeDescription() []string {
lines := make([]string, len(v.Values) + 1) lines := make([]string, len(v.Values)+1)
lines[0] = "Enum of:" lines[0] = "Enum of:"
for index, value := range v.Values { for index, value := range v.Values {
lines[index + 1] += "\t* " + value lines[index+1] += "\t* " + value
} }
return lines return lines
} }
type PositiveNumberValue struct {} type PositiveNumberValue struct{}
func (v PositiveNumberValue) getTypeDescription() []string { func (v PositiveNumberValue) getTypeDescription() []string {
return []string{ "Positive number" } return []string{"Positive number"}
} }
type ArrayValue struct { type ArrayValue struct {
SubValue Value SubValue Value
Separator string Separator string
AllowDuplicates bool AllowDuplicates bool
} }
func (v ArrayValue) getTypeDescription() []string { func (v ArrayValue) getTypeDescription() []string {
subValue := v.SubValue.(Value) subValue := v.SubValue.(Value)
return append( return append(
[]string{ "An Array separated by " + v.Separator + " of:" }, []string{"An Array separated by " + v.Separator + " of:"},
subValue.getTypeDescription()... subValue.getTypeDescription()...,
) )
} }
type OrValue struct { type OrValue struct {
Values []Value Values []Value
} }
func (v OrValue) getTypeDescription() []string { func (v OrValue) getTypeDescription() []string {
lines := make([]string, 0) lines := make([]string, 0)
@ -66,31 +70,34 @@ func (v OrValue) getTypeDescription() []string {
} }
return append( return append(
[]string{ "One of:" }, []string{"One of:"},
lines... lines...,
) )
} }
type StringValue struct {} type StringValue struct{}
func (v StringValue) getTypeDescription() []string { func (v StringValue) getTypeDescription() []string {
return []string{ "String" } return []string{"String"}
} }
type CustomValue struct { type CustomValue struct {
FetchValue func() Value FetchValue func() Value
} }
func (v CustomValue) getTypeDescription() []string { func (v CustomValue) getTypeDescription() []string {
return []string{ "Custom" } return []string{"Custom"}
} }
type Prefix struct { type Prefix struct {
Prefix string Prefix string
Meaning string Meaning string
} }
type PrefixWithMeaningValue struct { type PrefixWithMeaningValue struct {
Prefixes []Prefix Prefixes []Prefix
SubValue Value SubValue Value
} }
func (v PrefixWithMeaningValue) getTypeDescription() []string { func (v PrefixWithMeaningValue) getTypeDescription() []string {
subDescription := v.SubValue.getTypeDescription() subDescription := v.SubValue.getTypeDescription()
@ -100,23 +107,22 @@ func (v PrefixWithMeaningValue) getTypeDescription() []string {
return append(subDescription, return append(subDescription,
append( append(
[]string{ "The following prefixes are allowed:" }, []string{"The following prefixes are allowed:"},
prefixDescription..., prefixDescription...,
)..., )...,
) )
} }
type Option struct { type Option struct {
Documentation string Documentation string
Value Value Value Value
} }
func GetDocumentation(o *Option) protocol.MarkupContent { func GetDocumentation(o *Option) protocol.MarkupContent {
typeDescription := strings.Join(o.Value.getTypeDescription(), "\n") typeDescription := strings.Join(o.Value.getTypeDescription(), "\n")
return protocol.MarkupContent{ return protocol.MarkupContent{
Kind: protocol.MarkupKindPlainText, Kind: protocol.MarkupKindPlainText,
Value: "### Type\n" + typeDescription + "\n\n---\n\n### Documentation\n" + o.Documentation, Value: "### Type\n" + typeDescription + "\n\n---\n\n### Documentation\n" + o.Documentation,
} }
} }
@ -124,4 +130,3 @@ func GetDocumentation(o *Option) protocol.MarkupContent {
func NewOption(documentation string, value Value) Option { func NewOption(documentation string, value Value) Option {
return Option{documentation, value} return Option{documentation, value}
} }

View File

@ -5,12 +5,13 @@ import (
"strings" "strings"
) )
type ParserError interface {} type ParserError interface{}
type OptionAlreadyExistsError struct { type OptionAlreadyExistsError struct {
Option string Option string
FoundOnLine uint32 FoundOnLine uint32
} }
func (e OptionAlreadyExistsError) Error() string { func (e OptionAlreadyExistsError) Error() string {
return fmt.Sprintf("Option %s already exists", e.Option) return fmt.Sprintf("Option %s already exists", e.Option)
} }
@ -18,6 +19,7 @@ func (e OptionAlreadyExistsError) Error() string {
type OptionUnknownError struct { type OptionUnknownError struct {
Option string Option string
} }
func (e OptionUnknownError) Error() string { func (e OptionUnknownError) Error() string {
return fmt.Sprintf("Option '%s' does not exist", e.Option) return fmt.Sprintf("Option '%s' does not exist", e.Option)
} }
@ -25,20 +27,22 @@ func (e OptionUnknownError) Error() string {
type MalformedLineError struct { type MalformedLineError struct {
Line string Line string
} }
func (e MalformedLineError) Error() string { func (e MalformedLineError) Error() string {
return fmt.Sprintf("Malformed line: %s", e.Line) return fmt.Sprintf("Malformed line: %s", e.Line)
} }
type LineNotFoundError struct {} type LineNotFoundError struct{}
func (e LineNotFoundError) Error() string { func (e LineNotFoundError) Error() string {
return "Line not found" return "Line not found"
} }
type ValueNotInEnumError struct { type ValueNotInEnumError struct {
availableValues []string availableValues []string
providedValue string providedValue string
} }
func (e ValueNotInEnumError) Error() string { func (e ValueNotInEnumError) Error() string {
return fmt.Sprint("'%s' is not valid. Select one from: %s", e.providedValue, strings.Join(e.availableValues, ",")) return fmt.Sprint("'%s' is not valid. Select one from: %s", e.providedValue, strings.Join(e.availableValues, ","))
} }

View File

@ -6,9 +6,9 @@ import (
) )
type passwdInfo struct { type passwdInfo struct {
Name string Name string
UID string UID string
GID string GID string
HomePath string HomePath string
} }
@ -36,9 +36,9 @@ func fetchPasswdInfo() ([]passwdInfo, error) {
} }
info := passwdInfo{ info := passwdInfo{
Name: splitted[0], Name: splitted[0],
UID: splitted[2], UID: splitted[2],
GID: splitted[3], GID: splitted[3],
HomePath: splitted[5], HomePath: splitted[5],
} }
@ -50,7 +50,6 @@ func fetchPasswdInfo() ([]passwdInfo, error) {
return infos, nil return infos, nil
} }
// UserValue returns a Value that fetches user names from /etc/passwd // UserValue returns a Value that fetches user names from /etc/passwd
// if `separatorForMultiple` is not empty, it will return an ArrayValue // if `separatorForMultiple` is not empty, it will return an ArrayValue
func UserValue(separatorForMultiple string) Value { func UserValue(separatorForMultiple string) Value {
@ -73,11 +72,10 @@ func UserValue(separatorForMultiple string) Value {
} else { } else {
return ArrayValue{ return ArrayValue{
AllowDuplicates: false, AllowDuplicates: false,
SubValue: enumValues, SubValue: enumValues,
Separator: separatorForMultiple, Separator: separatorForMultiple,
} }
} }
}, },
} }
} }

View File

@ -10,7 +10,7 @@ type SimpleConfigPosition struct {
} }
type SimpleConfigLine struct { type SimpleConfigLine struct {
Value string Value string
Position SimpleConfigPosition Position SimpleConfigPosition
} }
@ -23,13 +23,13 @@ func (l SimpleConfigLine) IsCursorAtRootOption(cursor int) bool {
} }
type SimpleConfigOptions struct { type SimpleConfigOptions struct {
Separator string Separator string
IgnorePattern regexp.Regexp IgnorePattern regexp.Regexp
AvailableOptions *map[string]Option AvailableOptions *map[string]Option
} }
type SimpleConfigParser struct { type SimpleConfigParser struct {
Lines map[string]SimpleConfigLine Lines map[string]SimpleConfigLine
Options SimpleConfigOptions Options SimpleConfigOptions
} }
@ -58,7 +58,7 @@ func (p *SimpleConfigParser) AddLine(line string, lineNumber int) error {
if _, exists := p.Lines[option]; exists { if _, exists := p.Lines[option]; exists {
return OptionAlreadyExistsError{ return OptionAlreadyExistsError{
Option: option, Option: option,
FoundOnLine: uint32(lineNumber), FoundOnLine: uint32(lineNumber),
} }
} }
@ -71,7 +71,7 @@ func (p *SimpleConfigParser) AddLine(line string, lineNumber int) error {
} }
return nil return nil
} }
func (p *SimpleConfigParser) ReplaceOption(option string, value string) { func (p *SimpleConfigParser) ReplaceOption(option string, value string) {
@ -91,7 +91,7 @@ func (p *SimpleConfigParser) UpsertOption(option string, value string) {
if _, exists := p.Lines[option]; exists { if _, exists := p.Lines[option]; exists {
p.ReplaceOption(option, value) p.ReplaceOption(option, value)
} else { } else {
p.AddLine(option + p.Options.Separator + value, len(p.Lines)) p.AddLine(option+p.Options.Separator+value, len(p.Lines))
} }
} }
@ -105,7 +105,7 @@ func (p *SimpleConfigParser) GetOption(option string) (SimpleConfigLine, error)
Position: SimpleConfigPosition{ Position: SimpleConfigPosition{
Line: 0, Line: 0,
}, },
}, },
OptionUnknownError{ OptionUnknownError{
Option: option, Option: option,
} }
@ -144,4 +144,3 @@ func (p *SimpleConfigParser) FindByLineNumber(lineNumber int) (string, SimpleCon
return "", SimpleConfigLine{Value: "", Position: SimpleConfigPosition{Line: 0}}, LineNotFoundError{} return "", SimpleConfigLine{Value: "", Position: SimpleConfigPosition{Line: 0}}, LineNotFoundError{}
} }

View File

@ -28,4 +28,3 @@ func Map[T any, O any](values []T, f func(T) O) []O {
return result return result
} }

View File

@ -10,47 +10,47 @@ import (
) )
func ClearDiagnostics(context *glsp.Context, uri protocol.DocumentUri) { func ClearDiagnostics(context *glsp.Context, uri protocol.DocumentUri) {
context.Notify( context.Notify(
"textDocument/publishDiagnostics", "textDocument/publishDiagnostics",
protocol.PublishDiagnosticsParams{ protocol.PublishDiagnosticsParams{
URI: uri, URI: uri,
Diagnostics: []protocol.Diagnostic{}, Diagnostics: []protocol.Diagnostic{},
}, },
) )
} }
func SendDiagnosticsFromParserErrors(context *glsp.Context, uri protocol.DocumentUri, parserErrors []common.ParserError) { func SendDiagnosticsFromParserErrors(context *glsp.Context, uri protocol.DocumentUri, parserErrors []common.ParserError) {
diagnosticErrors := make([]protocol.Diagnostic, 0) diagnosticErrors := make([]protocol.Diagnostic, 0)
for _, parserError := range parserErrors { for _, parserError := range parserErrors {
switch parserError.(type) { switch parserError.(type) {
case common.OptionAlreadyExistsError: { case common.OptionAlreadyExistsError:
err := parserError.(common.OptionAlreadyExistsError) {
existingOption, _ := Parser.GetOption(err.Option) err := parserError.(common.OptionAlreadyExistsError)
existingOption, _ := Parser.GetOption(err.Option)
diagnosticErrors = append(diagnosticErrors, protocol.Diagnostic{ diagnosticErrors = append(diagnosticErrors, protocol.Diagnostic{
Range: protocol.Range{ Range: protocol.Range{
Start: protocol.Position{ Start: protocol.Position{
Line: err.FoundOnLine, Line: err.FoundOnLine,
Character: 0, Character: 0,
}, },
End: protocol.Position{ End: protocol.Position{
Line: err.FoundOnLine, Line: err.FoundOnLine,
Character: uint32(utf8.RuneCountInString(err.Option)), Character: uint32(utf8.RuneCountInString(err.Option)),
}, },
}, },
Message: fmt.Sprintf("Option '%s' has already been declared on line %v", err.Option, existingOption.Position.Line + 1), Message: fmt.Sprintf("Option '%s' has already been declared on line %v", err.Option, existingOption.Position.Line+1),
}) })
}
}
} }
}
}
context.Notify( context.Notify(
"textDocument/publishDiagnostics", "textDocument/publishDiagnostics",
protocol.PublishDiagnosticsParams{ protocol.PublishDiagnosticsParams{
URI: uri, URI: uri,
Diagnostics: diagnosticErrors, Diagnostics: diagnosticErrors,
}, },
) )
} }

File diff suppressed because it is too large Load Diff

View File

@ -6,21 +6,20 @@ import (
) )
func createOpenSSHConfigParser() common.SimpleConfigParser { func createOpenSSHConfigParser() common.SimpleConfigParser {
pattern, err := regexp.Compile(`^(?:#|\s*$)`) pattern, err := regexp.Compile(`^(?:#|\s*$)`)
if (err != nil) { if err != nil {
panic(err) panic(err)
} }
return common.SimpleConfigParser{ return common.SimpleConfigParser{
Lines: make(map[string]common.SimpleConfigLine), Lines: make(map[string]common.SimpleConfigLine),
Options: common.SimpleConfigOptions{ Options: common.SimpleConfigOptions{
Separator: " ", Separator: " ",
IgnorePattern: *pattern, IgnorePattern: *pattern,
AvailableOptions: &Options, AvailableOptions: &Options,
}, },
} }
} }
var Parser = createOpenSSHConfigParser() var Parser = createOpenSSHConfigParser()

View File

@ -39,9 +39,9 @@ func getRootCompletions() []protocol.CompletionItem {
format := protocol.InsertTextFormatSnippet format := protocol.InsertTextFormatSnippet
completions[index] = protocol.CompletionItem{ completions[index] = protocol.CompletionItem{
Label: label, Label: label,
Documentation: common.GetDocumentation(&option), Documentation: common.GetDocumentation(&option),
InsertText: &insertText, InsertText: &insertText,
InsertTextFormat: &format, InsertTextFormat: &format,
} }
} }
@ -59,7 +59,7 @@ func getCompletionsFromValue(value common.Value) []protocol.CompletionItem {
textFormat := protocol.InsertTextFormatPlainText textFormat := protocol.InsertTextFormatPlainText
completions[index] = protocol.CompletionItem{ completions[index] = protocol.CompletionItem{
Label: value, Label: value,
InsertTextFormat: &textFormat, InsertTextFormat: &textFormat,
} }
} }
@ -100,4 +100,3 @@ func getOptionCompletions(optionName string) []protocol.CompletionItem {
return completions return completions
} }

View File

@ -18,6 +18,5 @@ func TextDocumentDidChange(context *glsp.Context, params *protocol.DidChangeText
ClearDiagnostics(context, params.TextDocument.URI) ClearDiagnostics(context, params.TextDocument.URI)
} }
return nil return nil
} }

View File

@ -22,5 +22,3 @@ func TextDocumentDidOpen(context *glsp.Context, params *protocol.DidOpenTextDocu
return nil return nil
} }

13
main.go
View File

@ -25,12 +25,12 @@ func main() {
commonlog.Configure(1, nil) commonlog.Configure(1, nil)
handler = protocol.Handler{ handler = protocol.Handler{
Initialize: initialize, Initialize: initialize,
Initialized: initialized, Initialized: initialized,
Shutdown: shutdown, Shutdown: shutdown,
SetTrace: setTrace, SetTrace: setTrace,
TextDocumentDidOpen: openssh.TextDocumentDidOpen, TextDocumentDidOpen: openssh.TextDocumentDidOpen,
TextDocumentDidChange: openssh.TextDocumentDidChange, TextDocumentDidChange: openssh.TextDocumentDidChange,
TextDocumentCompletion: openssh.TextDocumentCompletion, TextDocumentCompletion: openssh.TextDocumentCompletion,
} }
@ -65,4 +65,3 @@ func setTrace(context *glsp.Context, params *protocol.SetTraceParams) error {
protocol.SetTraceValue(params.Value) protocol.SetTraceValue(params.Value)
return nil return nil
} }