Skip to content

Commit 7076bc8

Browse files
Fix for #309 prompt /rest gets fired even if the rest part is inbedded in other text (#313)
* Fix for #309 - Prompt "rest" gets fired even if the "rest"-part is inbedded in other text
1 parent 7c4d5d8 commit 7076bc8

1 file changed

Lines changed: 18 additions & 10 deletions

File tree

src/main/java/io/github/jeddict/ai/hints/AssistantChatManager.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -362,27 +362,35 @@ public void onSubmit() {
362362
}
363363
} else {
364364
result = null;
365-
String question = getQuestionPane().getText();
366-
Map<String, String> prompts = PreferencesManager.getInstance().getPrompts();
365+
final Map<String, String> prompts = PreferencesManager.getInstance().getPrompts();
367366

368367
//
369368
// To make sure the longest matching shortcut matches (i.e.
370369
// 'shortcutlong' is 'shortcut' is defined as well) let's
371370
// sort the shurtcuts in descending order; this guarantees
372371
// 'shortcut2' is matched before "shortcut" in the for loop
372+
// Replace only placeholders as delimited world to avoid
373+
// to detect a placeholder when it is part of a text (e.g.
374+
// a url)
373375
//
374-
ArrayList<String> promptKeys = new ArrayList();
376+
final ArrayList<String> promptKeys = new ArrayList();
375377
promptKeys.addAll(prompts.keySet());
376378
promptKeys.sort(Comparator.reverseOrder());
377379

380+
String question = getQuestionPane().getText();
378381
for (String key : promptKeys) {
379-
String prompt = prompts.get(key);
380-
381-
String toReplace = "/" + key;
382-
383-
if (question.contains(toReplace)) {
384-
question = question.replace(toReplace, prompt);
385-
}
382+
//
383+
// - (?<!\S)/ : A negative lookbehind that ensures the
384+
// character before /diff is not a non-whitespace
385+
// character (meaning it must be a space, a tab,
386+
// or the beginning of the string).
387+
// - key : the prompt to match
388+
// - (?!\S) : A negative lookahead that ensures the character
389+
// after /diff is not a non-whitespace character
390+
// (meaning it must be a space, a tab, or the
391+
// end of the string).
392+
//
393+
question = question.replaceAll("(?<!\\S)/" + key + "(?!\\S)", prompts.get(key));
386394
}
387395
if (!question.isEmpty()) {
388396
handlePrompt(question, true);

0 commit comments

Comments
 (0)