Ignore:
Timestamp:
Apr 6, 2020, 1:28:00 PM (4 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
057298e
Parents:
b52abe0
Message:

Added basic language server which now properly communicates with the client

Location:
tools/langserver/src
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • tools/langserver/src/json.hpp

    rb52abe0 rebe0f0d  
    44
    55namespace json {
    6       auto parse(const std::vector<char> & buff) {
     6      static inline auto parse(const std::vector<char> & buff) {
    77            return nlohmann::json::parse(buff.begin(), buff.end());
    88      }
  • tools/langserver/src/main.cpp

    rb52abe0 rebe0f0d  
     1#include <cstdio>
     2
    13#include <iostream>
    24#include <string>
    35#include <vector>
    46
     7#include <unistd.h>
     8
    59#include "json.hpp"
    610#include "log.hpp"
     11#include "server.hpp"
    712
    813static void request(std::vector<char> & buf);
     14static void reply(const json::obj & );
    915
    10 namespace server {
    11         json::obj init(const json::obj & ) {
    12                 return {
    13                         { "capabilities", json::obj::array() },
    14                         { "serverInfo", {
    15                                 { "name", "Cforall Language Server" },
    16                                 { "version", "0.1" }
    17                         }}
    18                 };
    19         }
     16json::obj noop( const json::obj & ) {
     17        return {};
    2018}
    2119
    2220std::unordered_map<std::string, json::obj (*)( const json::obj & )> actions {
    23         { "initialize", server::init }
     21        { "initialize", server::init },
     22        { "initialized", noop }
    2423};
    2524
     
    3231                log() << "\t- " << argv[i] << std::endl;
    3332        }
     33
     34        // std::cout.setf(std::ios::unitbuf);
     35
     36        // char buff[1000];
     37        // int r = 0;
     38        // while(0 != (r = read(STDIN_FILENO, buff, 1000))) {
     39        //      for(int i = 0; i < r; i++) {
     40        //              log() << buff[i];
     41        //      }
     42        // }
     43        // log() << std::endl;
     44
     45        // return 0;
    3446
    3547        std::vector<char> buffer;
     
    5365                                response["error"] = error;
    5466                        }
    55                         log() << "Responding with : " << response.dump(4) << std::endl;
    56                         std::cout << response.dump() << std::endl;
     67
     68                        reply(response);
    5769                }
    5870                else {
     
    8698        buffer.resize( len + 2 );
    8799        std::cin.read(buffer.data(), buffer.size());
     100
     101        for(char c : buffer) {
     102                log() << c;
     103        }
     104        log() << std::endl;
    88105        return;
    89106}
     107
     108static void reply( const json::obj & response) {
     109        if(response["result"].is_null()) {
     110                log() << "No response needed" << std::endl;
     111                return;
     112        }
     113
     114        log() << "Responding with : " << response.dump(4) << std::endl;
     115        const std::string & r = response.dump();
     116        std::cout << "Content-Length: " << r.length() << "\r\n\r\n";
     117        std::cout.flush();
     118        std::cout  << r << "\r\n";
     119        std::cout.flush();
     120}
  • tools/langserver/src/server.hpp

    rb52abe0 rebe0f0d  
     1#pragma once
     2
     3#include "json.hpp"
     4
     5namespace server {
     6        json::obj init(const json::obj & );
     7};
Note: See TracChangeset for help on using the changeset viewer.