Better code

This commit is contained in:
Martino Ferrari
2026-01-27 00:04:36 +01:00
parent 73cfc43f4b
commit aedc715ef3
2 changed files with 19 additions and 32 deletions

View File

@@ -23,7 +23,7 @@ import (
type CompletionParams struct { type CompletionParams struct {
TextDocument TextDocumentIdentifier `json:"textDocument"` TextDocument TextDocumentIdentifier `json:"textDocument"`
Position Position `json:"position"` Position Position `json:"position"`
Context CompletionContext `json:"context,omitempty"` Context CompletionContext `json:"context"`
} }
type CompletionContext struct { type CompletionContext struct {
@@ -395,7 +395,7 @@ func HandleFormatting(params DocumentFormattingParams) []TextEdit {
} }
} }
func runValidation(uri string) { func runValidation(_ string) {
v := validator.NewValidator(Tree, ProjectRoot) v := validator.NewValidator(Tree, ProjectRoot)
v.ValidateProject() v.ValidateProject()
v.CheckUnused() v.CheckUnused()
@@ -580,10 +580,7 @@ func HandleCompletion(params CompletionParams) *CompletionList {
} }
lineStr := lines[params.Position.Line] lineStr := lines[params.Position.Line]
col := params.Position.Character col := min(params.Position.Character, len(lineStr))
if col > len(lineStr) {
col = len(lineStr)
}
prefix := lineStr[:col] prefix := lineStr[:col]
@@ -628,7 +625,7 @@ func HandleCompletion(params CompletionParams) *CompletionList {
return nil return nil
} }
func suggestGAMSignals(container *index.ProjectNode, direction string) *CompletionList { func suggestGAMSignals(_ *index.ProjectNode, direction string) *CompletionList {
var items []CompletionItem var items []CompletionItem
processNode := func(node *index.ProjectNode) { processNode := func(node *index.ProjectNode) {
@@ -641,7 +638,7 @@ func suggestGAMSignals(container *index.ProjectNode, direction string) *Completi
return return
} }
dir := "INOUT" dir := "NIL"
if GlobalSchema != nil { if GlobalSchema != nil {
classPath := cue.ParsePath(fmt.Sprintf("#Classes.%s.direction", cls)) classPath := cue.ParsePath(fmt.Sprintf("#Classes.%s.direction", cls))
val := GlobalSchema.Value.LookupPath(classPath) val := GlobalSchema.Value.LookupPath(classPath)
@@ -652,16 +649,14 @@ func suggestGAMSignals(container *index.ProjectNode, direction string) *Completi
} }
} }
} }
compatible := false compatible := false
if direction == "Input" { switch direction {
if dir == "IN" || dir == "INOUT" { case "Input":
compatible = true compatible = dir == "IN" || dir == "INOUT"
} case "Output":
} else if direction == "Output" { compatible = dir == "OUT" || dir == "INOUT"
if dir == "OUT" || dir == "INOUT" { default:
compatible = true compatible = false
}
} }
if !compatible { if !compatible {
@@ -677,8 +672,8 @@ func suggestGAMSignals(container *index.ProjectNode, direction string) *Completi
dsName := node.Name dsName := node.Name
sigName := sig.Name sigName := sig.Name
label := fmt.Sprintf("%s:%s", sigName, dsName) label := fmt.Sprintf("%s:%s", dsName, sigName)
insertText := fmt.Sprintf("%s = { DataSource = %s }", sigName, dsName) insertText := fmt.Sprintf("%s = {\n DataSource = %s \n}", sigName, dsName)
items = append(items, CompletionItem{ items = append(items, CompletionItem{
Label: label, Label: label,
@@ -902,14 +897,11 @@ func suggestObjects(root *index.ProjectNode, filter string) *CompletionList {
var walk func(*index.ProjectNode) var walk func(*index.ProjectNode)
walk = func(node *index.ProjectNode) { walk = func(node *index.ProjectNode) {
match := false match := false
if filter == "GAM" { switch filter {
if isGAM(node) { case "GAM":
match = true match = isGAM(node)
} case "DataSource":
} else if filter == "DataSource" { match = isDataSource(node)
if isDataSource(node) {
match = true
}
} }
if match { if match {

View File

@@ -542,11 +542,6 @@ func isValidType(t string) bool {
return false return false
} }
func (v *Validator) checkType(val parser.Value, expectedType string) bool {
// Legacy function, replaced by CUE.
return true
}
func (v *Validator) getFileForField(f *parser.Field, node *index.ProjectNode) string { func (v *Validator) getFileForField(f *parser.Field, node *index.ProjectNode) string {
for _, frag := range node.Fragments { for _, frag := range node.Fragments {
for _, def := range frag.Definitions { for _, def := range frag.Definitions {