-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(codeCompletion): Optimize FIM request to get appropriate suffixR…
…eplaceRange (#3525) * feat(codeCompletion): enhance suffix handling with unpaired brackets and semicolons * test(codeCompletion): add tests for calculateReplaceRangeBySemiColon handling semicolons * refactor(codeCompletion): remove unnecessary logger.info statements and add TODO for string utility * test(codeCompletion): expand tests for calculateReplaceRangeBySemiColon to cover various semicolon scenarios * refactor(codeCompletion): simplify suffix handling and improve line end detection * refactor(string): remove redundant handling of semicolons in findUnpairedAutoClosingChars
- Loading branch information
Showing
7 changed files
with
216 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
clients/tabby-agent/src/codeCompletion/postprocess/calculateReplaceRange.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import { PostprocessFilter } from "./base"; | ||
import { CompletionItem } from "../solution"; | ||
import { calculateReplaceRangeByBracketStack } from "./calculateReplaceRangeByBracketStack"; | ||
import { calculateReplaceRangeBySemiColon } from "./calculateReplaceRangeBySemiColon"; | ||
|
||
export function calculateReplaceRange(): PostprocessFilter { | ||
return async (item: CompletionItem): Promise<CompletionItem> => { | ||
return calculateReplaceRangeByBracketStack(item); | ||
const afterBracket = calculateReplaceRangeByBracketStack(item); | ||
return calculateReplaceRangeBySemiColon(afterBracket); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 8 additions & 11 deletions
19
clients/tabby-agent/src/codeCompletion/postprocess/calculateReplaceRangeByBracketStack.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
clients/tabby-agent/src/codeCompletion/postprocess/calculateReplaceRangeBySemiColon.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import { documentContext, inline, assertFilterResult } from "./testUtils"; | ||
import { calculateReplaceRangeBySemiColon } from "./calculateReplaceRangeBySemiColon"; | ||
|
||
describe("postprocess", () => { | ||
describe("calculateReplaceRangeBySemiColon", () => { | ||
const filter = calculateReplaceRangeBySemiColon; | ||
|
||
it("should handle semicolon in string concatenation", async () => { | ||
const context = documentContext` | ||
const content = "hello world"; | ||
const a = "nihao" + ║; | ||
`; | ||
context.language = "typescript"; | ||
const completion = { | ||
text: inline` | ||
├content;┤ | ||
`, | ||
}; | ||
const expected = { | ||
text: inline` | ||
├content;┤ | ||
`, | ||
replaceSuffix: ";", | ||
}; | ||
await assertFilterResult(filter, context, completion, expected); | ||
}); | ||
|
||
it("should handle semicolon at the end of a statement", async () => { | ||
const context = documentContext` | ||
const content = "hello world"║; | ||
`; | ||
context.language = "typescript"; | ||
const completion = { | ||
text: inline` | ||
├;┤ | ||
`, | ||
}; | ||
const expected = { | ||
text: inline` | ||
├;┤ | ||
`, | ||
replaceSuffix: ";", | ||
}; | ||
await assertFilterResult(filter, context, completion, expected); | ||
}); | ||
|
||
it("should not handle any semicolon at the end of a statement", async () => { | ||
const context = documentContext` | ||
const content = "hello world"║ | ||
`; | ||
context.language = "typescript"; | ||
const completion = { | ||
text: inline` | ||
├┤ | ||
`, | ||
}; | ||
const expected = { | ||
text: inline` | ||
├┤ | ||
`, | ||
replaceSuffix: "", | ||
}; | ||
await assertFilterResult(filter, context, completion, expected); | ||
}); | ||
|
||
it("should not modify if no semicolon in completion text", async () => { | ||
const context = documentContext` | ||
const content = "hello world"║ | ||
`; | ||
context.language = "typescript"; | ||
const completion = { | ||
text: inline` | ||
├content┤ | ||
`, | ||
}; | ||
const expected = { | ||
text: inline` | ||
├content┤ | ||
`, | ||
replaceSuffix: "", | ||
}; | ||
await assertFilterResult(filter, context, completion, expected); | ||
}); | ||
|
||
it("should handle multiple semicolons in completion text", async () => { | ||
const context = documentContext` | ||
const content = "hello world"║; | ||
`; | ||
context.language = "typescript"; | ||
const completion = { | ||
text: inline` | ||
├content;;┤ | ||
`, | ||
}; | ||
const expected = { | ||
text: inline` | ||
├content;;┤ | ||
`, | ||
replaceSuffix: ";", | ||
}; | ||
await assertFilterResult(filter, context, completion, expected); | ||
}); | ||
|
||
it("should handle semicolon in the middle of a statement", async () => { | ||
const context = documentContext` | ||
const content = "hello; world"║; | ||
`; | ||
context.language = "typescript"; | ||
const completion = { | ||
text: inline` | ||
├content;┤ | ||
`, | ||
}; | ||
const expected = { | ||
text: inline` | ||
├content;┤ | ||
`, | ||
replaceSuffix: ";", | ||
}; | ||
await assertFilterResult(filter, context, completion, expected); | ||
}); | ||
}); | ||
}); |
34 changes: 34 additions & 0 deletions
34
clients/tabby-agent/src/codeCompletion/postprocess/calculateReplaceRangeBySemiColon.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { isBlank } from "../../utils/string"; | ||
import { CompletionItem } from "../solution"; | ||
import { logger } from "./base"; | ||
|
||
export function calculateReplaceRangeBySemiColon(item: CompletionItem): CompletionItem { | ||
const context = item.context; | ||
const { currentLineSuffix } = context; | ||
const suffixText = currentLineSuffix.trimEnd(); | ||
|
||
if (isBlank(suffixText)) { | ||
return item; | ||
} | ||
|
||
const completionText = item.text.trimEnd(); | ||
if (!completionText.includes(";")) { | ||
return item; | ||
} | ||
|
||
if (suffixText.startsWith(";") && completionText.endsWith(";")) { | ||
const modified = item.withSuffix(";"); | ||
|
||
logger.trace("Adjust replace range by semicolon.", { | ||
position: context.position, | ||
currentLineSuffix, | ||
completionText: item.text, | ||
modifiedText: item.text, | ||
replaceSuffix: modified.replaceSuffix, | ||
}); | ||
|
||
return modified; | ||
} | ||
|
||
return item; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters