github.com/getgauge/gauge@v1.6.9/api/lang/completionParams.go (about) 1 /*---------------------------------------------------------------- 2 * Copyright (c) ThoughtWorks, Inc. 3 * Licensed under the Apache License, Version 2.0 4 * See LICENSE in the project root for license information. 5 *----------------------------------------------------------------*/ 6 7 package lang 8 9 import ( 10 "strings" 11 12 "github.com/getgauge/gauge/gauge" 13 "github.com/getgauge/gauge/util" 14 "github.com/sourcegraph/go-langserver/pkg/lsp" 15 ) 16 17 func paramCompletion(line, pLine string, params lsp.TextDocumentPositionParams) (interface{}, error) { 18 list := completionList{IsIncomplete: false, Items: []completionItem{}} 19 argType, suffix, editRange := getParamArgTypeAndEditRange(line, pLine, params.Position) 20 file := util.ConvertURItoFilePath(params.TextDocument.URI) 21 for _, param := range provider.Params(file, argType) { 22 if !shouldAddParam(param.ArgType) { 23 continue 24 } 25 argValue := param.ArgValue() 26 list.Items = append(list.Items, completionItem{ 27 CompletionItem: lsp.CompletionItem{ 28 Label: argValue, 29 FilterText: argValue + suffix, 30 Detail: string(argType), 31 Kind: lsp.CIKVariable, 32 TextEdit: &lsp.TextEdit{Range: editRange, NewText: argValue + suffix}, 33 }, 34 InsertTextFormat: text, 35 }) 36 } 37 return list, nil 38 } 39 40 func shouldAddParam(argType gauge.ArgType) bool { 41 return argType != gauge.TableArg 42 } 43 44 func getParamArgTypeAndEditRange(line, pLine string, position lsp.Position) (gauge.ArgType, string, lsp.Range) { 45 quoteIndex := strings.LastIndex(pLine, "\"") 46 bracIndex := strings.LastIndex(pLine, "<") 47 if quoteIndex > bracIndex { 48 return gauge.Static, "\"", getEditRange(quoteIndex, position, pLine, line, "\"") 49 } else { 50 return gauge.Dynamic, ">", getEditRange(bracIndex, position, pLine, line, ">") 51 } 52 }