Skip to content
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

fix(LS): rename where map doesn't exist #1780

Merged
merged 4 commits into from
Jul 16, 2024
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
67 changes: 66 additions & 1 deletion packages/language-server/src/__test__/rename/multi-file.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ test('rename model', async () => {
`)
})

test('rename field', async () => {
test('rename field - map exists', async () => {
const helper = await getMultifileHelper('user-posts')
const user = helper.file('User.prisma')

Expand Down Expand Up @@ -168,3 +168,68 @@ test('rename field', async () => {
}
`)
})

test('rename field - map does not exist', async () => {
const helper = await getMultifileHelper('user-posts')
const user = helper.file('User.prisma')

const response = handleRenameRequest(helper.schema, user.textDocument, {
textDocument: {
uri: user.uri,
},
position: user.lineContaining('name String').characterAfter('na'),
newName: 'fullName',
})

expect(response).toMatchInlineSnapshot(`
{
"changes": {
"file:///user-posts/User.prisma": [
{
"newText": "fullName",
"range": {
"end": {
"character": 8,
"line": 3,
},
"start": {
"character": 4,
"line": 3,
},
},
},
{
"newText": " @map("name")",
"range": {
"end": {
"character": 16,
"line": 3,
},
"start": {
"character": 16,
"line": 3,
},
},
},
],
},
}
`)

expect(helper.applyChanges(response?.changes)).toMatchInlineSnapshot(`
{
"file:///user-posts/User.prisma": "/// This is the user of the platform
model User {
id String @id @default(uuid()) @map("_id")
fullName String @map("name")
email String
posts Post[]

address Address

favouriteAnimal FavouriteAnimal
}
",
}
`)
})
20 changes: 10 additions & 10 deletions packages/language-server/src/__test__/rename/single-file.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,8 @@ describe('Rename', () => {
{
newText: ' @map("Album")',
range: {
start: { line: 136, character: 33 },
end: { line: 136, character: 33 },
start: { line: 136, character: 34 },
end: { line: 136, character: 34 },
},
},
],
Expand Down Expand Up @@ -416,8 +416,8 @@ describe('Rename', () => {
{
newText: ' @map("authorId")',
range: {
start: { line: 4, character: 16 },
end: { line: 4, character: 16 },
start: { line: 4, character: 17 },
end: { line: 4, character: 17 },
},
},
{
Expand Down Expand Up @@ -458,8 +458,8 @@ describe('Rename', () => {
{
newText: ' @map("title")',
range: {
start: { line: 17, character: 16 },
end: { line: 17, character: 16 },
start: { line: 17, character: 17 },
end: { line: 17, character: 17 },
},
},
{
Expand Down Expand Up @@ -492,8 +492,8 @@ describe('Rename', () => {
{
newText: ' @map("humanId")',
range: {
start: { line: 25, character: 13 },
end: { line: 25, character: 13 },
start: { line: 25, character: 14 },
end: { line: 25, character: 14 },
},
},
{
Expand Down Expand Up @@ -567,8 +567,8 @@ describe('Rename', () => {
{
newText: ' @map("A_VARIANT_WITH_UNDERSCORES")',
range: {
start: { line: 8, character: 27 },
end: { line: 8, character: 27 },
start: { line: 8, character: 28 },
end: { line: 8, character: 28 },
},
},
{
Expand Down
16 changes: 9 additions & 7 deletions packages/language-server/src/lib/code-actions/rename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export function printLogMessage(
}

function insertInlineRename(currentName: string, line: Line): EditsMap {
const character = lastNoNewlineCharacter(line.untrimmedText)
const character = lastNewLineCharacter(line.untrimmedText)
return {
[line.document.uri]: [
{
Expand All @@ -175,13 +175,15 @@ function insertInlineRename(currentName: string, line: Line): EditsMap {
}
}

function lastNoNewlineCharacter(lineText: string) {
for (let i = lineText.length - 1; i >= 0; i--) {
if (lineText[i] !== '\n' && lineText[i] !== '\r') {
return i
}
function lastNewLineCharacter(lineText: string) {
const i = lineText.length - 1
if (lineText[i] === '\n' && lineText[i - 1] === '\r') {
return i - 1
} else if (lineText[i] === '\n') {
return i
} else {
return 0
}
return 0
}

function insertMapBlockAttribute(oldName: string, block: Block): EditsMap {
Expand Down
Loading