Skip to content

Commit

Permalink
--focus support in snify #76
Browse files Browse the repository at this point in the history
  • Loading branch information
jfschaefer committed Feb 3, 2025
1 parent b7c1422 commit e42b098
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 10 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

## Upcoming release

**New features:**

* `snify` accepts `--focus` argument to immediately focus on a symbol ([#76](https://github.com/slatex/stextools/issues/76))


**Bugfixes:**

* Fix misleading "focus mode ended" in `snify`
Expand Down
6 changes: 4 additions & 2 deletions stextools/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,11 @@ def translate(path):
@click.option('--ignore',
default=lambda: get_config().get('stextools.snify', 'ignore', fallback=None),
help='Pattern to exclude some archives (e.g. \'Papers/*,smglom/mv\')')
def snify_actual(files, filter, ignore):
@click.option('--focus',
help='Immediately focus on a specific word')
def snify_actual(files, filter, ignore, focus):
from stextools.snify.controller import snify
snify(files, filter, ignore)
snify(files, filter, ignore, focus)


@cli.command(name='version', help='Print the version of stextools.')
Expand Down
19 changes: 11 additions & 8 deletions stextools/snify/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def unapply(self, state: State):


class Controller:
def __init__(self, state: State, new_files: Optional[list[Path]] = None):
def __init__(self, state: State, new_files: Optional[list[Path]] = None, stem_focus: Optional[str] = None):
self.state: State = state
self.mh = Cache.get_mathhub(update_all=True)
self._linker: Optional[Linker] = None
Expand All @@ -170,9 +170,10 @@ def __init__(self, state: State, new_files: Optional[list[Path]] = None):
self._modification_future: list[list[Modification]] = [] # for re-doing

if new_files:
self.load_files_dialog(new_files)
self.load_files_dialog(new_files, stem_focus)

def load_files_dialog(self, new_files: list[Path]):
def load_files_dialog(self, new_files: list[Path], stem_focus: Optional[str]):
# note: better design would be to move this out of the controller, I think
have_inputs = False
for file in new_files:
stexdoc = self.mh.get_stex_doc(file)
Expand Down Expand Up @@ -215,9 +216,9 @@ def load_files_dialog(self, new_files: list[Path]):
else:
print(f'File {path} is not loaded')

self.state.push_focus(new_files=all_files)
self.state.push_focus(new_files=all_files, select_only_stem=stem_focus)
else:
self.state.push_focus(new_files=new_files)
self.state.push_focus(new_files=new_files, select_only_stem=stem_focus)

@property
def linker(self) -> Linker:
Expand Down Expand Up @@ -499,9 +500,11 @@ def get_current_lang(self) -> str:
return self.state.get_current_lang(self.linker)


def snify(files: list[str], filter: str, ignore: str):
def snify(files: list[str], filter: str, ignore: str, focus: Optional[str]):
session_storage = SessionStorage()
state = session_storage.get_session_dialog()
state: Optional[State] = None
if state is None and focus is None:
state = session_storage.get_session_dialog()
if state is None:
state = State(files=[], filter_pattern=filter,
ignore_pattern=ignore, cursor=PositionCursor(file_index=0, offset=0))
Expand All @@ -512,7 +515,7 @@ def snify(files: list[str], filter: str, ignore: str):
paths.extend(path.rglob('*.tex'))
else:
paths.append(path)
controller = Controller(state, new_files=paths)
controller = Controller(state, new_files=paths, stem_focus=focus)
else:
controller = Controller(state)
unfinished = controller.run()
Expand Down
50 changes: 50 additions & 0 deletions stextools/symnamer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from pathlib import Path

from pylatexenc.latexwalker import LatexWalker, LatexMathNode, LatexCommentNode, LatexSpecialsNode, LatexMacroNode, \
LatexEnvironmentNode, LatexCharsNode, LatexGroupNode

from stextools.core.macros import STEX_CONTEXT_DB


def quickcheck(filecontent: str) -> bool:
""" returns true iff the file may have to be processed, i.e. if it introduces symbols """
if 'symdecl' in filecontent or 'symdef' in filecontent:
return True
return False


def recurse(nodes):
for node in nodes:
if node is None or node.nodeType() in {LatexMathNode, LatexCommentNode, LatexSpecialsNode, LatexCharsNode}:
continue
if node.nodeType() == LatexMacroNode:
if node.macroname in MACRO_RECURSION_RULES:
for arg_idx in MACRO_RECURSION_RULES[node.macroname]:
if arg_idx >= len(node.nodeargd.argnlist):
click.clear()
standard_header('Error', bg='red')
print(f"Macro {node.macroname} does not have argument {arg_idx}")
print('Context:')
print(latex_text[max(node.pos - 100, 0):min(len(latex_text) - 1, node.pos + 300)])
print()
print('Please report this error')
click.pause()
continue
_recurse([node.nodeargd.argnlist[arg_idx]])
elif node.nodeType() in {LatexEnvironmentNode, LatexGroupNode}:
recurse(node.nodelist)
else:
raise RuntimeError(f"Unexpected node type: {node.nodeType()}")



def symname(file: Path):
with open(file) as fp:
filecontent = fp.read()

if not quickcheck(filecontent):
return

walker = LatexWalker(filecontent, latex_context=STEX_CONTEXT_DB)
nodes = walker.get_latex_nodes()[0]

0 comments on commit e42b098

Please sign in to comment.