forked from TabbyML/tabby
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(agent): format indentation if not match with editor config. (Tab…
- Loading branch information
Showing
5 changed files
with
287 additions
and
0 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
166 changes: 166 additions & 0 deletions
166
clients/tabby-agent/src/postprocess/formatIndentation.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,166 @@ | ||
import { expect } from "chai"; | ||
import { documentContext, inline } from "./testUtils"; | ||
import { formatIndentation } from "./formatIndentation"; | ||
|
||
describe("postprocess", () => { | ||
describe("formatIndentation", () => { | ||
it("should format indentation if first line of completion is over indented.", () => { | ||
const context = { | ||
...documentContext` | ||
function clamp(n: number, max: number, min: number): number { | ||
║ | ||
} | ||
`, | ||
indentation: " ", | ||
language: "typescript", | ||
}; | ||
const completion = inline` | ||
├ return Math.max(Math.min(n, max), min);┤ | ||
`; | ||
const expected = inline` | ||
├return Math.max(Math.min(n, max), min);┤ | ||
`; | ||
expect(formatIndentation(context)(completion)).to.eq(expected); | ||
}); | ||
|
||
it("should format indentation if first line of completion is wrongly indented.", () => { | ||
const context = { | ||
...documentContext` | ||
function clamp(n: number, max: number, min: number): number { | ||
║ | ||
} | ||
`, | ||
indentation: " ", | ||
language: "typescript", | ||
}; | ||
const completion = inline` | ||
├ return Math.max(Math.min(n, max), min);┤ | ||
`; | ||
const expected = inline` | ||
├ return Math.max(Math.min(n, max), min);┤ | ||
`; | ||
expect(formatIndentation(context)(completion)).to.eq(expected); | ||
}); | ||
|
||
it("should format indentation if completion lines is over indented.", () => { | ||
const context = { | ||
...documentContext` | ||
def findMax(arr):║ | ||
`, | ||
indentation: " ", | ||
language: "python", | ||
}; | ||
const completion = inline` | ||
├ | ||
max = arr[0] | ||
for i in range(1, len(arr)): | ||
if arr[i] > max: | ||
max = arr[i] | ||
return max | ||
}┤ | ||
`; | ||
const expected = inline` | ||
├ | ||
max = arr[0] | ||
for i in range(1, len(arr)): | ||
if arr[i] > max: | ||
max = arr[i] | ||
return max | ||
}┤ | ||
`; | ||
expect(formatIndentation(context)(completion)).to.eq(expected); | ||
}); | ||
|
||
it("should format indentation if completion lines is wrongly indented.", () => { | ||
const context = { | ||
...documentContext` | ||
def findMax(arr):║ | ||
`, | ||
indentation: " ", | ||
language: "python", | ||
}; | ||
const completion = inline` | ||
├ | ||
max = arr[0] | ||
for i in range(1, len(arr)): | ||
if arr[i] > max: | ||
max = arr[i] | ||
return max | ||
}┤ | ||
`; | ||
const expected = inline` | ||
├ | ||
max = arr[0] | ||
for i in range(1, len(arr)): | ||
if arr[i] > max: | ||
max = arr[i] | ||
return max | ||
}┤ | ||
`; | ||
expect(formatIndentation(context)(completion)).to.eq(expected); | ||
}); | ||
|
||
it("should keep it unchanged if it no indentation specified.", () => { | ||
const context = { | ||
...documentContext` | ||
def findMax(arr):║ | ||
`, | ||
indentation: undefined, | ||
language: "python", | ||
}; | ||
const completion = inline` | ||
├ | ||
max = arr[0] | ||
for i in range(1, len(arr)): | ||
if arr[i] > max: | ||
max = arr[i] | ||
return max | ||
}┤ | ||
`; | ||
expect(formatIndentation(context)(completion)).to.eq(completion); | ||
}); | ||
|
||
it("should keep it unchanged if there is indentation in the context.", () => { | ||
const context = { | ||
...documentContext` | ||
def hello(): | ||
return "world" | ||
def findMax(arr):║ | ||
`, | ||
indentation: "\t", | ||
language: "python", | ||
}; | ||
const completion = inline` | ||
├ | ||
max = arr[0] | ||
for i in range(1, len(arr)): | ||
if arr[i] > max: | ||
max = arr[i] | ||
return max | ||
}┤ | ||
`; | ||
expect(formatIndentation(context)(completion)).to.eq(completion); | ||
}); | ||
|
||
it("should keep it unchanged if it is well indented.", () => { | ||
const context = { | ||
...documentContext` | ||
def findMax(arr):║ | ||
`, | ||
indentation: " ", | ||
language: "python", | ||
}; | ||
const completion = inline` | ||
├ | ||
max = arr[0] | ||
for i in range(1, len(arr)): | ||
if arr[i] > max: | ||
max = arr[i] | ||
return max | ||
}┤ | ||
`; | ||
expect(formatIndentation(context)(completion)).to.eq(completion); | ||
}); | ||
}); | ||
}); |
100 changes: 100 additions & 0 deletions
100
clients/tabby-agent/src/postprocess/formatIndentation.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,100 @@ | ||
import { CompletionContext } from "../Agent"; | ||
import { PostprocessFilter, logger } from "./base"; | ||
import { isBlank, splitLines } from "../utils"; | ||
|
||
function detectIndentation(lines: string[]): string | null { | ||
const matches = { | ||
"\t": 0, | ||
" ": 0, | ||
" ": 0, | ||
}; | ||
for (const line of lines) { | ||
if (line.match(/^\t/)) { | ||
matches["\t"]++; | ||
} else { | ||
const spaces = line.match(/^ */)[0].length; | ||
if (spaces > 0) { | ||
if (spaces % 4 === 0) { | ||
matches[" "]++; | ||
} | ||
if (spaces % 2 === 0) { | ||
matches[" "]++; | ||
} | ||
} | ||
} | ||
} | ||
if (matches["\t"] > 0) { | ||
return "\t"; | ||
} | ||
if (matches[" "] > matches[" "]) { | ||
return " "; | ||
} | ||
if (matches[" "] > 0) { | ||
return " "; | ||
} | ||
return null; | ||
} | ||
|
||
function getIndentLevel(line: string, indentation: string): number { | ||
if (indentation === "\t") { | ||
return line.match(/^\t*/g)[0].length; | ||
} else { | ||
const spaces = line.match(/^ */)[0].length; | ||
return spaces / indentation.length; | ||
} | ||
} | ||
|
||
export function formatIndentation(context: CompletionContext): PostprocessFilter { | ||
return (input) => { | ||
const { prefixLines, suffixLines, indentation } = context; | ||
const inputLines = splitLines(input); | ||
|
||
// if no indentation is specified | ||
if (!indentation) { | ||
return input; | ||
} | ||
|
||
// if there is any indentation in context, the server output should have learned from it | ||
const prefixLinesForDetection = isBlank(prefixLines[prefixLines.length - 1]) | ||
? prefixLines.slice(0, prefixLines.length - 1) | ||
: prefixLines; | ||
if (prefixLines.length > 1 && detectIndentation(prefixLinesForDetection) !== null) { | ||
return input; | ||
} | ||
const suffixLinesForDetection = suffixLines.slice(1); | ||
if (suffixLines.length > 1 && detectIndentation(suffixLinesForDetection) !== null) { | ||
return input; | ||
} | ||
|
||
// if the input is well indented with specific indentation | ||
const inputLinesForDetection = inputLines.map((line, index) => { | ||
return index === 0 ? prefixLines[prefixLines.length - 1] + line : line; | ||
}); | ||
const inputIndentation = detectIndentation(inputLinesForDetection); | ||
if (inputIndentation === null || inputIndentation === indentation) { | ||
return input; | ||
} | ||
|
||
// otherwise, do formatting | ||
const formatted = inputLinesForDetection.map((line, index) => { | ||
const level = getIndentLevel(inputLinesForDetection[index], inputIndentation); | ||
if (level === 0) { | ||
return inputLines[index]; | ||
} | ||
const rest = line.slice(inputIndentation.length * level); | ||
if (index === 0) { | ||
// for first line | ||
if (!isBlank(prefixLines[prefixLines.length - 1])) { | ||
return inputLines[0]; | ||
} else { | ||
return indentation.repeat(level).slice(prefixLines[prefixLines.length - 1].length) + rest; | ||
} | ||
} else { | ||
// for next lines | ||
return indentation.repeat(level) + rest; | ||
} | ||
}); | ||
logger.debug({ prefixLines, suffixLines, inputLines, formatted }, "Format indentation."); | ||
return formatted.join(""); | ||
}; | ||
} |
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