Skip to content

Commit

Permalink
feat: support for Python.IndentationError
Browse files Browse the repository at this point in the history
  • Loading branch information
nedpals committed Dec 26, 2023
1 parent 1c5ade3 commit de434da
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
37 changes: 34 additions & 3 deletions error_templates/python/indentation_error.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,45 @@
package python

import lib "github.com/nedpals/errgoengine"
import (
"fmt"

lib "github.com/nedpals/errgoengine"
)

var IndentationError = lib.ErrorTemplate{
Name: "IndentationError",
Pattern: compileTimeError("IndentationError: unindent does not match any outer indentation level"),
OnGenExplainFn: func(cd *lib.ContextData, gen *lib.ExplainGenerator) {
gen.Add("The code is not indented properly")
gen.Add("This error occurs when there is a mismatch in the indentation levels in the code.")
},
OnGenBugFixFn: func(cd *lib.ContextData, gen *lib.BugFixGenerator) {
// TODO:
gen.Add("Correct the indentation", func(s *lib.BugFixSuggestion) {
// use the previous sibling's spacing as basis
prevSibling := cd.MainError.Nearest.PrevNamedSibling()
if prevSibling.Type() == "function_definition" {
prevSibling = prevSibling.ChildByFieldName("body").LastNamedChild()
}

fmt.Println(prevSibling.String())
spaces := cd.MainError.Document.LineAt(prevSibling.StartPosition().Line)[:prevSibling.StartPosition().Column]

s.AddStep("Ensure consistent indentation by using the correct spacing for each level of indentation.").
AddFix(lib.FixSuggestion{
NewText: "",
StartPosition: lib.Position{
Line: cd.MainError.Nearest.StartPosition().Line,
},
EndPosition: cd.MainError.Nearest.StartPosition(),
}).
AddFix(lib.FixSuggestion{
NewText: spaces,
StartPosition: lib.Position{
Line: cd.MainError.Nearest.StartPosition().Line,
},
EndPosition: lib.Position{
Line: cd.MainError.Nearest.StartPosition().Line,
},
})
})
},
}
19 changes: 18 additions & 1 deletion error_templates/python/test_files/indentation_error/test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,21 @@ IndentationError: unindent does not match any outer indentation level
===
template: "Python.IndentationError"
---
The code is not indented properly
# IndentationError
This error occurs when there is a mismatch in the indentation levels in the code.
```
print("Hello, world!")
print("This line is indented with two spaces instead of four.")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

```
## Steps to fix
### Correct the indentation
Ensure consistent indentation by using the correct spacing for each level of indentation.
```diff
def my_function():
print("Hello, world!")
- print("This line is indented with two spaces instead of four.")
+ print("This line is indented with two spaces instead of four.")

```
5 changes: 5 additions & 0 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ func (n SyntaxNode) PrevSibling() SyntaxNode {
return WrapNode(n.Doc, cNode)
}

func (n SyntaxNode) PrevNamedSibling() SyntaxNode {
cNode := n.Node.PrevNamedSibling()
return WrapNode(n.Doc, cNode)
}

func (n SyntaxNode) NamedDescendantForPointRange(posRange Location) SyntaxNode {
sRange := posRange.Range()
cNode := n.Node.NamedDescendantForPointRange(sRange.StartPoint, sRange.EndPoint)
Expand Down

0 comments on commit de434da

Please sign in to comment.