| 1 | // Package lspx contains extensions to the LSP protocol. |
| 2 | // |
| 3 | // An overview of the different protocol variants: |
| 4 | // |
| 5 | // // vanilla LSP |
| 6 | // github.com/sourcegraph/go-langserver/pkg/lsp |
| 7 | // |
| 8 | // // proxy (http gateway) server LSP extensions |
| 9 | // github.com/sourcegraph/sourcegraph/xlang |
| 10 | // |
| 11 | // // (this package) build/lang server LSP extensions |
| 12 | // github.com/sourcegraph/sourcegraph/xlang/lspx |
| 13 | // |
| 14 | package lspext |
| 15 | |
| 16 | import ( |
| 17 | "reflect" |
| 18 | "time" |
| 19 | |
| 20 | "github.com/sourcegraph/go-lsp" |
| 21 | ) |
| 22 | |
| 23 | // DependencyReference represents a reference to a dependency. []DependencyReference |
| 24 | // is the return type for the build server workspace/xdependencies method. |
| 25 | type DependencyReference struct { |
| 26 | // Attributes describing the dependency that is being referenced. It is up |
| 27 | // to the language server to define the schema of this object. |
| 28 | Attributes map[string]interface{} `json:"attributes,omitempty"` |
| 29 | |
| 30 | // Hints is treated as an opaque object and passed directly by Sourcegraph |
| 31 | // into the language server's workspace/xreferences method to help narrow |
| 32 | // down the search space for the references to the symbol. |
| 33 | // |
| 34 | // If a language server emits no hints, Sourcegraph will pass none as a |
| 35 | // parameter to workspace/xreferences which means it must search the entire |
| 36 | // repository (workspace) in order to find references. workspace/xdependencies |
| 37 | // should emit sufficient hints here for making all workspace/xreference |
| 38 | // queries complete in a reasonable amount of time (less than a few seconds |
| 39 | // for very large repositories). For example, one may include the |
| 40 | // containing "package" or other build-system level "code unit". Emitting |
| 41 | // the exact file is not recommended in general as that would produce more |
| 42 | // data for little performance gain in most situations. |
| 43 | Hints map[string]interface{} `json:"hints,omitempty"` |
| 44 | } |
| 45 | |
| 46 | // TelemetryEventParams is a telemetry/event message sent from a |
| 47 | // build/lang server back to the proxy. The information here is |
| 48 | // forwarded to our opentracing system. |
| 49 | type TelemetryEventParams struct { |
| 50 | Op string `json:"op"` // the operation name |
| 51 | StartTime time.Time `json:"startTime"` // when the operation started |
| 52 | EndTime time.Time `json:"endTime"` // when the operation ended |
| 53 | Tags map[string]string `json:"tags,omitempty"` // other metadata |
| 54 | } |
| 55 | |
| 56 | type InitializeParams struct { |
| 57 | lsp.InitializeParams |
| 58 | |
| 59 | // OriginalRootURI is the original rootUri for this LSP session, |
| 60 | // before any path rewriting occurred. It is typically a Git clone |
| 61 | // URL of the form |
| 62 | // "git://github.com/facebook/react.git?rev=master#lib". |
| 63 | // |
| 64 | // The Go lang/build server uses this to infer the import path |
| 65 | // root (and directory structure) to use for a workspace. |
| 66 | OriginalRootURI lsp.DocumentURI `json:"originalRootUri"` |
| 67 | |
| 68 | // Mode is the name of the language. It is used to determine the correct |
| 69 | // language server to route a request to, and to inform a language server |
| 70 | // what languages it should contribute. |
| 71 | Mode string `json:"mode"` |
| 72 | } |
| 73 | |
| 74 | // WalkURIFields walks the LSP params/result object for fields |
| 75 | // containing document URIs. |
| 76 | // |
| 77 | // If collect is non-nil, it calls collect(uri) for every URI |
| 78 | // encountered. Callers can use this to collect a list of all document |
| 79 | // URIs referenced in the params/result. |
| 80 | // |
| 81 | // If update is non-nil, it updates all document URIs in an LSP |
| 82 | // params/result with the value of f(existingURI). Callers can use |
| 83 | // this to rewrite paths in the params/result. |
| 84 | // |
| 85 | // TODO(sqs): does not support WorkspaceEdit (with a field whose |
| 86 | // TypeScript type is {[uri: string]: TextEdit[]}. |
| 87 | func WalkURIFields(o interface{}, collect func(lsp.DocumentURI), update func(lsp.DocumentURI) lsp.DocumentURI) { |
| 88 | var walk func(o interface{}) |
| 89 | walk = func(o interface{}) { |
| 90 | switch o := o.(type) { |
| 91 | case map[string]interface{}: |
| 92 | for k, v := range o { // Location, TextDocumentIdentifier, TextDocumentItem, etc. |
| 93 | if k == "uri" { |
| 94 | s, ok := v.(string) |
| 95 | if !ok { |
| 96 | s2, ok2 := v.(lsp.DocumentURI) |
| 97 | s = string(s2) |
| 98 | ok = ok2 |
| 99 | } |
| 100 | if ok { |
| 101 | if collect != nil { |
| 102 | collect(lsp.DocumentURI(s)) |
| 103 | } |
| 104 | if update != nil { |
| 105 | o[k] = update(lsp.DocumentURI(s)) |
| 106 | } |
| 107 | continue |
| 108 | } |
| 109 | } |
| 110 | walk(v) |
| 111 | } |
| 112 | case []interface{}: // Location[] |
| 113 | for _, v := range o { |
| 114 | walk(v) |
| 115 | } |
| 116 | default: // structs with a "URI" field |
| 117 | rv := reflect.ValueOf(o) |
| 118 | if rv.Kind() == reflect.Ptr { |
| 119 | rv = rv.Elem() |
| 120 | } |
| 121 | if rv.Kind() == reflect.Struct { |
| 122 | if fv := rv.FieldByName("URI"); fv.Kind() == reflect.String { |
| 123 | if collect != nil { |
| 124 | collect(lsp.DocumentURI(fv.String())) |
| 125 | } |
| 126 | if update != nil { |
| 127 | fv.SetString(string(update(lsp.DocumentURI(fv.String())))) |
| 128 | } |
| 129 | } |
| 130 | for i := 0; i < rv.NumField(); i++ { |
| 131 | fv := rv.Field(i) |
| 132 | if fv.Kind() == reflect.Ptr || fv.Kind() == reflect.Struct || fv.Kind() == reflect.Array { |
| 133 | walk(fv.Interface()) |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | walk(o) |
| 140 | } |
| 141 | |
| 142 | // ClientProxyInitializeParams are sent by the client to the proxy in |
| 143 | // the "initialize" request. It has a non-standard field "mode", which |
| 144 | // is the name of the language (using vscode terminology); "go" or |
| 145 | // "typescript", for example. |
| 146 | type ClientProxyInitializeParams struct { |
| 147 | lsp.InitializeParams |
| 148 | InitializationOptions ClientProxyInitializationOptions `json:"initializationOptions"` |
| 149 | |
| 150 | // Mode is DEPRECATED; it was moved to the subfield |
| 151 | // initializationOptions.Mode. It is still here for backward |
| 152 | // compatibility until the xlang service is upgraded. |
| 153 | Mode string `json:"mode,omitempty"` |
| 154 | } |
| 155 | |
| 156 | // ClientProxyInitializationOptions is the "initializationOptions" |
| 157 | // field of the "initialize" request params sent from the client to |
| 158 | // the LSP client proxy. |
| 159 | type ClientProxyInitializationOptions struct { |
| 160 | Mode string `json:"mode"` |
| 161 | |
| 162 | // Same as, but takes precedence over, InitializeParams.rootUri. |
| 163 | // vscode-languageserver-node's LanguageClient doesn't allow overriding |
| 164 | // InitializeParams.rootUri, so clients that use vscode-languageserver-node |
| 165 | // instead set InitializeParams.initializationOptions.rootUri. |
| 166 | RootURI *lsp.DocumentURI `json:"rootUri,omitempty"` |
| 167 | |
| 168 | // Session, if set, causes this session to be isolated from other |
| 169 | // LSP sessions using the same workspace and mode. See |
| 170 | // (contextID).session for more information. |
| 171 | Session string `json:"session,omitempty"` |
| 172 | |
| 173 | // ZipURL is the zip URL to forward to the language server. |
| 174 | ZipURL string `json:"zipURL,omitempty"` |
| 175 | } |