Akuli / porcupine

A decent editor written in tkinter
MIT License
160 stars 46 forks source link

arithmetic expression evaluator #422

Open Akuli opened 3 years ago

Akuli commented 3 years ago

Currently doing homework where I have to calculate things like 25 % 7 and I'm typing them into python (lol yeah), would be easier to press magic key binding and have 25 % 7 in the file replaced by....

>>> 25 % 7
4

...replaced by 4

Moosems commented 2 years ago

What would be your ideal magic key bind?

Moosems commented 2 years ago

What are your parameters for when to do the evaluation?

Akuli commented 2 years ago

Some kind of error message should appear if you try to evaluate something like asd + 1. It doesn't matter what exactly the magic key binding is, but it must

Moosems commented 2 years ago

Command-A-E? As for the evaluation should I just use eval(typed_expression)?

Akuli commented 2 years ago

I don't like Command-A-E, because for us non-Mac users, it would become Ctrl+A+E and Ctrl+A already has a well-known meaning. It could be Alt+A+E or Alt+A or Alt+E though.

You can use eval() if you first ensure that the expression is safe to evaluate, i.e. only contains numbers, parentheses, +, - and so on.

Even if you check the expression, eval() has a downside: 0.1 + 0.2 evals to 0.30000000000000004, and 2/6 evals to 0.3333333333333333. For math homework, the expected outputs for those would be 0.3 and 1/3. The plugin would still be better than nothing, and we can later switch to a custom evaluator if this ever becomes a problem (for some inspiration, maybe look at how ast.literal_eval() is implemented).

Moosems commented 1 year ago

The issue with alt, as we discussed previously, is that it also has special meaning on Mac. Command/Ctrl-E could work as there is nothing there to my knowledge. As for floating-point arithmetic precision errors the user can, in a plugin popup, choose a level of precision and the plugin can round to that level of precision. As for checking for safety a simple regex can check for letters and block the code if any is found. As for what line gets the treatment it would be easy to just do the current selected line but if multiple lines are selected how should this behave?

Akuli commented 1 year ago

It shouldn't care about whether the selected expression is one line or multiple lines. For example, if you select the 4 lines inside the parentheses, it should become print(46) (probably with 46 on a line of its own):

print(
    1
    + 23
    - 45
    + 67
)
Moosems commented 1 year ago

Good to know. I'll write up a quick regex and work on the plugin 😄

Moosems commented 1 year ago

@Akuli How can I get the current seletected text through porcupine and how do I add a keybind?

Akuli commented 1 year ago

How can I get the current seletected text through porcupine

Once you have a FileTab, you can do tab.textwidget.get("sel.first", "sel.last")

and how do I add a keybind?

To see how they work, take one of the existing key bindings in default_keybindings.tcl and search for its name with git grep.

Moosems commented 1 year ago

In most cases the rounding errors can be solved with using Fractions instead of floats