Skip to content

Commit cb6e463

Browse files
author
Colin McNeil
committed
Tree sitter improvements
1 parent 618bf67 commit cb6e463

File tree

4 files changed

+51
-20
lines changed

4 files changed

+51
-20
lines changed

functions/tree_sitter/main.js

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const codeContent = fs.readFileSync(args.path, 'utf8');
3333
// Parse the code using the chosen parser
3434
const parsed = parser.parse(codeContent);
3535

36-
const line_to_grab = args.line + 1
36+
const line_to_grab = args.line
3737

3838
if (line_to_grab === undefined) {
3939
console.log('No line number provided')
@@ -47,9 +47,10 @@ if (line_to_grab > codeContent.split('\n').length) {
4747

4848
// Look for node where node.startPosition.row and node.endPosition.row are equal to line_to_grab
4949
const search_node = (node) => {
50-
if (node.startPosition.row === line_to_grab && node.endPosition.row === line_to_grab) {
50+
if (node.startPosition.row === line_to_grab - 1 && node.endPosition.row === line_to_grab - 1) {
5151
return node
5252
}
53+
5354
for (const child of node.children) {
5455
const result = search_node(child)
5556
if (result) {
@@ -67,20 +68,46 @@ if (!line_node) {
6768
process.exit(1)
6869
}
6970

70-
const parent = line_node.parent
71-
72-
if (parent.text.length > 256) {
73-
console.log('Parent node text is too long, truncating')
74-
const index = parent.text.indexOf(line_node.text)
75-
const start = index - 128
76-
const end = index + 128
77-
parent.text = parent.text.slice(start, end)
71+
const parseParent = (childNode) => {
72+
const parent = childNode.parent
73+
if (!parent) {
74+
return childNode
75+
}
76+
const MIN_CONTEXT_LENGTH = 700
77+
const MAX_CONTEXT_LENGTH = 1500
78+
if (parent.text.length > MAX_CONTEXT_LENGTH) {
79+
console.log('Parent node text is too long, truncating')
80+
const index = parent.text.indexOf(line_node.text)
81+
const start = index - MAX_CONTEXT_LENGTH / 2
82+
const end = index + MAX_CONTEXT_LENGTH / 2
83+
parent.text = parent.text.slice(start, end)
84+
return parent
85+
}
86+
if (parent.text.length < MIN_CONTEXT_LENGTH) {
87+
if (parent.parent) {
88+
return parseParent(parent.parent)
89+
}
90+
else {
91+
return parent
92+
}
93+
}
94+
return parent
7895
}
7996

97+
const ancestor = parseParent(line_node)
98+
99+
100+
const siblings = line_node.parent ? line_node.parent.children : [line_node]
101+
102+
const offending_line = siblings.map(sibling => {
103+
if (sibling.startPosition.row === line_to_grab - 1 && sibling.endPosition.row === line_to_grab - 1) {
104+
return sibling.text
105+
}
106+
}).join('')
80107

81108
console.log({
82-
offending_line: line_node.text,
83-
line_node: line_node,
84-
parent: parent,
85-
parent_text: parent.text
109+
offending_line,
110+
line_node,
111+
ancestor,
112+
ancestor_text: ancestor.text
86113
})

prompts/eslint_fix/010_system_prompt.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
You are an AI assistant who specializes in resolving lint violations in projects. Use the tools available to quickly take action and be very brief.
1+
You are an AI assistant who specializes in resolving lint violations in projects. Use the tools available to quickly take action and be very brief. You must always respond with a real code snippet that will resolve the violation.
22

33
1. Run lint.
44
2. Evaluate total violations.
@@ -10,4 +10,9 @@ You are an AI assistant who specializes in resolving lint violations in projects
1010

1111
For each file:
1212

13-
{>fixing}
13+
{>fixing}
14+
15+
## Complaints:
16+
17+
Just report the json
18+
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
Fix any lint violations you can using the tools provided.
1+
Fix any `no-async-promise-executer` violations in my project.
22

3-
Please only fix `no-unused-vars`
3+
I need fixes in the form of new code to use instead.

prompts/eslint_fix/fixing.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
1. **Get the line**: Use read_eslint with the `path` arg to get all of the violations for a file.
2-
2. **Get the code**: Use tree_sitter with the path and line to read the surrounding code if necessary.
3-
3. **Make the correction**: Suggest an edit in the following format:
2+
2. **Make the correction**: Respond with an edit in JSON format:
43

54
```json
65
{

0 commit comments

Comments
 (0)