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