Skip to content

Commit

Permalink
Codegen: Improve Formatter for better field name handling (#5534)
Browse files Browse the repository at this point in the history
CLOSE
https://linear.app/sourcegraph/issue/CODY-3696/codegen-generated-invalid-field-name-causing-jetbrains-build-to-fail

This PR fixes the issue where codegen does not escape all the invalid
characters for field names.

- Replace non-alphanumeric characters with underscores for Kotlin field
names
- Add helper function `getEscapedValue` to handle various field name
formatting rules

Also removed the bindings from the
agent/bindings/kotlin/lib/bin/main/com/sourcegraph/cody/agent/protocol_generated
directory

## Test plan

<!-- Required. See
https://docs-legacy.sourcegraph.com/dev/background-information/testing_principles.
-->

Run the generate agent bindings command to confirm everything works as
expected.
Some before and after comparison:
before:
const val `recently used` = "recently used"
const val `https__/example.com` = "https://example.com"

after:
const val `recently-used` = "recently used"
const val `https-example-com` = "https://example.com"


## Changelog

<!-- OPTIONAL; info at
https://www.notion.so/sourcegraph/Writing-a-changelog-entry-dd997f411d524caabf0d8d38a24a878c
-->
  • Loading branch information
abeatrix authored Sep 10, 2024
1 parent 503154d commit ea37b2b
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 190 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ object Constants {
const val openctx = "openctx"
const val power = "power"
const val pro = "pro"
const val `recently used` = "recently used"
const val `recently-used` = "recently used"
const val recommended = "recommended"
const val `rename-file` = "rename-file"
const val replace = "replace"
Expand Down
18 changes: 16 additions & 2 deletions agent/src/cli/scip-codegen/Formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,13 @@ export class Formatter {
if (this.language === TargetLanguage.Kotlin) {
const isKeyword = this.kotlinKeywords.has(escaped)
const needsBacktick = isKeyword || !/^[a-zA-Z0-9_]+$/.test(escaped)
return needsBacktick ? `\`${escaped}\`` : escaped
// Replace all non-alphanumeric characters with underscores
const fieldName = getEscapedValue(escaped, '-')
return needsBacktick ? `\`${fieldName}\`` : fieldName
}
// CSharp
if (this.language === TargetLanguage.CSharp) {
return escaped
return getEscapedValue(escaped)
.split('_')
.map(part => part.charAt(0).toUpperCase() + part.slice(1))
.join('')
Expand Down Expand Up @@ -297,3 +300,14 @@ export class Formatter {
return `${capitalize(name)}Enum`
}
}

function getEscapedValue(name: string, replacer: '_' | '-' = '_'): string {
const nonAlphanumericRegex = replacer === '-' ? /[^a-zA-Z0-9]+/g : /[^a-zA-Z0-9]/g
const repeatedReplacerRegex = new RegExp(`${replacer}+`, 'g')
const trimReplacerRegex = new RegExp(`^${replacer}|${replacer}$`, 'g')

return name
.replace(nonAlphanumericRegex, replacer)
.replace(repeatedReplacerRegex, replacer)
.replace(trimReplacerRegex, '')
}

0 comments on commit ea37b2b

Please sign in to comment.