Skip to content

Commit 61c2b5f

Browse files
add languageId and version to didOpen message (#5536)
1 parent 35c0eaa commit 61c2b5f

File tree

4 files changed

+61
-9
lines changed

4 files changed

+61
-9
lines changed

plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/textdocument/TextDocumentServiceHandler.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ class TextDocumentServiceHandler(
6868
textDocument = TextDocumentItem().apply {
6969
this.uri = uri
7070
text = file.inputStream.readAllBytes().decodeToString()
71+
languageId = file.fileType.name.lowercase()
72+
version = file.modificationStamp.toInt()
7173
}
7274
}
7375
)

plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/workspace/WorkspaceServiceHandler.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ class WorkspaceServiceHandler(
156156
textDocument = TextDocumentItem().apply {
157157
this.uri = uri
158158
text = renamedFile.inputStream.readAllBytes().decodeToString()
159+
languageId = renamedFile.fileType.name.lowercase()
160+
version = renamedFile.modificationStamp.toInt()
159161
}
160162
}
161163
)

plugins/amazonq/shared/jetbrains-community/tst/software/aws/toolkits/jetbrains/services/amazonq/lsp/textdocument/TextDocumentServiceHandlerTest.kt

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import com.intellij.openapi.components.serviceIfCreated
1010
import com.intellij.openapi.editor.Document
1111
import com.intellij.openapi.fileEditor.FileDocumentManager
1212
import com.intellij.openapi.fileEditor.FileEditorManager
13+
import com.intellij.openapi.fileTypes.FileType
1314
import com.intellij.openapi.project.Project
1415
import com.intellij.openapi.vfs.VirtualFile
1516
import com.intellij.openapi.vfs.newvfs.events.VFileContentChangeEvent
@@ -149,27 +150,27 @@ class TextDocumentServiceHandlerTest {
149150
with(paramsSlot.captured.textDocument) {
150151
assertThat(this.uri).isEqualTo(normalizeFileUri(uri.toString()))
151152
assertThat(text).isEqualTo(content)
153+
assertThat(languageId).isEqualTo("java")
154+
assertThat(version).isEqualTo(1)
152155
}
153156
}
154157

155158
@Test
156159
fun `didOpen runs on fileOpened`() = runTest {
157-
// Create test file
158160
val uri = URI.create("file:///test/path/file.txt")
159161
val content = "test content"
160-
161162
val file = createMockVirtualFile(uri, content)
162163

163-
// Call the handler method
164164
sut.fileOpened(mockk(), file)
165165

166-
// Verify the correct LSP method was called with matching parameters
167166
val paramsSlot = slot<DidOpenTextDocumentParams>()
168167
verify { mockTextDocumentService.didOpen(capture(paramsSlot)) }
169168

170169
with(paramsSlot.captured.textDocument) {
171170
assertThat(this.uri).isEqualTo(normalizeFileUri(uri.toString()))
172171
assertThat(text).isEqualTo(content)
172+
assertThat(languageId).isEqualTo("java")
173+
assertThat(version).isEqualTo(1)
173174
}
174175
}
175176

@@ -297,11 +298,21 @@ class TextDocumentServiceHandlerTest {
297298
}
298299
}
299300

300-
private fun createMockVirtualFile(uri: URI, content: String = ""): VirtualFile {
301+
private fun createMockVirtualFile(
302+
uri: URI,
303+
content: String = "",
304+
fileTypeName: String = "JAVA",
305+
modificationStamp: Long = 1L,
306+
): VirtualFile {
301307
val path = mockk<Path> {
302308
every { toUri() } returns uri
303309
}
304310
val inputStream = content.byteInputStream()
311+
312+
val mockFileType = mockk<FileType> {
313+
every { name } returns fileTypeName
314+
}
315+
305316
return mockk<VirtualFile> {
306317
every { url } returns uri.path
307318
every { toNioPath() } returns path
@@ -310,6 +321,8 @@ class TextDocumentServiceHandlerTest {
310321
every { protocol } returns "file"
311322
}
312323
every { this@mockk.inputStream } returns inputStream
324+
every { fileType } returns mockFileType
325+
every { this@mockk.modificationStamp } returns modificationStamp
313326
}
314327
}
315328

plugins/amazonq/shared/jetbrains-community/tst/software/aws/toolkits/jetbrains/services/amazonq/lsp/workspace/WorkspaceServiceHandlerTest.kt

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import com.intellij.openapi.Disposable
77
import com.intellij.openapi.application.Application
88
import com.intellij.openapi.application.ApplicationManager
99
import com.intellij.openapi.components.serviceIfCreated
10+
import com.intellij.openapi.fileTypes.FileType
1011
import com.intellij.openapi.project.Project
1112
import com.intellij.openapi.vfs.VirtualFile
1213
import com.intellij.openapi.vfs.newvfs.events.VFileCopyEvent
@@ -394,6 +395,8 @@ class WorkspaceServiceHandlerTest {
394395
oldName = oldName,
395396
newName = newName,
396397
isDirectory = false,
398+
fileTypeName = "JAVA",
399+
modificationStamp = 123L
397400
)
398401

399402
// Act
@@ -405,8 +408,12 @@ class WorkspaceServiceHandlerTest {
405408

406409
val openParams = slot<DidOpenTextDocumentParams>()
407410
verify { mockTextDocumentService.didOpen(capture(openParams)) }
408-
assertThat(openParams.captured.textDocument.uri).isEqualTo(normalizeFileUri("file:///testDir/$newName"))
409-
assertThat(openParams.captured.textDocument.text).isEqualTo(normalizeFileUri("content"))
411+
with(openParams.captured.textDocument) {
412+
assertThat(uri).isEqualTo(normalizeFileUri("file:///testDir/$newName"))
413+
assertThat(text).isEqualTo("content")
414+
assertThat(languageId).isEqualTo("java")
415+
assertThat(version).isEqualTo(123)
416+
}
410417

411418
// Assert
412419
val paramsSlot = slot<RenameFilesParams>()
@@ -464,10 +471,14 @@ class WorkspaceServiceHandlerTest {
464471
val event1 = createMockPropertyChangeEvent(
465472
oldName = "old1.java",
466473
newName = "new1.java",
474+
fileTypeName = "JAVA",
475+
modificationStamp = 123L
467476
)
468477
val event2 = createMockPropertyChangeEvent(
469478
oldName = "old2.py",
470479
newName = "new2.py",
480+
fileTypeName = "Python",
481+
modificationStamp = 456L
471482
)
472483

473484
// Act
@@ -477,6 +488,17 @@ class WorkspaceServiceHandlerTest {
477488
val paramsSlot = slot<RenameFilesParams>()
478489
verify { mockWorkspaceService.didRenameFiles(capture(paramsSlot)) }
479490
assertThat(paramsSlot.captured.files).hasSize(2)
491+
492+
// Verify didClose and didOpen for both files
493+
verify(exactly = 2) { mockTextDocumentService.didClose(any()) }
494+
495+
val openParamsSlot = mutableListOf<DidOpenTextDocumentParams>()
496+
verify(exactly = 2) { mockTextDocumentService.didOpen(capture(openParamsSlot)) }
497+
498+
assertThat(openParamsSlot[0].textDocument.languageId).isEqualTo("java")
499+
assertThat(openParamsSlot[0].textDocument.version).isEqualTo(123)
500+
assertThat(openParamsSlot[1].textDocument.languageId).isEqualTo("python")
501+
assertThat(openParamsSlot[1].textDocument.version).isEqualTo(456)
480502
}
481503

482504
@Test
@@ -635,10 +657,19 @@ class WorkspaceServiceHandlerTest {
635657
assertThat(paramsSlot.captured.event.removed[0].name).isEqualTo("folder2")
636658
}
637659

638-
private fun createMockVirtualFile(uri: URI, fileName: String, isDirectory: Boolean = false): VirtualFile {
660+
private fun createMockVirtualFile(
661+
uri: URI,
662+
fileName: String,
663+
isDirectory: Boolean = false,
664+
fileTypeName: String = "PLAIN_TEXT",
665+
modificationStamp: Long = 1L,
666+
): VirtualFile {
639667
val nioPath = mockk<Path> {
640668
every { toUri() } returns uri
641669
}
670+
val mockFileType = mockk<FileType> {
671+
every { name } returns fileTypeName
672+
}
642673
return mockk<VirtualFile> {
643674
every { this@mockk.isDirectory } returns isDirectory
644675
every { toNioPath() } returns nioPath
@@ -648,6 +679,8 @@ class WorkspaceServiceHandlerTest {
648679
every { protocol } returns "file"
649680
}
650681
every { this@mockk.inputStream } returns "content".byteInputStream()
682+
every { fileType } returns mockFileType
683+
every { this@mockk.modificationStamp } returns modificationStamp
651684
}
652685
}
653686

@@ -671,10 +704,12 @@ class WorkspaceServiceHandlerTest {
671704
oldName: String,
672705
newName: String,
673706
isDirectory: Boolean = false,
707+
fileTypeName: String = "PLAIN_TEXT",
708+
modificationStamp: Long = 1L,
674709
): VFilePropertyChangeEvent {
675710
val parent = createMockVirtualFile(URI("file:///testDir/"), "testDir", true)
676711
val newUri = URI("file:///testDir/$newName")
677-
val file = createMockVirtualFile(newUri, newName, isDirectory)
712+
val file = createMockVirtualFile(newUri, newName, isDirectory, fileTypeName, modificationStamp)
678713
every { file.parent } returns parent
679714

680715
return mockk<VFilePropertyChangeEvent>().apply {

0 commit comments

Comments
 (0)