Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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 @@ -52,6 +52,27 @@ class TextDocumentServiceHandler(
FileDocumentManagerListener.TOPIC,
this
)

// open files on startup
val fileEditorManager = FileEditorManager.getInstance(project)
fileEditorManager.openFiles.forEach { file ->
handleFileOpened(file)
}
}

private fun handleFileOpened(file: VirtualFile) {
AmazonQLspService.executeIfRunning(project) { languageServer ->
toUriString(file)?.let { uri ->
languageServer.textDocumentService.didOpen(
DidOpenTextDocumentParams().apply {
textDocument = TextDocumentItem().apply {
this.uri = uri
text = file.inputStream.readAllBytes().decodeToString()
}
}
)
}
}
}

override fun beforeDocumentSaving(document: Document) {
Expand Down Expand Up @@ -99,18 +120,7 @@ class TextDocumentServiceHandler(
source: FileEditorManager,
file: VirtualFile,
) {
AmazonQLspService.executeIfRunning(project) { languageServer ->
toUriString(file)?.let { uri ->
languageServer.textDocumentService.didOpen(
DidOpenTextDocumentParams().apply {
textDocument = TextDocumentItem().apply {
this.uri = uri
text = file.inputStream.readAllBytes().decodeToString()
}
}
)
}
}
handleFileOpened(file)
}

override fun fileClosed(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.components.serviceIfCreated
import com.intellij.openapi.editor.Document
import com.intellij.openapi.fileEditor.FileDocumentManager
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.vfs.newvfs.events.VFileContentChangeEvent
Expand Down Expand Up @@ -43,6 +44,7 @@ import java.util.concurrent.CompletableFuture

class TextDocumentServiceHandlerTest {
private lateinit var project: Project
private lateinit var mockFileEditorManager: FileEditorManager
private lateinit var mockLanguageServer: AmazonQLanguageServer
private lateinit var mockTextDocumentService: TextDocumentService
private lateinit var sut: TextDocumentServiceHandler
Expand Down Expand Up @@ -90,6 +92,11 @@ class TextDocumentServiceHandlerTest {
every { messageBus.connect(any<Disposable>()) } returns mockConnection
every { mockConnection.subscribe(any(), any()) } just runs

// Mock FileEditorManager
mockFileEditorManager = mockk<FileEditorManager>()
every { mockFileEditorManager.openFiles } returns emptyArray()
every { project.getService(FileEditorManager::class.java) } returns mockFileEditorManager

sut = TextDocumentServiceHandler(project, mockk())
}

Expand Down Expand Up @@ -126,6 +133,25 @@ class TextDocumentServiceHandlerTest {
}
}

@Test
fun `didOpen runs on service init`() = runTest {
val uri = URI.create("file:///test/path/file.txt")
val content = "test content"
val file = createMockVirtualFile(uri, content)

every { mockFileEditorManager.openFiles } returns arrayOf(file)

sut = TextDocumentServiceHandler(project, mockk())

val paramsSlot = slot<DidOpenTextDocumentParams>()
verify { mockTextDocumentService.didOpen(capture(paramsSlot)) }

with(paramsSlot.captured.textDocument) {
assertEquals(normalizeFileUri(uri.toString()), this.uri)
assertEquals(content, text)
}
}

@Test
fun `didOpen runs on fileOpened`() = runTest {
// Create test file
Expand Down
Loading