tree-sitter-python
tree-sitter-python copied to clipboard
What is the best practice for getting string contents?
trafficstars
I was kind of expecting a string_contents node in the middle of the string.children. What is the best practice for getting string contents?
ipdb> root = py_parser().parse(b'"hi"').root_node
ipdb> root.text
b'"hi"'
ipdb> root.sexp()
'(module (expression_statement (string)))'
ipdb> string = root.children[0].children[0]
ipdb> string.text
b'"hi"'
ipdb> string.sexp()
'(string)'
ipdb> string.children
[<Node kind=""", start_point=(0, 0), end_point=(0, 1)>, <Node kind=""", start_point=(0, 3), end_point=(0, 4)>]
https://github.com/tree-sitter/tree-sitter-python/pull/114 can help with this, otherwise I think the other option would be to just check for ' or " and call node.strip("'")
There is a (string_content) node now https://github.com/tree-sitter/tree-sitter-python/blob/7c8930b6388b5590ebef248853f144185a9eda1d/grammar.js#L946-L958