eventMulticaster.addDocumentListener() returns weird values

Answered

I wrote a code to send the changes made by the user to another end via socket.

For some reason the variable `newFragment` which is returned by `documentChanged(...)` returns very weird values. Sometimes it returns IntellijIdeaRulezzz but I get that it is probably a test string or something but sometimes it returns this weird json file connected to this file schema-catalog.json or some other json files. All I need is to receive the changes, where these values come from???


fun listenToChanges(filename: String) { // filename is file that currently its changes are listened to
println("Listening to changes on $filename")

val disposable = Disposer.newDisposable(ApplicationManager.getApplication(), "RequestedPagesState.myDocumentListenerDisposable")
EditorFactory.getInstance().eventMulticaster.addDocumentListener(object : DocumentListener {
override fun documentChanged(event: DocumentEvent) {
super.documentChanged(event)
val document = event.document
val offset = event.offset
if (!event.isWholeTextReplaced) { // Normal change
sendWriteSocketData(event.oldRange, event.newRange, event.newFragment, filename)
} else { // Whole text is replaced
println("Whole text has replaced")
}
}

}, disposable)
myDocumentManagerListenerDisposable = disposable
}

1 comment
Comment actions Permalink

Hi Ilan,

The weird values you get come from the document copy created for auto-completion purposes.

To filter them out from your logic, you can get a VirtualFile associated with the document:
FileDocumentManager.getInstance().getFile(event.document)

and check its type/properties to determine if should be handled with your listener.

0

Please sign in to leave a comment.