Esempio n. 1
0
func (w *PkgWalker) LookupImport(pkg *types.Package, pkgInfo *types.Info, cursor *FileCursor, is *ast.ImportSpec) {
	fpath, err := strconv.Unquote(is.Path.Value)
	if err != nil {
		return
	}
	fbase := fpath
	pos := strings.LastIndexAny(fpath, "./-\\")
	if pos != -1 {
		fbase = fpath[pos+1:]
	}
	fid := fpath + "." + fbase
	//kind := ObjPkgName
	//fmt.Println(kind, true)

	if typeFindDef {
		fmt.Println(w.fset.Position(is.Pos()))
	}
	if typeFindInfo {
		fmt.Println("package", fpath)
	}
	if !typeFindUse {
		return
	}
	var usages []int
	for id, obj := range pkgInfo.Uses {
		if obj != nil && obj.Id() == fid { //!= nil && cursorObj.Pos() == obj.Pos() {
			usages = append(usages, int(id.Pos()))
		}
	}
	(sort.IntSlice(usages)).Sort()
	for _, pos := range usages {
		fmt.Println(w.fset.Position(token.Pos(pos)))
	}
}
Esempio n. 2
0
func (p *patchUnused) UnusedImport(imp *ast.ImportSpec) {
	if imp.Name != nil {
		p.patches = append(p.patches, patch.Replace(imp.Name, "_"))
	} else {
		p.patches = append(p.patches, patch.Insert(imp.Pos(), "_ "))
	}
}
Esempio n. 3
0
func (w *PkgWalker) LookupImport(pkg *types.Package, pkgInfo *types.Info, cursor *FileCursor, is *ast.ImportSpec) {
	fpath, err := strconv.Unquote(is.Path.Value)
	if err != nil {
		return
	}

	if typesFindDef {
		fmt.Println(w.fset.Position(is.Pos()))
	}

	fbase := fpath
	pos := strings.LastIndexAny(fpath, "./-\\")
	if pos != -1 {
		fbase = fpath[pos+1:]
	}

	var fname string
	if is.Name != nil {
		fname = is.Name.Name
	} else {
		fname = fbase
	}

	if typesFindInfo {
		if fname == fpath {
			fmt.Printf("package %s\n", fname)
		} else {
			fmt.Printf("package %s (\"%s\")\n", fname, fpath)
		}
	}

	if !typesFindUse {
		return
	}

	path := pkg.Path()

	if strings.Contains(path, "vendor/") {
		path = strings.Split(path, "vendor/")[1]
	}

	fid := path + "." + fname

	var usages []int
	for id, obj := range pkgInfo.Uses {
		if obj != nil && obj.Id() == fid { //!= nil && cursorObj.Pos() == obj.Pos() {
			usages = append(usages, int(id.Pos()))
		}
	}

	(sort.IntSlice(usages)).Sort()
	for _, pos := range usages {
		fmt.Println(w.fset.Position(token.Pos(pos)))
	}
}
Esempio n. 4
0
func (ps *ProjectStats) foundImport(fs *token.FileSet, i *ast.ImportSpec, path string) error {
	importPath, err := strconv.Unquote(i.Path.Value)
	if err != nil {
		return err
	}
	ref := fs.Position(i.Pos())
	_, found := ps.ImportStatsByPath[importPath]
	if found {
		ps.ImportStatsByPath[importPath].ReferencePositions = append(ps.ImportStatsByPath[importPath].ReferencePositions, ref)
	} else {
		ps.ImportStatsByPath[importPath] = NewImportStats(importPath, ref)
	}
	return nil
}
Esempio n. 5
0
func (w *PkgWalker) LookupImport(pkg *types.Package, pkgInfo *types.Info, cursor *FileCursor, is *ast.ImportSpec) []*Doc {
	fpath, err := strconv.Unquote(is.Path.Value)
	if err != nil {
		return []*Doc{}
	}

	ret := []*Doc{}

	if w.findDef {
		fpos := w.fset.Position(is.Pos())
		ret = append(ret, &Doc{
			Pkg:  pkg.Name(),
			Src:  "",
			Name: is.Name.Name,
			Kind: "package",
			Fn:   fpos.Filename,
			Row:  fpos.Line - 1,
			Col:  fpos.Column - 1,
		})
		fmt.Println(fpos)
	}

	fbase := fpath
	pos := strings.LastIndexAny(fpath, "./-\\")
	if pos != -1 {
		fbase = fpath[pos+1:]
	}

	var fname string
	if is.Name != nil {
		fname = is.Name.Name
	} else {
		fname = fbase
	}

	if w.findInfo {
		if fname == fpath {
			fmt.Printf("package %s\n", fname)
		} else {
			fmt.Printf("package %s (\"%s\")\n", fname, fpath)
		}
	}

	if !w.findUse {
		return ret
	}

	fid := pkg.Path() + "." + fname
	var usages []int
	for id, obj := range pkgInfo.Uses {
		if obj != nil && obj.Id() == fid { //!= nil && cursorObj.Pos() == obj.Pos() {
			usages = append(usages, int(id.Pos()))
		}
	}
	(sort.IntSlice(usages)).Sort()
	for _, pos := range usages {
		fpos := w.fset.Position(token.Pos(pos))
		ret = append(ret, &Doc{
			Pkg:  pkg.Name(),
			Src:  "",
			Name: fname,
			Kind: "package",
			Fn:   fpos.Filename,
			Row:  fpos.Line - 1,
			Col:  fpos.Column - 1,
		})
		if typeVerbose {
			log.Println(fpos)
		}
	}

	return ret
}