Skip to content

Commit

Permalink
✨ Support using pprint() to output other filetypes
Browse files Browse the repository at this point in the history
Add Trie.from_file()
  • Loading branch information
Freed-Wu committed Nov 29, 2023
1 parent b78d475 commit ded0414
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 9 deletions.
10 changes: 7 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ repos:
hooks:
- id: actionlint
- repo: https://github.com/adrienverge/yamllint
rev: v1.32.0
rev: v1.33.0
hooks:
- id: yamllint
- repo: https://github.com/executablebooks/mdformat
Expand All @@ -73,8 +73,12 @@ repos:
- id: markdownlint-cli2
additional_dependencies:
- markdown-it-texmath
- repo: https://github.com/Freed-Wu/pre-commit-hooks
rev: 0.0.11
hooks:
- id: update-pyproject.toml
- repo: https://github.com/psf/black
rev: 23.10.1
rev: 23.11.0
hooks:
- id: black
- repo: https://github.com/PyCQA/isort
Expand All @@ -88,7 +92,7 @@ repos:
additional_dependencies:
- tomli
- repo: https://github.com/kumaraditya303/mirrors-pyright
rev: v1.1.332
rev: v1.1.335
hooks:
- id: pyright
- repo: https://github.com/PyCQA/bandit
Expand Down
15 changes: 15 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,23 @@ tree_sitter_lsp = ["py.typed", "assets/**"]
file = "requirements.txt"

# begin: scripts/update-pyproject.toml.pl
[tool.setuptools.dynamic.optional-dependencies.colorize]
file = "requirements/colorize.txt"

[tool.setuptools.dynamic.optional-dependencies.dev]
file = "requirements/dev.txt"

[tool.setuptools.dynamic.optional-dependencies.languages]
file = "requirements/languages.txt"

[tool.setuptools.dynamic.optional-dependencies.misc]
file = "requirements/misc.txt"

[tool.setuptools.dynamic.optional-dependencies.toml]
file = "requirements/toml.txt"

[tool.setuptools.dynamic.optional-dependencies.yaml]
file = "requirements/yaml.txt"
# end: scripts/update-pyproject.toml.pl

[tool.setuptools_scm]
Expand Down
3 changes: 3 additions & 0 deletions requirements/toml.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env -S pip install -r

tomli-w
3 changes: 3 additions & 0 deletions requirements/yaml.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env -S pip install -r

pyyaml
16 changes: 15 additions & 1 deletion src/tree_sitter_lsp/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
==========
"""
from dataclasses import dataclass
from typing import Any
from typing import Any, Callable

from lsprotocol.types import Position, Range
from tree_sitter import Node, Tree
Expand Down Expand Up @@ -111,6 +111,20 @@ def from_tree(cls, tree: Tree) -> "Trie":
"""
return cls.from_node(tree.root_node, None)

@classmethod
def from_file(cls, file: str, parse: Callable[[bytes], Tree]) -> "Trie":
r"""From file.
:param file:
:type file: str
:param parse:
:type parse: Callable[[bytes], Tree]
:rtype: "Trie"
"""
with open(file, "rb") as f:
text = f.read()
return cls.from_tree(parse(text))

@classmethod
def from_node(cls, node: Node, parent: "Trie | None") -> "Trie":
r"""From node.
Expand Down
19 changes: 14 additions & 5 deletions src/tree_sitter_lsp/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
Some common functions used by formatters and linters.
"""
import json
import os
import sys
from typing import Any, Callable
from typing import Any, Callable, Literal

from . import Finder

Expand Down Expand Up @@ -60,21 +59,31 @@ def get_finders(


def pprint(
obj: object, filetype: str = "json", *args: Any, **kwargs: Any
obj: object,
filetype: Literal["json", "yaml", "toml"] = "json",
*args: Any,
**kwargs: Any,
) -> None:
r"""Pprint.
:param obj:
:type obj: object
:param filetype:
:type filetype: str
:type filetype: Literal["json", "yaml", "toml"]
:param args:
:type args: Any
:param kwargs:
:type kwargs: Any
:rtype: None
"""
text = json.dumps(obj, *args, **kwargs)
if filetype == "json":
from json import dumps
elif filetype == "yaml":
from yaml import dump as dumps
elif filetype == "toml":
from tomli_w import dumps

text = dumps(obj, *args, **kwargs)
TERM = os.getenv("TERM", "xterm")
if not sys.stdout.isatty():
TERM = "dumb"
Expand Down

0 comments on commit ded0414

Please sign in to comment.