|
1 | 1 | import * as sourcegraph from 'sourcegraph' |
2 | 2 | import * as rpc from 'vscode-ws-jsonrpc' |
| 3 | +import { BehaviorSubject, Unsubscribable, Subscription, EMPTY, of } from 'rxjs' |
| 4 | +import { map, switchMap, distinct, distinctUntilChanged } from 'rxjs/operators' |
3 | 5 |
|
4 | | -const PORT = 1234 |
| 6 | +interface FullSettings { |
| 7 | + 'graphql.langserver-address': string |
| 8 | +} |
| 9 | + |
| 10 | +type Settings = Partial<FullSettings> |
| 11 | + |
| 12 | +function connectTo(address: string): Promise<rpc.MessageConnection> { |
| 13 | + return new Promise(resolve => { |
| 14 | + const webSocket = new WebSocket('ws://' + address) |
| 15 | + rpc.listen({ |
| 16 | + webSocket, |
| 17 | + onConnection: (connection: rpc.MessageConnection) => { |
| 18 | + resolve(connection) |
| 19 | + }, |
| 20 | + }) |
| 21 | + }) |
| 22 | +} |
5 | 23 |
|
6 | 24 | export function activate(): void { |
7 | | - const webSocket = new WebSocket('ws://localhost:' + PORT) |
8 | | - rpc.listen({ |
9 | | - webSocket, |
10 | | - onConnection: (connection: rpc.MessageConnection) => { |
11 | | - connection.listen() |
| 25 | + const address = sourcegraph.configuration.get<Settings>().get('graphql.langserver-address') |
| 26 | + if (!address) { |
| 27 | + console.log('No graphql.langserver-address was set, exiting.') |
| 28 | + return |
| 29 | + } |
| 30 | + console.log('Connecting to', address) |
12 | 31 |
|
13 | | - sourcegraph.languages.registerHoverProvider(['*'], { |
14 | | - provideHover: async (doc, pos) => { |
15 | | - return connection.sendRequest<sourcegraph.Hover>('hover', doc, pos).then(hover => { |
16 | | - return ( |
17 | | - hover && { |
18 | | - contents: { |
19 | | - value: '```python\n' + (hover.contents as any).join('\n') + '\n```', |
20 | | - kind: sourcegraph.MarkupKind.Markdown, |
21 | | - }, |
22 | | - } |
23 | | - ) |
24 | | - }) |
25 | | - }, |
26 | | - }) |
| 32 | + connectTo(address).then((connection: rpc.MessageConnection) => { |
| 33 | + console.log('Connected') |
| 34 | + connection.listen() |
| 35 | + |
| 36 | + sourcegraph.languages.registerHoverProvider(['*'], { |
| 37 | + provideHover: async (doc, pos) => { |
| 38 | + return connection.sendRequest<sourcegraph.Hover>('hover', doc, pos).then(hover => { |
| 39 | + return ( |
| 40 | + hover && { |
| 41 | + contents: { |
| 42 | + value: '```python\n' + (hover.contents as any).join('\n') + '\n```', |
| 43 | + kind: sourcegraph.MarkupKind.Markdown, |
| 44 | + }, |
| 45 | + } |
| 46 | + ) |
| 47 | + }) |
| 48 | + }, |
| 49 | + }) |
27 | 50 |
|
28 | | - sourcegraph.languages.registerDefinitionProvider(['*'], { |
29 | | - provideDefinition: async (doc, pos) => { |
30 | | - return connection.sendRequest<any>('definition', doc, pos).then(definition => { |
31 | | - return ( |
32 | | - definition && |
33 | | - new sourcegraph.Location( |
34 | | - new sourcegraph.URI(doc.uri), |
35 | | - new sourcegraph.Range( |
36 | | - new sourcegraph.Position(definition.start.line, definition.start.character), |
37 | | - new sourcegraph.Position(definition.end.line, definition.end.character) |
38 | | - ) |
| 51 | + sourcegraph.languages.registerDefinitionProvider(['*'], { |
| 52 | + provideDefinition: async (doc, pos) => { |
| 53 | + return connection.sendRequest<any>('definition', doc, pos).then(definition => { |
| 54 | + return ( |
| 55 | + definition && |
| 56 | + new sourcegraph.Location( |
| 57 | + new sourcegraph.URI(doc.uri), |
| 58 | + new sourcegraph.Range( |
| 59 | + new sourcegraph.Position(definition.start.line, definition.start.character), |
| 60 | + new sourcegraph.Position(definition.end.line, definition.end.character) |
39 | 61 | ) |
40 | 62 | ) |
41 | | - }) |
42 | | - }, |
43 | | - }) |
44 | | - }, |
| 63 | + ) |
| 64 | + }) |
| 65 | + }, |
| 66 | + }) |
45 | 67 | }) |
46 | 68 | } |
0 commit comments