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

View File

@ -5,12 +5,13 @@ import (
"strings"
)
type ParserError interface {}
type ParserError interface{}
type OptionAlreadyExistsError struct {
Option string
Option string
FoundOnLine uint32
}
func (e OptionAlreadyExistsError) Error() string {
return fmt.Sprintf("Option %s already exists", e.Option)
}
@ -18,6 +19,7 @@ func (e OptionAlreadyExistsError) Error() string {
type OptionUnknownError struct {
Option string
}
func (e OptionUnknownError) Error() string {
return fmt.Sprintf("Option '%s' does not exist", e.Option)
}
@ -25,20 +27,22 @@ func (e OptionUnknownError) Error() string {
type MalformedLineError struct {
Line string
}
func (e MalformedLineError) Error() string {
return fmt.Sprintf("Malformed line: %s", e.Line)
}
type LineNotFoundError struct {}
type LineNotFoundError struct{}
func (e LineNotFoundError) Error() string {
return "Line not found"
}
type ValueNotInEnumError struct {
availableValues []string
providedValue string
providedValue string
}
func (e ValueNotInEnumError) Error() string {
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 {
Name string
UID string
GID string
Name string
UID string
GID string
HomePath string
}
@ -36,9 +36,9 @@ func fetchPasswdInfo() ([]passwdInfo, error) {
}
info := passwdInfo{
Name: splitted[0],
UID: splitted[2],
GID: splitted[3],
Name: splitted[0],
UID: splitted[2],
GID: splitted[3],
HomePath: splitted[5],
}
@ -50,7 +50,6 @@ func fetchPasswdInfo() ([]passwdInfo, error) {
return infos, nil
}
// UserValue returns a Value that fetches user names from /etc/passwd
// if `separatorForMultiple` is not empty, it will return an ArrayValue
func UserValue(separatorForMultiple string) Value {
@ -73,11 +72,10 @@ func UserValue(separatorForMultiple string) Value {
} else {
return ArrayValue{
AllowDuplicates: false,
SubValue: enumValues,
Separator: separatorForMultiple,
SubValue: enumValues,
Separator: separatorForMultiple,
}
}
},
}
}

View File

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

View File

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

File diff suppressed because it is too large Load Diff

View File

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

View File

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

View File

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

View File

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

13
main.go
View File

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