summaryrefslogtreecommitdiffstats
path: root/source/slang/slang-language-server.h
blob: 397a93b268bfaab83b0d6be98db377aa5cf684d0 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#pragma once
#include "../compiler-core/slang-json-rpc-connection.h"
#include "../compiler-core/slang-json-rpc.h"
#include "../core/slang-range.h"
#include "slang-language-server-auto-format.h"
#include "slang-language-server-completion.h"
#include "slang-language-server-inlay-hints.h"
#include "slang-workspace-version.h"
#include "slang.h"

#include <chrono>

namespace Slang
{
ArrayView<const char*> getCommitChars();

struct Command
{
    PersistentJSONValue id;
    String method;

    template<typename T>
    struct Optional
    {
    public:
        T* value = nullptr;
        bool isValid() { return value != nullptr; }
        T& operator=(const T& val)
        {
            delete value;
            value = new T(val);
            return *value;
        }
        T& operator=(Optional&& other)
        {
            if (other.isValid())
                *this = (other.get());
            other.value = nullptr;
            return *value;
        }
        T& get()
        {
            SLANG_ASSERT(isValid());
            return *value;
        }
        Optional() = default;
        Optional(const Optional& other)
        {
            if (other.isValid())
                *this = (other.get());
        }
        Optional(Optional&& other)
        {
            if (other.isValid())
                *this = (other.get());
            other.value = nullptr;
        }

        ~Optional() { delete value; }
    };

    Optional<LanguageServerProtocol::CompletionParams> completionArgs;
    Optional<LanguageServerProtocol::CompletionItem> completionResolveArgs;
    Optional<LanguageServerProtocol::TextEditCompletionItem> textEditCompletionResolveArgs;
    Optional<LanguageServerProtocol::DocumentSymbolParams> documentSymbolArgs;
    Optional<LanguageServerProtocol::InlayHintParams> inlayHintArgs;
    Optional<LanguageServerProtocol::DocumentFormattingParams> formattingArgs;
    Optional<LanguageServerProtocol::DocumentRangeFormattingParams> rangeFormattingArgs;
    Optional<LanguageServerProtocol::DocumentOnTypeFormattingParams> onTypeFormattingArgs;
    Optional<LanguageServerProtocol::DidChangeConfigurationParams> changeConfigArgs;
    Optional<LanguageServerProtocol::SignatureHelpParams> signatureHelpArgs;
    Optional<LanguageServerProtocol::DefinitionParams> definitionArgs;
    Optional<LanguageServerProtocol::SemanticTokensParams> semanticTokenArgs;
    Optional<LanguageServerProtocol::HoverParams> hoverArgs;
    Optional<LanguageServerProtocol::DidOpenTextDocumentParams> openDocArgs;
    Optional<LanguageServerProtocol::DidChangeTextDocumentParams> changeDocArgs;
    Optional<LanguageServerProtocol::DidCloseTextDocumentParams> closeDocArgs;
    Optional<LanguageServerProtocol::CancelParams> cancelArgs;
};

struct LanguageServerStartupOptions
{
    // Are we working with Visual Studio client?
    bool isVisualStudio = false;

    // A flag to control periodic diagnostic update. Defaults to true.
    bool periodicDiagnosticUpdate = true;

    SLANG_API void parse(int argc, const char* const* argv);
};

class LanguageServerCore
{
public:
    enum class TraceOptions
    {
        Off,
        Messages,
        Verbose
    };
    CommitCharacterBehavior m_commitCharacterBehavior = CommitCharacterBehavior::MembersOnly;
    ComPtr<slang::IGlobalSession> m_session;
    RefPtr<Workspace> m_workspace;
    FormatOptions m_formatOptions;
    Slang::InlayHintOptions m_inlayHintOptions;
    List<LanguageServerProtocol::WorkspaceFolder> m_workspaceFolders;
    LanguageServerStartupOptions m_options;

    LanguageServerCore(LanguageServerStartupOptions options)
        : m_options(options)
    {
    }

    SlangResult init(const LanguageServerProtocol::InitializeParams& args);
    SlangResult didOpenTextDocument(const LanguageServerProtocol::DidOpenTextDocumentParams& args);
    SlangResult didCloseTextDocument(
        const LanguageServerProtocol::DidCloseTextDocumentParams& args);
    SlangResult didChangeTextDocument(
        const LanguageServerProtocol::DidChangeTextDocumentParams& args);
    LanguageServerResult<LanguageServerProtocol::Hover> hover(
        const LanguageServerProtocol::HoverParams& args);
    LanguageServerResult<List<LanguageServerProtocol::Location>> gotoDefinition(
        const LanguageServerProtocol::DefinitionParams& args);

    LanguageServerResult<CompletionResult> completion(
        const LanguageServerProtocol::CompletionParams& args);
    LanguageServerResult<LanguageServerProtocol::CompletionItem> completionResolve(
        const LanguageServerProtocol::CompletionItem& args,
        const LanguageServerProtocol::TextEditCompletionItem& editItem);
    LanguageServerResult<LanguageServerProtocol::SemanticTokens> semanticTokens(
        const LanguageServerProtocol::SemanticTokensParams& args);
    LanguageServerResult<LanguageServerProtocol::SignatureHelp> signatureHelp(
        const LanguageServerProtocol::SignatureHelpParams& args);
    LanguageServerResult<List<LanguageServerProtocol::DocumentSymbol>> documentSymbol(
        const LanguageServerProtocol::DocumentSymbolParams& args);
    LanguageServerResult<List<LanguageServerProtocol::InlayHint>> inlayHint(
        const LanguageServerProtocol::InlayHintParams& args);
    LanguageServerResult<List<LanguageServerProtocol::TextEdit>> formatting(
        const LanguageServerProtocol::DocumentFormattingParams& args);
    LanguageServerResult<List<LanguageServerProtocol::TextEdit>> rangeFormatting(
        const LanguageServerProtocol::DocumentRangeFormattingParams& args);
    LanguageServerResult<List<LanguageServerProtocol::TextEdit>> onTypeFormatting(
        const LanguageServerProtocol::DocumentOnTypeFormattingParams& args);
    String getExprDeclSignature(
        Expr* expr,
        String* outDocumentation,
        List<Slang::Range<Index>>* outParamRanges);
    String getDeclRefSignature(
        DeclRef<Decl> declRef,
        String* outDocumentation,
        List<Slang::Range<Index>>* outParamRanges);

private:
    slang::IGlobalSession* getOrCreateGlobalSession();

    FormatOptions getFormatOptions(Workspace* workspace, FormatOptions inOptions);
    LanguageServerResult<LanguageServerProtocol::Hover> tryGetMacroHoverInfo(
        WorkspaceVersion* version,
        DocumentVersion* doc,
        Index line,
        Index col);
    LanguageServerResult<List<LanguageServerProtocol::Location>> tryGotoMacroDefinition(
        WorkspaceVersion* version,
        DocumentVersion* doc,
        Index line,
        Index col);
    LanguageServerResult<List<LanguageServerProtocol::Location>> tryGotoFileInclude(
        WorkspaceVersion* version,
        DocumentVersion* doc,
        Index line);
};

class LanguageServer
{
private:
    static const int kConfigResponseId = 0x1213;

public:
    enum class TraceOptions
    {
        Off,
        Messages,
        Verbose
    };

    bool m_quit = false;
    LanguageServerCore m_core;
    RefPtr<JSONRPCConnection> m_connection;
    RttiTypeFuncsMap m_typeMap;
    bool m_initialized = false;
    TraceOptions m_traceOptions = TraceOptions::Off;
    std::chrono::time_point<std::chrono::system_clock> m_lastDiagnosticUpdateTime;
    Dictionary<String, String> m_lastPublishedDiagnostics;
    HashSet<String> m_pendingModulesToUpdateDiagnostics;

    void removePendingModuleToUpdateDiagnostics(const String& uri);

    LanguageServer(LanguageServerStartupOptions options)
        : m_core(options)
    {
    }

    SlangResult init(const LanguageServerProtocol::InitializeParams& args);
    SlangResult execute();
    void update();
    void updateConfigFromJSON(const JSONValue& jsonVal);
    SlangResult didOpenTextDocument(const LanguageServerProtocol::DidOpenTextDocumentParams& args);
    SlangResult didCloseTextDocument(
        const LanguageServerProtocol::DidCloseTextDocumentParams& args);
    SlangResult didChangeTextDocument(
        const LanguageServerProtocol::DidChangeTextDocumentParams& args);
    SlangResult didChangeConfiguration(
        const LanguageServerProtocol::DidChangeConfigurationParams& args);
    SlangResult hover(const LanguageServerProtocol::HoverParams& args, const JSONValue& responseId);
    SlangResult gotoDefinition(
        const LanguageServerProtocol::DefinitionParams& args,
        const JSONValue& responseId);
    SlangResult completion(
        const LanguageServerProtocol::CompletionParams& args,
        const JSONValue& responseId);
    SlangResult completionResolve(
        const LanguageServerProtocol::CompletionItem& args,
        const LanguageServerProtocol::TextEditCompletionItem& editItem,
        const JSONValue& responseId);
    SlangResult semanticTokens(
        const LanguageServerProtocol::SemanticTokensParams& args,
        const JSONValue& responseId);
    SlangResult signatureHelp(
        const LanguageServerProtocol::SignatureHelpParams& args,
        const JSONValue& responseId);
    SlangResult documentSymbol(
        const LanguageServerProtocol::DocumentSymbolParams& args,
        const JSONValue& responseId);
    SlangResult inlayHint(
        const LanguageServerProtocol::InlayHintParams& args,
        const JSONValue& responseId);
    SlangResult formatting(
        const LanguageServerProtocol::DocumentFormattingParams& args,
        const JSONValue& responseId);
    SlangResult rangeFormatting(
        const LanguageServerProtocol::DocumentRangeFormattingParams& args,
        const JSONValue& responseId);
    SlangResult onTypeFormatting(
        const LanguageServerProtocol::DocumentOnTypeFormattingParams& args,
        const JSONValue& responseId);

private:
    SlangResult parseNextMessage();
    void resetDiagnosticUpdateTime();
    void publishDiagnostics();
    void updatePredefinedMacros(const JSONValue& macros);
    void updateSearchPaths(const JSONValue& value);
    void updateSearchInWorkspace(const JSONValue& value);
    void updateCommitCharacters(const JSONValue& value);
    void updateFormattingOptions(
        const JSONValue& enableFormatOnType,
        const JSONValue& clangFormatLoc,
        const JSONValue& clangFormatStyle,
        const JSONValue& clangFormatFallbackStyle,
        const JSONValue& allowLineBreakOnType,
        const JSONValue& allowLineBreakInRange);
    void updateInlayHintOptions(const JSONValue& deducedTypes, const JSONValue& parameterNames);
    void updateTraceOptions(const JSONValue& value);

    void sendConfigRequest();
    void registerCapability(const char* methodName);
    void logMessage(int type, String message);

    List<Command> commands;
    SlangResult queueJSONCall(JSONRPCCall call);
    SlangResult runCommand(Command& cmd);
    void processCommands();
};

inline bool _isIdentifierChar(char ch)
{
    return ch >= '0' && ch <= '9' || ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' || ch == '_';
}

SLANG_API SlangResult runLanguageServer(LanguageServerStartupOptions options);
SLANG_API SlangResult
getBuiltinModuleSource(const UnownedStringSlice& moduleName, slang::IBlob** blob);

} // namespace Slang