github.com/getgauge/gauge@v1.6.9/api/lang/references.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 "encoding/json" 11 "fmt" 12 13 "github.com/getgauge/common" 14 "github.com/getgauge/gauge/util" 15 "github.com/sourcegraph/go-langserver/pkg/lsp" 16 "github.com/sourcegraph/jsonrpc2" 17 ) 18 19 func stepReferences(req *jsonrpc2.Request) (interface{}, error) { 20 var params []string 21 if err := json.Unmarshal(*req.Params, ¶ms); err != nil { 22 return nil, fmt.Errorf("failed to parse request %v", err) 23 } 24 return getLocationFor(params[0]) 25 } 26 27 func stepValueAt(req *jsonrpc2.Request) (interface{}, error) { 28 var params lsp.TextDocumentPositionParams 29 if err := json.Unmarshal(*req.Params, ¶ms); err != nil { 30 return nil, fmt.Errorf("failed to parse request %v", err) 31 } 32 stepPositionsResponse, err := getStepPositionResponse(params.TextDocument.URI) 33 if err != nil { 34 return nil, err 35 } 36 for _, step := range stepPositionsResponse.StepPositions { 37 if (int(step.GetSpan().GetStart()) <= params.Position.Line+1) && (int(step.GetSpan().GetEnd()) >= params.Position.Line+1) { 38 return step.GetStepValue(), nil 39 } 40 } 41 return nil, nil 42 } 43 44 func getLocationFor(stepValue string) (interface{}, error) { 45 allSteps := provider.AllSteps(false) 46 var locations []lsp.Location 47 diskFileCache := &files{cache: make(map[lsp.DocumentURI][]string)} 48 for _, step := range allSteps { 49 if stepValue == step.Value { 50 uri := util.ConvertPathToURI(step.FileName) 51 var endPos int 52 lineNo := step.LineNo - 1 53 if isOpen(uri) { 54 endPos = len(getLine(uri, lineNo)) 55 } else { 56 if !diskFileCache.exists(uri) { 57 contents, err := common.ReadFileContents(step.FileName) 58 if err != nil { 59 return nil, err 60 } 61 diskFileCache.add(uri, contents) 62 } 63 endPos = len(diskFileCache.line(uri, lineNo)) 64 } 65 locations = append(locations, lsp.Location{ 66 URI: uri, 67 Range: lsp.Range{ 68 Start: lsp.Position{Line: lineNo, Character: 0}, 69 End: lsp.Position{Line: lineNo, Character: endPos}, 70 }, 71 }) 72 } 73 } 74 return locations, nil 75 }