Document.addDocumentListener throws "Already registered"

Is there any way to check if a listener already registered with the document? I'm getting `java.lang.Throwable: Already registered` error, but obviously I could've avoided it if I had a way to check if the listener is already registered.

Also, apparently there's no way to suppress this error by surrounding the code with try/catch.

 

0
7 comments

Please show the code/use-case where listener is attached.

 

0

Something like this:

PsiFile file = PsiManager.getInstance(project).findFile(input);
Document document = file.getViewProvider().getDocument();
Editor editor = EditorFactory.getInstance().createEditor(document, project, input, false);

try {
editor.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void documentChanged(@NotNull DocumentEvent event) {
...
});
} catch (Throwable e) {
}

So again, I see two issues here. First, I have no way to check if the listener is already added to the document, and second, even though I have try/catch, the throwable is not caught.

 

0

Please show the full source code of this snippet. You cannot "catch" the error message from LOG.error("Already registered: " + listener);

0

Hmm, ok, I see. 

OK, here's the full code. What I'm trying to do here is I have a folder that contains a number of "input" files, and there's a listener that reacts to the changes in these files.

List<VirtualFile> children = VfsUtil.collectChildrenRecursively(inputsFolder);
for (VirtualFile input : children) {
if (input.isDirectory()) {
continue;
}
PsiFile file = PsiManager.getInstance(project).findFile(input);
if (file == null) {
continue;
}

Document document = file.getViewProvider().getDocument();
if (document == null) {
continue;
}
Editor editor = EditorFactory.getInstance().createEditor(document, project, input, false);
try {
editor.getDocument().addDocumentListener(refreshPreview);
} catch (Throwable e) {
}
}
0

I would like to understand the use-case better. From where is this code collecting files and registering listeners called from? Why multiple times (as it seems)?  Why do you create Editor instance?

What kind of changes of the files in this special directory do you want to monitor?

0

I have a plugin for custom language/framework that processes multiple input files. The result of the processing changes depending on the content of these files, so I want to automatically detect the change in any of the input files and refresh the output. In addition to that, input files are grouped in sets and user has an option to switch between the sets - so every time the new set is selected, I'm resetting everything and process the input files again.

 

0

Folks, I posted this a while ago, but I'm still having the same issue. What I'm looking for is a way to reset the document listener or at least the way to check if document listener has already been added to the document. Is it possible? Are there any workarounds?

 

0

Please sign in to leave a comment.