-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathFileSection.java
More file actions
64 lines (53 loc) · 1.83 KB
/
FileSection.java
File metadata and controls
64 lines (53 loc) · 1.83 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
package io.github.syst3ms.skriptparser.file;
import io.github.syst3ms.skriptparser.lang.entries.OptionLoader;
import java.util.List;
import java.util.Optional;
/**
* A class describing a section of a script inside a file (e.g a line ending with a colon and containing all the lines that
* were indented after it. "all the lines" doesn't exclude sections.
*/
public class FileSection extends FileElement {
private final List<FileElement> elements;
private int length = -1;
public FileSection(String fileName, int line, String content, List<FileElement> elements, int indentation) {
super(fileName, line, content, indentation);
this.elements = elements;
}
/**
* Returns the elements inside of the section
* @return the elements inside of the section
*/
public List<FileElement> getElements() {
return elements;
}
public int length() {
if (length >= 0)
return length;
length = 0;
for (var e : elements) {
if (e instanceof FileSection) {
length += ((FileSection) e).length() + 1;
} else {
length++;
}
}
return length;
}
public Optional<FileElement> get(String line) {
return elements.stream()
.filter(element -> {
String content = element.getLineContent();
content = content.substring(0, content.lastIndexOf(OptionLoader.OPTION_SPLIT_PATTERN.trim()));
return content.equalsIgnoreCase(line);
})
.findFirst();
}
@Override
public boolean equals(Object obj) {
return super.equals(obj) && elements.equals(((FileSection) obj).elements);
}
@Override
public String toString() {
return super.toString() + ":";
}
}