Skip to content

Commit 21d174f

Browse files
committed
YCMD Completions Integration
This patch gets the server working end to end with the swift completion client in YCMD ( my fork atleast ). Add a shutdown endpoint so we can restart the sever easily. Add the ability to pass in options from the client including HMAC secret and logging level. Add a bootstrap script to easily get started. Additionally it includes other cleanups: - Use the correct JSON headers - Remove unused code and some naming fixes - Remove prototype code cleanup names - Cleanup build defs - Remove old server code
1 parent 5d8ebed commit 21d174f

File tree

11 files changed

+106
-224
lines changed

11 files changed

+106
-224
lines changed

CMakeLists.txt

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ message("__SSVI CMAKE START")
22

33
cmake_minimum_required(VERSION 2.8)
44

5-
message("parent prefix ${CMAKE_INSTALL_PREFIX}")
6-
75
# Boost
86
#
97

@@ -43,16 +41,14 @@ execute_process (
4341
COMMAND bash -c "xcode-select --print-path | tr -d '\n'"
4442
OUTPUT_VARIABLE XCODE_PATH
4543
)
46-
#
44+
45+
message(${XCODE_PATH})
4746

4847
set(SKT_FLAGS "-framework sourcekitd")
4948
set(SKT_FLAGS " ${SKT_FLAGS} -F ${XCODE_PATH}/Toolchains/XcodeDefault.xctoolchain/usr/lib")
5049
set(SKT_FLAGS " ${SKT_FLAGS} -rpath ${XCODE_PATH}/Toolchains/XcodeDefault.xctoolchain/usr/lib")
51-
message(${XCODE_PATH})
52-
set( XCLIB "-F ${XCODE_PATH}/Toolchains/XcodeDefault.xctoolchain/usr/lib")
5350

5451
set(GLOBAL_CXX_FLAGS "-std=c++1y -stdlib=libc++")
55-
set(GLOBAL_CXX_FLAGS "${GLOBAL_CXX_FLAGS} -std=c++1y -stdlib=libc++")
5652
set(GLOBAL_CXX_FLAGS "${GLOBAL_CXX_FLAGS} -Wall -Wextra -Wpedantic -Wno-unused-parameter")
5753
set(GLOBAL_CXX_FLAGS "${GLOBAL_CXX_FLAGS} -isystem /usr/local/Frameworks")
5854

@@ -61,10 +57,8 @@ set(CMAKE_CXX_FLAGS ${SKT_FLAGS})
6157

6258
add_executable (http_server
6359
file_body.hpp
64-
mime_type.hpp
65-
ssvi_http_server.hpp
66-
ssvi_http_server.cpp
67-
sema_task.cpp
60+
ssvim_http_server.hpp
61+
ssvim_http_server.cpp
6862
http_server.cpp
6963
SwiftCompleter.h
7064
SwiftCompleter.cpp
@@ -76,7 +70,5 @@ INSTALL( TARGETS http_server
7670
RUNTIME DESTINATION bin )
7771
INSTALL( FILES scripts/activate
7872
DESTINATION . )
79-
#INSTALL( PROGRAMS scripts/run.sh
80-
# DESTINATION . )
8173

8274
message("__SSVI CMAKE FINISH")

bootstrap

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/bash
2+
set -e
3+
cd `dirname $0`
4+
5+
# System Dependencies
6+
if [[ ! -d /Applications/Xcode.app/ ]]; then
7+
>&2 echo "Needs OSX and Xcode for SourceKitD"
8+
exit 1
9+
fi
10+
11+
# Use boost is installed via brew for now
12+
EXPECTED_BOOST=/usr/local/Cellar/boost/1.64.0_1
13+
if [[ ! -d $EXPECTED_BOOST ]]; then
14+
echo "Missing Boost $EXPECTED_BOOST. \
15+
trying $ brew install boost 1.64"
16+
brew install [email protected]
17+
fi
18+
19+
if [[ ! -d vendor/ ]]; then
20+
echo "Init repository"
21+
./repoinit.sh
22+
fi
23+
24+
echo "Deps satisfied.."
25+
26+
echo "Configure.."
27+
./configure
28+
29+
echo "Build.."
30+
make -C build
31+
32+
echo "Installing.."
33+
ditto build/http_server /usr/local/bin/swiftyswiftvim
34+

http_server.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
#include "ssvi_http_server.hpp"
1+
#include "ssvim_http_server.hpp"
22

33
#include <boost/program_options.hpp>
44
#include <dispatch/dispatch.h>
55
#include <iostream>
66

7-
void loop() {
7+
void main_loop() {
88
__block boost::asio::io_service* ios = new boost::asio::io_service();
99
boost::asio::signal_set signals(
1010
*ios, SIGINT, SIGTERM);
@@ -25,7 +25,7 @@ void loop() {
2525

2626
int main(int ac, char const* av[])
2727
{
28-
using namespace beast::http;
28+
using namespace ssvim::http;
2929
namespace po = boost::program_options;
3030
po::options_description desc("Options");
3131

@@ -38,7 +38,11 @@ int main(int ac, char const* av[])
3838
"Set the IP address to bind to, \"0.0.0.0\" for all")
3939
("threads,n", po::value<std::size_t>()->default_value(4),
4040
"Set the number of threads to use")
41-
("sync,s", "Launch a synchronous server")
41+
// DEBUG, INFO, WARNING
42+
("log,r", po::value<std::string>()->default_value("INFO"),
43+
"Set the logging level")
44+
("hmac-file-secret,r", po::value<std::string>()->default_value("none"),
45+
"Set the hmac secret")
4246
;
4347
po::variables_map vm;
4448
po::store(po::parse_command_line(ac, av, desc), vm);
@@ -55,9 +59,11 @@ int main(int ac, char const* av[])
5559
using address_type = boost::asio::ip::address;
5660

5761
endpoint_type ep{address_type::from_string(ip), port};
58-
62+
// TODO: HMAC and logging level in the endpoint_impl's
63+
// For now, we just dump logs to std::out
64+
// and HMAC isn't checked
5965
ssvi_http_server server(ep, threads, root);
60-
loop();
66+
main_loop();
6167
return 0;
6268
}
6369

mime_type.hpp

Lines changed: 0 additions & 51 deletions
This file was deleted.

notes.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,19 @@ cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=1 "Unix Makefiles" ..
9595
The compile commands are in build/compile_commands.json
9696

9797

98+
## Making sure file type is working
99+
100+
YCM uses the vim file type in the current buffer to identify what completion engine to use and more.
101+
102+
Assert this is set to the correct value
103+
:set ft?
104+
105+
manually set
106+
:set ft=swift
107+
108+
Set automatically in your vimrc
109+
autocmd BufNewFile,BufRead *.swift set filetype=swift
110+
111+
112+
113+

req.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ echo '{"line":0,"column":0,"file_name":"'$PWD'/Examples/some_swift.swift", "cont
2222

2323
# Manually test /completions or /diagnostics
2424
# TODO: write some integration tests
25+
# See original python commits with test.py run(19, 15)
2526
curl -i \
2627
-H "Content-Type: application/json" \
2728
-X POST \

sema_task.cpp

Lines changed: 0 additions & 16 deletions
This file was deleted.

ssvi_http_server.cpp renamed to ssvim_http_server.cpp

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
*/
66

77
#include "file_body.hpp"
8-
#include "mime_type.hpp"
9-
#include "ssvi_http_server.hpp"
10-
#include "sema_task.cpp"
8+
#include "ssvim_http_server.hpp"
119

1210
#include "SwiftCompleter.h"
1311

@@ -45,7 +43,9 @@ struct service_context {
4543
log_level logging;
4644
};
4745

48-
namespace beast { // FIXME: ssvi
46+
using namespace beast::http;
47+
48+
namespace ssvim {
4949
namespace http {
5050

5151
using socket_type = boost::asio::ip::tcp::socket;
@@ -89,10 +89,11 @@ class endpoint_impl : public std::enable_shared_from_this<endpoint_impl>{
8989
endpoint_fn _start;
9090
};
9191

92-
using namespace ssvi;
92+
using namespace ssvim;
9393

9494
endpoint_impl make_slow_test_endpoint();
9595
endpoint_impl make_status_endpoint();
96+
endpoint_impl make_shutdown_endpoint();
9697
endpoint_impl make_completions_endpoint();
9798
endpoint_impl make_diagnostics_endpoint();
9899

@@ -141,6 +142,7 @@ class session : public std::enable_shared_from_this<session>
141142
};
142143

143144
insert_endpoint("/status", make_status_endpoint());
145+
insert_endpoint("/shutdown", make_shutdown_endpoint());
144146
insert_endpoint("/completions", make_completions_endpoint());
145147
insert_endpoint("/diagnostics", make_diagnostics_endpoint());
146148
insert_endpoint("/slow_test", make_slow_test_endpoint());
@@ -270,8 +272,26 @@ endpoint_impl make_status_endpoint() {
270272
res.status = 200;
271273
res.version = session->request().version;
272274
res.fields.insert("Server", "ssvi_http_server");
273-
res.fields.insert("Content-Type", "text/html");
275+
res.fields.insert("Content-Type", "application/json");
276+
prepare(res);
277+
session->write(res);
278+
});
279+
}
280+
281+
endpoint_impl make_shutdown_endpoint() {
282+
return endpoint_impl([&](std::shared_ptr<session> session){
283+
std::cout << "Recieved Shutdown Request";
284+
response<string_body> res;
285+
res.status = 200;
286+
res.version = session->request().version;
287+
res.fields.insert("Server", "ssvi_http_server");
288+
res.fields.insert("Content-Type", "application/json");
274289
prepare(res);
290+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC),
291+
dispatch_get_main_queue(), ^{
292+
std::cout << "Shutting down...";
293+
exit(0);
294+
});
275295
session->write(res);
276296
});
277297
}
@@ -349,7 +369,7 @@ endpoint_impl make_completions_endpoint() {
349369
res.status = 200;
350370
res.version = session->request().version;
351371
res.fields.insert("Server", "ssvi_http_server");
352-
res.fields.insert("Content-Type", "text/html");
372+
res.fields.insert("Content-Type", "application/json");
353373
res.body = candidates;
354374
prepare(res);
355375
session->write(res);
@@ -403,7 +423,7 @@ endpoint_impl make_diagnostics_endpoint() {
403423
res.status = 200;
404424
res.version = session->request().version;
405425
res.fields.insert("Server", "ssvi_http_server");
406-
res.fields.insert("Content-Type", "text/html");
426+
res.fields.insert("Content-Type", "application/json");
407427
res.body = diagnostics;
408428
prepare(res);
409429
session->write(res);
@@ -414,7 +434,8 @@ endpoint_impl make_slow_test_endpoint()
414434
{
415435
return endpoint_impl([](std::shared_ptr<session> session){
416436
// Wait for 10 seconds to write hello world.
417-
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 10 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
437+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 10 * NSEC_PER_SEC),
438+
dispatch_get_main_queue(), ^{
418439
std::cout << "Enter main: " << session->instance_id() << "\n";
419440
std::cout << session->request().url;
420441
std::cout.flush();
@@ -423,7 +444,7 @@ endpoint_impl make_slow_test_endpoint()
423444
res.status = 200;
424445
res.version = session->request().version;
425446
res.fields.insert("Server", "ssvi_http_server");
426-
res.fields.insert("Content-Type", "text/html");
447+
res.fields.insert("Content-Type", "application/json");
427448
res.body = "Hello World";
428449
prepare(res);
429450
session->write(res);
@@ -438,7 +459,7 @@ error_response(req_type request, std::string message){
438459
res.reason = "Internal Error";
439460
res.version = request.version;
440461
res.fields.insert("Server", "http_async_server");
441-
res.fields.insert("Content-Type", "text/html");
462+
res.fields.insert("Content-Type", "application/json");
442463
res.body =
443464
std::string{"An internal error occurred"} + message;
444465
prepare(res);
@@ -452,12 +473,12 @@ not_found_response(req_type request){
452473
res.reason = "Not Found";
453474
res.version = request.version;
454475
res.fields.insert("Server", "http_async_server");
455-
res.fields.insert("Content-Type", "text/html");
476+
res.fields.insert("Content-Type", "application/json");
456477
res.body = "Endpoint: '" + request.url + "' not found";
457478
prepare(res);
458479
return res;
459480
}
460481

461482
} // http
462-
} // beast
483+
} // ssvim
463484

ssvi_http_server.hpp renamed to ssvim_http_server.hpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33
*
44
* Note: Assume that we are calling dispatch_main() after running the server
55
*/
6-
7-
#include "file_body.hpp"
8-
#include "mime_type.hpp"
9-
106
#include <beast/http.hpp>
117
#include <beast/core/handler_helpers.hpp>
128
#include <beast/core/handler_ptr.hpp>
@@ -22,10 +18,12 @@
2218
#include <thread>
2319
#include <utility>
2420
#include <sstream>
25-
#include <map>
2621

27-
namespace beast {
22+
namespace ssvim {
2823
namespace http {
24+
25+
using namespace beast;
26+
using namespace beast::http;
2927

3028
/**
3129
* SSVI HTTP Server is a HTTP front end for Swift Semantic
@@ -201,5 +199,5 @@ class ssvi_http_server
201199
};
202200

203201
} // http
204-
} // beast
202+
} // ssvim
205203

0 commit comments

Comments
 (0)