1 //===- MLIRServer.h - MLIR 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_MLIRLSPSERVER_SERVER_H_
10 #define LIB_MLIR_TOOLS_MLIRLSPSERVER_SERVER_H_
11 
12 #include "mlir/Support/LLVM.h"
13 #include <memory>
14 
15 namespace mlir {
16 class DialectRegistry;
17 
18 namespace lsp {
19 struct CodeAction;
20 struct CodeActionContext;
21 struct CompletionList;
22 struct Diagnostic;
23 struct DocumentSymbol;
24 struct Hover;
25 struct Location;
26 struct Position;
27 struct Range;
28 class URIForFile;
29 
30 /// This class implements all of the MLIR related functionality necessary for a
31 /// language server. This class allows for keeping the MLIR specific logic
32 /// separate from the logic that involves LSP server/client communication.
33 class MLIRServer {
34 public:
35   /// Construct a new server with the given dialect regitstry.
36   MLIRServer(DialectRegistry &registry);
37   ~MLIRServer();
38 
39   /// Add or update the document, with the provided `version`, at the given URI.
40   /// Any diagnostics emitted for this document should be added to
41   /// `diagnostics`.
42   void addOrUpdateDocument(const URIForFile &uri, StringRef contents,
43                            int64_t version,
44                            std::vector<Diagnostic> &diagnostics);
45 
46   /// Remove the document with the given uri. Returns the version of the removed
47   /// document, or None if the uri did not have a corresponding document within
48   /// the server.
49   Optional<int64_t> removeDocument(const URIForFile &uri);
50 
51   /// Return the locations of the object pointed at by the given position.
52   void getLocationsOf(const URIForFile &uri, const Position &defPos,
53                       std::vector<Location> &locations);
54 
55   /// Find all references of the object pointed at by the given position.
56   void findReferencesOf(const URIForFile &uri, const Position &pos,
57                         std::vector<Location> &references);
58 
59   /// Find a hover description for the given hover position, or None if one
60   /// couldn't be found.
61   Optional<Hover> findHover(const URIForFile &uri, const Position &hoverPos);
62 
63   /// Find all of the document symbols within the given file.
64   void findDocumentSymbols(const URIForFile &uri,
65                            std::vector<DocumentSymbol> &symbols);
66 
67   /// Get the code completion list for the position within the given file.
68   CompletionList getCodeCompletion(const URIForFile &uri,
69                                    const Position &completePos);
70 
71   /// Get the set of code actions within the file.
72   void getCodeActions(const URIForFile &uri, const Range &pos,
73                       const CodeActionContext &context,
74                       std::vector<CodeAction> &actions);
75 
76 private:
77   struct Impl;
78 
79   std::unique_ptr<Impl> impl;
80 };
81 
82 } // namespace lsp
83 } // namespace mlir
84 
85 #endif // LIB_MLIR_TOOLS_MLIRLSPSERVER_SERVER_H_
86