Add self-contained gometalinter build tooling.
This commit is contained in:
117
tools/vendor/github.com/mibk/dupl/output/html.go
generated
vendored
Normal file
117
tools/vendor/github.com/mibk/dupl/output/html.go
generated
vendored
Normal file
@@ -0,0 +1,117 @@
|
||||
package output
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"regexp"
|
||||
"sort"
|
||||
|
||||
"github.com/mibk/dupl/syntax"
|
||||
)
|
||||
|
||||
type HTMLPrinter struct {
|
||||
iota int
|
||||
*TextPrinter
|
||||
}
|
||||
|
||||
func NewHTMLPrinter(w io.Writer, fr FileReader) *HTMLPrinter {
|
||||
fmt.Fprint(w, `<!DOCTYPE html>
|
||||
<meta charset="utf-8"/>
|
||||
<title>Duplicates</title>
|
||||
<style>
|
||||
pre {
|
||||
background-color: #FFD;
|
||||
border: 1px solid #E2E2E2;
|
||||
padding: 1ex;
|
||||
}
|
||||
</style>
|
||||
`)
|
||||
return &HTMLPrinter{
|
||||
TextPrinter: NewTextPrinter(w, fr),
|
||||
}
|
||||
}
|
||||
|
||||
func (p *HTMLPrinter) Print(dups [][]*syntax.Node) error {
|
||||
p.iota++
|
||||
fmt.Fprintf(p.writer, "<h1>#%d found %d clones</h1>\n", p.iota, len(dups))
|
||||
|
||||
clones := make([]clone, len(dups))
|
||||
for i, dup := range dups {
|
||||
cnt := len(dup)
|
||||
if cnt == 0 {
|
||||
panic("zero length dup")
|
||||
}
|
||||
nstart := dup[0]
|
||||
nend := dup[cnt-1]
|
||||
|
||||
file, err := p.freader.ReadFile(nstart.Filename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
lineStart, _ := blockLines(file, nstart.Pos, nend.End)
|
||||
cl := clone{filename: nstart.Filename, lineStart: lineStart}
|
||||
start := findLineBeg(file, nstart.Pos)
|
||||
content := append(toWhitespace(file[start:nstart.Pos]), file[nstart.Pos:nend.End]...)
|
||||
cl.fragment = deindent(content)
|
||||
clones[i] = cl
|
||||
}
|
||||
|
||||
sort.Sort(byNameAndLine(clones))
|
||||
for _, cl := range clones {
|
||||
fmt.Fprintf(p.writer, "<h2>%s:%d</h2>\n<pre>%s</pre>\n", cl.filename, cl.lineStart, cl.fragment)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (*HTMLPrinter) Finish() {}
|
||||
|
||||
func findLineBeg(file []byte, index int) int {
|
||||
for i := index; i >= 0; i-- {
|
||||
if file[i] == '\n' {
|
||||
return i + 1
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func toWhitespace(str []byte) []byte {
|
||||
var out []byte
|
||||
for _, c := range bytes.Runes(str) {
|
||||
if c == '\t' {
|
||||
out = append(out, '\t')
|
||||
} else {
|
||||
out = append(out, ' ')
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func deindent(block []byte) []byte {
|
||||
const maxVal = 99
|
||||
min := maxVal
|
||||
re := regexp.MustCompile(`(^|\n)(\t*)\S`)
|
||||
for _, line := range re.FindAllSubmatch(block, -1) {
|
||||
indent := line[2]
|
||||
if len(indent) < min {
|
||||
min = len(indent)
|
||||
}
|
||||
}
|
||||
if min == 0 || min == maxVal {
|
||||
return block
|
||||
}
|
||||
block = block[min:]
|
||||
Loop:
|
||||
for i := 0; i < len(block); i++ {
|
||||
if block[i] == '\n' && i != len(block)-1 {
|
||||
for j := 0; j < min; j++ {
|
||||
if block[i+j+1] != '\t' {
|
||||
continue Loop
|
||||
}
|
||||
}
|
||||
block = append(block[:i+1], block[i+1+min:]...)
|
||||
}
|
||||
}
|
||||
return block
|
||||
}
|
||||
33
tools/vendor/github.com/mibk/dupl/output/plumbing.go
generated
vendored
Normal file
33
tools/vendor/github.com/mibk/dupl/output/plumbing.go
generated
vendored
Normal file
@@ -0,0 +1,33 @@
|
||||
package output
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"sort"
|
||||
|
||||
"github.com/mibk/dupl/syntax"
|
||||
)
|
||||
|
||||
type PlumbingPrinter struct {
|
||||
*TextPrinter
|
||||
}
|
||||
|
||||
func NewPlumbingPrinter(w io.Writer, fr FileReader) *PlumbingPrinter {
|
||||
return &PlumbingPrinter{NewTextPrinter(w, fr)}
|
||||
}
|
||||
|
||||
func (p *PlumbingPrinter) Print(dups [][]*syntax.Node) error {
|
||||
clones, err := p.prepareClonesInfo(dups)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sort.Sort(byNameAndLine(clones))
|
||||
for i, cl := range clones {
|
||||
nextCl := clones[(i+1)%len(clones)]
|
||||
fmt.Fprintf(p.writer, "%s:%d-%d: duplicate of %s:%d-%d\n", cl.filename, cl.lineStart, cl.lineEnd,
|
||||
nextCl.filename, nextCl.lineStart, nextCl.lineEnd)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *PlumbingPrinter) Finish() {}
|
||||
109
tools/vendor/github.com/mibk/dupl/output/text.go
generated
vendored
Normal file
109
tools/vendor/github.com/mibk/dupl/output/text.go
generated
vendored
Normal file
@@ -0,0 +1,109 @@
|
||||
package output
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"sort"
|
||||
|
||||
"github.com/mibk/dupl/syntax"
|
||||
)
|
||||
|
||||
type FileReader interface {
|
||||
ReadFile(filename string) ([]byte, error)
|
||||
}
|
||||
|
||||
type Printer interface {
|
||||
Print(dups [][]*syntax.Node) error
|
||||
Finish()
|
||||
}
|
||||
|
||||
type TextPrinter struct {
|
||||
writer io.Writer
|
||||
freader FileReader
|
||||
cnt int
|
||||
}
|
||||
|
||||
func NewTextPrinter(w io.Writer, fr FileReader) *TextPrinter {
|
||||
return &TextPrinter{
|
||||
writer: w,
|
||||
freader: fr,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *TextPrinter) Print(dups [][]*syntax.Node) error {
|
||||
p.cnt++
|
||||
fmt.Fprintf(p.writer, "found %d clones:\n", len(dups))
|
||||
clones, err := p.prepareClonesInfo(dups)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sort.Sort(byNameAndLine(clones))
|
||||
for _, cl := range clones {
|
||||
fmt.Fprintf(p.writer, " %s:%d,%d\n", cl.filename, cl.lineStart, cl.lineEnd)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *TextPrinter) prepareClonesInfo(dups [][]*syntax.Node) ([]clone, error) {
|
||||
clones := make([]clone, len(dups))
|
||||
for i, dup := range dups {
|
||||
cnt := len(dup)
|
||||
if cnt == 0 {
|
||||
panic("zero length dup")
|
||||
}
|
||||
nstart := dup[0]
|
||||
nend := dup[cnt-1]
|
||||
|
||||
file, err := p.freader.ReadFile(nstart.Filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cl := clone{filename: nstart.Filename}
|
||||
cl.lineStart, cl.lineEnd = blockLines(file, nstart.Pos, nend.End)
|
||||
clones[i] = cl
|
||||
}
|
||||
return clones, nil
|
||||
}
|
||||
|
||||
func (p *TextPrinter) Finish() {
|
||||
fmt.Fprintf(p.writer, "\nFound total %d clone groups.\n", p.cnt)
|
||||
}
|
||||
|
||||
func blockLines(file []byte, from, to int) (int, int) {
|
||||
line := 1
|
||||
lineStart, lineEnd := 0, 0
|
||||
for offset, b := range file {
|
||||
if b == '\n' {
|
||||
line++
|
||||
}
|
||||
if offset == from {
|
||||
lineStart = line
|
||||
}
|
||||
if offset == to-1 {
|
||||
lineEnd = line
|
||||
break
|
||||
}
|
||||
}
|
||||
return lineStart, lineEnd
|
||||
}
|
||||
|
||||
type clone struct {
|
||||
filename string
|
||||
lineStart int
|
||||
lineEnd int
|
||||
fragment []byte
|
||||
}
|
||||
|
||||
type byNameAndLine []clone
|
||||
|
||||
func (c byNameAndLine) Len() int { return len(c) }
|
||||
|
||||
func (c byNameAndLine) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
|
||||
|
||||
func (c byNameAndLine) Less(i, j int) bool {
|
||||
if c[i].filename == c[j].filename {
|
||||
return c[i].lineStart < c[j].lineStart
|
||||
}
|
||||
return c[i].filename < c[j].filename
|
||||
}
|
||||
Reference in New Issue
Block a user