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
|
#include "slang-language-server-inlay-hints.h"
#include "../core/slang-char-util.h"
#include "slang-ast-iterator.h"
#include "slang-ast-support-types.h"
#include "slang-language-server.h"
#include "slang-visitor.h"
namespace Slang
{
List<LanguageServerProtocol::InlayHint> getInlayHints(
Linkage* linkage,
Module* module,
UnownedStringSlice fileName,
DocumentVersion* doc,
LanguageServerProtocol::Range range,
const InlayHintOptions& options)
{
List<LanguageServerProtocol::InlayHint> result;
auto manager = linkage->getSourceManager();
auto docText = doc->getText().getUnownedSlice();
iterateASTWithLanguageServerFilter(
fileName,
manager,
module->getModuleDecl(),
[&](SyntaxNode* node)
{
if (auto invokeExpr = as<InvokeExpr>(node))
{
if (!options.showParameterNames)
return;
auto humaneLoc = manager->getHumaneLoc(node->loc);
if (humaneLoc.line - 1 < range.start.line || humaneLoc.line - 1 > range.end.line)
return;
if (humaneLoc.pathInfo.foundPath != fileName)
return;
auto funcExpr = as<DeclRefExpr>(invokeExpr->functionExpr);
if (!funcExpr)
return;
if (as<ConstructorDecl>(funcExpr->declRef.getDecl()))
return;
auto callable = as<CallableDecl>(funcExpr->declRef.getDecl());
if (!callable)
return;
auto params = callable->getParameters();
Index i = 0;
for (auto param : params)
{
if (i >= invokeExpr->argumentDelimeterLocs.getCount() - 1)
break;
if (auto name = param->getName())
{
LanguageServerProtocol::InlayHint hint;
auto loc = manager->getHumaneLoc(invokeExpr->argumentDelimeterLocs[i]);
auto offset = doc->getOffset(loc.line, loc.column);
offset++;
while (offset < docText.getLength() &&
CharUtil::isWhitespace(docText[offset]))
offset++;
Index posLine, posCol;
doc->offsetToLineCol(offset, posLine, posCol);
Index utf16line, utf16col;
doc->oneBasedUTF8LocToZeroBasedUTF16Loc(
posLine,
posCol,
utf16line,
utf16col);
hint.position.line = (int)utf16line;
hint.position.character = (int)utf16col;
hint.paddingLeft = false;
hint.kind = LanguageServerProtocol::kInlayHintKindParameter;
StringBuilder lblSb;
if (param->hasModifier<OutModifier>())
lblSb << "out ";
else if (param->hasModifier<InOutModifier>())
lblSb << "inout ";
else if (param->hasModifier<RefModifier>())
lblSb << "ref ";
else if (param->hasModifier<BorrowModifier>())
lblSb << "constref ";
lblSb << name->text;
lblSb << ":";
hint.label = lblSb.produceString();
result.add(hint);
}
i++;
}
}
else if (auto varDecl = as<VarDeclBase>(node))
{
if (!options.showDeducedType)
return;
auto humaneLoc = manager->getHumaneLoc(node->loc);
if (humaneLoc.line - 1 < range.start.line || humaneLoc.line - 1 > range.end.line)
return;
if (humaneLoc.pathInfo.foundPath != fileName)
return;
if (varDecl->type.exp)
return;
if (!varDecl->type.type)
return;
if (as<ErrorType>(varDecl->type.type))
return;
if (!varDecl->getName())
return;
LanguageServerProtocol::InlayHint hint;
auto loc = manager->getHumaneLoc(varDecl->nameAndLoc.loc);
auto offset = doc->getOffset(loc.line, loc.column);
offset++;
while (offset < docText.getLength() && _isIdentifierChar(docText[offset]))
offset++;
Index posLine, posCol;
doc->offsetToLineCol(offset, posLine, posCol);
Index utf16line, utf16col;
doc->oneBasedUTF8LocToZeroBasedUTF16Loc(posLine, posCol, utf16line, utf16col);
hint.position.line = (int)utf16line;
hint.position.character = (int)utf16col;
hint.kind = LanguageServerProtocol::kInlayHintKindType;
StringBuilder lblSb;
lblSb << ": " << varDecl->type.type->toString();
hint.label = lblSb.produceString();
LanguageServerProtocol::TextEdit edit;
edit.range.start = hint.position;
edit.range.end = hint.position;
edit.newText = " " + hint.label;
hint.textEdits.add(edit);
result.add(hint);
}
});
return result;
}
} // namespace Slang
|