Skip to content

fix: added/deleted lines is incorrect #1044

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Will be deleted or merged.
*/

import * as path from 'path'

Check warning on line 6 in server/aws-lsp-codewhisperer/src/language-server/agenticChat/agenticChatController.ts

View workflow job for this annotation

GitHub Actions / Test

Do not import Node.js builtin module "path"

Check warning on line 6 in server/aws-lsp-codewhisperer/src/language-server/agenticChat/agenticChatController.ts

View workflow job for this annotation

GitHub Actions / Test (Windows)

Do not import Node.js builtin module "path"
import {
ChatTriggerType,
GenerateAssistantResponseCommandInput,
Expand Down Expand Up @@ -97,7 +97,7 @@
import { workspaceUtils } from '@aws/lsp-core'
import { FsReadParams } from './tools/fsRead'
import { ListDirectoryParams } from './tools/listDirectory'
import { FsWrite, FsWriteParams } from './tools/fsWrite'
import { FsWriteParams, getDiffChanges } from './tools/fsWrite'

type ChatHandlers = Omit<
LspHandlers<Chat>,
Expand Down Expand Up @@ -496,10 +496,11 @@

async #getFsWriteChatResult(toolUse: ToolUse): Promise<ChatMessage> {
const input = toolUse.input as unknown as FsWriteParams
const fileName = path.basename(input.path)
// TODO: right now diff changes is coupled with fsWrite class, we should move it to shared utils
const fsWrite = new FsWrite(this.#features)
const diffChanges = await fsWrite.getDiffChanges(input)
const oldContent = this.#triggerContext.getToolUseLookup().get(toolUse.toolUseId!)?.oldContent ?? ''
const diffChanges = getDiffChanges(input, oldContent)
// TODO: support multi folder workspaces
const workspaceRoot = workspaceUtils.getWorkspaceFolderPaths(this.#features.lsp)[0]
const relativeFilePath = path.relative(workspaceRoot, input.path)
const changes = diffChanges.reduce(
(acc, { count = 0, added, removed }) => {
if (added) {
Expand All @@ -516,8 +517,8 @@
messageId: toolUse.toolUseId,
header: {
fileList: {
filePaths: [fileName],
details: { [fileName]: { changes } },
filePaths: [relativeFilePath],
details: { [relativeFilePath]: { changes } },
},
buttons: [{ id: 'undo-changes', text: 'Undo', icon: 'undo' }],
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AppendParams, CreateParams, FsWrite, InsertParams, StrReplaceParams } from './fsWrite'
import { AppendParams, CreateParams, FsWrite, getDiffChanges, InsertParams, StrReplaceParams } from './fsWrite'
import { testFolder } from '@aws/lsp-core'
import * as path from 'path'
import * as assert from 'assert'
Expand Down Expand Up @@ -392,113 +392,120 @@ describe('FsWrite Tool', function () {
await assert.rejects(() => fsWrite.invoke(params), /no such file or directory/)
})
})
})

describe('getDiffChanges', function () {
it('handles create case', async function () {
const testContent = 'newFileText'
const fsWrite = new FsWrite(features)
describe('getDiffChanges', function () {
let tempFolder: testFolder.TestFolder
before(async function () {
tempFolder = await testFolder.TestFolder.create()
})

const filepath = path.join(tempFolder.path, 'testFile.txt')

const result = await fsWrite.getDiffChanges({ command: 'create', path: filepath, fileText: testContent })
assert.deepStrictEqual(result, [
{
added: true,
count: 1,
removed: false,
value: testContent,
},
])
})
it('handles create case', async function () {
const testContent = 'newFileText'

it('handles replace case', async function () {
const fsWrite = new FsWrite(features)
const content = 'replace this old word'
const filepath = await tempFolder.write('testFile.txt', content)
const filepath = path.join(tempFolder.path, 'testFile.txt')

const result = await fsWrite.getDiffChanges({
const result = getDiffChanges({ command: 'create', path: filepath, fileText: testContent }, '')
assert.deepStrictEqual(result, [
{
added: true,
count: 1,
removed: false,
value: testContent,
},
])
})

it('handles replace case', async function () {
const content = 'replace this old word'
const filepath = await tempFolder.write('testFile.txt', content)

const result = getDiffChanges(
{
command: 'strReplace',
path: filepath,
oldStr: 'old',
newStr: 'new',
})
assert.deepStrictEqual(result, [
{
added: false,
count: 1,
removed: true,
value: content,
},
{
added: true,
count: 1,
removed: false,
value: content.replace('old', 'new'),
},
])
})
},
content
)
assert.deepStrictEqual(result, [
{
added: false,
count: 1,
removed: true,
value: content,
},
{
added: true,
count: 1,
removed: false,
value: content.replace('old', 'new'),
},
])
})

it('handles insert case', async function () {
const fsWrite = new FsWrite(features)
const content = 'line1 \n line2 \n line3'
const filepath = await tempFolder.write('testFile.txt', content)
it('handles insert case', async function () {
const content = 'line1 \n line2 \n line3'
const filepath = await tempFolder.write('testFile.txt', content)

const result = await fsWrite.getDiffChanges({
const result = getDiffChanges(
{
command: 'insert',
path: filepath,
insertLine: 2,
newStr: 'new text',
})

assert.deepStrictEqual(result, [
{
added: false,
count: 2,
removed: false,
value: 'line1 \n line2 \n',
},
{
added: true,
count: 1,
removed: false,
value: 'new text\n',
},
{
added: false,
count: 1,
removed: false,
value: ' line3',
},
])
})
},
content
)

assert.deepStrictEqual(result, [
{
added: false,
count: 2,
removed: false,
value: 'line1 \n line2 \n',
},
{
added: true,
count: 1,
removed: false,
value: 'new text\n',
},
{
added: false,
count: 1,
removed: false,
value: ' line3',
},
])
})

it('handles append case', async function () {
const fsWrite = new FsWrite(features)
const content = 'line1 \n line2'
const filepath = await tempFolder.write('testFile.txt', content)

const result = await fsWrite.getDiffChanges({ command: 'append', path: filepath, newStr: 'line3' })

assert.deepStrictEqual(result, [
{
added: false,
count: 1,
removed: false,
value: 'line1 \n',
},
{
added: false,
count: 1,
removed: true,
value: ' line2',
},
{
added: true,
count: 2,
removed: false,
value: ' line2\nline3',
},
])
})
it('handles append case', async function () {
const content = 'line1 \n line2'
const filepath = await tempFolder.write('testFile.txt', content)

const result = getDiffChanges({ command: 'append', path: filepath, newStr: 'line3' }, content)

assert.deepStrictEqual(result, [
{
added: false,
count: 1,
removed: false,
value: 'line1 \n',
},
{
added: false,
count: 1,
removed: true,
value: ' line2',
},
{
added: true,
count: 2,
removed: false,
value: ' line2\nline3',
},
])
})
})
Loading
Loading