Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
Imported from go-langserver
  • Loading branch information
keegancsmith committed Oct 9, 2018
0 parents commit 4631ffd
Show file tree
Hide file tree
Showing 12 changed files with 1,216 additions and 0 deletions.
19 changes: 19 additions & 0 deletions LICENSE
@@ -0,0 +1,19 @@
Copyright (c) 2018 Sourcegraph

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
8 changes: 8 additions & 0 deletions README.md
@@ -0,0 +1,8 @@
# go-lsp

Package lsp contains Go types for the messages used in the Language Server
Protocol.

See
https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md
for more information.
6 changes: 6 additions & 0 deletions doc.go
@@ -0,0 +1,6 @@
// Package lsp contains Go types for the messages used in the Language
// Server Protocol.
//
// See https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md
// for more information.
package lsp
52 changes: 52 additions & 0 deletions jsonrpc2.go
@@ -0,0 +1,52 @@
package lsp

import (
"encoding/json"
"strconv"
)

// ID represents a JSON-RPC 2.0 request ID, which may be either a
// string or number (or null, which is unsupported).
type ID struct {
// At most one of Num or Str may be nonzero. If both are zero
// valued, then IsNum specifies which field's value is to be used
// as the ID.
Num uint64
Str string

// IsString controls whether the Num or Str field's value should be
// used as the ID, when both are zero valued. It must always be
// set to true if the request ID is a string.
IsString bool
}

func (id ID) String() string {
if id.IsString {
return strconv.Quote(id.Str)
}
return strconv.FormatUint(id.Num, 10)
}

// MarshalJSON implements json.Marshaler.
func (id ID) MarshalJSON() ([]byte, error) {
if id.IsString {
return json.Marshal(id.Str)
}
return json.Marshal(id.Num)
}

// UnmarshalJSON implements json.Unmarshaler.
func (id *ID) UnmarshalJSON(data []byte) error {
// Support both uint64 and string IDs.
var v uint64
if err := json.Unmarshal(data, &v); err == nil {
*id = ID{Num: v}
return nil
}
var v2 string
if err := json.Unmarshal(data, &v2); err != nil {
return err
}
*id = ID{Str: v2, IsString: true}
return nil
}
30 changes: 30 additions & 0 deletions lspext/cache.go
@@ -0,0 +1,30 @@
package lspext

import "encoding/json"

// See https://github.com/sourcegraph/language-server-protocol/pull/14

// CacheGetParams is the input for 'cache/get'. The response is any or null.
type CacheGetParams struct {
// Key is the cache key. The key namespace is shared for a language
// server, but with other language servers. For example the PHP
// language server on different workspaces share the same key
// namespace, but does not share the namespace with a Go language
// server.
Key string `json:"key"`
}

// CacheSetParams is the input for the notification 'cache/set'.
type CacheSetParams struct {
// Key is the cache key. The key namespace is shared for a language
// server, but with other language servers. For example the PHP
// language server on different workspaces share the same key
// namespace, but does not share the namespace with a Go language
// server.
Key string `json:"key"`

// Value is type any. We use json.RawMessage since we expect caching
// implementation to cache the raw bytes, and not bother with
// Unmarshaling/Marshalling.
Value *json.RawMessage `json:"value"`
}
16 changes: 16 additions & 0 deletions lspext/filesext.go
@@ -0,0 +1,16 @@
package lspext

import "github.com/sourcegraph/go-lsp"

// See https://github.com/sourcegraph/language-server-protocol/pull/4.

// ContentParams is the input for 'textDocument/content'. The response is a
// 'TextDocumentItem'.
type ContentParams struct {
TextDocument lsp.TextDocumentIdentifier `json:"textDocument"`
}

// FilesParams is the input for 'workspace/xfiles'. The response is '[]TextDocumentIdentifier'
type FilesParams struct {
Base string `json:"base,omitempty"`
}
31 changes: 31 additions & 0 deletions lspext/implementation.go
@@ -0,0 +1,31 @@
package lspext

import "github.com/sourcegraph/go-lsp"

// ImplementationLocation is a superset of lsp.Location with additional Go-specific information
// about the implementation.
type ImplementationLocation struct {
lsp.Location // the location of the implementation

// Type is the type of implementation relationship described by this location.
//
// If a type T was queried, the set of possible values are:
//
// - "to": named or ptr-to-named types assignable to interface T
// - "from": named interfaces assignable from T (or only from *T if Ptr == true)
//
// If a method M on type T was queried, the same set of values above is used, except they refer
// to methods on the described type (not the described type itself).
//
// (This type description is taken from golang.org/x/tools/cmd/guru.)
Type string `json:"type,omitempty"`

// Ptr is whether this implementation location is only assignable from a pointer *T (where T is
// the queried type).
Ptr bool `json:"ptr,omitempty"`

// Method is whether a method was queried. If so, then the implementation locations refer to the
// corresponding methods on the types found by the implementation query (not the types
// themselves).
Method bool `json:"method"`
}
98 changes: 98 additions & 0 deletions lspext/lspext.go
@@ -0,0 +1,98 @@
package lspext

import (
"fmt"
"sort"
"strings"

"github.com/sourcegraph/go-lsp"
)

// WorkspaceSymbolParams is the extension workspace/symbol parameter type.
type WorkspaceSymbolParams struct {
Query string `json:"query,omitempty"`
Limit int `json:"limit"`
Symbol SymbolDescriptor `json:"symbol,omitempty"`
}

// WorkspaceReferencesParams is parameters for the `workspace/xreferences` extension
//
// See: https://github.com/sourcegraph/language-server-protocol/blob/master/extension-workspace-reference.md
//
type WorkspaceReferencesParams struct {
// Query represents metadata about the symbol that is being searched for.
Query SymbolDescriptor `json:"query"`

// Hints provides optional hints about where the language server should
// look in order to find the symbol (this is an optimization). It is up to
// the language server to define the schema of this object.
Hints map[string]interface{} `json:"hints,omitempty"`

// Limit if positive will limit the number of results returned.
Limit int `json:"limit,omitempty"`
}

// ReferenceInformation represents information about a reference to programming
// constructs like variables, classes, interfaces etc.
type ReferenceInformation struct {
// Reference is the location in the workspace where the `symbol` has been
// referenced.
Reference lsp.Location `json:"reference"`

// Symbol is metadata information describing the symbol being referenced.
Symbol SymbolDescriptor `json:"symbol"`
}

// SymbolDescriptor represents information about a programming construct like a
// variable, class, interface, etc that has a reference to it. It is up to the
// language server to define the schema of this object.
//
// SymbolDescriptor usually uniquely identifies a symbol, but it is not
// guaranteed to do so.
type SymbolDescriptor map[string]interface{}

// SymbolLocationInformation is the response type for the `textDocument/xdefinition` extension.
type SymbolLocationInformation struct {
// A concrete location at which the definition is located, if any.
Location lsp.Location `json:"location,omitempty"`
// Metadata about the definition.
Symbol SymbolDescriptor `json:"symbol"`
}

// Contains tells if this SymbolDescriptor fully contains all of the keys and
// values in the other symbol descriptor.
func (s SymbolDescriptor) Contains(other SymbolDescriptor) bool {
for k, v := range other {
v2, ok := s[k]
if !ok || v != v2 {
return false
}
}
return true
}

// String returns a consistently ordered string representation of the
// SymbolDescriptor. It is useful for testing.
func (s SymbolDescriptor) String() string {
sm := make(sortedMap, 0, len(s))
for k, v := range s {
sm = append(sm, mapValue{key: k, value: v})
}
sort.Sort(sm)
var str string
for _, v := range sm {
str += fmt.Sprintf("%s:%v ", v.key, v.value)
}
return strings.TrimSpace(str)
}

type mapValue struct {
key string
value interface{}
}

type sortedMap []mapValue

func (s sortedMap) Len() int { return len(s) }
func (s sortedMap) Less(i, j int) bool { return s[i].key < s[j].key }
func (s sortedMap) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
16 changes: 16 additions & 0 deletions lspext/partial.go
@@ -0,0 +1,16 @@
package lspext

import "github.com/sourcegraph/go-lsp"

// PartialResultParams is the input for "$/partialResult", a notification.
type PartialResultParams struct {
// ID is the jsonrpc2 ID of the request we are returning partial
// results for.
ID lsp.ID `json:"id"`

// Patch is a JSON patch as specified at http://jsonpatch.com/
//
// It is an interface{}, since our only requirement is that it JSON
// marshals to a valid list of JSON Patch operations.
Patch interface{} `json:"patch"`
}

0 comments on commit 4631ffd

Please sign in to comment.