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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
|
// language-server.cpp
// This file implements the language server for Slang, conforming to the Language Server Protocol.
// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <thread>
#include "../core/slang-secure-crt.h"
#include "../core/slang-range.h"
#include "../../slang-com-helper.h"
#include "../compiler-core/slang-json-rpc-connection.h"
#include "../compiler-core/slang-language-server-protocol.h"
#include "slang-language-server.h"
#include "slang-workspace-version.h"
#include "slang-language-server-ast-lookup.h"
#include "slang-language-server-collect-member.h"
#include "slang-language-server-semantic-tokens.h"
#include "slang-ast-print.h"
#include "slang-doc-markdown-writer.h"
namespace Slang
{
using namespace LanguageServerProtocol;
class LanguageServer
{
public:
RefPtr<JSONRPCConnection> m_connection;
ComPtr<slang::IGlobalSession> m_session;
RefPtr<Workspace> m_workspace;
Dictionary<String, String> m_lastPublishedDiagnostics;
time_t m_lastDiagnosticUpdateTime = 0;
bool m_quit = false;
List<LanguageServerProtocol::WorkspaceFolder> m_workspaceFolders;
SlangResult init(const LanguageServerProtocol::InitializeParams& args);
SlangResult execute();
void update();
SlangResult didOpenTextDocument(const LanguageServerProtocol::DidOpenTextDocumentParams& args);
SlangResult didCloseTextDocument(
const LanguageServerProtocol::DidCloseTextDocumentParams& args);
SlangResult didChangeTextDocument(
const LanguageServerProtocol::DidChangeTextDocumentParams& 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 JSONValue& responseId);
SlangResult semanticTokens(
const LanguageServerProtocol::SemanticTokensParams& args, const JSONValue& responseId);
SlangResult signatureHelp(
const LanguageServerProtocol::SignatureHelpParams& args, const JSONValue& responseId);
List<LanguageServerProtocol::CompletionItem> collectMembers(
WorkspaceVersion* wsVersion, Module* module, Expr* baseExpr);
private:
SlangResult _executeSingle();
slang::IGlobalSession* getOrCreateGlobalSession();
void resetDiagnosticUpdateTime();
void publishDiagnostics();
};
SlangResult LanguageServer::init(const InitializeParams& args)
{
SLANG_RETURN_ON_FAIL(m_connection->initWithStdStreams(JSONRPCConnection::CallStyle::Object));
m_workspaceFolders = args.workspaceFolders;
m_workspace = new Workspace();
List<URI> rootUris;
for (auto& wd : m_workspaceFolders)
{
rootUris.add(URI::fromString(wd.uri.getUnownedSlice()));
}
m_workspace->init(rootUris, getOrCreateGlobalSession());
return SLANG_OK;
}
slang::IGlobalSession* LanguageServer::getOrCreateGlobalSession()
{
if (!m_session)
{
// Just create the global session in the regular way if there isn't one set
if (SLANG_FAILED(slang_createGlobalSession(SLANG_API_VERSION, m_session.writeRef())))
{
return nullptr;
}
}
return m_session;
}
void LanguageServer::resetDiagnosticUpdateTime() { time(&m_lastDiagnosticUpdateTime); }
String uriToCanonicalPath(const String& uri)
{
String canonnicalPath;
Path::getCanonical(URI::fromString(uri.getUnownedSlice()).getPath(), canonnicalPath);
return canonnicalPath;
}
SlangResult LanguageServer::_executeSingle()
{
// If we don't have a message, we can quit for now
if (!m_connection->hasMessage())
{
return SLANG_OK;
}
const JSONRPCMessageType msgType = m_connection->getMessageType();
switch (msgType)
{
case JSONRPCMessageType::Call:
{
JSONRPCCall call;
SLANG_RETURN_ON_FAIL(m_connection->getRPCOrSendError(&call));
// Do different things
if (call.method == ExitParams::methodName)
{
m_quit = true;
return SLANG_OK;
}
else if (call.method == ShutdownParams::methodName)
{
m_connection->sendResult(NullResponse::get(), call.id);
return SLANG_OK;
}
else if (call.method == InitializeParams::methodName)
{
InitializeParams args = {};
m_connection->toNativeArgsOrSendError(call.params, &args, call.id);
init(args);
InitializeResult result = {};
result.serverInfo.name = "SlangLanguageServer";
result.serverInfo.version = "1.0";
result.capabilities.positionEncoding = "utf-8";
result.capabilities.textDocumentSync.openClose = true;
result.capabilities.textDocumentSync.change = (int)TextDocumentSyncKind::Full;
result.capabilities.hoverProvider = true;
result.capabilities.definitionProvider = true;
const char* commitChars[] = {",", ".", ";", ":", "(", ")", "[", "]",
"<", ">", "{", "}", "*", "&", "^", "%",
"!", "-", "=", "+", "|", "/", "?"};
for (auto ch : commitChars)
result.capabilities.completionProvider.allCommitCharacters.add(ch);
result.capabilities.completionProvider.triggerCharacters.add(".");
result.capabilities.completionProvider.triggerCharacters.add(":");
result.capabilities.completionProvider.resolveProvider = true;
result.capabilities.completionProvider.workDoneToken = "";
result.capabilities.semanticTokensProvider.full = true;
result.capabilities.semanticTokensProvider.range = false;
result.capabilities.signatureHelpProvider.triggerCharacters.add("(");
result.capabilities.signatureHelpProvider.retriggerCharacters.add(",");
for (auto tokenType : kSemanticTokenTypes)
result.capabilities.semanticTokensProvider.legend.tokenTypes.add(tokenType);
m_connection->sendResult(&result, call.id);
return SLANG_OK;
}
else if (call.method == DidOpenTextDocumentParams::methodName)
{
DidOpenTextDocumentParams args;
SLANG_RETURN_ON_FAIL(
m_connection->toNativeArgsOrSendError(call.params, &args, call.id));
return didOpenTextDocument(args);
}
else if (call.method == DidCloseTextDocumentParams::methodName)
{
DidCloseTextDocumentParams args;
SLANG_RETURN_ON_FAIL(
m_connection->toNativeArgsOrSendError(call.params, &args, call.id));
return didCloseTextDocument(args);
}
else if (call.method == DidChangeTextDocumentParams::methodName)
{
DidChangeTextDocumentParams args;
SLANG_RETURN_ON_FAIL(
m_connection->toNativeArgsOrSendError(call.params, &args, call.id));
return didChangeTextDocument(args);
}
else if (call.method == HoverParams::methodName)
{
HoverParams args;
SLANG_RETURN_ON_FAIL(
m_connection->toNativeArgsOrSendError(call.params, &args, call.id));
return hover(args, call.id);
}
else if (call.method == DefinitionParams::methodName)
{
DefinitionParams args;
SLANG_RETURN_ON_FAIL(
m_connection->toNativeArgsOrSendError(call.params, &args, call.id));
return gotoDefinition(args, call.id);
}
else if (call.method == CompletionParams::methodName)
{
CompletionParams args;
SLANG_RETURN_ON_FAIL(
m_connection->toNativeArgsOrSendError(call.params, &args, call.id));
return completion(args, call.id);
}
else if (call.method == SemanticTokensParams::methodName)
{
SemanticTokensParams args;
SLANG_RETURN_ON_FAIL(
m_connection->toNativeArgsOrSendError(call.params, &args, call.id));
return semanticTokens(args, call.id);
}
else if (call.method == SignatureHelpParams::methodName)
{
SignatureHelpParams args;
SLANG_RETURN_ON_FAIL(
m_connection->toNativeArgsOrSendError(call.params, &args, call.id));
return signatureHelp(args, call.id);
}
else if (call.method == "completionItem/resolve")
{
CompletionItem args;
SLANG_RETURN_ON_FAIL(
m_connection->toNativeArgsOrSendError(call.params, &args, call.id));
return completionResolve(args, call.id);
}
else if (call.method == "initialized")
{
return SLANG_OK;
}
else if (call.method.startsWith("$/"))
{
// Ignore.
return SLANG_OK;
}
else
{
return m_connection->sendError(JSONRPC::ErrorCode::MethodNotFound, call.id);
}
}
default:
{
return m_connection->sendError(
JSONRPC::ErrorCode::InvalidRequest, m_connection->getCurrentMessageId());
}
}
}
SlangResult LanguageServer::didOpenTextDocument(const DidOpenTextDocumentParams& args)
{
String canonicalPath = uriToCanonicalPath(args.textDocument.uri);
m_workspace->openDoc(canonicalPath, args.textDocument.text);
return SLANG_OK;
}
String getDeclSignatureString(DeclRef<Decl> declRef, ASTBuilder* astBuilder)
{
if (declRef.getDecl())
{
ASTPrinter printer(
astBuilder,
ASTPrinter::OptionFlag::ParamNames | ASTPrinter::OptionFlag::NoInternalKeywords |
ASTPrinter::OptionFlag::SimplifiedBuiltinType);
printer.addDeclSignature(declRef);
return printer.getString();
}
return "unknown";
}
static String _formatDocumentation(String doc)
{
// TODO: may want to use DocMarkdownWriter in the future to format the text.
// For now just insert line breaks before `\param` and `\returns` markups.
List<UnownedStringSlice> lines;
StringUtil::split(doc.getUnownedSlice(), '\n', lines);
StringBuilder result;
for (Index i = 0; i < lines.getCount(); i++)
{
auto trimedLine = lines[i].trimStart();
if (i > 0)
{
if (trimedLine.startsWith("\\") && lines[i - 1].trim().getLength() != 0)
{
result << " \n";
}
else
{
result << "\n";
}
}
if (trimedLine.startsWith("\\returns "))
{
trimedLine = trimedLine.subString(9, trimedLine.getLength());
result << "**returns** ";
}
else if (trimedLine.startsWith("\\return "))
{
trimedLine = trimedLine.subString(8, trimedLine.getLength());
result << "**Returns** ";
}
result << trimedLine;
}
result << "\n";
return result.ProduceString();
}
static void _tryGetDocumentation(StringBuilder& sb, WorkspaceVersion* workspace, Decl* decl)
{
auto definingModule = getModuleDecl(decl);
if (definingModule)
{
auto markupAST = workspace->getOrCreateMarkupAST(definingModule);
auto markupEntry = markupAST->getEntry(decl);
if (markupEntry)
{
sb << "\n";
sb << _formatDocumentation(markupEntry->m_markup);
sb << "\n";
}
}
}
SlangResult LanguageServer::hover(
const LanguageServerProtocol::HoverParams& args, const JSONValue& responseId)
{
String canonicalPath = uriToCanonicalPath(args.textDocument.uri);
auto version = m_workspace->getCurrentVersion();
Module* parsedModule = version->getOrLoadModule(canonicalPath);
if (!parsedModule)
{
m_connection->sendResult(NullResponse::get(), responseId);
return SLANG_OK;
}
auto findResult = findASTNodesAt(
version->linkage->getSourceManager(),
parsedModule->getModuleDecl(),
ASTLookupType::Decl,
canonicalPath.getUnownedSlice(),
args.position.line + 1,
args.position.character + 1);
if (findResult.getCount() == 0 || findResult[0].path.getCount() == 0)
{
m_connection->sendResult(NullResponse::get(), responseId);
return SLANG_OK;
}
StringBuilder sb;
Hover hover = {};
auto leafNode = findResult[0].path.getLast();
auto fillDeclRefHoverInfo = [&](DeclRef<Decl> declRef)
{
if (declRef.getDecl())
{
sb << "```\n"
<< getDeclSignatureString(declRef, version->linkage->getASTBuilder())
<< "\n```\n";
_tryGetDocumentation(sb, version, declRef.getDecl());
auto humaneLoc = version->linkage->getSourceManager()->getHumaneLoc(
declRef.getLoc(), SourceLocType::Actual);
sb << "Defined in " << humaneLoc.pathInfo.foundPath << "(" << humaneLoc.line
<< ")\n";
auto nodeHumaneLoc =
version->linkage->getSourceManager()->getHumaneLoc(leafNode->loc);
hover.range.start.line = int(nodeHumaneLoc.line - 1);
hover.range.end.line = int(nodeHumaneLoc.line - 1);
hover.range.start.character = int(nodeHumaneLoc.column - 1);
if (declRef.getName())
{
hover.range.end.character =
int(nodeHumaneLoc.column + declRef.getName()->text.getLength() - 1);
}
}
};
if (auto declRefExpr = as<DeclRefExpr>(leafNode))
{
fillDeclRefHoverInfo(declRefExpr->declRef);
}
else if (auto overloadedExpr = as<OverloadedExpr>(leafNode))
{
LookupResultItem& item = overloadedExpr->lookupResult2.item;
fillDeclRefHoverInfo(item.declRef);
}
else if (auto decl = as<Decl>(leafNode))
{
fillDeclRefHoverInfo(DeclRef<Decl>(decl, nullptr));
}
if (sb.getLength() == 0)
{
m_connection->sendResult(NullResponse::get(), responseId);
return SLANG_OK;
}
else
{
hover.contents.kind = "markdown";
hover.contents.value = sb.ProduceString();
m_connection->sendResult(&hover, responseId);
return SLANG_OK;
}
}
SlangResult LanguageServer::gotoDefinition(
const LanguageServerProtocol::DefinitionParams& args, const JSONValue& responseId)
{
String canonicalPath = uriToCanonicalPath(args.textDocument.uri);
auto version = m_workspace->getCurrentVersion();
Module* parsedModule = version->getOrLoadModule(canonicalPath);
if (!parsedModule)
{
m_connection->sendResult(NullResponse::get(), responseId);
return SLANG_OK;
}
auto findResult = findASTNodesAt(
version->linkage->getSourceManager(),
parsedModule->getModuleDecl(),
ASTLookupType::Decl,
canonicalPath.getUnownedSlice(),
args.position.line + 1,
args.position.character + 1);
if (findResult.getCount() == 0 || findResult[0].path.getCount() == 0)
{
m_connection->sendResult(NullResponse::get(), responseId);
return SLANG_OK;
}
struct LocationResult
{
HumaneSourceLoc loc;
int length;
};
List<LocationResult> locations;
auto leafNode = findResult[0].path.getLast();
if (auto declRefExpr = as<DeclRefExpr>(leafNode))
{
if (declRefExpr->declRef.getDecl())
{
auto location = version->linkage->getSourceManager()->getHumaneLoc(
declRefExpr->declRef.getNameLoc(), SourceLocType::Actual);
auto name = declRefExpr->declRef.getName();
locations.add(LocationResult{location, name ? (int)name->text.getLength() : 0});
}
}
else if (auto overloadedExpr = as<OverloadedExpr>(leafNode))
{
if (overloadedExpr->lookupResult2.items.getCount())
{
for (auto item : overloadedExpr->lookupResult2.items)
{
auto location = version->linkage->getSourceManager()->getHumaneLoc(
item.declRef.getNameLoc(), SourceLocType::Actual);
auto name = item.declRef.getName();
locations.add(LocationResult{location, name ? (int)name->text.getLength() : 0});
}
}
else
{
LookupResultItem& item = overloadedExpr->lookupResult2.item;
if (item.declRef.getDecl() != nullptr)
{
auto location = version->linkage->getSourceManager()->getHumaneLoc(
item.declRef.getNameLoc(), SourceLocType::Actual);
auto name = item.declRef.getName();
locations.add(LocationResult{location, name ? (int)name->text.getLength() : 0});
}
}
}
if (locations.getCount() == 0)
{
m_connection->sendResult(NullResponse::get(), responseId);
return SLANG_OK;
}
else
{
List<Location> results;
for (auto loc : locations)
{
Location result;
result.uri = URI::fromLocalFilePath(loc.loc.pathInfo.foundPath.getUnownedSlice()).uri;
result.range.start.line = int(loc.loc.line - 1);
result.range.start.character = int(loc.loc.column - 1);
result.range.end = result.range.start;
result.range.end.character += loc.length;
results.add(result);
}
m_connection->sendResult(&results, responseId);
return SLANG_OK;
}
}
bool _isIdentifierChar(char ch)
{
return ch >= '0' && ch <= '9' || ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' || ch == '_';
}
bool _isWhitespaceChar(char ch)
{
return ch == ' ' || ch == '\r' || ch == '\n' || ch == '\t';
}
SlangResult LanguageServer::completion(
const LanguageServerProtocol::CompletionParams& args, const JSONValue& responseId)
{
String canonicalPath = uriToCanonicalPath(args.textDocument.uri);
RefPtr<DocumentVersion> doc;
if (!m_workspace->openedDocuments.TryGetValue(canonicalPath, doc))
{
m_connection->sendResult(NullResponse::get(), responseId);
return SLANG_OK;
}
auto cursorOffset = doc->getOffset(args.position.line + 1, args.position.character + 1);
if (cursorOffset == -1 || doc->getText().getLength() == 0)
{
m_connection->sendResult(NullResponse::get(), responseId);
return SLANG_OK;
}
// Scan backward until we locate a '.' or ':'.
if (cursorOffset == doc->getText().getLength())
cursorOffset--;
while (cursorOffset > 0 && _isWhitespaceChar(doc->getText()[cursorOffset]))
{
cursorOffset--;
}
while (cursorOffset > 0 && _isIdentifierChar(doc->getText()[cursorOffset]))
{
cursorOffset--;
}
while (cursorOffset > 0 && _isWhitespaceChar(doc->getText()[cursorOffset]))
{
cursorOffset--;
}
if (cursorOffset > 0 && doc->getText()[cursorOffset] == ':')
cursorOffset--;
if (cursorOffset <= 0 ||
(doc->getText()[cursorOffset] != '.' && doc->getText()[cursorOffset] != ':'))
{
m_connection->sendResult(NullResponse::get(), responseId);
return SLANG_OK;
}
Index line = 0;
Index col = 0;
doc->offsetToLineCol(cursorOffset, line, col);
auto version = m_workspace->getCurrentVersion();
Module* parsedModule = version->getOrLoadModule(canonicalPath);
if (!parsedModule)
{
m_connection->sendResult(NullResponse::get(), responseId);
return SLANG_OK;
}
auto findResult = findASTNodesAt(
version->linkage->getSourceManager(),
parsedModule->getModuleDecl(),
ASTLookupType::Decl,
canonicalPath.getUnownedSlice(),
line,
col);
if (findResult.getCount() != 1)
{
m_connection->sendResult(NullResponse::get(), responseId);
return SLANG_OK;
}
if (findResult[0].path.getCount() == 0)
{
m_connection->sendResult(NullResponse::get(), responseId);
return SLANG_OK;
}
Expr* baseExpr = nullptr;
if (auto memberExpr = as<MemberExpr>(findResult[0].path.getLast()))
{
baseExpr = memberExpr->baseExpression;
}
else if (auto staticMemberExpr = as<StaticMemberExpr>(findResult[0].path.getLast()))
{
baseExpr = staticMemberExpr->baseExpression;
}
else if (auto swizzleExpr = as<SwizzleExpr>(findResult[0].path.getLast()))
{
baseExpr = swizzleExpr->base;
}
else if (auto matSwizzleExpr = as<MatrixSwizzleExpr>(findResult[0].path.getLast()))
{
baseExpr = matSwizzleExpr->base;
}
if (!baseExpr || !baseExpr->type.type || baseExpr->type.type->equals(version->linkage->getASTBuilder()->getErrorType()))
{
m_connection->sendResult(NullResponse::get(), responseId);
return SLANG_OK;
}
List<LanguageServerProtocol::CompletionItem> items = collectMembers(version, parsedModule, baseExpr);
m_connection->sendResult(&items, responseId);
return SLANG_OK;
}
SlangResult LanguageServer::completionResolve(
const LanguageServerProtocol::CompletionItem& args, const JSONValue& responseId)
{
LanguageServerProtocol::CompletionItem resolvedItem = args;
int itemId = StringToInt(args.data);
auto version = m_workspace->getCurrentVersion();
if (itemId >= 0 && itemId < version->currentCompletionItems.getCount())
{
auto decl = version->currentCompletionItems[itemId];
resolvedItem.detail = getDeclSignatureString(
DeclRef<Decl>(decl, nullptr), version->linkage->getASTBuilder());
StringBuilder docSB;
_tryGetDocumentation(docSB, version, decl);
resolvedItem.documentation.value = docSB.ProduceString();
resolvedItem.documentation.kind = "markdown";
}
m_connection->sendResult(&resolvedItem, responseId);
return SLANG_OK;
}
SlangResult LanguageServer::semanticTokens(
const LanguageServerProtocol::SemanticTokensParams& args, const JSONValue& responseId)
{
String canonicalPath = uriToCanonicalPath(args.textDocument.uri);
auto version = m_workspace->getCurrentVersion();
Module* parsedModule = version->getOrLoadModule(canonicalPath);
if (!parsedModule)
{
m_connection->sendResult(NullResponse::get(), responseId);
return SLANG_OK;
}
auto tokens = getSemanticTokens(version->linkage, parsedModule, canonicalPath.getUnownedSlice());
SemanticTokens response;
response.resultId = "";
response.data = getEncodedTokens(tokens);
m_connection->sendResult(&response, responseId);
return SLANG_OK;
}
SlangResult LanguageServer::signatureHelp(
const LanguageServerProtocol::SignatureHelpParams& args, const JSONValue& responseId)
{
String canonicalPath = uriToCanonicalPath(args.textDocument.uri);
auto version = m_workspace->getCurrentVersion();
Module* parsedModule = version->getOrLoadModule(canonicalPath);
if (!parsedModule)
{
m_connection->sendResult(NullResponse::get(), responseId);
return SLANG_OK;
}
auto findResult = findASTNodesAt(
version->linkage->getSourceManager(),
parsedModule->getModuleDecl(),
ASTLookupType::Invoke,
canonicalPath.getUnownedSlice(),
args.position.line + 1,
args.position.character + 1);
if (findResult.getCount() == 0)
{
m_connection->sendResult(NullResponse::get(), responseId);
return SLANG_OK;
}
AppExprBase* appExpr = nullptr;
auto& declPath = findResult[0].path;
for (Index i = declPath.getCount() - 1; i >= 0; i--)
{
if (auto expr = as<AppExprBase>(declPath[i]))
{
// Find the inner most invoke expr that has source token info.
// This allows us to skip the invoke expr nodes for operators/implcit casts.
if (expr->argumentDelimeterLocs.getCount())
{
appExpr = expr;
break;
}
}
}
if (!appExpr)
{
m_connection->sendResult(NullResponse::get(), responseId);
return SLANG_OK;
}
if (appExpr->argumentDelimeterLocs.getCount() == 0)
{
m_connection->sendResult(NullResponse::get(), responseId);
return SLANG_OK;
}
auto funcExpr =
appExpr->originalFunctionExpr ? appExpr->originalFunctionExpr : appExpr->functionExpr;
if (!funcExpr)
{
m_connection->sendResult(NullResponse::get(), responseId);
return SLANG_OK;
}
SignatureHelp response;
auto addDeclRef = [&](DeclRef<Decl> declRef)
{
if (!declRef.getDecl())
return;
SignatureInformation sigInfo;
List<Slang::Range<Index>> paramRanges;
ASTPrinter printer(
version->linkage->getASTBuilder(),
ASTPrinter::OptionFlag::ParamNames | ASTPrinter::OptionFlag::NoInternalKeywords |
ASTPrinter::OptionFlag::SimplifiedBuiltinType);
printer.addDeclKindPrefix(declRef.getDecl());
printer.addDeclPath(declRef);
printer.addDeclParams(declRef, ¶mRanges);
printer.addDeclResultType(declRef);
sigInfo.label = printer.getString();
StringBuilder docSB;
auto humaneLoc = version->linkage->getSourceManager()->getHumaneLoc(declRef.getLoc(), SourceLocType::Actual);
_tryGetDocumentation(docSB, version, declRef.getDecl());
docSB << "Defined in " << humaneLoc.pathInfo.foundPath << "(" << humaneLoc.line << ")\n";
sigInfo.documentation.value = docSB.ProduceString();
sigInfo.documentation.kind = "markdown";
for (auto& range : paramRanges)
{
ParameterInformation paramInfo;
paramInfo.label[0] = (uint32_t)range.begin;
paramInfo.label[1] = (uint32_t)range.end;
sigInfo.parameters.add(paramInfo);
}
response.signatures.add(sigInfo);
};
if (auto declRefExpr = as<DeclRefExpr>(funcExpr))
{
addDeclRef(declRefExpr->declRef);
}
else if (auto overloadedExpr = as<OverloadedExpr>(funcExpr))
{
for (auto item : overloadedExpr->lookupResult2)
{
addDeclRef(item.declRef);
}
}
response.activeSignature = 0;
response.activeParameter = 0;
for (int i = 1; i < appExpr->argumentDelimeterLocs.getCount(); i++)
{
auto delimLoc = version->linkage->getSourceManager()->getHumaneLoc(
appExpr->argumentDelimeterLocs[i], SourceLocType::Actual);
if (delimLoc.line > args.position.line + 1 ||
delimLoc.line == args.position.line + 1 && delimLoc.column >= args.position.character + 1)
{
response.activeParameter = i - 1;
break;
}
}
m_connection->sendResult(&response, responseId);
return SLANG_OK;
}
List<LanguageServerProtocol::CompletionItem> LanguageServer::collectMembers(WorkspaceVersion* version, Module* module, Expr* baseExpr)
{
List<LanguageServerProtocol::CompletionItem> result;
auto linkage = version->linkage;
Type* type = baseExpr->type.type;
if (auto typeType = as<TypeType>(type))
{
type = typeType->type;
}
version->currentCompletionItems.clear();
if (type)
{
if (as<ArithmeticExpressionType>(type))
{
// Hard code members for vector and matrix types.
result.clear();
version->currentCompletionItems.clear();
int elementCount = 0;
Type* elementType = nullptr;
const char* memberNames[4] = {"x", "y", "z", "w"};
if (auto vectorType = as<VectorExpressionType>(type))
{
if (auto elementCountVal = as<ConstantIntVal>(vectorType->elementCount))
{
elementCount = (int)elementCountVal->value;
elementType = vectorType->elementType;
}
}
else if (auto matrixType = as<MatrixExpressionType>(type))
{
if (auto elementCountVal = as<ConstantIntVal>(matrixType->getRowCount()))
{
elementCount = (int)elementCountVal->value;
elementType = matrixType->getRowType();
}
}
String typeStr;
if (elementType)
typeStr = elementType->toString();
for (int i = 0; i < elementCount; i++)
{
CompletionItem item;
item.data = 0;
item.detail = typeStr;
item.kind = LanguageServerProtocol::kCompletionItemKindVariable;
item.label = memberNames[i];
result.add(item);
}
}
else
{
DiagnosticSink sink;
MemberCollectingContext context(linkage, module, &sink);
context.astBuilder = linkage->getASTBuilder();
collectMembersInType(&context, type);
HashSet<String> deduplicateSet;
for (auto member : context.members)
{
CompletionItem item;
item.label = member->getName()->text;
item.kind = 0;
if (as<TypeConstraintDecl>(member))
{
continue;
}
if (as<ConstructorDecl>(member))
{
continue;
}
if (as<SubscriptDecl>(member))
{
continue;
}
if (item.label.startsWith("$"))
continue;
if (!deduplicateSet.Add(item.label))
continue;
if (as<StructDecl>(member))
{
item.kind = LanguageServerProtocol::kCompletionItemKindStruct;
}
else if (as<ClassDecl>(member))
{
item.kind = LanguageServerProtocol::kCompletionItemKindClass;
}
else if (as<InterfaceDecl>(member))
{
item.kind = LanguageServerProtocol::kCompletionItemKindInterface;
}
else if (as<SimpleTypeDecl>(member))
{
item.kind = LanguageServerProtocol::kCompletionItemKindClass;
}
else if (as<PropertyDecl>(member))
{
item.kind = LanguageServerProtocol::kCompletionItemKindProperty;
}
else if (as<EnumDecl>(member))
{
item.kind = LanguageServerProtocol::kCompletionItemKindEnum;
}
else if (as<VarDeclBase>(member))
{
item.kind = LanguageServerProtocol::kCompletionItemKindVariable;
}
else if (as<EnumCaseDecl>(member))
{
item.kind = LanguageServerProtocol::kCompletionItemKindEnumMember;
}
else if (as<CallableDecl>(member))
{
item.kind = LanguageServerProtocol::kCompletionItemKindMethod;
}
else if (as<AssocTypeDecl>(member))
{
item.kind = LanguageServerProtocol::kCompletionItemKindClass;
}
item.data = String(version->currentCompletionItems.getCount());
result.add(item);
version->currentCompletionItems.add(member);
}
}
for (auto& item : result)
{
switch (item.kind)
{
case LanguageServerProtocol::kCompletionItemKindMethod:
item.commitCharacters.add("(");
item.commitCharacters.add("[");
item.commitCharacters.add(" ");
break;
default:
item.commitCharacters.add("(");
item.commitCharacters.add(")");
item.commitCharacters.add(".");
item.commitCharacters.add(";");
item.commitCharacters.add(":");
item.commitCharacters.add(",");
item.commitCharacters.add("<");
item.commitCharacters.add(">");
item.commitCharacters.add("[");
item.commitCharacters.add("]");
item.commitCharacters.add("{");
item.commitCharacters.add("}");
item.commitCharacters.add("-");
item.commitCharacters.add("*");
item.commitCharacters.add("/");
item.commitCharacters.add("%");
item.commitCharacters.add("+");
item.commitCharacters.add("=");
item.commitCharacters.add("&");
item.commitCharacters.add("|");
item.commitCharacters.add("!");
item.commitCharacters.add(" ");
break;
}
}
}
return result;
}
void LanguageServer::publishDiagnostics()
{
time_t timeNow = 0;
time(&timeNow);
if (timeNow - m_lastDiagnosticUpdateTime < 3)
{
return;
}
m_lastDiagnosticUpdateTime = timeNow;
auto version = m_workspace->getCurrentVersion();
// Send updates to clear diagnostics for files that no longer have any messages.
List<String> filesToRemove;
for (auto& file : m_lastPublishedDiagnostics)
{
if (!version->diagnostics.ContainsKey(file.Key))
{
PublishDiagnosticsParams args;
args.uri = URI::fromLocalFilePath(file.Key.getUnownedSlice()).uri;
m_connection->sendCall(UnownedStringSlice("textDocument/publishDiagnostics"), &args);
filesToRemove.add(file.Key);
}
}
for (auto& toRemove : filesToRemove)
{
m_lastPublishedDiagnostics.Remove(toRemove);
}
// Send updates for any files whose diagnostic messages has changed since last update.
for (auto& list : version->diagnostics)
{
auto lastPublished = m_lastPublishedDiagnostics.TryGetValue(list.Key);
if (!lastPublished || *lastPublished != list.Value.originalOutput)
{
PublishDiagnosticsParams args;
args.uri = URI::fromLocalFilePath(list.Key.getUnownedSlice()).uri;
for (auto& d : list.Value.messages)
args.diagnostics.add(d);
m_connection->sendCall(UnownedStringSlice("textDocument/publishDiagnostics"), &args);
m_lastPublishedDiagnostics[list.Key] = list.Value.originalOutput;
}
}
}
SlangResult LanguageServer::didCloseTextDocument(const DidCloseTextDocumentParams& args)
{
String canonicalPath = uriToCanonicalPath(args.textDocument.uri);
m_workspace->openedDocuments.Remove(canonicalPath);
m_workspace->invalidate();
resetDiagnosticUpdateTime();
return SLANG_OK;
}
SlangResult LanguageServer::didChangeTextDocument(const DidChangeTextDocumentParams& args)
{
String canonicalPath = uriToCanonicalPath(args.textDocument.uri);
RefPtr<DocumentVersion> doc;
if (m_workspace->openedDocuments.TryGetValue(canonicalPath, doc))
{
doc->setText(args.contentChanges[0].text.getUnownedSlice());
}
m_workspace->invalidate();
resetDiagnosticUpdateTime();
return SLANG_OK;
}
void LanguageServer::update()
{
if (!m_workspace)
return;
publishDiagnostics();
}
SlangResult LanguageServer::execute()
{
m_connection = new JSONRPCConnection();
m_connection->initWithStdStreams();
while (m_connection->isActive() && !m_quit)
{
// Consume all messages first.
while (true)
{
m_connection->tryReadMessage();
if (!m_connection->hasMessage())
break;
const SlangResult res = _executeSingle();
}
// Now we can use this time to reparse user's code, report diagnostics, etc.
update();
}
return SLANG_OK;
}
SLANG_API SlangResult runLanguageServer()
{
Slang::LanguageServer server;
SLANG_RETURN_ON_FAIL(server.execute());
return SLANG_OK;
}
} // namespace Slang
|