Skip to content
Merged
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
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Changelog
=========

vNext
-----

- Add support for Cython
- Add the function ``source_inspector.symbols_tree_sitter.get_tree_and_language_info``

v0.6.1
------

Expand Down
28 changes: 24 additions & 4 deletions src/source_inspector/symbols_tree_sitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,34 @@ def get_treesitter_symbols(location, **kwargs):
)


def collect_symbols_and_strings(location):
"""
Return lists containing mappings of symbols and strings collected from file at location.
def get_tree_and_language_info(location):
"""
symbols, strings = [], []
Given the `location` of a file, determine the correct parser to use, parse
the file, and return a tuple of (`tree`, `language_info`).

Return (None, None) if a parser is not found for the file type at `location`
"""
tree = None
language_info = None
if parser_result := get_parser(location):
parser, language_info = parser_result

with open(location, "rb") as f:
source = f.read()

tree = parser.parse(source)
return tree, language_info
return tree, language_info


def collect_symbols_and_strings(location):
"""
Return lists containing mappings of symbols and strings collected from file at location.
"""
symbols, strings = [], []

tree, language_info = get_tree_and_language_info(location)
if tree and language_info:
traverse(tree.root_node, symbols, strings, language_info)

return symbols, strings
Expand Down Expand Up @@ -165,6 +180,11 @@ def traverse(node, symbols, strings, language_info):
"identifiers": ["identifier"],
"string_literals": ["string_literal"],
},
"Cython": {
"wheel": "tree_sitter_python",
"identifiers": ["identifier"],
"string_literals": ["string_literal"],
},
"Go": {
"wheel": "tree_sitter_go",
"identifiers": ["identifier"],
Expand Down
Loading