Skip to content

Commit dc0acf6

Browse files
author
Colin McNeil
committed
Tree sitter tool
1 parent a8b29b2 commit dc0acf6

File tree

4 files changed

+111
-0
lines changed

4 files changed

+111
-0
lines changed

functions/tree_sitter/Dockerfile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
FROM node:alpine3.20 AS build
2+
3+
WORKDIR /tree-sitter
4+
5+
# Install python/pip
6+
ENV PYTHONUNBUFFERED=1
7+
RUN apk add --update --no-cache python3 make gcc g++ && ln -sf python3 /usr/bin/python
8+
9+
RUN npm install tree-sitter tree-sitter-python tree-sitter-typescript tree-sitter-javascript
10+
11+
12+
# Copy tree-sitter dir to new image
13+
FROM node:alpine3.20
14+
15+
COPY main.js /main.js
16+
COPY --from=build /tree-sitter/node_modules /node_modules
17+
18+
ENTRYPOINT [ "node", "main.js" ]

functions/tree_sitter/main.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const TreeSitter = require('tree-sitter');
2+
const Python = require('tree-sitter-python');
3+
const fs = require('fs');
4+
5+
const args = JSON.parse(process.argv[2])
6+
7+
// Load the Python parser
8+
const parser = new TreeSitter();
9+
// Set the language to the parser
10+
parser.setLanguage(Python);
11+
// Read the code file content
12+
const codeContent = fs.readFileSync(args.path, 'utf8');
13+
// Parse the code using the chosen parser
14+
const parsed = parser.parse(codeContent);
15+
16+
const line_to_grab = args.line
17+
18+
// Look for node where node.startPosition.row and node.endPosition.row are equal to line_to_grab
19+
const search_node = (node) => {
20+
if (node.startPosition.row === line_to_grab && node.endPosition.row === line_to_grab) {
21+
return node
22+
}
23+
for (const child of node.children) {
24+
const result = search_node(child)
25+
if (result) {
26+
return result
27+
}
28+
}
29+
return null
30+
}
31+
32+
const line_node = search_node(parsed.rootNode)
33+
34+
const parent = line_node.parent
35+
36+
if (parent) {
37+
const start_line = parent.startPosition.row
38+
const end_line = parent.endPosition.row
39+
// Return codeContent from start_line to end_line
40+
const lines = codeContent.split('\n').slice(start_line, end_line + 1)
41+
parent.content = lines.join('\n')
42+
}
43+
44+
console.log({
45+
offending_line: line_node.text,
46+
line_node: line_node,
47+
parent: parent,
48+
parent_text: parent.text
49+
})

functions/tree_sitter/readme.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
```sh
2+
docker build . -t vonwig/tree_sitter
3+
docker run --mount type=bind,source=$PWD,target=/project --workdir /project vonwig/tree_sitter '{"path": "test_file.py", "line": 32}'
4+
```

functions/tree_sitter/test_file.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from pprint import pprint
2+
from textwrap import wrap
3+
from flask import Flask, jsonify, request
4+
5+
app = Flask(__name__)
6+
methods = ["GET", "POST", "PATCH", "DELETE"]
7+
8+
9+
@app.route("/", methods=methods, defaults={"path": ""})
10+
@app.route("/<path:path>", methods=methods)
11+
def hello_world(path):
12+
divider = "================================================================"
13+
j = request.get_json()
14+
15+
print(divider)
16+
print(f"*** Received data at: {path}")
17+
18+
print("\n** data:")
19+
print("\n".join(wrap(request.data.decode())))
20+
21+
print("\n** form:")
22+
pprint(request.form)
23+
24+
print("\n** json:")
25+
pprint(j)
26+
27+
print(f"{divider}\n\n")
28+
29+
return jsonify(
30+
{
31+
"endpoint": path,
32+
"data": request.data.decode("utf-8"),
33+
"form": request.form,
34+
"json": request.get_json(),
35+
}
36+
)
37+
38+
39+
if __name__ == "__main__":
40+
app.run()

0 commit comments

Comments
 (0)