xref: /llvm-project/mlir/lib/Tools/tblgen-lsp-server/TableGenServer.h (revision db791b278a414fb6df1acc1799adcf11d8fb9169)
1 //===- TableGenServer.h - TableGen Language Server --------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LIB_MLIR_TOOLS_TBLGENLSPSERVER_TABLEGENSERVER_H_
10 #define LIB_MLIR_TOOLS_TBLGENLSPSERVER_TABLEGENSERVER_H_
11 
12 #include "mlir/Support/LLVM.h"
13 #include "llvm/ADT/StringRef.h"
14 #include <memory>
15 #include <optional>
16 #include <string>
17 #include <vector>
18 
19 namespace mlir {
20 namespace lsp {
21 struct Diagnostic;
22 struct DocumentLink;
23 struct Hover;
24 struct Location;
25 struct Position;
26 struct TextDocumentContentChangeEvent;
27 class URIForFile;
28 
29 /// This class implements all of the TableGen related functionality necessary
30 /// for a language server. This class allows for keeping the TableGen specific
31 /// logic separate from the logic that involves LSP server/client communication.
32 class TableGenServer {
33 public:
34   struct Options {
OptionsOptions35     Options(const std::vector<std::string> &compilationDatabases,
36             const std::vector<std::string> &extraDirs)
37         : compilationDatabases(compilationDatabases), extraDirs(extraDirs) {}
38 
39     /// The filenames for databases containing compilation commands for TableGen
40     /// files passed to the server.
41     const std::vector<std::string> &compilationDatabases;
42 
43     /// Additional list of include directories to search.
44     const std::vector<std::string> &extraDirs;
45   };
46 
47   TableGenServer(const Options &options);
48   ~TableGenServer();
49 
50   /// Add the document, with the provided `version`, at the given URI. Any
51   /// diagnostics emitted for this document should be added to `diagnostics`.
52   void addDocument(const URIForFile &uri, StringRef contents, int64_t version,
53                    std::vector<Diagnostic> &diagnostics);
54 
55   /// Update the document, with the provided `version`, at the given URI. Any
56   /// diagnostics emitted for this document should be added to `diagnostics`.
57   void updateDocument(const URIForFile &uri,
58                       ArrayRef<TextDocumentContentChangeEvent> changes,
59                       int64_t version, std::vector<Diagnostic> &diagnostics);
60 
61   /// Remove the document with the given uri. Returns the version of the removed
62   /// document, or std::nullopt if the uri did not have a corresponding document
63   /// within the server.
64   std::optional<int64_t> removeDocument(const URIForFile &uri);
65 
66   /// Return the locations of the object pointed at by the given position.
67   void getLocationsOf(const URIForFile &uri, const Position &defPos,
68                       std::vector<Location> &locations);
69 
70   /// Find all references of the object pointed at by the given position.
71   void findReferencesOf(const URIForFile &uri, const Position &pos,
72                         std::vector<Location> &references);
73 
74   /// Return the document links referenced by the given file.
75   void getDocumentLinks(const URIForFile &uri,
76                         std::vector<DocumentLink> &documentLinks);
77 
78   /// Find a hover description for the given hover position, or std::nullopt if
79   /// one couldn't be found.
80   std::optional<Hover> findHover(const URIForFile &uri,
81                                  const Position &hoverPos);
82 
83 private:
84   struct Impl;
85   std::unique_ptr<Impl> impl;
86 };
87 
88 } // namespace lsp
89 } // namespace mlir
90 
91 #endif // LIB_MLIR_TOOLS_TBLGENLSPSERVER_TABLEGENSERVER_H_
92