Skip to content

Commit 8760360

Browse files
committed
Separate context data from context entry
1 parent 51d15bf commit 8760360

File tree

7 files changed

+218
-198
lines changed

7 files changed

+218
-198
lines changed

plugin/src/de/hetzge/eclipse/aicoder/ContextView.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import de.hetzge.eclipse.aicoder.context.ContextEntry;
5151
import de.hetzge.eclipse.aicoder.context.ContextEntryKey;
5252
import de.hetzge.eclipse.aicoder.context.CustomContextEntry;
53+
import de.hetzge.eclipse.aicoder.context.CustomContextEntryData;
5354
import de.hetzge.eclipse.aicoder.context.EmptyContextEntry;
5455
import de.hetzge.eclipse.aicoder.context.UserContextEntry;
5556
import de.hetzge.eclipse.aicoder.handler.ToggleMultilineHandler;
@@ -133,9 +134,9 @@ private void fillContextMenu(IMenuManager manager) {
133134
public void run() {
134135
final CustomContextEntryDialog dialog = new CustomContextEntryDialog(ContextView.this.viewer.getControl().getShell(), null);
135136
if (dialog.open() == Dialog.OK) {
136-
final CustomContextEntry newEntry = dialog.createEntry();
137-
final List<CustomContextEntry> currentEntries = ContextPreferences.getCustomContextEntries();
138-
final List<CustomContextEntry> newEntries = new ArrayList<>(currentEntries);
137+
final CustomContextEntryData newEntry = dialog.createEntry();
138+
final List<CustomContextEntryData> currentEntries = ContextPreferences.getCustomContextEntryDatas();
139+
final List<CustomContextEntryData> newEntries = new ArrayList<>(currentEntries);
139140
newEntries.add(newEntry);
140141
ContextPreferences.setCustomContextEntries(newEntries);
141142
ContextView.this.viewer.refresh(firstEntry);
@@ -151,9 +152,9 @@ public void run() {
151152
final CustomContextEntry customEntry = (CustomContextEntry) firstEntry;
152153
final CustomContextEntryDialog dialog = new CustomContextEntryDialog(ContextView.this.viewer.getControl().getShell(), customEntry);
153154
if (dialog.open() == Dialog.OK) {
154-
final CustomContextEntry editedEntry = dialog.createEntry();
155-
final List<CustomContextEntry> currentEntries = ContextPreferences.getCustomContextEntries();
156-
final List<CustomContextEntry> newEntries = new ArrayList<>(currentEntries);
155+
final CustomContextEntryData editedEntry = dialog.createEntry();
156+
final List<CustomContextEntryData> currentEntries = ContextPreferences.getCustomContextEntryDatas();
157+
final List<CustomContextEntryData> newEntries = new ArrayList<>(currentEntries);
157158
// Replace the existing entry with the edited one
158159
for (int i = 0; i < newEntries.size(); i++) {
159160
if (newEntries.get(i).getId().equals(editedEntry.getId())) {
@@ -174,8 +175,8 @@ public void run() {
174175
public void run() {
175176
final CustomContextEntry customEntry = (CustomContextEntry) firstEntry;
176177
if (MessageDialog.openConfirm(ContextView.this.viewer.getControl().getShell(), "Confirm", "Are you sure?")) {
177-
final List<CustomContextEntry> currentEntries = ContextPreferences.getCustomContextEntries();
178-
final List<CustomContextEntry> newEntries = currentEntries.stream().filter(it -> !Objects.equals(customEntry, it)).toList();
178+
final List<CustomContextEntryData> currentDatas = ContextPreferences.getCustomContextEntryDatas();
179+
final List<CustomContextEntryData> newEntries = currentDatas.stream().filter(it -> !Objects.equals(customEntry.getData(), it)).toList();
179180
ContextPreferences.setCustomContextEntries(newEntries);
180181
ContextView.this.viewer.refresh(firstEntry);
181182
}
Lines changed: 96 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package de.hetzge.eclipse.aicoder;
22

3-
import java.time.Duration;
43
import java.util.List;
54
import java.util.UUID;
65

@@ -15,105 +14,104 @@
1514
import org.eclipse.swt.widgets.Shell;
1615
import org.eclipse.swt.widgets.Text;
1716

18-
import de.hetzge.eclipse.aicoder.context.ContextContext;
1917
import de.hetzge.eclipse.aicoder.context.CustomContextEntry;
18+
import de.hetzge.eclipse.aicoder.context.CustomContextEntryData;
2019

2120
public class CustomContextEntryDialog extends Dialog {
2221

23-
private Text titleText;
24-
private Text contentText;
25-
private Text globText;
26-
27-
private String title;
28-
private String content;
29-
private String glob;
30-
31-
private final CustomContextEntry existingEntry;
32-
33-
public CustomContextEntryDialog(Shell parentShell, CustomContextEntry existingEntry) {
34-
super(parentShell);
35-
this.existingEntry = existingEntry;
36-
if (existingEntry != null) {
37-
this.title = existingEntry.getTitle();
38-
this.content = existingEntry.getContent(new ContextContext());
39-
this.glob = existingEntry.getGlob();
40-
}
41-
}
42-
43-
@Override
44-
protected Control createDialogArea(Composite parent) {
45-
final Composite container = (Composite) super.createDialogArea(parent);
46-
final GridLayout layout = new GridLayout(2, false);
47-
container.setLayout(layout);
48-
49-
// Title
50-
final Label titleLabel = new Label(container, SWT.NONE);
51-
titleLabel.setText("Title:");
52-
53-
this.titleText = new Text(container, SWT.BORDER);
54-
this.titleText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
55-
if (this.title != null) {
56-
this.titleText.setText(this.title);
57-
}
58-
59-
// Glob pattern
60-
final Label globLabel = new Label(container, SWT.NONE);
61-
globLabel.setText("Glob Pattern:");
62-
63-
this.globText = new Text(container, SWT.BORDER);
64-
this.globText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
65-
if (this.glob != null) {
66-
this.globText.setText(this.glob);
67-
}
68-
69-
// Content
70-
final Label contentLabel = new Label(container, SWT.NONE);
71-
contentLabel.setText("Content:");
72-
contentLabel.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false));
73-
74-
this.contentText = new Text(container, SWT.BORDER | SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
75-
final GridData contentData = new GridData(GridData.FILL_BOTH);
76-
contentData.heightHint = 200; // Reasonable default height
77-
this.contentText.setLayoutData(contentData);
78-
if (this.content != null) {
79-
this.contentText.setText(this.content);
80-
}
81-
82-
return container;
83-
}
84-
85-
@Override
86-
protected void configureShell(Shell shell) {
87-
super.configureShell(shell);
88-
shell.setText(this.existingEntry != null ? "Edit Custom Context Entry" : "New Custom Context Entry");
89-
shell.setMinimumSize(600, 400); // Set a wider minimum size
90-
}
91-
92-
@Override
93-
protected void createButtonsForButtonBar(Composite parent) {
94-
createButton(parent, IDialogConstants.OK_ID, "Save", true);
95-
createButton(parent, IDialogConstants.CANCEL_ID, "Cancel", false);
96-
}
97-
98-
@Override
99-
protected void okPressed() {
100-
// Save the values
101-
this.title = this.titleText.getText().trim();
102-
this.content = this.contentText.getText();
103-
this.glob = this.globText.getText().trim();
104-
105-
super.okPressed();
106-
}
107-
108-
public CustomContextEntry createEntry() {
109-
final UUID id = this.existingEntry != null ? this.existingEntry.getId() : UUID.randomUUID();
110-
return new CustomContextEntry(
111-
this.existingEntry != null ? this.existingEntry.getChildContextEntries() : List.of(),
112-
id,
113-
this.title,
114-
this.content,
115-
this.glob,
116-
Duration.ZERO
117-
);
118-
}
22+
private Text titleText;
23+
private Text contentText;
24+
private Text globText;
25+
26+
private String title;
27+
private String content;
28+
private String glob;
29+
30+
private final CustomContextEntry existingEntry;
31+
32+
public CustomContextEntryDialog(Shell parentShell, CustomContextEntry existingEntry) {
33+
super(parentShell);
34+
this.existingEntry = existingEntry;
35+
if (existingEntry != null) {
36+
final CustomContextEntryData data = existingEntry.getData();
37+
this.title = data.getTitle();
38+
this.content = data.getContent();
39+
this.glob = data.getGlob();
40+
}
41+
}
42+
43+
@Override
44+
protected Control createDialogArea(Composite parent) {
45+
final Composite container = (Composite) super.createDialogArea(parent);
46+
final GridLayout layout = new GridLayout(2, false);
47+
container.setLayout(layout);
48+
49+
// Title
50+
final Label titleLabel = new Label(container, SWT.NONE);
51+
titleLabel.setText("Title:");
52+
53+
this.titleText = new Text(container, SWT.BORDER);
54+
this.titleText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
55+
if (this.title != null) {
56+
this.titleText.setText(this.title);
57+
}
58+
59+
// Glob pattern
60+
final Label globLabel = new Label(container, SWT.NONE);
61+
globLabel.setText("Glob Pattern:");
62+
63+
this.globText = new Text(container, SWT.BORDER);
64+
this.globText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
65+
if (this.glob != null) {
66+
this.globText.setText(this.glob);
67+
}
68+
69+
// Content
70+
final Label contentLabel = new Label(container, SWT.NONE);
71+
contentLabel.setText("Content:");
72+
contentLabel.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false));
73+
74+
this.contentText = new Text(container, SWT.BORDER | SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
75+
final GridData contentData = new GridData(GridData.FILL_BOTH);
76+
contentData.heightHint = 200; // Reasonable default height
77+
this.contentText.setLayoutData(contentData);
78+
if (this.content != null) {
79+
this.contentText.setText(this.content);
80+
}
81+
82+
return container;
83+
}
84+
85+
@Override
86+
protected void configureShell(Shell shell) {
87+
super.configureShell(shell);
88+
shell.setText(this.existingEntry != null ? "Edit Custom Context Entry" : "New Custom Context Entry");
89+
shell.setMinimumSize(600, 400); // Set a wider minimum size
90+
}
91+
92+
@Override
93+
protected void createButtonsForButtonBar(Composite parent) {
94+
createButton(parent, IDialogConstants.OK_ID, "Save", true);
95+
createButton(parent, IDialogConstants.CANCEL_ID, "Cancel", false);
96+
}
97+
98+
@Override
99+
protected void okPressed() {
100+
// Save the values
101+
this.title = this.titleText.getText().trim();
102+
this.content = this.contentText.getText();
103+
this.glob = this.globText.getText().trim();
104+
105+
super.okPressed();
106+
}
107+
108+
public CustomContextEntryData createEntry() {
109+
final UUID id = this.existingEntry != null ? this.existingEntry.getData().getId() : UUID.randomUUID();
110+
return new CustomContextEntryData(
111+
id,
112+
this.existingEntry != null ? this.existingEntry.getData().getChildren() : List.of(),
113+
this.title,
114+
this.content,
115+
this.glob);
116+
}
119117
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public static Optional<? extends ContextEntry> create(ContextEntryKey key) throw
8383
LambdaExceptionUtils.rethrowFunction(TypeMemberContextEntry::create),
8484
LambdaExceptionUtils.rethrowFunction(PackageContextEntry::create),
8585
LambdaExceptionUtils.rethrowFunction(TypeContextEntry::create),
86-
LambdaExceptionUtils.rethrowFunction(CustomContextEntry::create));
86+
LambdaExceptionUtils.rethrowFunction(CustomContextEntryData::create));
8787
return factories.stream().flatMap(factory -> factory.apply(key).stream()).findFirst();
8888
}
8989
}
Lines changed: 17 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,43 @@
11
package de.hetzge.eclipse.aicoder.context;
22

3-
import java.nio.file.FileSystems;
4-
import java.nio.file.Path;
53
import java.time.Duration;
64
import java.util.List;
7-
import java.util.Objects;
8-
import java.util.Optional;
9-
import java.util.UUID;
10-
11-
import de.hetzge.eclipse.aicoder.preferences.ContextPreferences;
12-
import mjson.Json;
135

146
public class CustomContextEntry extends ContextEntry {
157
public static final String PREFIX = "CUSTOM";
168

17-
private final List<CustomContextEntry> childContextEntries;
18-
private final UUID id;
19-
private final String title;
20-
private final String content;
21-
private final String glob;
22-
23-
public CustomContextEntry(List<CustomContextEntry> childContextEntries, UUID id, String title, String content, String glob, Duration creationDuration) {
24-
super(childContextEntries, creationDuration);
25-
this.childContextEntries = childContextEntries;
26-
this.id = id;
27-
this.title = title;
28-
this.content = content;
29-
this.glob = glob;
30-
}
9+
private final CustomContextEntryData data;
10+
private final boolean active;
3111

32-
@Override
33-
public String getContent(ContextContext context) {
34-
return String.format("%s\n%s\n", this.content, super.getContent(context));
12+
public CustomContextEntry(CustomContextEntryData data, boolean active) {
13+
super(List.of(), Duration.ZERO);
14+
this.data = data;
15+
this.active = active;
3516
}
3617

3718
@Override
3819
public ContextEntryKey getKey() {
39-
return new ContextEntryKey(PREFIX, this.id.toString());
20+
return new ContextEntryKey(PREFIX, this.data.getId().toString());
4021
}
4122

4223
@Override
4324
public String getLabel() {
44-
return this.title;
25+
return "%s %s".formatted(this.active ? "✅" : "❌", this.data.getTitle());
4526
}
4627

4728
@Override
48-
public List<CustomContextEntry> getChildContextEntries() {
49-
return this.childContextEntries;
50-
}
51-
52-
public UUID getId() {
53-
return this.id;
54-
}
55-
56-
public String getTitle() {
57-
return this.title;
58-
}
59-
60-
public String getGlob() {
61-
return this.glob;
62-
}
63-
64-
public boolean matches(Path path) {
65-
if (this.glob == null) {
66-
return false;
29+
public String getContent(ContextContext context) {
30+
if (!this.active) {
31+
return "";
6732
}
68-
return FileSystems.getDefault().getPathMatcher("glob:" + this.glob).matches(path);
69-
}
70-
71-
public Json toJson() {
72-
return Json.object()
73-
.set("children", this.childContextEntries.stream().map(CustomContextEntry::toJson).toList())
74-
.set("id", this.id.toString())
75-
.set("title", this.title)
76-
.set("content", this.content)
77-
.set("glob", this.glob);
33+
return String.format("%s\n%s", this.data.getContent(), super.getContent(context));
7834
}
7935

80-
public static Optional<CustomContextEntry> create(ContextEntryKey key) {
81-
return ContextPreferences.getCustomContextEntries().stream().filter(it -> Objects.equals(it.getId().toString(), key.value())).findFirst();
36+
public CustomContextEntryData getData() {
37+
return this.data;
8238
}
8339

84-
public static CustomContextEntry createFromJson(Json json) {
85-
final long before = System.currentTimeMillis();
86-
final List<CustomContextEntry> childEntries = json.at("children").asJsonList().stream().map(CustomContextEntry::createFromJson).toList();
87-
final UUID id = UUID.fromString(json.at("id").asString());
88-
final String title = json.at("title").asString();
89-
final String content = json.at("content").asString();
90-
final String glob = json.at("glob").asString();
91-
return new CustomContextEntry(childEntries, id, title, content, glob, Duration.ofMillis(System.currentTimeMillis() - before));
40+
public boolean isActive() {
41+
return this.active;
9242
}
93-
}
43+
}

0 commit comments

Comments
 (0)