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

Reload syntax on saving #196

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
30 changes: 16 additions & 14 deletions babi/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,21 @@ def __init__(
self.selection = Selection()
self._file_hls: tuple[FileHL, ...] = ()

def refresh_syntax(self) -> None:
file_hls = []
for factory in self._hl_factories:
if self.filename is not None:
hl = factory.file_highlighter(self.filename, self.buf[0])
file_hls.append(hl)
else:
file_hls.append(factory.blank_file_highlighter())
self._file_hls = (
*file_hls,
self._trailing_whitespace, self._replace_hl, self.selection,
)
for file_hl in self._file_hls:
file_hl.register_callbacks(self.buf)

def ensure_loaded(
self,
status: Status,
Expand Down Expand Up @@ -255,20 +270,7 @@ def ensure_loaded(
status.update(f'mixed newlines will be converted to {self.nl!r}')
self.modified = True

file_hls = []
for factory in self._hl_factories:
if self.filename is not None:
hl = factory.file_highlighter(self.filename, self.buf[0])
file_hls.append(hl)
else:
file_hls.append(factory.blank_file_highlighter())
self._file_hls = (
*file_hls,
self._trailing_whitespace, self._replace_hl, self.selection,
)
for file_hl in self._file_hls:
file_hl.register_callbacks(self.buf)

self.refresh_syntax()
self.go_to_line(self.initial_line, margin)

def __repr__(self) -> str:
Expand Down
14 changes: 14 additions & 0 deletions babi/screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,16 @@ def save(self) -> PromptResult | None:
else:
self.file.filename = filename

if os.path.exists(self.file.filename):
with open(self.file.filename) as f:
x = f.readline()[:-1]
refresh_syntax = (
x != self.file.buf[0] and
self.file.buf[0].startswith('#!/') or x.startswith('#!/')
)
else:
refresh_syntax = True

if not os.path.isfile(self.file.filename):
sha256: str | None = None
else:
Expand Down Expand Up @@ -596,6 +606,10 @@ def save(self) -> PromptResult | None:
lines = 'lines' if num_lines != 1 else 'line'
self.status.update(f'saved! ({num_lines} {lines} written)')
self.file.reset_modified_state()

if refresh_syntax:
self.file.refresh_syntax()

return None

def save_filename(self) -> PromptResult | None:
Expand Down