This repository has been archived by the owner on Dec 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
handle.swift
76 lines (73 loc) · 3.03 KB
/
handle.swift
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
//
// handle.swift
// langserver-swift
//
// Created by Ryan Lovelett on 11/22/16.
//
//
import Argo
import Foundation
import JSONRPC
import LanguageServerProtocol
var workspace: Workspace!
var exitCode: Int32 = 1
func handle(_ request: Request) -> Response {
do {
switch request.method {
case "initialize":
let parameters: InitializeParams = try request.parse()
workspace = Workspace(parameters)
let response = Response(to: request, is: InitializeResult(workspace.capabilities))
return response
case "textDocument/didChange":
let parameters: DidChangeTextDocumentParams = try request.parse()
try workspace.update(byClient: parameters)
return Response(to: request, is: JSON.null)
case "textDocument/didClose":
let parameters: DidCloseTextDocumentParams = try request.parse()
workspace.close(byClient: parameters)
return Response(to: request, is: JSON.null)
case "textDocument/didOpen":
let parameters: DidOpenTextDocumentParams = try request.parse()
workspace.open(byClient: parameters)
return Response(to: request, is: JSON.null)
// case "textDocument/didSave":
// fatalError("\(request.method) is not implemented yet.")
case "textDocument/completion":
let parameters: TextDocumentPositionParams = try request.parse()
let items = try workspace.complete(forText: parameters)
let response = Response(to: request, is: items)
return response
case "textDocument/definition":
let parameters: TextDocumentPositionParams = try request.parse()
let location = try workspace.findDeclaration(forText: parameters)
let response = Response(to: request, is: location)
return response
case "textDocument/hover":
let parameters: TextDocumentPositionParams = try request.parse()
let hover = try workspace.cursor(forText: parameters)
let response = Response(to: request, is: hover)
return response
// case "textDocument/documentSymbol":
// fatalError("\(request.method) is not implemented yet.")
// case "textDocument/references":
// fatalError("\(request.method) is not implemented yet.")
// case "workspace/symbol":
// fatalError("\(request.method) is not implemented yet.")
case "shutdown":
workspace = nil
// The server should exit with `success` code 0 if the shutdown request has been received
// before; otherwise with `error` code 1.
exitCode = 0
return Response(to: request, is: JSON.null)
case "exit":
exit(exitCode)
default:
throw PredefinedError.methodNotFound
}
} catch let error as ServerError {
return Response(to: request, is: error)
} catch {
return Response(to: request, is: PredefinedError.invalidParams)
}
}