diff --git a/LICENSE b/LICENSE index f166619..5ae193c 100644 --- a/LICENSE +++ b/LICENSE @@ -1,5 +1,3 @@ -go-outline - The MIT License (MIT) Copyright (c) Microsoft Corporation @@ -20,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. \ No newline at end of file +SOFTWARE. diff --git a/README.md b/README.md index 6b76df9..43e86d0 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Go source file. ## Installing ```bash -go get -u github.com/lukehoban/go-outline +go get -u github.com/ramya-rao-a/go-outline ``` ## Using @@ -20,6 +20,14 @@ To parse and return only imports > go-outline -f file.go -imports-only ``` +To parse unsaved file contents, use the `-modified` flag along with the `-f` flag and write an archive to stdin. +File in the archive will be preferred over the one on disk. + +The archive entry consists of: + - the file name, followed by a newline + - the (decimal) file size, followed by a newline + - the contents of the file + ### Schema ```go type Declaration struct { diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..7038c5d --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/ramya-rao-a/go-outline + +go 1.16 + +require golang.org/x/tools v0.1.1 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..9ba29a2 --- /dev/null +++ b/go.sum @@ -0,0 +1,27 @@ +github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo= +golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210510120138-977fb7262007 h1:gG67DSER+11cZvqIMb8S8bt0vZtiN6xWYARwirrOSfE= +golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.1 h1:wGiQel/hW0NnEkJUk8lbzkX2gFJU6PFxf1v5OlCfuOs= +golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/main.go b/main.go index 4ef56c9..7374b62 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,8 @@ import ( "go/parser" "go/token" "os" + + "golang.org/x/tools/go/buildutil" ) type Declaration struct { @@ -24,7 +26,7 @@ type Declaration struct { var ( file = flag.String("f", "", "the path to the file to outline") importsOnly = flag.Bool("imports-only", false, "parse imports only") - src = flag.String("src", "", "source code of the file to outline") + modified = flag.Bool("modified", false, "read an archive of the modified file from standard input") ) func main() { @@ -38,8 +40,16 @@ func main() { var fileAst *ast.File var err error - if len(*src) > 0 { - fileAst, err = parser.ParseFile(fset, *file, *src, parserMode) + if *modified == true { + archive, err := buildutil.ParseOverlayArchive(os.Stdin) + if err != nil { + reportError(fmt.Errorf("failed to parse -modified archive: %v", err)) + } + fc, ok := archive[*file] + if !ok { + reportError(fmt.Errorf("couldn't find %s in archive", *file)) + } + fileAst, err = parser.ParseFile(fset, *file, fc, parserMode) } else { fileAst, err = parser.ParseFile(fset, *file, nil, parserMode) } @@ -89,9 +99,13 @@ func main() { }) case *ast.ValueSpec: for _, id := range spec.Names { + varOrConst := "variable" + if decl.Tok == token.CONST { + varOrConst = "constant" + } declarations = append(declarations, Declaration{ id.Name, - "variable", + varOrConst, "", id.Pos(), id.End(), @@ -103,7 +117,7 @@ func main() { } } default: - reportError(fmt.Errorf("Unknown declaration @", decl.Pos())) + reportError(fmt.Errorf("Unknown declaration @ %v", decl.Pos())) } }