-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTriviaExtensions.cs
More file actions
60 lines (45 loc) · 1.6 KB
/
Copy pathTriviaExtensions.cs
File metadata and controls
60 lines (45 loc) · 1.6 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
using System;
using System.Linq;
namespace Discord.CX.Parser;
public static class TriviaExtensions
{
extension(LexedCXTrivia trivia)
{
public LexedCXTrivia NewlinesOnly()
=> [..trivia.Where(x => x is CXTrivia.Token { Kind: CXTriviaTokenKind.Newline })];
public LexedCXTrivia WhitespaceOnly()
=> [..trivia.Where(x => x.IsWhitespaceTrivia)];
public LexedCXTrivia ToIndentationOnly()
{
var ws = trivia.Where(x => x.IsWhitespaceTrivia).ToArray().AsSpan();
// find the last newline
var fromIndex = ws.Length - 1;
for (; fromIndex >= 0; fromIndex--)
{
if (ws[fromIndex] is CXTrivia.Token { Kind: CXTriviaTokenKind.Newline }) break;
}
if (fromIndex is -1) return [..ws];
return [..ws.Slice(fromIndex + 1)];
}
public LexedCXTrivia TrimLeadingSyntaxIndentation()
{
for (var i = 0; i < trivia.Count; i++)
{
var item = trivia[i];
if (item is not CXTrivia.Token { Kind: CXTriviaTokenKind.Newline }) continue;
return [..trivia.Skip(i + 1)];
}
return trivia;
}
public LexedCXTrivia TrimTrailingSyntaxIndentation()
{
for (var i = trivia.Count - 1; i >= 0; i--)
{
var item = trivia[i];
if (item is not CXTrivia.Token { Kind: CXTriviaTokenKind.Newline }) continue;
return [..trivia.Take(i)];
}
return trivia;
}
}
}