Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added variable declaration support 🧰 #19

Merged
merged 3 commits into from
May 7, 2024
Merged
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
57 changes: 57 additions & 0 deletions sourcecode-parser/construct.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ type GraphNode struct {
ImportPackage []string
SuperClass string
Interface []string
DataType string
Scope string
VariableValue string
}

type GraphEdge struct {
Expand Down Expand Up @@ -193,6 +196,60 @@ func buildGraphFromAST(node *sitter.Node, sourceCode []byte, graph *CodeGraph, c
Interface: implementedInterface,
}
graph.AddNode(classNode)
case "local_variable_declaration", "field_declaration":
// Extract variable name, type, and modifiers
variableName := ""
variableType := ""
variableModifier := ""
variableValue := ""
var scope string
if node.Type() == "local_variable_declaration" {
scope = "local"
} else {
scope = "field"
}
for i := 0; i < int(node.ChildCount()); i++ {
child := node.Child(i)
switch child.Type() {
case "variable_declarator":
variableName = child.Content(sourceCode)
for j := 0; j < int(child.ChildCount()); j++ {
if child.Child(j).Type() == "identifier" {
variableName = child.Child(j).Content(sourceCode)
}
// if child type contains =, iterate through and get remaining content
if child.Child(j).Type() == "=" {
for k := j + 1; k < int(child.ChildCount()); k++ {
variableValue += child.Child(k).Content(sourceCode)
}
}

}
// remove spaces from variable value
variableValue = strings.ReplaceAll(variableValue, " ", "")
// remove new line from variable value
variableValue = strings.ReplaceAll(variableValue, "\n", "")
case "modifiers":
variableModifier = child.Content(sourceCode)
}
// if child type contains type, get the type of variable
if strings.Contains(child.Type(), "type") {
variableType = child.Content(sourceCode)
}
}
// Create a new node for the variable
variableNode := &GraphNode{
ID: generateMethodID(variableName, []string{}),
Type: "variable_declaration",
Name: variableName,
CodeSnippet: node.Content(sourceCode),
LineNumber: node.StartPoint().Row + 1,
Modifier: extractVisibilityModifier(variableModifier),
DataType: variableType,
Scope: scope,
VariableValue: variableValue,
}
graph.AddNode(variableNode)
}

// Recursively process child nodes
Expand Down
Loading