Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
3d4b55ad
Commit
3d4b55ad
authored
Apr 13, 2011
by
Robert Griesemer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
gofmt: minor refactor to permit easy testing
R=rsc CC=golang-dev
https://golang.org/cl/4397046
parent
7b6ee1a5
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
21 additions
and
20 deletions
+21
-20
gofmt.go
src/cmd/gofmt/gofmt.go
+21
-20
No files found.
src/cmd/gofmt/gofmt.go
View file @
3d4b55ad
...
...
@@ -13,6 +13,7 @@ import (
"go/printer"
"go/scanner"
"go/token"
"io"
"io/ioutil"
"os"
"path/filepath"
...
...
@@ -86,14 +87,23 @@ func isGoFile(f *os.FileInfo) bool {
}
func
processFile
(
f
*
os
.
File
)
os
.
Error
{
src
,
err
:=
ioutil
.
ReadAll
(
f
)
// If in == nil, the source is the contents of the file with the given filename.
func
processFile
(
filename
string
,
in
io
.
Reader
,
out
io
.
Writer
)
os
.
Error
{
if
in
==
nil
{
f
,
err
:=
os
.
Open
(
filename
)
if
err
!=
nil
{
return
err
}
defer
f
.
Close
()
in
=
f
}
src
,
err
:=
ioutil
.
ReadAll
(
in
)
if
err
!=
nil
{
return
err
}
file
,
err
:=
parser
.
ParseFile
(
fset
,
f
.
Name
(),
src
,
parserMode
)
file
,
err
:=
parser
.
ParseFile
(
fset
,
filename
,
src
,
parserMode
)
if
err
!=
nil
{
return
err
}
...
...
@@ -116,10 +126,10 @@ func processFile(f *os.File) os.Error {
if
!
bytes
.
Equal
(
src
,
res
)
{
// formatting has changed
if
*
list
{
fmt
.
Fprintln
(
o
s
.
Stdout
,
f
.
Name
()
)
fmt
.
Fprintln
(
o
ut
,
filename
)
}
if
*
write
{
err
=
ioutil
.
WriteFile
(
f
.
Name
()
,
res
,
0
)
err
=
ioutil
.
WriteFile
(
f
ilename
,
res
,
0
)
if
err
!=
nil
{
return
err
}
...
...
@@ -127,23 +137,13 @@ func processFile(f *os.File) os.Error {
}
if
!*
list
&&
!*
write
{
_
,
err
=
o
s
.
Stdo
ut
.
Write
(
res
)
_
,
err
=
out
.
Write
(
res
)
}
return
err
}
func
processFileByName
(
filename
string
)
os
.
Error
{
file
,
err
:=
os
.
Open
(
filename
)
if
err
!=
nil
{
return
err
}
defer
file
.
Close
()
return
processFile
(
file
)
}
type
fileVisitor
chan
os
.
Error
func
(
v
fileVisitor
)
VisitDir
(
path
string
,
f
*
os
.
FileInfo
)
bool
{
...
...
@@ -154,7 +154,7 @@ func (v fileVisitor) VisitDir(path string, f *os.FileInfo) bool {
func
(
v
fileVisitor
)
VisitFile
(
path
string
,
f
*
os
.
FileInfo
)
{
if
isGoFile
(
f
)
{
v
<-
nil
// synchronize error handler
if
err
:=
processFile
ByName
(
path
);
err
!=
nil
{
if
err
:=
processFile
(
path
,
nil
,
os
.
Stdout
);
err
!=
nil
{
v
<-
err
}
}
...
...
@@ -210,9 +210,10 @@ func gofmtMain() {
initRewrite
()
if
flag
.
NArg
()
==
0
{
if
err
:=
processFile
(
os
.
Stdin
);
err
!=
nil
{
if
err
:=
processFile
(
"<standard input>"
,
os
.
Stdin
,
os
.
Stdout
);
err
!=
nil
{
report
(
err
)
}
return
}
for
i
:=
0
;
i
<
flag
.
NArg
();
i
++
{
...
...
@@ -221,7 +222,7 @@ func gofmtMain() {
case
err
!=
nil
:
report
(
err
)
case
dir
.
IsRegular
()
:
if
err
:=
processFile
ByName
(
path
);
err
!=
nil
{
if
err
:=
processFile
(
path
,
nil
,
os
.
Stdout
);
err
!=
nil
{
report
(
err
)
}
case
dir
.
IsDirectory
()
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment