jupyterlab-contrib / jupyterlab_code_formatter

A JupyterLab plugin to facilitate invocation of code formatters.
https://jupyterlab-code-formatter.readthedocs.io/
MIT License
855 stars 55 forks source link

Auto format code when saving the python file instead of notebook #211

Open bbo2adwuff opened 3 years ago

bbo2adwuff commented 3 years ago

Checklist prior to opening an issue

Describe the feature request Auto format code when saving a notebook works like a charm since the merge of https://github.com/ryantam626/jupyterlab_code_formatter/pull/195

So this is rather a feature request, but when I open a .py file in JupyterLab it would be great to also have the formatOnSave option.

Do you think along the following lines it would be possible to also use the formatter for the py file editor? https://github.com/ryantam626/jupyterlab_code_formatter/blob/ac5fbd1e2d0b3f031763743e53558dba74d7380a/src/index.ts#L107-L114

bbo2adwuff commented 3 years ago

Well a first try could be something along these lines?

  private async onSaveEditor(
    context: DocumentRegistry.IContext<IEditorTracker>,
    state: DocumentRegistry.SaveState
  ) {
    if (state === 'started' && this.config.formatOnSave) {
      await this.fileEditorCodeFormatter.formatAction(this.config, 'black');
    }
  }

But where to call it? Because here at https://github.com/wallneradam/jupyterlab-trailing-space-remover/blob/master/src/index.ts they seem to have one createNew in the class RemoveTrailingSpaceEditor and one in class RemoveTrailingSpaceNotebook.

ryantam626 commented 1 year ago

Sorry for the late reply, I haven't given this project much love, I just kinda lost steam over the years as I got increasingly burnt out, but lately I have gotten a second wind (perhaps only for a brief period...)

FWIW I am in middle to a big refactor for the project (mostly due to the evolution of jupyterlab's plugin tooling), this would be one of the things I can look at after that.

Without the the refactor, the development envrionment for this plugin is just nightmare-ish to use, so that is currently trumping all tasks.