示例#1
0
文件: main.go 项目: e8vm/shanhu
// Format formats a single file.
func Format(input string) string {
	output, errs := gfmt.File("_.g", []byte(input))

	type result struct {
		Output string   `json:"output"`
		Errs   []string `json:"errs"`
	}

	if errs != nil {
		res := new(result)
		for _, err := range errs {
			res.Errs = append(res.Errs, err.Error())
		}
		return jsonStr(res)
	}

	return jsonStr(&result{Output: string(output)})
}
示例#2
0
文件: gfmt.go 项目: e8vm/shanhu
		}
		res.Add(fmt.Sprintf(
			"snapshot before format: %s", shortHash(hash),
		))

		return s.space.ForEach(func(p string, m *fs.Meta) error {
			if !strings.HasSuffix(p, ".g") {
				return nil
			}

			bs, err := ss.Read(p)
			if err != nil {
				return err
			}

			out, errs := gformat.File(p, bs)
			if errs != nil {
				for _, err := range errs {
					res.Add(&Error{err.Error()})
				}
				res.Add(&Error{
					fmt.Sprintf("parsing error on file %q", p),
				})
				return nil
			}

			if !reflect.DeepEqual(out, bs) {
				res.Add(p)
			}
			return s.space.Write(p, out)
		})