Skip to content

Commit

Permalink
🐛 Fix #6: parse quotes correctly
Browse files Browse the repository at this point in the history
Add --convert
  • Loading branch information
Freed-Wu committed Nov 29, 2023
1 parent 610ae14 commit 01675e5
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 33 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ platformdirs
# uri
rfc3987
tree-sitter-languages
tree-sitter-lsp >= 0.0.3
tree-sitter-lsp >= 0.0.5
75 changes: 48 additions & 27 deletions src/termux_language_server/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def get_parser():
parser.add_argument(
"--generate-schema",
choices=FILETYPE.__args__, # type: ignore
help="generate schema json",
help="generate schema in an output format",
)
parser.add_argument(
"--indent",
Expand All @@ -57,41 +57,62 @@ def get_parser():
"--color",
choices=["auto", "always", "never"],
default="auto",
help="when to display color",
help="when to display color, default: %(default)s",
)
parser.add_argument(
"--convert",
nargs="*",
default={},
help="convert files to output format",
)
parser.add_argument(
"--output-format",
choices=["json", "yaml", "toml"],
default="json",
help="output format: %(default)s",
)
return parser


def main():
r"""Parse arguments and provide shell completions."""
parser = get_parser()
args = parser.parse_args()
args = get_parser().parse_args()

if args.generate_schema:
if args.generate_schema or args.format or args.check or args.convert:
from tree_sitter_languages import get_parser as _get_parser
from tree_sitter_lsp.diagnose import check
from tree_sitter_lsp.format import format
from tree_sitter_lsp.utils import pprint

from .misc import get_schema

pprint(get_schema(args.generate_schema), indent=args.indent)
exit()
from tree_sitter_languages import get_parser as _get_parser
from tree_sitter_lsp.diagnose import check
from tree_sitter_lsp.format import format

from .finders import DIAGNOSTICS_FINDER_CLASSES, FORMAT_FINDER_CLASSES
from .utils import get_filetype

parser = _get_parser("bash")
format(args.format, parser.parse, FORMAT_FINDER_CLASSES, get_filetype)
result = check(
args.check,
parser.parse,
DIAGNOSTICS_FINDER_CLASSES,
get_filetype,
args.color,
)
if args.format or args.check:
exit(result)
from .finders import DIAGNOSTICS_FINDER_CLASSES, FORMAT_FINDER_CLASSES
from .schema import BashTrie
from .utils import get_filetype

parser = _get_parser("bash")
if args.generate_schema:
from .misc import get_schema

pprint(
get_schema(args.generate_schema),
filetype=args.output_format,
indent=args.indent,
)
for file in args.convert:
pprint(
BashTrie.from_file(file, parser.parse).to_json(),
filetype=args.output_format,
indent=args.indent,
)
format(args.format, parser.parse, FORMAT_FINDER_CLASSES, get_filetype)
exit(
check(
args.check,
parser.parse,
DIAGNOSTICS_FINDER_CLASSES,
get_filetype,
args.color,
)
)

from .server import TermuxLanguageServer

Expand Down
30 changes: 26 additions & 4 deletions src/termux_language_server/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,29 @@ class BashTrie(Trie):

value: dict[str, "Trie"] | list["Trie"] | str | Literal[0] = 0

@classmethod
def from_string_node(cls, node: Node, parent: "Trie | None") -> "Trie":
r"""From string node.
`<https://github.com/tree-sitter/tree-sitter-bash/issues/101>`_
:param cls:
:param node:
:type node: Node
:param parent:
:type parent: Trie | None
:rtype: "Trie"
"""
if node.type == "string" and node.children == 3:
node = node.children[1]
text = UNI.node2text(node)
_range = UNI.node2range(node)
if node.type in {"string", "raw_string"} and node.children != 3:
text = eval(text) # nosec: B307
_range.start.character += 1
_range.end.character -= 1
return cls(_range, parent, text)

@classmethod
def from_node(cls, node: Node, parent: "Trie | None") -> "Trie":
r"""From node.
Expand All @@ -29,14 +52,13 @@ def from_node(cls, node: Node, parent: "Trie | None") -> "Trie":
string_types = {
"word",
"string",
"raw_string",
"concatenation",
"number",
"simple_expansion",
}
if node.type in string_types:
if node.type == "string":
node = node.children[1]
return cls(UNI.node2range(node), parent, UNI.node2text(node))
return cls.from_string_node(node, parent)
if node.type == "function_definition":
return cls(UNI.node2range(node), parent, 0)
if node.type == "variable_assignment":
Expand All @@ -53,7 +75,7 @@ def from_node(cls, node: Node, parent: "Trie | None") -> "Trie":
]
return trie
if node.type in string_types:
return cls(UNI.node2range(node), parent, UNI.node2text(node))
return cls.from_string_node(node, parent)
if node.type == "program":
trie = cls(Range(Position(0, 0), Position(1, 0)), parent, {})
value: dict[str, Trie] = trie.value # type: ignore
Expand Down
2 changes: 1 addition & 1 deletion tests/PKGBUILD
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ url=https://github.com/Freed-Wu/pkgbuild-language-server
depends=(python-colorama python-jinja python-platformdirs python-pygls python-tree-sitter)
optdepends=(python-pypandoc python-markdown-it-py pacman pyalpm namcap)
makedepends=python-installer
license=(GPL3)
license=('GPL3')
_py=py3
source=("https://files.pythonhosted.org/packages/$_py/${pkgname::1}/${pkgname//-/_}/${pkgname//-/_}-$pkgver-$_py-none-any.whl")
sha256sums=('db062b5028e93aa9304d2783cd73017320587ac64fc4d8c01f514ae1015a4bf0')
Expand Down

0 comments on commit 01675e5

Please sign in to comment.