Skip to content

Commit 4f62d74

Browse files
committed
didOpen captures already open files in IDE
1 parent 4367af0 commit 4f62d74

File tree

2 files changed

+48
-12
lines changed

2 files changed

+48
-12
lines changed

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

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,27 @@ class TextDocumentServiceHandler(
5252
FileDocumentManagerListener.TOPIC,
5353
this
5454
)
55+
56+
// open files on startup
57+
val fileEditorManager = FileEditorManager.getInstance(project)
58+
fileEditorManager.openFiles.forEach { file ->
59+
handleFileOpened(file)
60+
}
61+
}
62+
63+
private fun handleFileOpened(file: VirtualFile) {
64+
AmazonQLspService.executeIfRunning(project) { languageServer ->
65+
toUriString(file)?.let { uri ->
66+
languageServer.textDocumentService.didOpen(
67+
DidOpenTextDocumentParams().apply {
68+
textDocument = TextDocumentItem().apply {
69+
this.uri = uri
70+
text = file.inputStream.readAllBytes().decodeToString()
71+
}
72+
}
73+
)
74+
}
75+
}
5576
}
5677

5778
override fun beforeDocumentSaving(document: Document) {
@@ -99,18 +120,7 @@ class TextDocumentServiceHandler(
99120
source: FileEditorManager,
100121
file: VirtualFile,
101122
) {
102-
AmazonQLspService.executeIfRunning(project) { languageServer ->
103-
toUriString(file)?.let { uri ->
104-
languageServer.textDocumentService.didOpen(
105-
DidOpenTextDocumentParams().apply {
106-
textDocument = TextDocumentItem().apply {
107-
this.uri = uri
108-
text = file.inputStream.readAllBytes().decodeToString()
109-
}
110-
}
111-
)
112-
}
113-
}
123+
handleFileOpened(file)
114124
}
115125

116126
override fun fileClosed(

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import com.intellij.openapi.application.ApplicationManager
99
import com.intellij.openapi.components.serviceIfCreated
1010
import com.intellij.openapi.editor.Document
1111
import com.intellij.openapi.fileEditor.FileDocumentManager
12+
import com.intellij.openapi.fileEditor.FileEditorManager
1213
import com.intellij.openapi.project.Project
1314
import com.intellij.openapi.vfs.VirtualFile
1415
import com.intellij.openapi.vfs.newvfs.events.VFileContentChangeEvent
@@ -43,6 +44,7 @@ import java.util.concurrent.CompletableFuture
4344

4445
class TextDocumentServiceHandlerTest {
4546
private lateinit var project: Project
47+
private lateinit var mockFileEditorManager: FileEditorManager
4648
private lateinit var mockLanguageServer: AmazonQLanguageServer
4749
private lateinit var mockTextDocumentService: TextDocumentService
4850
private lateinit var sut: TextDocumentServiceHandler
@@ -90,6 +92,11 @@ class TextDocumentServiceHandlerTest {
9092
every { messageBus.connect(any<Disposable>()) } returns mockConnection
9193
every { mockConnection.subscribe(any(), any()) } just runs
9294

95+
// Mock FileEditorManager
96+
mockFileEditorManager = mockk<FileEditorManager>()
97+
every { mockFileEditorManager.openFiles } returns emptyArray()
98+
every { project.getService(FileEditorManager::class.java) } returns mockFileEditorManager
99+
93100
sut = TextDocumentServiceHandler(project, mockk())
94101
}
95102

@@ -126,6 +133,25 @@ class TextDocumentServiceHandlerTest {
126133
}
127134
}
128135

136+
@Test
137+
fun `didOpen runs on service init`() = runTest {
138+
val uri = URI.create("file:///test/path/file.txt")
139+
val content = "test content"
140+
val file = createMockVirtualFile(uri, content)
141+
142+
every { mockFileEditorManager.openFiles } returns arrayOf(file)
143+
144+
sut = TextDocumentServiceHandler(project, mockk())
145+
146+
val paramsSlot = slot<DidOpenTextDocumentParams>()
147+
verify { mockTextDocumentService.didOpen(capture(paramsSlot)) }
148+
149+
with(paramsSlot.captured.textDocument) {
150+
assertEquals(normalizeFileUri(uri.toString()), this.uri)
151+
assertEquals(content, text)
152+
}
153+
}
154+
129155
@Test
130156
fun `didOpen runs on fileOpened`() = runTest {
131157
// Create test file

0 commit comments

Comments
 (0)