Skip to content
This repository was archived by the owner on Feb 19, 2020. It is now read-only.

Commit 1026538

Browse files
committed
update
1 parent f941966 commit 1026538

File tree

1 file changed

+46
-34
lines changed

1 file changed

+46
-34
lines changed

src/extension.ts

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,12 @@
11
import * as sourcegraph from 'sourcegraph'
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'
2+
import { ajax } from 'rxjs/ajax'
53

64
interface FullSettings {
75
'graphql.langserver-address': string
86
}
97

108
type Settings = Partial<FullSettings>
119

12-
function connectTo(address: string): Promise<rpc.MessageConnection> {
13-
return new Promise(resolve => {
14-
const webSocket = new WebSocket(address)
15-
rpc.listen({
16-
webSocket,
17-
onConnection: (connection: rpc.MessageConnection) => {
18-
resolve(connection)
19-
},
20-
})
21-
})
22-
}
23-
2410
export function activate(): void {
2511
function afterActivate() {
2612
const address = sourcegraph.configuration.get<Settings>().get('graphql.langserver-address')
@@ -29,42 +15,68 @@ export function activate(): void {
2915
return
3016
}
3117

32-
connectTo(address).then((connection: rpc.MessageConnection) => {
33-
connection.listen()
18+
const docSelector = [{ pattern: '*.{graphql,gql,schema}' }]
3419

35-
const docSelector = [{ pattern: '*.{graphql,gql,schema}' }]
36-
37-
sourcegraph.languages.registerHoverProvider(docSelector, {
38-
provideHover: async (doc, pos) => {
39-
return connection.sendRequest<sourcegraph.Hover>('hover', doc, pos).then(hover => {
20+
sourcegraph.languages.registerHoverProvider(docSelector, {
21+
provideHover: async (doc, pos) => {
22+
return ajax({
23+
method: 'POST',
24+
url: address,
25+
body: JSON.stringify({ method: 'hover', doc, pos }),
26+
responseType: 'json',
27+
headers: {
28+
'Content-Type': 'application/json',
29+
},
30+
})
31+
.toPromise()
32+
.then(response => {
4033
return (
41-
hover && {
34+
response &&
35+
response.response &&
36+
response.response.contents && {
4237
contents: {
43-
value: '```python\n' + (hover.contents as any).join('\n') + '\n```',
38+
// python syntax highlighting works pretty well for GraphQL
39+
value: '```python\n' + response.response.contents.join('\n') + '\n```',
4440
kind: sourcegraph.MarkupKind.Markdown,
4541
},
4642
}
4743
)
4844
})
49-
},
50-
})
45+
},
46+
})
5147

52-
sourcegraph.languages.registerDefinitionProvider(docSelector, {
53-
provideDefinition: async (doc, pos) => {
54-
return connection.sendRequest<any>('definition', doc, pos).then(definition => {
48+
sourcegraph.languages.registerDefinitionProvider(docSelector, {
49+
provideDefinition: async (doc, pos) => {
50+
return ajax({
51+
method: 'POST',
52+
url: address,
53+
body: JSON.stringify({ method: 'definition', doc, pos }),
54+
responseType: 'json',
55+
headers: {
56+
'Content-Type': 'application/json',
57+
},
58+
})
59+
.toPromise()
60+
.then(response => {
5561
return (
56-
definition &&
62+
response &&
63+
response.response.definition &&
5764
new sourcegraph.Location(
5865
new sourcegraph.URI(doc.uri),
5966
new sourcegraph.Range(
60-
new sourcegraph.Position(definition.start.line, definition.start.character),
61-
new sourcegraph.Position(definition.end.line, definition.end.character)
67+
new sourcegraph.Position(
68+
response.response.definition.start.line,
69+
response.response.definition.start.character
70+
),
71+
new sourcegraph.Position(
72+
response.response.definition.end.line,
73+
response.response.definition.end.character
74+
)
6275
)
6376
)
6477
)
6578
})
66-
},
67-
})
79+
},
6880
})
6981
}
7082
// Error creating extension host: Error: Configuration is not yet available.

0 commit comments

Comments
 (0)