Skip to content

Commit 3994d00

Browse files
committed
Respect user/custom context glob
1 parent dd089e5 commit 3994d00

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

plugin/src/de/hetzge/eclipse/aicoder/context/CustomContextEntry.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package de.hetzge.eclipse.aicoder.context;
22

3+
import java.nio.file.FileSystems;
4+
import java.nio.file.Path;
35
import java.time.Duration;
46
import java.util.List;
57
import java.util.Objects;
@@ -59,6 +61,13 @@ public String getGlob() {
5961
return this.glob;
6062
}
6163

64+
public boolean matches(Path path) {
65+
if (this.glob == null) {
66+
return false;
67+
}
68+
return FileSystems.getDefault().getPathMatcher("glob:" + this.glob).matches(path);
69+
}
70+
6271
public Json toJson() {
6372
return Json.object()
6473
.set("children", this.childContextEntries.stream().map(CustomContextEntry::toJson).toList())

plugin/src/de/hetzge/eclipse/aicoder/context/RootContextEntry.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.awt.datatransfer.UnsupportedFlavorException;
44
import java.io.IOException;
5+
import java.nio.file.Path;
56
import java.time.Duration;
67
import java.util.ArrayList;
78
import java.util.Comparator;
@@ -42,17 +43,18 @@ public ContextEntryKey getKey() {
4243
}
4344

4445
public static RootContextEntry create(IDocument document, IEditorInput editorInput, int offset) throws BadLocationException, UnsupportedFlavorException, IOException, CoreException {
46+
final long before = System.currentTimeMillis();
4547
final IProject project = EclipseUtils.getProject(editorInput);
4648
final String filename = EclipseUtils.getFilename(editorInput).orElse("Active File");
47-
final long before = System.currentTimeMillis();
49+
final Path path = Path.of(filename);
4850
final Optional<ICompilationUnit> compilationUnitOptional = EclipseUtils.getCompilationUnit(editorInput);
4951
final List<ContextEntryFactory> factories = new ArrayList<>();
5052
factories.add(ProjectInformationsContextEntry.factory(project));
5153
factories.add(FileTreeContextEntry.factory(editorInput));
5254
factories.add(DependenciesContextEntry.factory(project));
5355
factories.add(OpenEditorsContextEntry.factory());
5456
factories.add(StickyContextEntry.factory());
55-
factories.add(UserContextEntry.factory());
57+
factories.add(UserContextEntry.factory(path));
5658
if (compilationUnitOptional.isPresent()) {
5759
final ICompilationUnit unit = compilationUnitOptional.get();
5860
factories.add(SuperContextEntry.factory(unit, offset));

plugin/src/de/hetzge/eclipse/aicoder/context/UserContextEntry.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
package de.hetzge.eclipse.aicoder.context;
22

3+
import java.nio.file.Path;
34
import java.time.Duration;
45
import java.util.List;
6+
import java.util.stream.Collectors;
57

68
import de.hetzge.eclipse.aicoder.preferences.ContextPreferences;
79

810
public class UserContextEntry extends ContextEntry {
911
public static final String PREFIX = "USER";
12+
private final Path path;
1013

11-
private UserContextEntry(List<CustomContextEntry> childContextEntries, Duration creationDuration) {
14+
private UserContextEntry(List<CustomContextEntry> childContextEntries, Path path, Duration creationDuration) {
1215
super(childContextEntries, creationDuration);
16+
this.path = path;
1317
}
1418

1519
@Override
@@ -24,16 +28,19 @@ public String getLabel() {
2428

2529
@Override
2630
public String getContent(ContextContext context) {
27-
return super.getContent(context) + "\n";
31+
return this.childContextEntries.stream()
32+
.filter(entry -> ((CustomContextEntry) entry).matches(this.path))
33+
.map(entry -> apply(entry, context))
34+
.collect(Collectors.joining()) + "\n";
2835
}
2936

30-
public static ContextEntryFactory factory() {
31-
return new ContextEntryFactory(PREFIX, () -> create());
37+
public static ContextEntryFactory factory(Path path) {
38+
return new ContextEntryFactory(PREFIX, () -> create(path));
3239
}
3340

34-
public static UserContextEntry create() {
41+
public static UserContextEntry create(Path path) {
3542
final long before = System.currentTimeMillis();
3643
final List<CustomContextEntry> entries = ContextPreferences.getCustomContextEntries();
37-
return new UserContextEntry(entries, Duration.ofMillis(System.currentTimeMillis() - before));
44+
return new UserContextEntry(entries, path, Duration.ofMillis(System.currentTimeMillis() - before));
3845
}
3946
}

0 commit comments

Comments
 (0)