summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/clangd/ProtocolHandlers.h
blob: 2805ba46cf25c1541b109ae1683df839d645511e (plain)
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
//===--- ProtocolHandlers.h - LSP callbacks ---------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file contains the actions performed when the server gets a specific
// request.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_PROTOCOLHANDLERS_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_PROTOCOLHANDLERS_H

#include "JSONRPCDispatcher.h"
#include "Protocol.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Support/raw_ostream.h"

namespace clang {
namespace clangd {
class DocumentStore;

struct InitializeHandler : Handler {
  InitializeHandler(llvm::raw_ostream &Outs, llvm::raw_ostream &Logs)
      : Handler(Outs, Logs) {}

  void handleMethod(llvm::yaml::MappingNode *Params, StringRef ID) override {
    writeMessage(
        R"({"jsonrpc":"2.0","id":)" + ID +
        R"(,"result":{"capabilities":{
          "textDocumentSync": 1,
          "documentFormattingProvider": true,
          "documentRangeFormattingProvider": true
        }}})");
  }
};

struct ShutdownHandler : Handler {
  ShutdownHandler(llvm::raw_ostream &Outs, llvm::raw_ostream &Logs)
      : Handler(Outs, Logs) {}

  void handleMethod(llvm::yaml::MappingNode *Params, StringRef ID) override {
    // FIXME: Calling exit is rude, can we communicate to main somehow?
    exit(0);
  }
};

struct TextDocumentDidOpenHandler : Handler {
  TextDocumentDidOpenHandler(llvm::raw_ostream &Outs, llvm::raw_ostream &Logs,
                             DocumentStore &Store)
      : Handler(Outs, Logs), Store(Store) {}

  void handleNotification(llvm::yaml::MappingNode *Params) override;

private:
  DocumentStore &Store;
};

struct TextDocumentDidChangeHandler : Handler {
  TextDocumentDidChangeHandler(llvm::raw_ostream &Outs, llvm::raw_ostream &Logs,
                               DocumentStore &Store)
      : Handler(Outs, Logs), Store(Store) {}

  void handleNotification(llvm::yaml::MappingNode *Params) override;

private:
  DocumentStore &Store;
};

struct TextDocumentRangeFormattingHandler : Handler {
  TextDocumentRangeFormattingHandler(llvm::raw_ostream &Outs,
                                     llvm::raw_ostream &Logs,
                                     DocumentStore &Store)
      : Handler(Outs, Logs), Store(Store) {}

  void handleMethod(llvm::yaml::MappingNode *Params, StringRef ID) override;

private:
  DocumentStore &Store;
};

struct TextDocumentFormattingHandler : Handler {
  TextDocumentFormattingHandler(llvm::raw_ostream &Outs,
                                llvm::raw_ostream &Logs, DocumentStore &Store)
      : Handler(Outs, Logs), Store(Store) {}

  void handleMethod(llvm::yaml::MappingNode *Params, StringRef ID) override;

private:
  DocumentStore &Store;
};

} // namespace clangd
} // namespace clang

#endif
OpenPOWER on IntegriCloud