| 180 | } |
| 181 | |
| 182 | func Completion(ctx context.Context, params *protocol.CompletionParams, manager *document.Manager, hub *hub.Service, doc document.ComposeDocument) (*protocol.CompletionList, error) { |
| 183 | documentPath, err := doc.DocumentPath() |
| 184 | if err != nil { |
| 185 | return nil, fmt.Errorf("LSP client sent invalid URI: %v", params.TextDocument.URI) |
| 186 | } |
| 187 | |
| 188 | file := doc.File() |
| 189 | if file == nil || len(file.Docs) == 0 { |
| 190 | return nil, nil |
| 191 | } |
| 192 | if m, ok := file.Docs[0].Body.(*ast.MappingNode); ok && len(m.Values) == 0 { |
| 193 | return nil, nil |
| 194 | } |
| 195 | |
| 196 | lspLine := int(params.Position.Line) |
| 197 | topLevelNodeOffset := calculateTopLevelNodeOffset(file) |
| 198 | if topLevelNodeOffset != -1 && params.Position.Character == uint32(topLevelNodeOffset) { |
| 199 | return &protocol.CompletionList{Items: createTopLevelItems()}, nil |
| 200 | } |
| 201 | |
| 202 | lines := strings.Split(string(doc.Input()), "\n") |
| 203 | if len(lines) <= lspLine { |
| 204 | return nil, nil |
| 205 | } |
| 206 | currentLineTrimmed := strings.TrimSpace(lines[lspLine]) |
| 207 | if strings.HasPrefix(currentLineTrimmed, "#") { |
| 208 | return nil, nil |
| 209 | } |
| 210 | |
| 211 | character := int(params.Position.Character) + 1 |
| 212 | if len(lines[lspLine]) < character-1 { |
| 213 | return nil, nil |
| 214 | } |
| 215 | whitespaceLine := currentLineTrimmed == "" |
| 216 | line := int(lspLine) + 1 |
| 217 | path := constructCompletionNodePath(file, line) |
| 218 | prefixContent := prefix(lines[lspLine], character-1) |
| 219 | prefixLength := protocol.UInteger(len(prefixContent)) |
| 220 | if len(path) == 0 { |
| 221 | if topLevelNodeOffset != -1 && params.Position.Character != uint32(topLevelNodeOffset) { |
| 222 | return nil, nil |
| 223 | } |
| 224 | return &protocol.CompletionList{Items: createTopLevelItems()}, nil |
| 225 | } else if len(path) == 1 { |
| 226 | if path[0].Key.GetToken().Value == "include" { |
| 227 | schema := schemaProperties()["include"].Items.(*jsonschema.Schema) |
| 228 | items := createSchemaItems(params, schema.Ref.OneOf[1].Properties, lines, lspLine, whitespaceLine, prefixLength, file, manager, documentPath, path) |
| 229 | items = append(items, folderStructureCompletionItems(documentPath, path, removeQuote(prefixContent))...) |
| 230 | return processItems(items, whitespaceLine), nil |
| 231 | } |
| 232 | return nil, nil |
| 233 | } else if path[1].Key.GetToken().Position.Column >= character { |
| 234 | return nil, nil |
| 235 | } |
| 236 | |
| 237 | path, nodeProps, arrayAttributes := nodeProperties(path, line, character) |
| 238 | dependencies := dependencyCompletionItems(file, documentPath, path, params, prefixLength) |
| 239 | if len(dependencies) > 0 { |