forked from swiftlang/swift-integration-tests
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-sourcekit-lsp.py
More file actions
152 lines (127 loc) · 4.08 KB
/
Copy pathtest-sourcekit-lsp.py
File metadata and controls
152 lines (127 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# Canary test for sourcekit-lsp, covering interaction with swiftpm and toolchain
# language services.
# REQUIRES: have-sourcekit-lsp
# Make a sandbox dir.
# RUN: rm -rf %t.dir
# RUN: mkdir -p %t.dir
# RUN: cp -r %S/pkg %t.dir/
# RUN: env SWIFTPM_ENABLE_CLANG_INDEX_STORE=1 %{swift-build} --package-path %t.dir/pkg -Xswiftc -index-ignore-system-modules -v 2>&1 | tee %t.build-log
# RUN: %{FileCheck} --check-prefix CHECK-BUILD-LOG --input-file %t.build-log %s
# CHECK-BUILD-LOG-NOT: error:
# RUN: %{python} -u %s %{sourcekit-lsp} %t.dir/pkg 2>&1 | tee %t.run-log
# RUN: %{FileCheck} --input-file %t.run-log %s
import argparse
import json
import os
import subprocess
import sys
class LspScript(object):
def __init__(self):
self.request_id = 0
self.script = ''
def request(self, method, params):
body = json.dumps({
'jsonrpc': '2.0',
'id': self.request_id,
'method': method,
'params': params
})
self.request_id += 1
self.script += 'Content-Length: {}\r\n\r\n{}'.format(len(body), body)
def note(self, method, params):
body = json.dumps({
'jsonrpc': '2.0',
'method': method,
'params': params
})
self.script += 'Content-Length: {}\r\n\r\n{}'.format(len(body), body)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('sourcekit_lsp')
parser.add_argument('package')
args = parser.parse_args()
lsp = LspScript()
lsp.request('initialize', {
'rootPath': args.package,
'capabilities': {},
'initializationOptions': {
'listenToUnitEvents': False,
}
})
main_swift = os.path.join(args.package, 'Sources', 'exec', 'main.swift')
with open(main_swift, 'r') as f:
main_swift_content = f.read()
lsp.note('textDocument/didOpen', {
'textDocument': {
'uri': 'file://' + main_swift,
'languageId': 'swift',
'version': 0,
'text': main_swift_content,
}
})
lsp.request('workspace/_pollIndex', {})
lsp.request('textDocument/definition', {
'textDocument': { 'uri': 'file://' + main_swift },
'position': { 'line': 3, 'character': 6}, ## zero-based
})
# CHECK: "result":[
# CHECK-DAG: lib.swift
# CHECK-DAG: "line":1
# CHECK-DAG: "character":14
# CHECK: ]
lsp.request('textDocument/definition', {
'textDocument': { 'uri': 'file://' + main_swift },
'position': { 'line': 4, 'character': 0}, ## zero-based
})
# CHECK: "result":[
# CHECK-DAG: clib.c
# CHECK-DAG: "line":2
# CHECK-DAG: "character":5
# CHECK: ]
lsp.request('textDocument/completion', {
'textDocument': { 'uri': 'file://' + main_swift },
'position': { 'line': 3, 'character': 6}, ## zero-based
})
# CHECK: "items":[
# CHECK-DAG: "label":"foo()"
# CHECK-DAG: "label":"self"
# CHECK: ]
clib_c = os.path.join(args.package, 'Sources', 'clib', 'clib.c')
with open(clib_c, 'r') as f:
clib_c_content = f.read()
lsp.note('textDocument/didOpen', {
'textDocument': {
'uri': 'file://' + clib_c,
'languageId': 'c',
'version': 0,
'text': clib_c_content,
}
})
lsp.request('textDocument/completion', {
'textDocument': { 'uri': 'file://' + clib_c },
'position': { 'line': 2, 'character': 22}, ## zero-based
})
# CHECK: "items":[
# CHECK-DAG: "insertText":"clib_func"
# Missing "clib_other" from clangd on rebranch - rdar://73762053
# DISABLED-DAG: "insertText":"clib_other"
# CHECK: ]
lsp.request('shutdown', {})
lsp.note('exit', {})
print('==== INPUT ====')
print(lsp.script)
print('')
print('==== OUTPUT ====')
skargs = [args.sourcekit_lsp, '--sync', '-Xclangd', '-sync']
p = subprocess.Popen(skargs, stdin=subprocess.PIPE, stdout=subprocess.PIPE, encoding='utf-8')
out, _ = p.communicate(lsp.script)
print(out)
print('')
if p.returncode == 0:
print('OK')
else:
print('error: sourcekit-lsp exited with code {}'.format(p.returncode))
sys.exit(1)
# CHECK: OK
if __name__ == "__main__":
main()