Skip to content
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
4 changes: 0 additions & 4 deletions javascript/packages/language-server/src/formatting_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,10 +382,6 @@ export class FormattingService {
}).join('\n')
}

if (!formattedText.endsWith('\n')) {
formattedText += '\n'
}

if (formattedText === rangeText) {
return []
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,31 @@ describe('FormattingService', () => {

expect(result[0].newText).toBe(expected)
})

it('should add trailing newline to formatted document', async () => {
vi.mocked(settings.getDocumentSettings).mockResolvedValue({} as any)

const input = '<div>test</div>'
const document = TextDocument.create('file:///test/file.erb', 'erb', 1, input)
vi.mocked(documents.get).mockReturnValue(document)

const result = await formattingService.formatDocument(params)

expect(result[0].newText).toBe('<div>test</div>\n')
expect(result[0].newText.endsWith('\n')).toBe(true)
})

it('should add trailing newline even when input already has content after last tag', async () => {
vi.mocked(settings.getDocumentSettings).mockResolvedValue({} as any)

const input = '<div>test</div>extra'
const document = TextDocument.create('file:///test/file.erb', 'erb', 1, input)
vi.mocked(documents.get).mockReturnValue(document)

const result = await formattingService.formatDocument(params)

expect(result[0].newText.endsWith('\n')).toBe(true)
})
})

describe('formatDocumentIgnoreConfig', () => {
Expand Down Expand Up @@ -354,6 +379,67 @@ describe('FormattingService', () => {
expect(result[0].newText).toMatch(/^\s*<span>test<\/span>/)
}
})

it('should NOT add trailing newline to range-formatted text', async () => {
vi.mocked(settings.getDocumentSettings).mockResolvedValue({} as any)

const input = '<div class="">test</div>'
const document = TextDocument.create('file:///test/file.erb', 'erb', 1, input)
vi.mocked(documents.get).mockReturnValue(document)

const range: Range = {
start: Position.create(0, 12),
end: Position.create(0, 12)
}

const params = createRangeParams(range)
const result = await formattingService.formatRange(params)

if (result.length > 0) {
expect(result[0].newText.endsWith('\n')).toBe(false)
}
})

it('should NOT add trailing newline when formatting a simple range (issue #1125)', async () => {
vi.mocked(settings.getDocumentSettings).mockResolvedValue({} as any)

const input = '<div><span>content</span></div>'
const document = TextDocument.create('file:///test/file.erb', 'erb', 1, input)
vi.mocked(documents.get).mockReturnValue(document)

const range: Range = {
start: Position.create(0, 5),
end: Position.create(0, 25)
}

const params = createRangeParams(range)
const result = await formattingService.formatRange(params)

if (result.length > 0) {
expect(result[0].newText.endsWith('\n')).toBe(false)
}
})

it('should preserve original trailing whitespace behavior in range formatting', async () => {
vi.mocked(settings.getDocumentSettings).mockResolvedValue({} as any)

const input = '<p>Hello</p>'
const document = TextDocument.create('file:///test/file.erb', 'erb', 1, input)
vi.mocked(documents.get).mockReturnValue(document)

const range: Range = {
start: Position.create(0, 0),
end: Position.create(0, 12)
}

const params = createRangeParams(range)
const result = await formattingService.formatRange(params)

if (result.length > 0) {
expect(result[0].newText).toBe('<p>Hello</p>')
expect(result[0].newText.endsWith('\n')).toBe(false)
}
})
})

describe('formatRangeIgnoreConfig', () => {
Expand Down Expand Up @@ -412,7 +498,7 @@ describe('FormattingService', () => {
' <p>\n' +
' some <%= formatted %> text, that needs <% needs %> <% to_be_formatted %>\n' +
' without being reset to the <b><i>start of the line</i></b>.\n' +
' </p>\n'
' </p>'
)
})
})
Expand Down
Loading