Пример #1
0
// only Decls as args (doesn't go in depth)
func (vis *getSelectedArgumentVisitor) Visit(node ast.Node) (w ast.Visitor) {
	switch t := node.(type) {
	case *ast.FuncDecl:
		if t.Type == nil {
			return nil
		}
		if t.Type.Params == nil {
			return nil
		}
		for _, f := range t.Type.Params.List {
			for i, n := range f.Names {
				if utils.ComparePosWithinFile(vis.FileSet.Position(n.Pos()), vis.argPos) == 0 {
					vis.result = f
					vis.nameNumber = i
					return nil
				}
			}
			if utils.ComparePosWithinFile(vis.FileSet.Position(f.Pos()), vis.argPos) == 0 {
				vis.result = f
				vis.nameNumber = -1
				return nil
			}
		}
	}
	return nil
}
Пример #2
0
func (vis *extractedSetVisitor) Visit(node ast.Node) ast.Visitor {

	if vis.foundFirst() {
		return nil
	}
	switch t := node.(type) {
	case *ast.BlockStmt:
		vis.checkStmtList(t.List)
		if vis.isValid() {
			vis.nodeFrom = t
		}
	case *ast.SwitchStmt:
		for _, cc := range t.Body.List {
			caseClause := cc.(*ast.CaseClause)
			vis.checkStmtList(caseClause.Body)
			if vis.isValid() {
				vis.nodeFrom = caseClause
				break
			}
		}
	case *ast.TypeSwitchStmt:
		for _, cc := range t.Body.List {
			caseClause := cc.(*ast.CaseClause)
			vis.checkStmtList(caseClause.Body)
			if vis.isValid() {
				vis.nodeFrom = caseClause
				break
			}
		}
	case *ast.SelectStmt:
		for _, cc := range t.Body.List {
			caseClause := cc.(*ast.CommClause)
			vis.checkStmtList(caseClause.Body)
			if vis.isValid() {
				vis.nodeFrom = caseClause
				break
			}
		}
	case *ast.CallExpr:
		vis.checkExprList(t.Args)
	case *ast.AssignStmt:
		vis.checkExprList(t.Rhs)
	case *ast.ReturnStmt:
		vis.checkExprList(t.Results)
	case ast.Expr:
		if utils.ComparePosWithinFile(vis.Package.FileSet.Position(t.Pos()), vis.firstNodePos) == 0 {
			if utils.ComparePosWithinFile(vis.Package.FileSet.Position(t.End()), vis.lastNodePos) == 0 {
				vis.firstNode = t
				vis.lastNode = t
				vis.resultBlock.Push(t)
			}
		}
	}

	return vis
}
Пример #3
0
func (vis *findNodeVisitor) Visit(node ast.Node) (w ast.Visitor) {
	if node == nil || vis.result != nil {
		return nil
	}
	if utils.ComparePosWithinFile(vis.stPos, vis.fset.Position(node.Pos())) == 0 &&
		utils.ComparePosWithinFile(vis.endPos, vis.fset.Position(node.End())) == 0 {
		vis.result = node
		return nil
	}
	return vis
}
Пример #4
0
func (vis *extractedSetVisitor) checkExprList(list []ast.Expr) {
	if len(list) == 0 {
		return
	}
	if utils.ComparePosWithinFile(vis.Package.FileSet.Position(list[0].Pos()), vis.firstNodePos) == 0 &&
		utils.ComparePosWithinFile(vis.Package.FileSet.Position(list[len(list)-1].End()), vis.lastNodePos) == 0 {
		vis.firstNode = list[0]
		vis.lastNode = list[len(list)-1]
		vis.resultBlock.Push(list)
	}
}
Пример #5
0
func (vis *extractedSetVisitor) checkStmtList(list []ast.Stmt) {

	for i, stmt := range list {
		if !vis.foundFirst() {
			if utils.ComparePosWithinFile(vis.Package.FileSet.Position(stmt.Pos()), vis.firstNodePos) == 0 {
				vis.firstNode = stmt
				vis.resultBlock.Push(stmt)

				if utils.ComparePosWithinFile(vis.Package.FileSet.Position(stmt.End()), vis.lastNodePos) == 0 {
					//one stmt method
					vis.lastNode = stmt
				}
			}
		} else {
			if !vis.isValid() {
				vis.resultBlock.Push(stmt)
				if utils.ComparePosWithinFile(vis.Package.FileSet.Position(stmt.End()), vis.lastNodePos) == 0 {
					vis.lastNode = list[i]
				}
			}
		}
	}
}
Пример #6
0
func (vis *getCallVisitor) find(n ast.Node) bool {
	return utils.ComparePosWithinFile(vis.FileSet.Position(n.Pos()), vis.callStart) == 0 &&
		utils.ComparePosWithinFile(vis.FileSet.Position(n.End()), vis.callEnd) == 0
}