1 //===- PDLLServer.h - PDL General 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_MLIRPDLLSPSERVER_SERVER_H_
10 #define LIB_MLIR_TOOLS_MLIRPDLLSPSERVER_SERVER_H_
11 
12 #include "mlir/Support/LLVM.h"
13 #include "llvm/ADT/StringRef.h"
14 #include <memory>
15 #include <string>
16 
17 namespace mlir {
18 namespace lsp {
19 struct Diagnostic;
20 class CompilationDatabase;
21 struct PDLLViewOutputResult;
22 enum class PDLLViewOutputKind;
23 struct CompletionList;
24 struct DocumentLink;
25 struct DocumentSymbol;
26 struct Hover;
27 struct InlayHint;
28 struct Location;
29 struct Position;
30 struct Range;
31 struct SignatureHelp;
32 struct TextDocumentContentChangeEvent;
33 class URIForFile;
34 
35 /// This class implements all of the PDLL related functionality necessary for a
36 /// language server. This class allows for keeping the PDLL specific logic
37 /// separate from the logic that involves LSP server/client communication.
38 class PDLLServer {
39 public:
40   struct Options {
OptionsOptions41     Options(const std::vector<std::string> &compilationDatabases,
42             const std::vector<std::string> &extraDirs)
43         : compilationDatabases(compilationDatabases), extraDirs(extraDirs) {}
44 
45     /// The filenames for databases containing compilation commands for PDLL
46     /// files passed to the server.
47     const std::vector<std::string> &compilationDatabases;
48 
49     /// Additional list of include directories to search.
50     const std::vector<std::string> &extraDirs;
51   };
52 
53   PDLLServer(const Options &options);
54   ~PDLLServer();
55 
56   /// Add the document, with the provided `version`, at the given URI. Any
57   /// diagnostics emitted for this document should be added to `diagnostics`.
58   void addDocument(const URIForFile &uri, StringRef contents, int64_t version,
59                    std::vector<Diagnostic> &diagnostics);
60 
61   /// Update the document, with the provided `version`, at the given URI. Any
62   /// diagnostics emitted for this document should be added to `diagnostics`.
63   void updateDocument(const URIForFile &uri,
64                       ArrayRef<TextDocumentContentChangeEvent> changes,
65                       int64_t version, std::vector<Diagnostic> &diagnostics);
66 
67   /// Remove the document with the given uri. Returns the version of the removed
68   /// document, or None if the uri did not have a corresponding document within
69   /// the server.
70   Optional<int64_t> removeDocument(const URIForFile &uri);
71 
72   /// Return the locations of the object pointed at by the given position.
73   void getLocationsOf(const URIForFile &uri, const Position &defPos,
74                       std::vector<Location> &locations);
75 
76   /// Find all references of the object pointed at by the given position.
77   void findReferencesOf(const URIForFile &uri, const Position &pos,
78                         std::vector<Location> &references);
79 
80   /// Return the document links referenced by the given file.
81   void getDocumentLinks(const URIForFile &uri,
82                         std::vector<DocumentLink> &documentLinks);
83 
84   /// Find a hover description for the given hover position, or None if one
85   /// couldn't be found.
86   Optional<Hover> findHover(const URIForFile &uri, const Position &hoverPos);
87 
88   /// Find all of the document symbols within the given file.
89   void findDocumentSymbols(const URIForFile &uri,
90                            std::vector<DocumentSymbol> &symbols);
91 
92   /// Get the code completion list for the position within the given file.
93   CompletionList getCodeCompletion(const URIForFile &uri,
94                                    const Position &completePos);
95 
96   /// Get the signature help for the position within the given file.
97   SignatureHelp getSignatureHelp(const URIForFile &uri,
98                                  const Position &helpPos);
99 
100   /// Get the inlay hints for the range within the given file.
101   void getInlayHints(const URIForFile &uri, const Range &range,
102                      std::vector<InlayHint> &inlayHints);
103 
104   /// Get the output of the given PDLL file, or None if there is no valid
105   /// output.
106   Optional<PDLLViewOutputResult> getPDLLViewOutput(const URIForFile &uri,
107                                                    PDLLViewOutputKind kind);
108 
109 private:
110   struct Impl;
111   std::unique_ptr<Impl> impl;
112 };
113 
114 } // namespace lsp
115 } // namespace mlir
116 
117 #endif // LIB_MLIR_TOOLS_MLIRPDLLSPSERVER_SERVER_H_
118