Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pattern for only executing a plugin for a specific filetype #1310

Closed
benjamin-kirkbride opened this issue Jun 29, 2023 · 9 comments
Closed
Labels

Comments

@benjamin-kirkbride
Copy link
Contributor

I can't seem to figure out how this is supposed to be done for #1305

For this example:

"""If configuration says so, insert spaces when the tab key is pressed."""

from __future__ import annotations

import tkinter

from porcupine import get_tab_manager, tabs, textutils, utils

setup_before = ["tabs2spaces"]


def on_tab_key(
    event: tkinter.Event[textutils.MainText], shift_pressed: bool
) -> str:
    if not event.widget.tag_ranges("sel"):
        # nothing selected
        if shift_pressed:
            event.widget.dedent("insert")
        else:
            event.widget.indent("insert")

    # don't insert a tab when it's not supposed to be inserted, or if
    # shift is pressed down, don't move focus out of the widget
    return "break"


def on_new_filetab(tab: tabs.FileTab) -> None:
    utils.bind_tab_key(tab.textwidget, on_tab_key, add=True)


def setup() -> None:
    get_tab_manager().add_filetab_callback(on_new_filetab)

Where would I put a line in to only have the plugin function for markdown files?

@Akuli
Copy link
Owner

Akuli commented Jun 29, 2023

I would put the check inside on_tab_key(), because the filetype of a tab can change and it's easier to check it on the fly than it would be to run some updating code when it changes. To do that, you need to give the tab to on_tab_key() with functools.partial() or a lambda.

So, something like this:

def on_tab_key(
    tab: tabs.FileTab,  # this comes from partial()
    event: tkinter.Event[textutils.MainText],
    shift_pressed: bool,
) -> str:
    tab.settings.debug_dump()  # it's here somewhere, don't remember where

def on_new_filetab(tab: tabs.FileTab) -> None:
    utils.bind_tab_key(tab.textwidget, partial(on_tab_key, tab), add=True)

@benjamin-kirkbride
Copy link
Contributor Author

tab being used for both the pane of the window and the key in question makes this hard to grok, but I think I get you :)

@benjamin-kirkbride
Copy link
Contributor Author

This is what I ended up with:

if tab.settings.get("filetype_name", str) == "Markdown":
    ...

It seems to work fine. Does this seem right/idiomatic?

@Akuli
Copy link
Owner

Akuli commented Jun 30, 2023

Yes, that seems right. It is the best you can do if you need to rely on the filetype. In general, I don't like checking the filetype name, because it means your code will not work with anything else than markdown, but for a first version of the plugin this is good.

@benjamin-kirkbride
Copy link
Contributor Author

In general, I don't like checking the filetype name, because it means your code will not work with anything else than markdown

I'm not sure that it makes sense to use with anything else though. Maybe like ASCIIDoc or something?

@Akuli
Copy link
Owner

Akuli commented Jul 1, 2023

Yes, things like ASCIIDoc and reStructuredText. Also other custom filetypes that users might want to have and we have no idea about.

Example of a custom thing: One of my older projects is htmlthingy, which generates HTML from a custom markup language that looks a little bit like markdown. Because it isn't exactly markdown, I don't name the files with .md (I use .txt, but it's irrelevant what exactly the suffix is). It would be nice to use the plugin with these files.

@benjamin-kirkbride
Copy link
Contributor Author

I see, so how would you envision that working then? Add config options?

@Akuli
Copy link
Owner

Akuli commented Jul 1, 2023

Yes, this would ideally be configurable in filetypes.toml, similar to other indentation settings. But as I have said before, that can be done after the first version that hard-codes the "Markdown" filetype name is merged.

@Akuli Akuli added the docs label Jul 1, 2023
@benjamin-kirkbride
Copy link
Contributor Author

This issue is obsolete in the face of #1357 specifically and #1342 broadly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants