-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathCSharpLanguage.cs
More file actions
338 lines (288 loc) · 11.2 KB
/
Copy pathCSharpLanguage.cs
File metadata and controls
338 lines (288 loc) · 11.2 KB
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
namespace PixUI.CodeEditor;
internal sealed class CSharpLanguage : ICodeLanguage
{
public char? GetAutoClosingPairs(char ch) => ch switch
{
'{' => '}',
'[' => ']',
'(' => ')',
'"' => '"',
_ => null
};
public bool IsBlockStartBracket(char ch) => ch == '{';
public bool IsBlockEndBracket(char ch) => ch == '}';
#region ====ITokensProvider====
private static readonly Dictionary<string, TokenType> TokenMap = new()
{
{ ";", TokenType.PunctuationDelimiter },
{ ".", TokenType.PunctuationDelimiter },
{ ",", TokenType.PunctuationDelimiter },
{ "--", TokenType.Operator },
{ "-", TokenType.Operator },
{ "-=", TokenType.Operator },
{ "&", TokenType.Operator },
{ "&&", TokenType.Operator },
{ "+", TokenType.Operator },
{ "++", TokenType.Operator },
{ "+=", TokenType.Operator },
{ "<", TokenType.Operator },
{ "<<", TokenType.Operator },
{ "=", TokenType.Operator },
{ "==", TokenType.Operator },
{ "!", TokenType.Operator },
{ "!=", TokenType.Operator },
{ "=>", TokenType.Operator },
{ ">", TokenType.Operator },
{ ">>", TokenType.Operator },
{ "|", TokenType.Operator },
{ "||", TokenType.Operator },
{ "?", TokenType.Operator },
{ "??", TokenType.Operator },
{ "^", TokenType.Operator },
{ "~", TokenType.Operator },
{ "*", TokenType.Operator },
{ "/", TokenType.Operator },
{ "%", TokenType.Operator },
{ ":", TokenType.Operator },
{ "(", TokenType.PunctuationBracket },
{ ")", TokenType.PunctuationBracket },
{ "[", TokenType.PunctuationBracket },
{ "]", TokenType.PunctuationBracket },
{ "{", TokenType.PunctuationBracket },
{ "}", TokenType.PunctuationBracket },
{ "as", TokenType.Keyword },
{ "base", TokenType.Keyword },
{ "break", TokenType.Keyword },
{ "case", TokenType.Keyword },
{ "catch", TokenType.Keyword },
{ "checked", TokenType.Keyword },
{ "class", TokenType.Keyword },
{ "continue", TokenType.Keyword },
{ "default", TokenType.Keyword },
{ "delegate", TokenType.Keyword },
{ "do", TokenType.Keyword },
{ "else", TokenType.Keyword },
{ "enum", TokenType.Keyword },
{ "event", TokenType.Keyword },
{ "explicit", TokenType.Keyword },
{ "finally", TokenType.Keyword },
{ "for", TokenType.Keyword },
{ "foreach", TokenType.Keyword },
{ "goto", TokenType.Keyword },
{ "if", TokenType.Keyword },
{ "implicit", TokenType.Keyword },
{ "interface", TokenType.Keyword },
{ "is", TokenType.Keyword },
{ "lock", TokenType.Keyword },
{ "namespace", TokenType.Keyword },
{ "operator", TokenType.Keyword },
{ "params", TokenType.Keyword },
{ "return", TokenType.Keyword },
{ "sizeof", TokenType.Keyword },
{ "stackalloc", TokenType.Keyword },
{ "struct", TokenType.Keyword },
{ "switch", TokenType.Keyword },
{ "throw", TokenType.Keyword },
{ "try", TokenType.Keyword },
{ "typeof", TokenType.Keyword },
{ "unchecked", TokenType.Keyword },
{ "using", TokenType.Keyword },
{ "while", TokenType.Keyword },
{ "new", TokenType.Keyword },
{ "await", TokenType.Keyword },
{ "in", TokenType.Keyword },
{ "yield", TokenType.Keyword },
{ "get", TokenType.Keyword },
{ "set", TokenType.Keyword },
{ "when", TokenType.Keyword },
{ "out", TokenType.Keyword },
{ "ref", TokenType.Keyword },
{ "from", TokenType.Keyword },
{ "where", TokenType.Keyword },
{ "select", TokenType.Keyword },
{ "record", TokenType.Keyword },
{ "init", TokenType.Keyword },
{ "with", TokenType.Keyword },
{ "let", TokenType.Keyword },
{ "var", TokenType.Keyword },
{ "this", TokenType.Keyword },
//----Named----
{ "implicit_type", TokenType.BuiltinType },
{ "pointer_type", TokenType.BuiltinType },
{ "function_pointer_type", TokenType.BuiltinType },
{ "predefined_type", TokenType.BuiltinType },
{ "real_literal", TokenType.LiteralNumber },
{ "integer_literal", TokenType.LiteralNumber },
{ "interpolated_string_text", TokenType.LiteralString },
{ "string_literal", TokenType.LiteralString },
{ "character_literal", TokenType.LiteralString },
{ "null_literal", TokenType.Constant },
{ "boolean_literal", TokenType.Constant },
{ "modifier", TokenType.Keyword },
{ "void_keyword", TokenType.Keyword },
{ "comment", TokenType.Comment },
{ "Error", TokenType.Error }
};
public bool IsLeafNode(in TSNode node)
{
var typeId = node.TypeId;
var type = TSCSharpLanguage.Instance.GetType(typeId);
return type == "modifier" ||
type == "string_literal" ||
type == "character_literal" ||
type == "boolean_literal";
}
public TokenType GetTokenType(in TSNode node)
{
var typeId = node.TypeId;
var type = TSCSharpLanguage.Instance.GetType(typeId);
return type == "identifier"
? GetIdentifierTokenType(node)
: TokenMap.GetValueOrDefault(type, TokenType.Unknown);
}
private static TokenType GetIdentifierTokenType(in TSNode node)
{
var parentType = node.Parent!.Value.Type;
if (parentType == "Error")
return TokenType.Unknown;
switch (parentType)
{
case "namespace_declaration":
case "using_directive":
return TokenType.Module;
case "class_declaration":
case "interface_declaration":
case "enum_declaration":
case "struct_declaration":
case "record_declaration":
case "object_creation_expression":
case "constructor_declaration":
case "array_type":
case "base_list":
case "variable_declaration":
case "nullable_type":
case "attribute":
case "generic_name":
case "type_parameter":
case "type_argument_list":
return TokenType.Type;
case "constant_pattern":
return TokenType.Type;
case "declaration_pattern":
case "recursive_pattern":
return node.PrevNamedSibling == null ? TokenType.Type : TokenType.Variable;
case "argument":
case "variable_declarator":
return TokenType.Variable;
case "property_declaration":
return node.PrevNamedSibling == null || node.PrevNamedSibling.Value.Type == "modifier"
? TokenType.Type
: TokenType.Variable;
case "parameter":
return node.PrevNamedSibling == null || node.PrevNamedSibling.Value.Type == "parameter_modifier"
? TokenType.Type
: TokenType.Variable;
case "catch_declaration":
return node.PrevNamedSibling == null ? TokenType.Type : TokenType.Variable;
case "invocation_expression":
return TokenType.Function;
case "method_declaration":
return GetIdentifierTypeFromMethodDeclaration(node);
case "member_binding_expression":
return GetIdentifierTypeFromMemberBinding(node);
case "qualified_name":
return GetIdentifierTypeFromQualifiedName(node);
case "member_access_expression":
return GetIdentifierTypeFromMemberAccess(node);
default:
return TokenType.Unknown;
}
}
private static TokenType GetIdentifierTypeFromMethodDeclaration(in TSNode node)
{
return node.NextSibling is { Type: "identifier" }
? TokenType.Type
: TokenType.Function;
}
private static TokenType GetIdentifierTypeFromMemberBinding(in TSNode node)
{
var parent = node.Parent!.Value.Parent;
if (parent?.Type == "conditional_access_expression")
{
var parentParent = parent.Value.Parent;
if (parentParent?.Type == "invocation_expression")
return TokenType.Function;
}
return TokenType.Unknown;
}
private static TokenType GetIdentifierTypeFromQualifiedName(in TSNode node)
{
var parent = node.Parent;
if (parent.HasValue)
{
var parentParent = parent.Value.Parent;
if (parentParent.HasValue)
{
var parentParentType = parentParent.Value.Type;
if (parentParentType == "qualified_name")
return TokenType.Module;
if (parentParentType == "assignment_expression")
return TokenType.Variable; //TODO:是否静态类型的成员
}
}
return node.NextNamedSibling == null ? TokenType.Type : TokenType.Module;
}
/// <summary>
/// Get identifier token type from MemberAccess
/// </summary>
/// <param name="node">MemberAccessNode, eg: "some.identifier"</param>
private static TokenType GetIdentifierTypeFromMemberAccess(in TSNode node)
{
if (node.Parent?.Parent?.Type == "invocation_expression" && node.NextNamedSibling == null)
return TokenType.Function;
return TokenType.Unknown;
// //TODO:查找上下文变量列表
// return node.NextNamedSibling == null ? TokenType.Variable : TokenType.Type;
}
#endregion
#region ====IFoldingProvider====
//参考: https://github.com/nvim-treesitter/nvim-treesitter/blob/master/queries/c_sharp/folds.scm
private const string FoldQuery = @"
body: [
(declaration_list)
(switch_body)
(enum_member_declaration_list)
] @fold
accessors: [
(accessor_list)
] @fold
initializer: [
(initializer_expression)
] @fold
(block) @fold
";
private TSQuery? _foldQuery;
public IEnumerable<NewFolding> GenerateFoldMarkers(Document document)
{
var syntaxParser = (TreeSitterSyntaxParser)document.SyntaxParser;
var rootNode = syntaxParser.RootNode;
if (rootNode == null)
yield break;
_foldQuery ??= TSCSharpLanguage.Instance.Query(FoldQuery)!;
var captures = _foldQuery.Captures(rootNode.Value);
var lastNodeId = IntPtr.Zero;
foreach (var capture in captures)
{
if (lastNodeId == capture.node.id) continue;
lastNodeId = capture.node.id;
var node = TSNode.Create(capture.node)!.Value;
//暂跳过同一行的
if (node.StartPosition.row == node.EndPosition.row) continue;
var startIndex = node.StartIndex / TreeSitterSyntaxParser.ParserEncoding;
var endIndex = node.EndIndex / TreeSitterSyntaxParser.ParserEncoding;
var folding = new NewFolding(startIndex, endIndex - startIndex, "{...}");
yield return folding;
}
}
#endregion
}