Rerunning FoldingBuilder on settings change

I have a plugin that uses Code Folding regions. The code folding is driven by some settings (e.g. disabling/enabling a whole class of folding cases). I can very easily pass my settings as a dependency in FoldingDescriptor. However, I don’t see how to do this “the other way around”, as in I want fold regions to “appear” when settings are changed.

The way I got this working was to remove the existing folds from open editors and then update the their folding regions.

Here’s the implementation in one of my plugins: MamlCodeFoldingOptionsProvider.kt

I didn’t have too much luck with that, specifically in the case of their being no folding regions in an editor already, and then a settings change is meant to enable folding. What I had much better luck with, however, is:

EditorFactory.getInstance().allEditors.forEach { editor ->
  if (editor.virtualFile?.extension == "kt" && editor.project == this.project) {
    CodeFoldingManager.getInstance(project).scheduleAsyncFoldingUpdate(editor)
  }
}

The key being scheduleAsyncFoldingUpdate, which seems to do exactly what I want!

1 Like