-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(agent): improve postprocess to trim whitespace. (#550)
* fix(agent): improve postprocess to trim white space. * fix: lint.
- Loading branch information
Showing
4 changed files
with
118 additions
and
2 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
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,83 @@ | ||
import { expect } from "chai"; | ||
import { documentContext, inline } from "./testUtils"; | ||
import { trimSpace } from "./trimSpace"; | ||
|
||
describe("postprocess", () => { | ||
describe("trimSpace", () => { | ||
it("should remove trailing space", () => { | ||
const context = { | ||
...documentContext` | ||
let foo = new ║ | ||
`, | ||
language: "javascript", | ||
}; | ||
const completion = inline` | ||
├Foo(); ┤ | ||
`; | ||
const expected = inline` | ||
├Foo();┤ | ||
`; | ||
expect(trimSpace(context)(completion)).to.eq(expected); | ||
}); | ||
|
||
it("should not remove trailing space if filling in line", () => { | ||
const context = { | ||
...documentContext` | ||
let foo = sum(║baz) | ||
`, | ||
language: "javascript", | ||
}; | ||
const completion = inline` | ||
├bar, ┤ | ||
`; | ||
expect(trimSpace(context)(completion)).to.eq(completion); | ||
}); | ||
|
||
it("should remove trailing space if filling in line with suffix starts with space", () => { | ||
const context = { | ||
...documentContext` | ||
let foo = sum(║ baz) | ||
`, | ||
language: "javascript", | ||
}; | ||
const completion = inline` | ||
├bar, ┤ | ||
`; | ||
const expected = inline` | ||
├bar,┤ | ||
`; | ||
expect(trimSpace(context)(completion)).to.eq(expected); | ||
}); | ||
|
||
it("should not remove leading space if current line is blank", () => { | ||
const context = { | ||
...documentContext` | ||
function sum(a, b) { | ||
║ | ||
} | ||
`, | ||
language: "javascript", | ||
}; | ||
const completion = inline` | ||
├ return a + b;┤ | ||
`; | ||
expect(trimSpace(context)(completion)).to.eq(completion); | ||
}); | ||
|
||
it("should remove leading space if current line is not blank and ends with space", () => { | ||
const context = { | ||
...documentContext` | ||
let foo = ║ | ||
`, | ||
language: "javascript", | ||
}; | ||
const completion = inline` | ||
├ sum(bar, baz);┤ | ||
`; | ||
const expected = inline` | ||
├sum(bar, baz);┤ | ||
`; | ||
expect(trimSpace(context)(completion)).to.eq(expected); | ||
}); | ||
}); | ||
}); |
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,24 @@ | ||
import { PostprocessFilter, PostprocessContext } from "./base"; | ||
import { splitLines, isBlank } from "../utils"; | ||
|
||
export const trimSpace: (context: PostprocessContext) => PostprocessFilter = (context) => { | ||
return (input) => { | ||
const { prefixLines, suffixLines } = context; | ||
const inputLines = splitLines(input); | ||
let trimmedInput = input; | ||
const prefixCurrentLine = prefixLines[prefixLines.length - 1] ?? ""; | ||
const suffixCurrentLine = suffixLines[0] ?? ""; | ||
if (!isBlank(prefixCurrentLine) && prefixCurrentLine.match(/\s$/)) { | ||
trimmedInput = trimmedInput.trimStart(); | ||
} | ||
|
||
if ( | ||
inputLines.length > 1 || | ||
isBlank(suffixCurrentLine) || | ||
(!isBlank(suffixCurrentLine) && suffixCurrentLine.match(/^\s/)) | ||
) { | ||
trimmedInput = trimmedInput.trimEnd(); | ||
} | ||
return trimmedInput; | ||
}; | ||
}; |
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