9 Commits

Author SHA1 Message Date
Anthony Sottile
29062628f9 v0.0.14 2020-08-24 14:03:36 -07:00
Anthony Sottile
1fab2a4b71 Merge pull request #85 from asottile/comment
add :comment command for toggling comments
2020-08-24 14:02:23 -07:00
Anthony Sottile
9f5e8c02cb add :comment command for toggling comments 2020-08-24 13:52:35 -07:00
Anthony Sottile
31624856d2 Merge pull request #84 from asottile/asottile-patch-1
add logo to README
2020-08-11 23:27:25 -07:00
Anthony Sottile
97b3b4deef add logo to README 2020-08-11 23:20:48 -07:00
Anthony Sottile
41880d5f8c v0.0.13 2020-07-24 15:28:01 -07:00
Anthony Sottile
effe988f60 Merge pull request #81 from asottile/fix_begin_end_hang
fix highlighting hang with empty begin end rules
2020-07-24 15:26:38 -07:00
Anthony Sottile
84b20a4016 fix highlighting hang with empty begin end rules 2020-07-24 15:13:35 -07:00
Anthony Sottile
5d2c9532a3 s/usually use nano/used to use nano/ 2020-07-20 20:06:24 -07:00
7 changed files with 174 additions and 11 deletions

View File

@@ -1,6 +1,8 @@
[![Build Status](https://dev.azure.com/asottile/asottile/_apis/build/status/asottile.babi?branchName=master)](https://dev.azure.com/asottile/asottile/_build/latest?definitionId=29&branchName=master) [![Build Status](https://dev.azure.com/asottile/asottile/_apis/build/status/asottile.babi?branchName=master)](https://dev.azure.com/asottile/asottile/_build/latest?definitionId=29&branchName=master)
[![Azure DevOps coverage](https://img.shields.io/azure-devops/coverage/asottile/asottile/29/master.svg)](https://dev.azure.com/asottile/asottile/_build/latest?definitionId=29&branchName=master) [![Azure DevOps coverage](https://img.shields.io/azure-devops/coverage/asottile/asottile/29/master.svg)](https://dev.azure.com/asottile/asottile/_build/latest?definitionId=29&branchName=master)
![babi logo](https://user-images.githubusercontent.com/1810591/89981369-9ed84e80-dc28-11ea-9708-5f4c49c09632.png)
babi babi
==== ====
@@ -12,7 +14,7 @@ a text editor, eventually...
### why is it called babi? ### why is it called babi?
I usually use the text editor `nano`, frequently I typo this. on a qwerty I used to use the text editor `nano`, frequently I typo this. on a qwerty
keyboard, when the right hand is shifted left by one, `nano` becomes `babi`. keyboard, when the right hand is shifted left by one, `nano` becomes `babi`.
### quitting babi ### quitting babi

View File

@@ -6,6 +6,7 @@ import hashlib
import io import io
import itertools import itertools
import os.path import os.path
import re
from typing import Any from typing import Any
from typing import Callable from typing import Callable
from typing import cast from typing import cast
@@ -38,6 +39,8 @@ if TYPE_CHECKING:
TCallable = TypeVar('TCallable', bound=Callable[..., Any]) TCallable = TypeVar('TCallable', bound=Callable[..., Any])
WS_RE = re.compile(r'^\s*')
def get_lines(sio: IO[str]) -> Tuple[List[str], str, bool, str]: def get_lines(sio: IO[str]) -> Tuple[List[str], str, bool, str]:
sha256 = hashlib.sha256() sha256 = hashlib.sha256()
@@ -650,6 +653,13 @@ class File:
self.buf.x = 0 self.buf.x = 0
self.buf.scroll_screen_if_needed(margin) self.buf.scroll_screen_if_needed(margin)
def _selection_lines(self) -> Tuple[int, int]:
(s_y, _), (e_y, _) = self.selection.get()
e_y = min(e_y + 1, len(self.buf) - 1)
if self.buf[e_y - 1] == '':
e_y -= 1
return s_y, e_y
@edit_action('sort', final=True) @edit_action('sort', final=True)
def sort(self, margin: Margin, reverse: bool = False) -> None: def sort(self, margin: Margin, reverse: bool = False) -> None:
self._sort(margin, 0, len(self.buf) - 1, reverse=reverse) self._sort(margin, 0, len(self.buf) - 1, reverse=reverse)
@@ -657,12 +667,46 @@ class File:
@edit_action('sort selection', final=True) @edit_action('sort selection', final=True)
@clear_selection @clear_selection
def sort_selection(self, margin: Margin, reverse: bool = False) -> None: def sort_selection(self, margin: Margin, reverse: bool = False) -> None:
(s_y, _), (e_y, _) = self.selection.get() s_y, e_y = self._selection_lines()
e_y = min(e_y + 1, len(self.buf) - 1)
if self.buf[e_y - 1] == '':
e_y -= 1
self._sort(margin, s_y, e_y, reverse=reverse) self._sort(margin, s_y, e_y, reverse=reverse)
def _is_commented(self, lineno: int, prefix: str) -> bool:
return self.buf[lineno].lstrip().startswith(prefix)
def _comment_remove(self, lineno: int, prefix: str) -> None:
line = self.buf[lineno]
ws_match = WS_RE.match(line)
assert ws_match is not None
ws_len = len(ws_match[0])
rest_offset = ws_len + len(prefix)
if line.startswith(prefix, ws_len):
self.buf[lineno] = f'{ws_match[0]}{line[rest_offset:].lstrip()}'
def _comment_add(self, lineno: int, prefix: str) -> None:
line = self.buf[lineno]
ws_match = WS_RE.match(line)
assert ws_match is not None
ws_len = len(ws_match[0])
self.buf[lineno] = f'{ws_match[0]}{prefix} {line[ws_len:]}'
@edit_action('comment', final=True)
def toggle_comment(self, prefix: str) -> None:
if self._is_commented(self.buf.y, prefix):
self._comment_remove(self.buf.y, prefix)
else:
self._comment_add(self.buf.y, prefix)
@edit_action('comment selection', final=True)
@clear_selection
def toggle_comment_selection(self, prefix: str) -> None:
s_y, e_y = self._selection_lines()
commented = self._is_commented(s_y, prefix)
for lineno in range(s_y, e_y):
if commented:
self._comment_remove(lineno, prefix)
else:
self._comment_add(lineno, prefix)
DISPATCH = { DISPATCH = {
# movement # movement
b'KEY_UP': up, b'KEY_UP': up,

View File

@@ -273,6 +273,7 @@ class CompiledRegsetRule(CompiledRule, Protocol):
class Entry(NamedTuple): class Entry(NamedTuple):
scope: Tuple[str, ...] scope: Tuple[str, ...]
rule: CompiledRule rule: CompiledRule
start: Tuple[str, int]
reg: _Reg = ERR_REG reg: _Reg = ERR_REG
boundary: bool = False boundary: bool = False
@@ -284,7 +285,7 @@ def _inner_capture_parse(
scope: Scope, scope: Scope,
rule: CompiledRule, rule: CompiledRule,
) -> Regions: ) -> Regions:
state = State.root(Entry(scope + rule.name, rule)) state = State.root(Entry(scope + rule.name, rule, (s, 0)))
_, regions = highlight_line(compiler, state, s, first_line=False) _, regions = highlight_line(compiler, state, s, first_line=False)
return tuple( return tuple(
r._replace(start=r.start + start, end=r.end + start) for r in regions r._replace(start=r.start + start, end=r.end + start) for r in regions
@@ -440,7 +441,8 @@ class EndRule(NamedTuple):
boundary = match.end() == len(match.string) boundary = match.end() == len(match.string)
reg = make_reg(expand_escaped(match, self.end)) reg = make_reg(expand_escaped(match, self.end))
state = state.push(Entry(next_scope, self, reg, boundary)) start = (match.string, match.start())
state = state.push(Entry(next_scope, self, start, reg, boundary))
regions = _captures(compiler, scope, match, self.begin_captures) regions = _captures(compiler, scope, match, self.begin_captures)
return state, True, regions return state, True, regions
@@ -455,7 +457,16 @@ class EndRule(NamedTuple):
if m.start() > pos: if m.start() > pos:
ret.append(Region(pos, m.start(), state.cur.scope)) ret.append(Region(pos, m.start(), state.cur.scope))
ret.extend(_captures(compiler, state.cur.scope, m, self.end_captures)) ret.extend(_captures(compiler, state.cur.scope, m, self.end_captures))
return state.pop(), m.end(), False, tuple(ret) # this is probably a bug in the grammar, but it pushed and popped at
# the same position.
# we'll advance the highlighter by one position to get past the loop
# this appears to be what vs code does as well
if state.entries[-1].start == (m.string, m.end()):
ret.append(Region(m.end(), m.end() + 1, state.cur.scope))
end = m.end() + 1
else:
end = m.end()
return state.pop(), end, False, tuple(ret)
def search( def search(
self, self,
@@ -501,7 +512,9 @@ class WhileRule(NamedTuple):
boundary = match.end() == len(match.string) boundary = match.end() == len(match.string)
reg = make_reg(expand_escaped(match, self.while_)) reg = make_reg(expand_escaped(match, self.while_))
state = state.push_while(self, Entry(next_scope, self, reg, boundary)) start = (match.string, match.start())
entry = Entry(next_scope, self, start, reg, boundary)
state = state.push_while(self, entry)
regions = _captures(compiler, scope, match, self.begin_captures) regions = _captures(compiler, scope, match, self.begin_captures)
return state, True, regions return state, True, regions
@@ -541,7 +554,7 @@ class Compiler:
self._rule_to_grammar: Dict[_Rule, Grammar] = {} self._rule_to_grammar: Dict[_Rule, Grammar] = {}
self._c_rules: Dict[_Rule, CompiledRule] = {} self._c_rules: Dict[_Rule, CompiledRule] = {}
root = self._compile_root(grammar) root = self._compile_root(grammar)
self.root_state = State.root(Entry(root.name, root)) self.root_state = State.root(Entry(root.name, root, ('', 0)))
def _visit_rule(self, grammar: Grammar, rule: _Rule) -> _Rule: def _visit_rule(self, grammar: Grammar, rule: _Rule) -> _Rule:
self._rule_to_grammar[rule] = grammar self._rule_to_grammar[rule] = grammar

View File

@@ -457,6 +457,13 @@ class Screen:
for file in self.files: for file in self.files:
file.buf.set_tab_size(parsed_tab_size) file.buf.set_tab_size(parsed_tab_size)
self.status.update('updated!') self.status.update('updated!')
elif response == ':comment' or response.startswith(':comment '):
_, _, comment = response.partition(' ')
comment = (comment or '#').strip()
if self.file.selection.start:
self.file.toggle_comment_selection(comment)
else:
self.file.toggle_comment(comment)
else: else:
self.status.update(f'invalid command: {response}') self.status.update(f'invalid command: {response}')
return None return None

View File

@@ -1,6 +1,6 @@
[metadata] [metadata]
name = babi name = babi
version = 0.0.12 version = 0.0.14
description = a text editor description = a text editor
long_description = file: README.md long_description = file: README.md
long_description_content_type = text/markdown long_description_content_type = text/markdown

View File

@@ -0,0 +1,75 @@
from testing.runner import and_exit
from testing.runner import trigger_command_mode
def test_comment_some_code(run, ten_lines):
with run(str(ten_lines)) as h, and_exit(h):
h.press('S-Down')
trigger_command_mode(h)
h.press_and_enter(':comment')
h.await_text('# line_0\n# line_1\nline_2\n')
def test_comment_some_code_with_alternate_comment_character(run, ten_lines):
with run(str(ten_lines)) as h, and_exit(h):
h.press('S-Down')
trigger_command_mode(h)
h.press_and_enter(':comment //')
h.await_text('// line_0\n// line_1\nline_2\n')
def test_comment_partially_commented(run, ten_lines):
with run(str(ten_lines)) as h, and_exit(h):
h.press('#')
h.press('S-Down')
h.await_text('#line_0\nline_1\nline_2')
trigger_command_mode(h)
h.press_and_enter(':comment')
h.await_text('line_0\nline_1\nline_2\n')
def test_comment_partially_uncommented(run, ten_lines):
with run(str(ten_lines)) as h, and_exit(h):
h.press('Down')
h.press('#')
h.press('Up')
h.press('S-Down')
h.await_text('line_0\n#line_1\nline_2')
trigger_command_mode(h)
h.press_and_enter(':comment')
h.await_text('# line_0\n# #line_1\nline_2\n')
def test_comment_single_line(run, ten_lines):
with run(str(ten_lines)) as h, and_exit(h):
trigger_command_mode(h)
h.press_and_enter(':comment')
h.await_text('# line_0\nline_1\n')
def test_uncomment_single_line(run, ten_lines):
with run(str(ten_lines)) as h, and_exit(h):
h.press('#')
h.await_text('#line_0\nline_1\n')
trigger_command_mode(h)
h.press_and_enter(':comment')
h.await_text('line_0\nline_1\n')
def test_comment_with_trailing_whitespace(run, ten_lines):
with run(str(ten_lines)) as h, and_exit(h):
trigger_command_mode(h)
h.press_and_enter(':comment // ')
h.await_text('// line_0\nline_1\n')

View File

@@ -637,3 +637,25 @@ def test_backslash_z(compiler_state):
assert regions2 == ( assert regions2 == (
Region(0, 6, ('test', 'comment')), Region(0, 6, ('test', 'comment')),
) )
def test_buggy_begin_end_grammar(compiler_state):
# before this would result in an infinite loop of start / end
compiler, state = compiler_state({
'scopeName': 'test',
'patterns': [
{
'begin': '(?=</style)',
'end': '(?=</style)',
'name': 'css',
},
],
})
state, regions = highlight_line(compiler, state, 'test </style', True)
assert regions == (
Region(0, 5, ('test',)),
Region(5, 6, ('test', 'css')),
Region(6, 12, ('test',)),
)