diff --git a/src/main/java/com/sourcegraph/cody/PromptPanel.kt b/src/main/java/com/sourcegraph/cody/PromptPanel.kt index 38ee37e97f..44b5c49d1a 100644 --- a/src/main/java/com/sourcegraph/cody/PromptPanel.kt +++ b/src/main/java/com/sourcegraph/cody/PromptPanel.kt @@ -269,13 +269,18 @@ data class AtExpression( val value: String ) -val atExpressionPattern = """(@(?:\\\s|[^\s])+)(?:\s|$)""".toRegex() +val atExpressionPattern = """(@(?:\\\s|\S)*)(?:\s|$)""".toRegex() fun findAtExpressions(text: String): List { val matches = atExpressionPattern.findAll(text) val expressions = ArrayList() for (match in matches) { - val subMatch = match.groups.get(1) + val mainMatch = match.groups[0] ?: continue + val prevIndex = mainMatch.range.first - 1 + // filter out things like email addresses + if (prevIndex >= 0 && !text[prevIndex].isWhitespace()) continue + + val subMatch = match.groups[1] if (subMatch != null) { val value = subMatch.value.substring(1).replace("\\ ", " ") expressions.add( diff --git a/src/test/kotlin/com/sourcegraph/cody/PromptPanelTest.kt b/src/test/kotlin/com/sourcegraph/cody/PromptPanelTest.kt index a7e83eab34..7731aa736d 100644 --- a/src/test/kotlin/com/sourcegraph/cody/PromptPanelTest.kt +++ b/src/test/kotlin/com/sourcegraph/cody/PromptPanelTest.kt @@ -33,7 +33,12 @@ class PromptPanelTest : TestCase() { "foo ".length + "@file\\ with\\ spaces".length, "@file\\ with\\ spaces", "file with spaces"), - ))) + )), + Case("@", listOf(AtExpression(0, 1, "@", ""))), + Case("foo @", listOf(AtExpression("foo ".length, "foo @".length, "@", ""))), + Case("@ foo", listOf(AtExpression(0, 1, "@", ""))), + Case("foo@email.com", listOf()), + ) for (case in cases) { assertEquals(case.expected, findAtExpressions(case.text))