Skip to content

Commit

Permalink
Find All looks in *all* files regardless of open status; Open looks i…
Browse files Browse the repository at this point in the history
…n open
  • Loading branch information
rcoreilly committed Jan 21, 2024
1 parent f42d922 commit 77a35e6
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 21 deletions.
34 changes: 19 additions & 15 deletions code/code/enumgen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

87 changes: 84 additions & 3 deletions code/code/filetree.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package code

import (
"fmt"
"io/fs"
"log"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -247,15 +248,17 @@ type FileSearchResults struct {
}

// FileTreeSearch returns list of all nodes starting at given node of given
// language(s) that contain the given string (non regexp version), sorted in
// descending order by number of occurrences -- ignoreCase transforms
// everything into lowercase
// language(s) that contain the given string, sorted in descending order by number
// of occurrences -- ignoreCase transforms everything into lowercase
func FileTreeSearch(start *filetree.Node, find string, ignoreCase, regExp bool, loc FindLoc, activeDir string, langs []fi.Known) []FileSearchResults {
fb := []byte(find)
fsz := len(find)
if fsz == 0 {
return nil
}
if loc == FindLocAll {
return FindAll(start, find, ignoreCase, regExp, langs)
}
var re *regexp.Regexp
var err error
if regExp {
Expand Down Expand Up @@ -324,6 +327,84 @@ func FileTreeSearch(start *filetree.Node, find string, ignoreCase, regExp bool,
return mls
}

// FindAll returns list of all files (regardless of what is currently open)
// starting at given node of given language(s) that contain the given string,
// sorted in descending order by number of occurrences. ignoreCase transforms
// everything into lowercase.
func FindAll(start *filetree.Node, find string, ignoreCase, regExp bool, langs []fi.Known) []FileSearchResults {
fb := []byte(find)
fsz := len(find)
if fsz == 0 {
return nil
}
var re *regexp.Regexp
var err error
if regExp {
re, err = regexp.Compile(find)
if err != nil {
log.Println(err)
return nil
}
}
mls := make([]FileSearchResults, 0)
spath := string(start.FPath) // note: is already Abs
filepath.Walk(spath, func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
if info.Name() == ".git" {
return filepath.SkipDir
}
if info.IsDir() {
return nil
}
if int(info.Size()) > gi.SystemSettings.BigFileSize {

Check failure on line 361 in code/code/filetree.go

View workflow job for this annotation

GitHub Actions / build

gi.SystemSettings.BigFileSize undefined (type *gi.SystemSettingsData has no field or method BigFileSize)
return nil
}
if strings.HasSuffix(info.Name(), ".code") { // exclude self
return nil
}
if len(langs) > 0 {
mtyp, _, err := fi.MimeFromFile(path)
if err != nil {
return nil
}
known := fi.MimeKnown(mtyp)
if !fi.IsMatchList(langs, known) {
return nil
}
}
sfn, found := start.FindFile(path)
var cnt int
var matches []textbuf.Match
if found && sfn.IsOpen() && sfn.Buf != nil {
if regExp {
cnt, matches = sfn.Buf.SearchRegexp(re)
} else {
cnt, matches = sfn.Buf.Search(fb, ignoreCase, false)
}
} else {
if regExp {
cnt, matches = textbuf.SearchFileRegexp(path, re)
} else {
cnt, matches = textbuf.SearchFile(path, fb, ignoreCase)
}
}
if cnt > 0 {
if found {
mls = append(mls, FileSearchResults{sfn, cnt, matches})
} else {
fmt.Println("file not found in FindFile:", path)
}
}
return nil
})
sort.Slice(mls, func(i, j int) bool {
return mls[i].Count > mls[j].Count
})
return mls
}

// EditFiles calls EditFile on selected files
func (fn *FileNode) EditFiles() { //gti:add
sels := fn.SelectedViews()
Expand Down
5 changes: 4 additions & 1 deletion code/code/findview.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ import (
type FindLoc int32 //enums:enum -trim-prefix FindLoc

const (
// FindLocAll finds in all open folders in the left file browser
// FindLocAll finds in all directories under the root path
FindLocAll FindLoc = iota

// FindOpen finds in all open folders in the left file browser
FindLocOpen

// FindLocFile only finds in the current active file
FindLocFile

Expand Down
18 changes: 18 additions & 0 deletions code/code/gtigen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions code/codev/views.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,10 @@ func (ge *CodeView) UpdateStatusLabel() {
}
}
if tv.ISearch.On {
msg = fmt.Sprintf("\tISearch: %v (n=%v)\t%v", tv.ISearch.Find, len(tv.ISearch.Matches), ge.StatusMessage)
msg = fmt.Sprintf("\tISearch: %v (n=%v)", tv.ISearch.Find, len(tv.ISearch.Matches))
}
if tv.QReplace.On {
msg = fmt.Sprintf("\tQReplace: %v -> %v (n=%v)\t%v", tv.QReplace.Find, tv.QReplace.Replace, len(tv.QReplace.Matches), ge.StatusMessage)
msg = fmt.Sprintf("\tQReplace: %v -> %v (n=%v)", tv.QReplace.Find, tv.QReplace.Replace, len(tv.QReplace.Matches))
}
}

Expand Down

0 comments on commit 77a35e6

Please sign in to comment.