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

Markdown #1328

Open
wants to merge 33 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
6689420
add .venv to .gitignore
benjamin-kirkbride Jun 30, 2023
fa712b7
markdown plugin
benjamin-kirkbride Jun 30, 2023
a5d0b8c
Merge branch 'Akuli:main' into main
benjamin-kirkbride Jun 30, 2023
2079603
Run pycln, black and isort
benjamin-kirkbride Jun 30, 2023
fb1a190
add explicit return
benjamin-kirkbride Jul 1, 2023
cd3841e
Run pycln, black and isort
benjamin-kirkbride Jul 1, 2023
98d3497
improve coverage and regex
benjamin-kirkbride Jul 1, 2023
856d4b4
Run pycln, black and isort
benjamin-kirkbride Jul 1, 2023
e32016c
remove autoindent regexes for markdown
benjamin-kirkbride Jul 1, 2023
ca220ec
remove extranious sleep
benjamin-kirkbride Jul 1, 2023
8b3e3df
update module docstring
benjamin-kirkbride Jul 1, 2023
a1479f6
fix autocomplete
benjamin-kirkbride Jul 1, 2023
c27e782
fix assert
benjamin-kirkbride Jul 2, 2023
3a8aee7
fix flaking (?) tests
benjamin-kirkbride Jul 2, 2023
225732b
add list continuation
benjamin-kirkbride Jul 3, 2023
5ced3d5
add update() before all events
benjamin-kirkbride Jul 3, 2023
3c55d31
Merge remote-tracking branch 'origin/main' into HEAD
Akuli Jul 3, 2023
503cc23
Delete test.md
Akuli Jul 3, 2023
c24298c
Revert unrelated comment style change to pytest.ini
Akuli Jul 3, 2023
28a4e27
remove print
benjamin-kirkbride Jul 3, 2023
65c71bd
remove bad test
benjamin-kirkbride Jul 3, 2023
6753a1e
fix crash in case no filetype
benjamin-kirkbride Jul 3, 2023
c0ebdc3
fix list continuation tests
benjamin-kirkbride Jul 3, 2023
997c67d
actually fix the list continuation test
benjamin-kirkbride Jul 4, 2023
9aa190a
clear empty list item on return
benjamin-kirkbride Jul 4, 2023
5953040
Fix pastebin plugin description (#1336)
Akuli Jul 5, 2023
f3bc25b
Prevent crash when opening file dialog on Mac (#1337)
ThePhilgrim Jul 5, 2023
723c555
Add Mac & VS Code stuff to .gitignore (#1338)
ThePhilgrim Jul 5, 2023
af45e4a
Testing Improvements (#1327)
benjamin-kirkbride Jul 6, 2023
4a975d9
Merge branch 'Akuli:main' into feat_markdown
benjamin-kirkbride Jul 6, 2023
5954b23
Merge branch 'main' into feat_markdown
benjamin-kirkbride Jul 7, 2023
decdb6b
Merge branch 'main' into feat_markdown
benjamin-kirkbride Jul 12, 2023
5d06a99
Merge branch 'main' into feat_markdown
benjamin-kirkbride Aug 1, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion porcupine/default_filetypes.toml
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,6 @@ filename_patterns = ["*.md", "*.markdown"]
pygments_lexer = "pygments.lexers.MarkdownLexer"
syntax_highlighter = "tree_sitter"
tree_sitter_language_name = "markdown"
autoindent_regexes = {dedent = '.*\.', indent = '^([0-9]+\.|-) .*'}

[YAML]
filename_patterns = ["*.yml", "*.yaml"]
Expand Down
2 changes: 2 additions & 0 deletions porcupine/plugins/autoindent.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ def after_enter(tab: tabs.FileTab, alt_pressed: bool) -> None:
if dedent_prev_line:
tab.textwidget.dedent("insert - 1 line")

tab.textwidget.event_generate("<<post-autoindent>>")


def on_enter_press(
tab: tabs.FileTab, alt_pressed: bool, event: tkinter.Event[tkinter.Text]
Expand Down
109 changes: 109 additions & 0 deletions porcupine/plugins/markdown.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
"""Features for working with Markdown Files.

- Indenting and dedenting lists
"""

from __future__ import annotations

import logging
import re
import tkinter
from functools import partial

from porcupine import get_tab_manager, tabs, textutils, utils

log = logging.getLogger(__name__)


setup_before = ["tabs2spaces", "autoindent"]
Akuli marked this conversation as resolved.
Show resolved Hide resolved


def _list_item(line: str) -> re.Match[str] | None:
"""Regex for markdown list item

1st group is the whitespace (if any) preceding the item
2nd group is the list item prefix (ex `-`, `+`, `6.`, `#.`)
3rd group is the item text

According to:
- https://spec.commonmark.org/0.30/#lists
- https://pandoc.org/MANUAL.html#lists
Technically `#)` is not in either spec, but I won't tell if you won't
"""
assert isinstance(line, str)
if not line:
# empty string
return None

assert len(line.splitlines()) == 1

list_item_regex = re.compile(r"(^[\t ]*)(\d{1,9}[.)]|[-+*]|#\)|#\.) (.*)")
match = list_item_regex.search(line)
return match if match else None


def on_tab_key(
tab: tabs.FileTab, event: tkinter.Event[textutils.MainText], shift_pressed: bool
) -> str | None:
"""Indenting and dedenting list items"""
if tab.settings.get("filetype_name", object) == "Markdown":
line = event.widget.get("insert linestart", "insert lineend")
list_item_status = _list_item(line)

# shift-tab
if shift_pressed and list_item_status:
event.widget.dedent("insert linestart")
return "break"

# if it isn't, we want tab to trigger autocomplete instead
char_before_cursor_is_space = tab.textwidget.get("insert - 1 char", "insert") == " "

# tab
if list_item_status and char_before_cursor_is_space:
event.widget.indent("insert linestart")
return "break"

return None


def continue_list(tab: tabs.FileTab, event: tkinter.Event[tkinter.Text]) -> str | None:
"""Automatically continue lists

This happens after the `autoindent` plugin automatically handles indentation
"""
if tab.settings.get("filetype_name", object) == "Markdown":
current_line = event.widget.get("insert - 1l linestart", "insert -1l lineend")
list_item_match = _list_item(current_line)
if list_item_match:
indentation, prefix, item_text = list_item_match.groups()

tab.textwidget.insert("insert", prefix + " ")
tab.update()

return None


def on_enter_press(tab: tabs.FileTab, event: tkinter.Event[tkinter.Text]) -> str | None:
if tab.settings.get("filetype_name", object) == "Markdown":
current_line = event.widget.get("insert linestart", "insert lineend")
list_item_match = _list_item(current_line)
if list_item_match:
indentation, prefix, item_text = list_item_match.groups()
if item_text:
# there is item text, so we are done here
return None

event.widget.delete("insert linestart", "insert lineend")
return "break"

return None


def on_new_filetab(tab: tabs.FileTab) -> None:
utils.bind_tab_key(tab.textwidget, partial(on_tab_key, tab), add=True)
tab.textwidget.bind("<<post-autoindent>>", partial(continue_list, tab), add=True)
tab.textwidget.bind("<Return>", partial(on_enter_press, tab), add=True)


def setup() -> None:
get_tab_manager().add_filetab_callback(on_new_filetab)
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ line_length = 100
profile = "black"
multi_line_output = 3

[tool.ruff]
ignore = ["E501"]

# Flit configuration ([build-system] and [project]) are used when pip installing with github url.
# See commands in README.
[build-system]
Expand Down
22 changes: 0 additions & 22 deletions tests/test_indent_dedent.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,28 +198,6 @@ def check(filename, input_commands, output):
return check


def test_markdown_autoindent(check_autoindents):
check_autoindents(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we keep this, but just modify it so that it passes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got rid of it because it felt redundant with all the markdown tests, but I can look back and see if there is a way to make that happen

"hello.md",
"""
1. Lol and
wat.
- Foo and
bar and
baz.
End of list
""",
"""
1. Lol and
wat.
- Foo and
bar and
baz.
End of list
""",
)


def test_shell_autoindent(check_autoindents):
check_autoindents(
"loll.sh",
Expand Down
Loading