github.com/getgauge/gauge@v1.6.9/api/lang/document.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  	"context"
    11  	"encoding/json"
    12  	"fmt"
    13  
    14  	gm "github.com/getgauge/gauge-proto/go/gauge_messages"
    15  	"github.com/getgauge/gauge/util"
    16  	"github.com/sourcegraph/go-langserver/pkg/lsp"
    17  	"github.com/sourcegraph/jsonrpc2"
    18  )
    19  
    20  func documentOpened(req *jsonrpc2.Request, ctx context.Context, conn jsonrpc2.JSONRPC2) error {
    21  	var params lsp.DidOpenTextDocumentParams
    22  	var err error
    23  	if err = json.Unmarshal(*req.Params, &params); err != nil {
    24  		return fmt.Errorf("failed to parse request %s", err.Error())
    25  	}
    26  	if util.IsGaugeFile(string(params.TextDocument.URI)) {
    27  		openFile(params)
    28  	} else if lRunner.runner != nil {
    29  		err = cacheFileOnRunner(params.TextDocument.URI, params.TextDocument.Text, false, gm.CacheFileRequest_OPENED)
    30  	}
    31  	go publishDiagnostics(ctx, conn)
    32  	return err
    33  }
    34  
    35  func documentChange(req *jsonrpc2.Request, ctx context.Context, conn jsonrpc2.JSONRPC2) error {
    36  	var params lsp.DidChangeTextDocumentParams
    37  	var err error
    38  	if err = json.Unmarshal(*req.Params, &params); err != nil {
    39  		return fmt.Errorf("failed to parse request %s", err.Error())
    40  	}
    41  	file := params.TextDocument.URI
    42  	if util.IsGaugeFile(string(file)) {
    43  		changeFile(params)
    44  	} else if lRunner.runner != nil {
    45  		err = cacheFileOnRunner(params.TextDocument.URI, params.ContentChanges[0].Text, false, gm.CacheFileRequest_CHANGED)
    46  	}
    47  	go publishDiagnostics(ctx, conn)
    48  	return err
    49  }
    50  
    51  func documentClosed(req *jsonrpc2.Request, ctx context.Context, conn jsonrpc2.JSONRPC2) error {
    52  	var params lsp.DidCloseTextDocumentParams
    53  	var err error
    54  	if err := json.Unmarshal(*req.Params, &params); err != nil {
    55  		return fmt.Errorf("failed to parse request. %s", err.Error())
    56  	}
    57  	if util.IsGaugeFile(string(params.TextDocument.URI)) {
    58  		closeFile(params)
    59  	} else if lRunner.runner != nil {
    60  		err = cacheFileOnRunner(params.TextDocument.URI, "", true, gm.CacheFileRequest_CLOSED)
    61  	}
    62  	go publishDiagnostics(ctx, conn)
    63  	return err
    64  }
    65  
    66  func documentChangeWatchedFiles(req *jsonrpc2.Request, ctx context.Context, conn jsonrpc2.JSONRPC2) error {
    67  	var params lsp.DidChangeWatchedFilesParams
    68  	if err := json.Unmarshal(*req.Params, &params); err != nil {
    69  		return fmt.Errorf("failed to parse request. %s", err.Error())
    70  	}
    71  	for _, fileEvent := range params.Changes {
    72  		if fileEvent.Type == int(lsp.Created) {
    73  			if err := documentCreate(fileEvent.URI, ctx, conn); err != nil {
    74  				return err
    75  			}
    76  		} else if fileEvent.Type == int(lsp.Deleted) {
    77  			if err := documentDelete(fileEvent.URI, ctx, conn); err != nil {
    78  				return err
    79  			}
    80  		} else {
    81  			if err := documentCreate(fileEvent.URI, ctx, conn); err != nil {
    82  				return err
    83  			}
    84  		}
    85  	}
    86  	go publishDiagnostics(ctx, conn)
    87  	return nil
    88  }
    89  
    90  func documentCreate(uri lsp.DocumentURI, ctx context.Context, conn jsonrpc2.JSONRPC2) error {
    91  	var err error
    92  	if !util.IsGaugeFile(string(uri)) {
    93  		if lRunner.runner != nil {
    94  			err = cacheFileOnRunner(uri, "", false, gm.CacheFileRequest_CREATED)
    95  		}
    96  	}
    97  	return err
    98  }
    99  
   100  func documentDelete(uri lsp.DocumentURI, ctx context.Context, conn jsonrpc2.JSONRPC2) error {
   101  	if !util.IsGaugeFile(string(uri)) {
   102  		if lRunner.runner != nil {
   103  			return cacheFileOnRunner(uri, "", false, gm.CacheFileRequest_DELETED)
   104  		}
   105  		return fmt.Errorf("Language runner is not instantiated.")
   106  	}
   107  	return publishDiagnostic(uri, []lsp.Diagnostic{}, conn, ctx)
   108  }