LEFT | RIGHT |
(no file at all) | |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // This file contains the infrastructure to create an | 5 // This file contains the infrastructure to create an |
6 // identifier and full-text index for a set of Go files. | 6 // identifier and full-text index for a set of Go files. |
7 // | 7 // |
8 // Algorithm for identifier index: | 8 // Algorithm for identifier index: |
9 // - traverse all .go files of the file tree specified by root | 9 // - traverse all .go files of the file tree specified by root |
10 // - for each word (identifier) encountered, collect all occurrences (spots) | 10 // - for each word (identifier) encountered, collect all occurrences (spots) |
(...skipping 27 matching lines...) Expand all Loading... |
38 package main | 38 package main |
39 | 39 |
40 import ( | 40 import ( |
41 "bytes" | 41 "bytes" |
42 "container/vector" | 42 "container/vector" |
43 "go/ast" | 43 "go/ast" |
44 "go/parser" | 44 "go/parser" |
45 "go/token" | 45 "go/token" |
46 "go/scanner" | 46 "go/scanner" |
47 "index/suffixarray" | 47 "index/suffixarray" |
48 "io/ioutil" | |
49 "os" | 48 "os" |
50 "path/filepath" | 49 "path/filepath" |
51 "regexp" | 50 "regexp" |
52 "sort" | 51 "sort" |
53 "strings" | 52 "strings" |
54 ) | 53 ) |
55 | 54 |
56 | 55 |
57 // ---------------------------------------------------------------------------- | 56 // ---------------------------------------------------------------------------- |
58 // RunList | 57 // RunList |
(...skipping 558 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
617 } | 616 } |
618 return file.Name.Name | 617 return file.Name.Name |
619 } | 618 } |
620 | 619 |
621 | 620 |
622 // addFile adds a file to the index if possible and returns the file set file | 621 // addFile adds a file to the index if possible and returns the file set file |
623 // and the file's AST if it was successfully parsed as a Go file. If addFile | 622 // and the file's AST if it was successfully parsed as a Go file. If addFile |
624 // failed (that is, if the file was not added), it returns file == nil. | 623 // failed (that is, if the file was not added), it returns file == nil. |
625 func (x *Indexer) addFile(filename string, goFile bool) (file *token.File, ast *
ast.File) { | 624 func (x *Indexer) addFile(filename string, goFile bool) (file *token.File, ast *
ast.File) { |
626 // open file | 625 // open file |
627 » f, err := os.Open(filename) | 626 » f, err := fs.Open(filename) |
628 if err != nil { | 627 if err != nil { |
629 return | 628 return |
630 } | 629 } |
631 defer f.Close() | 630 defer f.Close() |
632 | 631 |
633 // The file set's base offset and x.sources size must be in lock-step; | 632 // The file set's base offset and x.sources size must be in lock-step; |
634 // this permits the direct mapping of suffix array lookup results to | 633 // this permits the direct mapping of suffix array lookup results to |
635 // to corresponding Pos values. | 634 // to corresponding Pos values. |
636 // | 635 // |
637 // When a file is added to the file set, it's offset base increases by | 636 // When a file is added to the file set, it's offset base increases by |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
720 func isWhitelisted(filename string) bool { | 719 func isWhitelisted(filename string) bool { |
721 key := filepath.Ext(filename) | 720 key := filepath.Ext(filename) |
722 if key == "" { | 721 if key == "" { |
723 // file has no extension - use entire filename | 722 // file has no extension - use entire filename |
724 key = filename | 723 key = filename |
725 } | 724 } |
726 return whitelisted[key] | 725 return whitelisted[key] |
727 } | 726 } |
728 | 727 |
729 | 728 |
730 func (x *Indexer) visitFile(dirname string, f *os.FileInfo, fulltextIndex bool)
{ | 729 func (x *Indexer) visitFile(dirname string, f FileInfo, fulltextIndex bool) { |
731 if !f.IsRegular() { | 730 if !f.IsRegular() { |
732 return | 731 return |
733 } | 732 } |
734 | 733 |
735 » filename := filepath.Join(dirname, f.Name) | 734 » filename := filepath.Join(dirname, f.Name()) |
736 goFile := false | 735 goFile := false |
737 | 736 |
738 switch { | 737 switch { |
739 case isGoFile(f): | 738 case isGoFile(f): |
740 if !includeTestFiles && (!isPkgFile(f) || strings.HasPrefix(file
name, "test/")) { | 739 if !includeTestFiles && (!isPkgFile(f) || strings.HasPrefix(file
name, "test/")) { |
741 return | 740 return |
742 } | 741 } |
743 if !includeMainPackages && pkgName(filename) == "main" { | 742 if !includeMainPackages && pkgName(filename) == "main" { |
744 return | 743 return |
745 } | 744 } |
746 goFile = true | 745 goFile = true |
747 | 746 |
748 » case !fulltextIndex || !isWhitelisted(f.Name): | 747 » case !fulltextIndex || !isWhitelisted(f.Name()): |
749 return | 748 return |
750 } | 749 } |
751 | 750 |
752 file, fast := x.addFile(filename, goFile) | 751 file, fast := x.addFile(filename, goFile) |
753 if file == nil { | 752 if file == nil { |
754 return // addFile failed | 753 return // addFile failed |
755 } | 754 } |
756 | 755 |
757 if fast != nil { | 756 if fast != nil { |
758 // we've got a Go file to index | 757 // we've got a Go file to index |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
797 // | 796 // |
798 func NewIndex(dirnames <-chan string, fulltextIndex bool) *Index { | 797 func NewIndex(dirnames <-chan string, fulltextIndex bool) *Index { |
799 var x Indexer | 798 var x Indexer |
800 | 799 |
801 // initialize Indexer | 800 // initialize Indexer |
802 x.fset = token.NewFileSet() | 801 x.fset = token.NewFileSet() |
803 x.words = make(map[string]*IndexResult) | 802 x.words = make(map[string]*IndexResult) |
804 | 803 |
805 // index all files in the directories given by dirnames | 804 // index all files in the directories given by dirnames |
806 for dirname := range dirnames { | 805 for dirname := range dirnames { |
807 » » list, err := ioutil.ReadDir(dirname) | 806 » » list, err := fs.ReadDir(dirname) |
808 if err != nil { | 807 if err != nil { |
809 continue // ignore this directory | 808 continue // ignore this directory |
810 } | 809 } |
811 for _, f := range list { | 810 for _, f := range list { |
812 if !f.IsDirectory() { | 811 if !f.IsDirectory() { |
813 x.visitFile(dirname, f, fulltextIndex) | 812 x.visitFile(dirname, f, fulltextIndex) |
814 } | 813 } |
815 } | 814 } |
816 } | 815 } |
817 | 816 |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1034 if m.filename != last { | 1033 if m.filename != last { |
1035 addLines() | 1034 addLines() |
1036 last = m.filename | 1035 last = m.filename |
1037 } | 1036 } |
1038 lines = append(lines, m.line) | 1037 lines = append(lines, m.line) |
1039 } | 1038 } |
1040 addLines() | 1039 addLines() |
1041 | 1040 |
1042 return | 1041 return |
1043 } | 1042 } |
LEFT | RIGHT |