Skip to content

Commit

Permalink
show context files on empty at-expression (#555)
Browse files Browse the repository at this point in the history
  • Loading branch information
beyang authored Feb 9, 2024
1 parent a1da1ee commit e4d470b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/main/java/com/sourcegraph/cody/PromptPanel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<AtExpression> {
val matches = atExpressionPattern.findAll(text)
val expressions = ArrayList<AtExpression>()
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(
Expand Down
7 changes: 6 additions & 1 deletion src/test/kotlin/com/sourcegraph/cody/PromptPanelTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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("[email protected]", listOf()),
)

for (case in cases) {
assertEquals(case.expected, findAtExpressions(case.text))
Expand Down

0 comments on commit e4d470b

Please sign in to comment.