github.com/getgauge/gauge@v1.6.9/api/lang/customResponses.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/gauge/gauge"
    14  	"github.com/getgauge/gauge/parser"
    15  	"github.com/getgauge/gauge/util"
    16  	"github.com/sourcegraph/go-langserver/pkg/lsp"
    17  	"github.com/sourcegraph/jsonrpc2"
    18  )
    19  
    20  type ScenarioInfo struct {
    21  	Heading             string `json:"heading"`
    22  	LineNo              int    `json:"lineNo"`
    23  	ExecutionIdentifier string `json:"executionIdentifier"`
    24  }
    25  
    26  type specInfo struct {
    27  	Heading             string `json:"heading"`
    28  	ExecutionIdentifier string `json:"executionIdentifier"`
    29  }
    30  
    31  func specs() (interface{}, error) {
    32  	specDetails := provider.GetAvailableSpecDetails([]string{})
    33  	specs := make([]specInfo, 0)
    34  	for _, d := range specDetails {
    35  		specs = append(specs, specInfo{Heading: d.Spec.Heading.Value, ExecutionIdentifier: d.Spec.FileName})
    36  	}
    37  	return specs, nil
    38  }
    39  
    40  func scenarios(req *jsonrpc2.Request) (interface{}, error) {
    41  	var params lsp.TextDocumentPositionParams
    42  	var err error
    43  	if err = json.Unmarshal(*req.Params, &params); err != nil {
    44  		return nil, fmt.Errorf("failed to parse request %s", err)
    45  	}
    46  	file := util.ConvertURItoFilePath(params.TextDocument.URI)
    47  	content := ""
    48  	if !isOpen(params.TextDocument.URI) {
    49  		specDetails := provider.GetAvailableSpecDetails([]string{file})
    50  		return getScenarioAt(specDetails[0].Spec.Scenarios, file, params.Position.Line), nil
    51  	}
    52  	content = getContent(params.TextDocument.URI)
    53  	spec, parseResult, err := new(parser.SpecParser).Parse(content, gauge.NewConceptDictionary(), string(file))
    54  	if err != nil {
    55  		return nil, err
    56  	}
    57  	if !parseResult.Ok {
    58  		return nil, fmt.Errorf("parsing failed")
    59  	}
    60  	return getScenarioAt(spec.Scenarios, file, params.Position.Line), nil
    61  }
    62  
    63  func getScenarioAt(scenarios []*gauge.Scenario, file string, line int) interface{} {
    64  	var ifs []ScenarioInfo
    65  	for _, sce := range scenarios {
    66  		info := getScenarioInfo(sce, file)
    67  		if sce.InSpan(line + 1) {
    68  			return info
    69  		}
    70  		ifs = append(ifs, info)
    71  	}
    72  	return ifs
    73  }
    74  func getScenarioInfo(sce *gauge.Scenario, file string) ScenarioInfo {
    75  	return ScenarioInfo{
    76  		Heading:             sce.Heading.Value,
    77  		LineNo:              sce.Heading.LineNo,
    78  		ExecutionIdentifier: fmt.Sprintf("%s:%d", file, sce.Heading.LineNo),
    79  	}
    80  }