aboutsummaryrefslogtreecommitdiff
path: root/inspect.go
blob: 8452de5c998f3e576ce948863bf2eaf83b058d69 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main

import (
	"fmt"
	"go/ast"
	"go/token"
)

func isGeneralDeclarationScoped(generalDeclaration *ast.GenDecl) bool {
	for _, specification := range generalDeclaration.Specs {
		if typeSpecification, isTypeSpecification := specification.(*ast.TypeSpec); isTypeSpecification {
			switch typeSpecification.Type.(type) {
			case *ast.StructType, *ast.InterfaceType:
				return true
			}
		}
	}

	return false
}

func (f *Formatter) buildLineInfo(tokenFileSet *token.FileSet, parsedFile *ast.File) map[int]*lineInformation {
	lineInformationMap := make(map[int]*lineInformation, 2*len(parsedFile.Decls))
	tokenFile := tokenFileSet.File(parsedFile.Pos())

	if tokenFile == nil {
		return lineInformationMap
	}

	for _, declaration := range parsedFile.Decls {
		startLine := tokenFile.Line(declaration.Pos())
		endLine := tokenFile.Line(declaration.End())
		statementType := ""
		isScoped := false

		switch typedDeclaration := declaration.(type) {
		case *ast.GenDecl:
			statementType = typedDeclaration.Tok.String()
			isScoped = isGeneralDeclarationScoped(typedDeclaration)
		case *ast.FuncDecl:
			statementType = "func"
			isScoped = true
		default:
			statementType = fmt.Sprintf("%T", declaration)
		}

		lineInformationMap[startLine] = &lineInformation{statementType: statementType, isTopLevel: true, isScoped: isScoped, isStartLine: true}

		if endLine != startLine {
			lineInformationMap[endLine] = &lineInformation{statementType: statementType, isTopLevel: true, isScoped: isScoped, isStartLine: false}
		}
	}

	ast.Inspect(parsedFile, func(astNode ast.Node) bool {
		if astNode == nil {
			return true
		}

		switch typedNode := astNode.(type) {
		case *ast.BlockStmt:
			f.processBlock(tokenFile, typedNode, lineInformationMap)
		case *ast.CaseClause:
			f.processStatementList(tokenFile, typedNode.Body, lineInformationMap)
		case *ast.CommClause:
			f.processStatementList(tokenFile, typedNode.Body, lineInformationMap)
		}

		return true
	})

	return lineInformationMap
}

func (f *Formatter) processBlock(tokenFile *token.File, blockStatement *ast.BlockStmt, lineInformationMap map[int]*lineInformation) {
	if blockStatement == nil {
		return
	}

	f.processStatementList(tokenFile, blockStatement.List, lineInformationMap)
}

func (f *Formatter) processStatementList(tokenFile *token.File, statements []ast.Stmt, lineInformationMap map[int]*lineInformation) {
	for _, statement := range statements {
		startLine := tokenFile.Line(statement.Pos())
		endLine := tokenFile.Line(statement.End())
		statementType := ""
		isScoped := false

		switch typedStatement := statement.(type) {
		case *ast.DeclStmt:
			if generalDeclaration, isGeneralDeclaration := typedStatement.Decl.(*ast.GenDecl); isGeneralDeclaration {
				statementType = generalDeclaration.Tok.String()
			} else {
				statementType = fmt.Sprintf("%T", statement)
			}
		case *ast.IfStmt, *ast.ForStmt, *ast.RangeStmt, *ast.SwitchStmt,
			*ast.TypeSwitchStmt, *ast.SelectStmt, *ast.BlockStmt:
			statementType = fmt.Sprintf("%T", statement)
			isScoped = true
		default:
			statementType = fmt.Sprintf("%T", statement)
		}

		existingStart := lineInformationMap[startLine]

		if existingStart == nil || !existingStart.isStartLine {
			lineInformationMap[startLine] = &lineInformation{statementType: statementType, isTopLevel: false, isScoped: isScoped, isStartLine: true}
		}

		existingEnd := lineInformationMap[endLine]

		if existingEnd == nil || !existingEnd.isStartLine {
			lineInformationMap[endLine] = &lineInformation{statementType: statementType, isTopLevel: false, isScoped: isScoped, isStartLine: false}
		}

		switch typedStatement := statement.(type) {
		case *ast.IfStmt:
			f.processBlock(tokenFile, typedStatement.Body, lineInformationMap)

			if typedStatement.Else != nil {
				if elseBlock, isBlockStatement := typedStatement.Else.(*ast.BlockStmt); isBlockStatement {
					f.processBlock(tokenFile, elseBlock, lineInformationMap)
				} else if elseIfStatement, isIfStatement := typedStatement.Else.(*ast.IfStmt); isIfStatement {
					f.processIfStatement(tokenFile, elseIfStatement, lineInformationMap)
				}
			}
		case *ast.ForStmt:
			f.processBlock(tokenFile, typedStatement.Body, lineInformationMap)
		case *ast.RangeStmt:
			f.processBlock(tokenFile, typedStatement.Body, lineInformationMap)
		case *ast.SwitchStmt:
			f.processBlock(tokenFile, typedStatement.Body, lineInformationMap)
		case *ast.TypeSwitchStmt:
			f.processBlock(tokenFile, typedStatement.Body, lineInformationMap)
		case *ast.SelectStmt:
			f.processBlock(tokenFile, typedStatement.Body, lineInformationMap)
		case *ast.BlockStmt:
			f.processBlock(tokenFile, typedStatement, lineInformationMap)
		}
	}
}

func (f *Formatter) processIfStatement(tokenFile *token.File, ifStatement *ast.IfStmt, lineInformationMap map[int]*lineInformation) {
	startLine := tokenFile.Line(ifStatement.Pos())
	endLine := tokenFile.Line(ifStatement.End())
	existingStart := lineInformationMap[startLine]

	if existingStart == nil || !existingStart.isStartLine {
		lineInformationMap[startLine] = &lineInformation{statementType: "*ast.IfStmt", isTopLevel: false, isScoped: true, isStartLine: true}
	}

	existingEnd := lineInformationMap[endLine]

	if existingEnd == nil || !existingEnd.isStartLine {
		lineInformationMap[endLine] = &lineInformation{statementType: "*ast.IfStmt", isTopLevel: false, isScoped: true, isStartLine: false}
	}

	f.processBlock(tokenFile, ifStatement.Body, lineInformationMap)

	if ifStatement.Else != nil {
		if elseBlock, isBlockStatement := ifStatement.Else.(*ast.BlockStmt); isBlockStatement {
			f.processBlock(tokenFile, elseBlock, lineInformationMap)
		} else if elseIfStatement, isIfStatement := ifStatement.Else.(*ast.IfStmt); isIfStatement {
			f.processIfStatement(tokenFile, elseIfStatement, lineInformationMap)
		}
	}
}