github.com/goplus/gop@v1.2.6/x/langserver/client.go (about)

     1  /*
     2   * Copyright (c) 2023 The GoPlus Authors (goplus.org). All rights reserved.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package langserver
    18  
    19  import (
    20  	"context"
    21  
    22  	"github.com/goplus/gop/x/jsonrpc2"
    23  )
    24  
    25  // -----------------------------------------------------------------------------
    26  
    27  const (
    28  	methodGenGo   = "gengo"
    29  	methodChanged = "changed"
    30  )
    31  
    32  // -----------------------------------------------------------------------------
    33  
    34  // Dialer is used by clients to dial a server.
    35  type Dialer = jsonrpc2.Dialer
    36  
    37  type AsyncCall = jsonrpc2.AsyncCall
    38  
    39  // Client is a client of the language server.
    40  type Client struct {
    41  	conn *jsonrpc2.Connection
    42  }
    43  
    44  // Open uses the dialer to make a new connection and returns a client of the LangServer
    45  // based on the connection.
    46  func Open(ctx context.Context, dialer Dialer, onDone func()) (ret Client, err error) {
    47  	c, err := jsonrpc2.Dial(ctx, dialer, jsonrpc2.BinderFunc(
    48  		func(ctx context.Context, c *jsonrpc2.Connection) (ret jsonrpc2.ConnectionOptions) {
    49  			return
    50  		}), onDone)
    51  	if err != nil {
    52  		return
    53  	}
    54  	ret = Client{c}
    55  	return
    56  }
    57  
    58  func (p Client) Close() error {
    59  	return p.conn.Close()
    60  }
    61  
    62  func (p Client) AsyncGenGo(ctx context.Context, pattern ...string) *AsyncCall {
    63  	return p.conn.Call(ctx, methodGenGo, pattern)
    64  }
    65  
    66  func (p Client) GenGo(ctx context.Context, pattern ...string) (err error) {
    67  	return p.AsyncGenGo(ctx, pattern...).Await(ctx, nil)
    68  }
    69  
    70  func (p Client) Changed(ctx context.Context, files ...string) (err error) {
    71  	return p.conn.Notify(ctx, methodChanged, files)
    72  }
    73  
    74  // -----------------------------------------------------------------------------