Skip to content

Commit

Permalink
Renaming packeges and other variables, coderefactor
Browse files Browse the repository at this point in the history
  • Loading branch information
lovestaco authored and shrsv committed Sep 9, 2023
1 parent 18b9ecb commit 9f2d115
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 90 deletions.
20 changes: 10 additions & 10 deletions l2lsp/identify_method.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
package l2lsp

import (
"github.com/HexmosTech/lama2/l2lsp/lsp_req"
"github.com/HexmosTech/lama2/l2lsp/lsp_res"
"github.com/HexmosTech/lama2/l2lsp/methods"
"github.com/HexmosTech/lama2/l2lsp/request"
"github.com/HexmosTech/lama2/l2lsp/response"
)

var isShutdownRequested bool

func HandleMethod(request lsp_req.JSONRPCRequest) lsp_res.JSONRPCResponse {
if isShutdownRequested && request.Method != "exit" {
return lsp_res.InvalidReqAfterShutdown(request)
func HandleMethod(req request.JSONRPCRequest) response.JSONRPCResponse {
if isShutdownRequested && req.Method != "exit" {
return response.InvalidReqAfterShutdown(req)
}

switch request.Method {
switch req.Method {
case "initialize":
return methods.Initialize(request)
return methods.Initialize(req)

case "shutdown":
return methods.Shutdown(request, isShutdownRequested)
return methods.Shutdown(req, isShutdownRequested)

case "exit":
return methods.Exit(isShutdownRequested)

case "suggest/environmentVariables":
return methods.SuggestEnvironmentVariables(request)
return methods.SuggestEnvironmentVariables(req)

default:
return lsp_res.DefaultResp(request)
return response.DefaultResp(req)
}
}
11 changes: 0 additions & 11 deletions l2lsp/lsp_res/suggestenv_response.go

This file was deleted.

21 changes: 12 additions & 9 deletions l2lsp/methods/lsp_lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ package methods
import (
"os"

"github.com/HexmosTech/lama2/l2lsp/lsp_req"
"github.com/HexmosTech/lama2/l2lsp/lsp_res"
"github.com/HexmosTech/lama2/l2lsp/request"
"github.com/HexmosTech/lama2/l2lsp/response"
"github.com/rs/zerolog/log"
)

func Initialize(request lsp_req.JSONRPCRequest) lsp_res.JSONRPCResponse {
func Initialize(req request.JSONRPCRequest) response.JSONRPCResponse {
/*
{
"jsonrpc": "2.0",
Expand Down Expand Up @@ -43,18 +43,20 @@ func Initialize(request lsp_req.JSONRPCRequest) lsp_res.JSONRPCResponse {
*/
log.Info().Msg("L2 LSP initialized")

serverCapabilities := lsp_res.ServerCapabilities{
serverCapabilities := response.ServerCapabilities{
TextDocumentSync: 0,
SuggestL2Envs: true,
HoverProvider: false,
}
res := map[string]interface{}{
"capabilities": serverCapabilities,
}
return lsp_res.CreateSuccessResponse(request.ID, res)
return response.CreateSuccessResponse(req.ID, res)
}

func Shutdown(request lsp_req.JSONRPCRequest, isShutdownRequested bool) lsp_res.JSONRPCResponse {
// Shutdown is a way to gracefully terminate the server.
// The server can perform cleanup operations, like closing open files, releasing resources, or saving state.
func Shutdown(req request.JSONRPCRequest, isShutdownRequested bool) response.JSONRPCResponse {
/*
{
"jsonrpc": "2.0",
Expand All @@ -66,10 +68,11 @@ func Shutdown(request lsp_req.JSONRPCRequest, isShutdownRequested bool) lsp_res.
log.Info().Msg("L2 LSP shutdown requested")

isShutdownRequested = true
return lsp_res.CreateSuccessResponse(request.ID, nil)
return response.CreateSuccessResponse(req.ID, nil)
}

func Exit(isShutdownRequested bool) lsp_res.JSONRPCResponse {
// Exit terminates the server process, with the exit code depending on whether a shutdown was requested.
func Exit(isShutdownRequested bool) response.JSONRPCResponse {
/*
{
"jsonrpc": "2.0",
Expand All @@ -84,5 +87,5 @@ func Exit(isShutdownRequested bool) lsp_res.JSONRPCResponse {
exitCode = 0
}
os.Exit(exitCode)
return lsp_res.JSONRPCResponse{}
return response.JSONRPCResponse{}
}
38 changes: 19 additions & 19 deletions l2lsp/methods/lsp_suggestEnv.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,48 @@ import (
"strings"

l2envpackege "github.com/HexmosTech/lama2/l2env"
"github.com/HexmosTech/lama2/l2lsp/lsp_req"
"github.com/HexmosTech/lama2/l2lsp/lsp_res"
"github.com/HexmosTech/lama2/l2lsp/request"
"github.com/HexmosTech/lama2/l2lsp/response"
"github.com/rs/zerolog/log"
)

func getSearchQueryString(request lsp_req.JSONRPCRequest) string {
if request.Params.SearchQuery != nil {
return *request.Params.SearchQuery
func getSearchQueryString(req request.JSONRPCRequest) string {
if req.Params.SearchQuery != nil {
return *req.Params.SearchQuery
}
return ""
}

func getRequestURI(request lsp_req.JSONRPCRequest) (string, int, error) {
if request.Params.TextDocument.Uri == nil {
return "", lsp_res.ErrInvalidURI, errors.New("URI cannot be empty. Ex: 'file:///path/to/workspace/myapi.l2'")
func getRequestURI(req request.JSONRPCRequest) (string, int, error) {
if req.Params.TextDocument.Uri == nil {
return "", response.ErrInvalidURI, errors.New("URI cannot be empty. Ex: 'file:///path/to/workspace/myapi.l2'")
}
uri := *request.Params.TextDocument.Uri
uri := *req.Params.TextDocument.Uri

// Handle local files
if strings.HasPrefix(uri, "file://") {
return uri[len("file://"):], 0, nil

// Handle remote files (example for SSH)
} else if strings.HasPrefix(uri, "ssh://") {
return "", lsp_res.ErrUnsupportedFeature, errors.New("SSH is not supported as of now. To contribute visit here: https://github.com/HexmosTech/Lama2")
return "", response.ErrUnsupportedFeature, errors.New("SSH is not supported as of now. To contribute visit here: https://github.com/HexmosTech/Lama2")

// Handle WSL files
} else if strings.HasPrefix(uri, "wsl://") {
return "", lsp_res.ErrUnsupportedFeature, errors.New("WSL is not supported as of now. To contribute visit here: https://github.com/HexmosTech/Lama2")
return "", response.ErrUnsupportedFeature, errors.New("WSL is not supported as of now. To contribute visit here: https://github.com/HexmosTech/Lama2")

// Handle Windows files
} else if strings.Contains(uri, "\\") {
return "", lsp_res.ErrUnsupportedFeature, errors.New("Windows is not supported as of now. To contribute visit here: https://github.com/HexmosTech/Lama2")
return "", response.ErrUnsupportedFeature, errors.New("Windows is not supported as of now. To contribute visit here: https://github.com/HexmosTech/Lama2")

} else {
// Log the unexpected URI scheme
log.Warn().Str("URI", uri).Msg("Encountered unexpected URI scheme.")
return "", lsp_res.ErrUnexpectedURIScheme, errors.New("encountered unexpected URI scheme. Ex: 'file:///path/to/workspace/myapi.l2'")
return "", response.ErrUnexpectedURIScheme, errors.New("encountered unexpected URI scheme. Ex: 'file:///path/to/workspace/myapi.l2'")
}
}

func SuggestEnvironmentVariables(request lsp_req.JSONRPCRequest) lsp_res.JSONRPCResponse {
func SuggestEnvironmentVariables(req request.JSONRPCRequest) response.JSONRPCResponse {
/*
{
"jsonrpc": "2.0",
Expand All @@ -68,14 +68,14 @@ func SuggestEnvironmentVariables(request lsp_req.JSONRPCRequest) lsp_res.JSONRPC
*/

log.Info().Msg("L2 LSP environment variables suggestion requested")
log.Info().Str("Method", request.Method).Interface("Params", request.Params)
log.Info().Str("Method", req.Method).Interface("Params", req.Params)

searchQuery := getSearchQueryString(request)
uri, errorCode, err := getRequestURI(request)
searchQuery := getSearchQueryString(req)
uri, errorCode, err := getRequestURI(req)
if err != nil {
return lsp_res.ErrorResp(request, errorCode, err.Error())
return response.ErrorResp(req, errorCode, err.Error())
}
parentFolder := filepath.Dir(uri)
res := l2envpackege.ProcessEnvironmentVariables(searchQuery, parentFolder)
return lsp_res.CreateEnvironmentVariablesResp(request, res)
return response.CreateEnvironmentVariablesResp(req, res)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package lsp_req
package request

type Params struct {
ProcessID *int64 `json:"processId"`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package lsp_res
package response

type JSONRPCError struct {
Code int `json:"code"`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package lsp_res
package response

import "github.com/HexmosTech/lama2/l2lsp/lsp_req"
import "github.com/HexmosTech/lama2/l2lsp/request"

type InitializeResult struct {
Capabilities ServerCapabilities `json:"capabilities"`
Expand All @@ -18,9 +18,9 @@ type CompletionOptions struct {
ResolveProvider bool `json:"resolveProvider,omitempty"`
}

func InvalidReqAfterShutdown(request lsp_req.JSONRPCRequest) JSONRPCResponse {
func InvalidReqAfterShutdown(req request.JSONRPCRequest) JSONRPCResponse {
return JSONRPCResponse{
ID: request.ID,
ID: req.ID,
JSONRPC: "2.0",
Error: &JSONRPCError{
Code: ErrInvalidAfterShutdown,
Expand Down
14 changes: 7 additions & 7 deletions l2lsp/lsp_res/respond.go → l2lsp/response/respond.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package lsp_res
package response

import "github.com/HexmosTech/lama2/l2lsp/lsp_req"
import "github.com/HexmosTech/lama2/l2lsp/request"

// Utility function to create a general success response
func CreateSuccessResponse(requestID int, result interface{}) JSONRPCResponse {
Expand All @@ -11,21 +11,21 @@ func CreateSuccessResponse(requestID int, result interface{}) JSONRPCResponse {
}
}

func DefaultResp(request lsp_req.JSONRPCRequest) JSONRPCResponse {
func DefaultResp(req request.JSONRPCRequest) JSONRPCResponse {
return JSONRPCResponse{
ID: request.ID,
ID: req.ID,
JSONRPC: "2.0",
Error: &JSONRPCError{
Code: ErrMethodNotFound,
Message: "Method not supported by the server. Method: " + request.Method,
Message: "Method not supported by the server. Method: " + req.Method,
},
}
}

// Utility function to create a general error response
func ErrorResp(request lsp_req.JSONRPCRequest, errorCode int, errorMsg string) JSONRPCResponse {
func ErrorResp(req request.JSONRPCRequest, errorCode int, errorMsg string) JSONRPCResponse {
return JSONRPCResponse{
ID: request.ID,
ID: req.ID,
JSONRPC: "2.0",
Error: &JSONRPCError{
Code: errorCode,
Expand Down
11 changes: 11 additions & 0 deletions l2lsp/response/suggestenv_response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package response

import "github.com/HexmosTech/lama2/l2lsp/request"

func CreateEnvironmentVariablesResp(req request.JSONRPCRequest, envs interface{}) JSONRPCResponse {
return JSONRPCResponse{
ID: req.ID,
JSONRPC: "2.0",
Result: envs,
}
}
4 changes: 2 additions & 2 deletions l2lsp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"encoding/json"
"os"

"github.com/HexmosTech/lama2/l2lsp/lsp_req"
"github.com/HexmosTech/lama2/l2lsp/request"
outputmanager "github.com/HexmosTech/lama2/outputManager"
"github.com/rs/zerolog/log"
)
Expand All @@ -29,7 +29,7 @@ func StartLspServer() {
func handleInput(input string, writer *bufio.Writer) {
log.Info().Msgf("Received input: %s", input)

var rpcRequest lsp_req.JSONRPCRequest
var rpcRequest request.JSONRPCRequest
if err := json.Unmarshal([]byte(input), &rpcRequest); err != nil {
log.Error().Err(err).Msg("Error decoding JSON-RPC request")
return
Expand Down
32 changes: 16 additions & 16 deletions tests/env_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ func TestForEmptySearchQuery(t *testing.T) {

searchQuery := ""

request := fmt.Sprintf(`{"jsonrpc":"2.0","id":1,"method":"suggest/environmentVariables","params":{"textDocument":{"uri":"file://%s"},"position":{"line":1,"character":45},"searchQuery":"%s"}}`, absPath, searchQuery)
_, err = stdin.Write([]byte(request + "\n"))
req := fmt.Sprintf(`{"jsonrpc":"2.0","id":1,"method":"suggest/environmentVariables","params":{"textDocument":{"uri":"file://%s"},"position":{"line":1,"character":45},"searchQuery":"%s"}}`, absPath, searchQuery)
_, err = stdin.Write([]byte(req + "\n"))
if err != nil {
t.Fatalf("Failed to write to LSP server stdin: %v", err)
}
Expand Down Expand Up @@ -76,8 +76,8 @@ func TestL2SuggestEnvForNoL2Config(t *testing.T) {
}
searchQuery := ""

request := fmt.Sprintf(`{"jsonrpc":"2.0","id":1,"method":"suggest/environmentVariables","params":{"textDocument":{"uri":"file://%s"},"position":{"line":1,"character":45},"searchQuery":"%s"}}`, absPath, searchQuery)
_, err = stdin.Write([]byte(request + "\n"))
req := fmt.Sprintf(`{"jsonrpc":"2.0","id":1,"method":"suggest/environmentVariables","params":{"textDocument":{"uri":"file://%s"},"position":{"line":1,"character":45},"searchQuery":"%s"}}`, absPath, searchQuery)
_, err = stdin.Write([]byte(req + "\n"))
if err != nil {
t.Fatalf("Failed to write to LSP server stdin: %v", err)
}
Expand Down Expand Up @@ -125,42 +125,42 @@ func TestL2RelevantEnvForAString(t *testing.T) {
}
searchQuery := "A"
jreq := `{"jsonrpc":"2.0","id":1,"method":"suggest/environmentVariables","params":{"textDocument":{"uri":"file://%s"},"position":{"line":1,"character":45},"searchQuery":"%s"}}`
request := fmt.Sprintf(jreq, absPath, searchQuery)
_, err = stdin.Write([]byte(request + "\n"))
req := fmt.Sprintf(jreq, absPath, searchQuery)
_, err = stdin.Write([]byte(req + "\n"))
if err != nil {
t.Fatalf("Failed to write to LSP server stdin: %v", err)
t.Fatalf("JSON-RPC request: %v", request)
t.Fatalf("JSON-RPC request: %v", req)
}

buffer := make([]byte, 2048)
n, err := stdout.Read(buffer)
if err != nil {
t.Fatalf("Failed to read from LSP server stdout: %v", err)
t.Fatalf("JSON-RPC request: %v", request)
t.Fatalf("JSON-RPC request: %v", req)
}

var rawResponse RawJSONRPCResponse
err = json.Unmarshal(buffer[:n], &rawResponse)
if err != nil {
t.Fatalf("Failed to unmarshal LSP raw response: %v", err)
t.Fatalf("JSON-RPC request: %v", request)
t.Fatalf("JSON-RPC request: %v", req)
}

if rawResponse.ID != 1 {
t.Fatalf("Expected response ID to be 1, got %v", rawResponse.ID)
t.Fatalf("JSON-RPC request: %v", request)
t.Fatalf("JSON-RPC request: %v", req)
}
if rawResponse.JSONRPC != "2.0" {
t.Fatalf("Expected jsonrpc version to be 2.0, got %v", rawResponse.JSONRPC)
t.Fatalf("JSON-RPC request: %v", request)
t.Fatalf("JSON-RPC request: %v", req)
}

// Parse the Result into a map
var envMap map[string]testutils.EnvData
err = json.Unmarshal(rawResponse.Result, &envMap)
if err != nil {
t.Fatalf("Failed to unmarshal LSP response result: %v", err)
t.Fatalf("JSON-RPC request: %v", request)
t.Fatalf("JSON-RPC request: %v", req)
}

// Use the helper functions to check the AHOST and BHOST values
Expand All @@ -184,8 +184,8 @@ func TestL2RelevantEnvForBString(t *testing.T) {
}
searchQuery := "B"

request := fmt.Sprintf(`{"jsonrpc":"2.0","id":1,"method":"suggest/environmentVariables","params":{"textDocument":{"uri":"file://%s"},"position":{"line":1,"character":45},"searchQuery":"%s"}}`, absPath, searchQuery)
_, err = stdin.Write([]byte(request + "\n"))
req := fmt.Sprintf(`{"jsonrpc":"2.0","id":1,"method":"suggest/environmentVariables","params":{"textDocument":{"uri":"file://%s"},"position":{"line":1,"character":45},"searchQuery":"%s"}}`, absPath, searchQuery)
_, err = stdin.Write([]byte(req + "\n"))
if err != nil {
t.Fatalf("Failed to write to LSP server stdin: %v", err)
}
Expand Down Expand Up @@ -240,8 +240,8 @@ func TestL2EnvWithoutL2config(t *testing.T) {
}
searchQuery := ""

request := fmt.Sprintf(`{"jsonrpc":"2.0","id":1,"method":"suggest/environmentVariables","params":{"textDocument":{"uri":"file://%s"},"position":{"line":1,"character":45},"searchQuery":"%s"}}`, absPath, searchQuery)
_, err = stdin.Write([]byte(request + "\n"))
req := fmt.Sprintf(`{"jsonrpc":"2.0","id":1,"method":"suggest/environmentVariables","params":{"textDocument":{"uri":"file://%s"},"position":{"line":1,"character":45},"searchQuery":"%s"}}`, absPath, searchQuery)
_, err = stdin.Write([]byte(req + "\n"))
if err != nil {
t.Fatalf("Failed to write to LSP server stdin: %v", err)
}
Expand Down
Loading

0 comments on commit 9f2d115

Please sign in to comment.