github.com/getgauge/gauge@v1.6.9/api/lang/stubImplementation.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 "path/filepath" 13 "strings" 14 15 "github.com/getgauge/common" 16 gm "github.com/getgauge/gauge-proto/go/gauge_messages" 17 "github.com/getgauge/gauge/util" 18 "github.com/sourcegraph/go-langserver/pkg/lsp" 19 "github.com/sourcegraph/jsonrpc2" 20 ) 21 22 type stubImpl struct { 23 ImplementationFilePath string `json:"implementationFilePath"` 24 Codes []string `json:"codes"` 25 } 26 27 type concpetInfo struct { 28 ConceptName string `json:"conceptName"` 29 ConceptFile string `json:"conceptFile"` 30 Dir string `json:"dir"` 31 } 32 33 type editInfo struct { 34 fileName string 35 endLineNo int 36 newText string 37 } 38 39 func getImplFiles(req *jsonrpc2.Request) (interface{}, error) { 40 fileList := []string{} 41 var info = struct { 42 Concept bool `json:"concept"` 43 }{} 44 45 if req.Params != nil { 46 if err := json.Unmarshal(*req.Params, &info); err != nil { 47 return nil, fmt.Errorf("failed to parse request %s", err.Error()) 48 } 49 if info.Concept { 50 return append(fileList, util.GetConceptFiles()...), nil 51 } 52 } 53 response, err := getImplementationFileList() 54 if err != nil { 55 return nil, err 56 } 57 return append(fileList, response.GetImplementationFilePaths()...), nil 58 } 59 60 func putStubImpl(req *jsonrpc2.Request) (interface{}, error) { 61 var stubImplParams stubImpl 62 if err := json.Unmarshal(*req.Params, &stubImplParams); err != nil { 63 return nil, fmt.Errorf("failed to parse request %s", err.Error()) 64 } 65 fileDiff, err := putStubImplementation(stubImplParams.ImplementationFilePath, stubImplParams.Codes) 66 if err != nil { 67 return nil, err 68 } 69 70 return getWorkspaceEditForStubImpl(fileDiff), nil 71 } 72 73 func getWorkspaceEditForStubImpl(fileDiff *gm.FileDiff) lsp.WorkspaceEdit { 74 var result lsp.WorkspaceEdit 75 result.Changes = make(map[string][]lsp.TextEdit) 76 uri := util.ConvertPathToURI(fileDiff.FilePath) 77 78 var textDiffs = fileDiff.TextDiffs 79 for _, textDiff := range textDiffs { 80 span := textDiff.Span 81 textEdit := createTextEdit(textDiff.Content, int(span.Start), int(span.StartChar), int(span.End), int(span.EndChar)) 82 result.Changes[string(uri)] = append(result.Changes[string(uri)], textEdit) 83 } 84 return result 85 } 86 87 func generateConcept(req *jsonrpc2.Request) (interface{}, error) { 88 var params concpetInfo 89 if err := json.Unmarshal(*req.Params, ¶ms); err != nil { 90 return nil, fmt.Errorf("Failed to parse request %s", err.Error()) 91 } 92 conceptFile := string(params.ConceptFile) 93 edit := editInfo{ 94 fileName: conceptFile, 95 endLineNo: 0, 96 newText: params.ConceptName, 97 } 98 content, err := common.ReadFileContents(conceptFile) 99 if err != nil { 100 edit.fileName = getFileName(params.Dir, 1) 101 } else if content != "" { 102 content = strings.Join(util.GetLinesFromText(content), "\n") 103 edit.newText = fmt.Sprintf("%s\n\n%s", strings.TrimSpace(content), params.ConceptName) 104 edit.endLineNo = len(strings.Split(content, "\n")) 105 } 106 return createWorkSpaceEdits(edit), nil 107 } 108 109 func createWorkSpaceEdits(edit editInfo) lsp.WorkspaceEdit { 110 var result = lsp.WorkspaceEdit{Changes: map[string][]lsp.TextEdit{}} 111 textEdiit := createTextEdit(edit.newText, 0, 0, edit.endLineNo, 0) 112 uri := util.ConvertPathToURI(edit.fileName) 113 result.Changes[string(uri)] = []lsp.TextEdit{textEdiit} 114 return result 115 } 116 117 func createTextEdit(text string, start, startChar, end, endChar int) lsp.TextEdit { 118 return lsp.TextEdit{ 119 Range: lsp.Range{ 120 Start: lsp.Position{ 121 Line: start, 122 Character: startChar, 123 }, 124 End: lsp.Position{ 125 Line: end, 126 Character: endChar, 127 }, 128 }, 129 NewText: text, 130 } 131 } 132 133 func getFileName(dir string, count int) string { 134 var fileName = filepath.Join(dir, fmt.Sprintf("concept%d.cpt", count)) 135 if !common.FileExists(fileName) { 136 return fileName 137 } 138 return getFileName(dir, count+1) 139 }