-
Notifications
You must be signed in to change notification settings - Fork 368
Expand file tree
/
Copy pathUsingLineProcessor.cs
More file actions
47 lines (38 loc) · 1.35 KB
/
UsingLineProcessor.cs
File metadata and controls
47 lines (38 loc) · 1.35 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
using ScriptCs.Contracts;
namespace ScriptCs
{
public interface IUsingLineProcessor : ILineProcessor
{
}
public class UsingLineProcessor : IUsingLineProcessor
{
private const string UsingString = "using ";
public bool ProcessLine(IFileParser parser, FileParserContext context, string line, bool isBeforeCode)
{
Guard.AgainstNullArgument("context", context);
if (!IsUsingLine(line))
{
return false;
}
// for using static, we will not extract the import into the context, but rather let it be treated as code
if (line.Contains(" static "))
{
return false;
}
var @namespace = GetNamespace(line);
if (!context.Namespaces.Contains(@namespace))
{
context.Namespaces.Add(@namespace);
}
return true;
}
private static bool IsUsingLine(string line) => line.Trim(' ').StartsWith(UsingString) && !line.Contains("{") && line.Contains(";") && !line.Contains("=");
private static string GetNamespace(string line)
{
return line.Trim(' ')
.Replace(UsingString, string.Empty)
.Replace("\"", string.Empty)
.Replace(";", string.Empty);
}
}
}