summaryrefslogtreecommitdiff
path: root/elpa/irony-20220110.849/server/src/Irony.h
blob: 66968f5894f902f5f227e0788f234d6ebdba0748 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**-*-C++-*-
 * \file
 * \author Guillaume Papin <guillaume.papin@epitech.eu>
 *
 * \brief irony-server "API" declarations.
 *
 * Contains the commands that the Emacs package relies on. These commands are
 * mostly wrappers around a subset of the features provided by libclang. Command
 * results are printed to \c std::cout as s-expr, in order to make it easy for
 * Emacs to consume.
 *
 * This file is distributed under the GNU General Public License. See
 * COPYING for details.
 */

#ifndef IRONY_MODE_SERVER_IRONY_H_
#define IRONY_MODE_SERVER_IRONY_H_

#include "TUManager.h"

#include "CompDBCache.h"
#include "Style.h"

#include <string>
#include <vector>

class Irony {
public:
  // use std::string over std::vector<char> because I have some doubts
  // that libclang expect unsaved buffers to be a null terminated C strings
  typedef std::string UnsavedBuffer;

public:
  Irony();

  bool isDebugEnabled() const {
    return debug_;
  }

  /// \name Command
  /// \{

  /// \brief Set or unset debugging of commands.
  void setDebug(bool enable) {
    debug_ = enable;
  }

  /// Parse or reparse the given file and compile options.
  ///
  /// If the compile options have changed, the translation unit is re-created to
  /// take this into account.
  ///
  /// Output \c nil or \c t, whether or not parsing the translation unit
  /// succeeded.
  ///
  /// \sa diagnostics(), getType()
  void parse(const std::string &file, const std::vector<std::string> &flags);

  /// Parse the given file for code completion.
  ///
  /// Shares the same semantics and output as \c parse().
  ///
  /// \sa candidates(), completionDiagnostics()
  void complete(const std::string &file,
                unsigned line,
                unsigned col,
                const std::vector<std::string> &flags);

  void setUnsaved(const std::string &file,
                  const std::string &unsavedContentFile);

  void resetUnsaved(const std::string &file);

  /// \}

  /// \name Queries
  /// \{

  /// \brief Retrieve the last parse diagnostics for the given file.
  ///
  /// \pre parse() was called.
  void diagnostics() const;

  /// \brief Get types of symbol at a given location.
  ///
  /// Example:
  ///
  /// \code
  ///   typedef int MyType;
  ///   MyType a;
  /// \endcode
  ///
  /// Type of cursor location for 'a' is:
  ///
  /// \code{.el}
  ///    ("MyType" "int")
  /// \endcode
  ///
  /// TODO: test with CXString(), seems to be twice the same string
  ///
  void getType(unsigned line, unsigned col) const;

  /// Get all the completion candidates.
  ///
  /// \pre complete() was called.
  void candidates(const std::string &prefix, PrefixMatchStyle style) const;

  /// Get the diagnostics produced by the last \c complete().
  ///
  /// \pre complete() was called.
  void completionDiagnostics() const;

  /// \brief Get compile options from JSON database.
  ///
  /// \param buildDir Directory containing compile_commands.json
  /// \param file File to obtain compile commands for.
  ///
  /// Example output:
  ///
  /// \code{.el}
  ///    (
  ///     (("-Wfoo" "-DBAR" "-Iqux") . "/path/to/working/directory")
  ///     (("-Wfoo-alt" "-DBAR_ALT" "-Iqux/alt") . "/alt/working/directory")
  ///    )
  /// \endcode
  ///
  void getCompileOptions(const std::string &buildDir,
                         const std::string &file) const;

  /// \}

private:
  void resetCache();
  void computeCxUnsaved();

private:
  mutable CompDBCache compDBCache_;
  TUManager tuManager_;
  std::map<std::string, UnsavedBuffer> filenameToContent_;
  CXTranslationUnit activeTu_;
  std::string file_;
  std::vector<CXUnsavedFile> cxUnsavedFiles_;
  CXCodeCompleteResults *activeCompletionResults_;
  bool debug_;
};

#endif // IRONY_MODE_SERVER_IRONY_H_