From 2875086717344bbc161f3f3cecbce6751d8ab1dc Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Sat, 10 Aug 2024 20:22:48 +0300 Subject: [PATCH] tools: make `v where` ignore .git/ folders (they contain binary files); let `-dir .` work recursively, so that `-dir vlib` works --- cmd/tools/vwhere/finder.v | 3 +-- cmd/tools/vwhere/finder_utils.v | 9 ++++---- cmd/tools/vwhere/test/file_common.v | 3 +++ .../vwhere/test/nested_mod/nested_file.v | 1 + cmd/tools/vwhere/vwhere_test.v | 21 +++++++++++++++++++ 5 files changed, 30 insertions(+), 7 deletions(-) create mode 100644 cmd/tools/vwhere/test/file_common.v diff --git a/cmd/tools/vwhere/finder.v b/cmd/tools/vwhere/finder.v index 31c272986..2d0e75f04 100644 --- a/cmd/tools/vwhere/finder.v +++ b/cmd/tools/vwhere/finder.v @@ -15,7 +15,7 @@ mut: modul string receiver string dirs []string - matches []Match + matches []Match // all the results are collected here } fn (mut fdr Finder) configure_from_arguments(args []string) { @@ -74,7 +74,6 @@ fn (mut fdr Finder) search_for_matches() { paths_to_search << if fdr.modul == 'main' { current_dir } else { resolve_module(fdr.modul) or { panic(err)} } } else if fdr.dirs.len != 0 && fdr.modul == '' { - recursive = false paths_to_search << fdr.dirs.map(resolve_module(it) or { panic(err) }) } else { recursive = false diff --git a/cmd/tools/vwhere/finder_utils.v b/cmd/tools/vwhere/finder_utils.v index df73463bb..5b257b076 100644 --- a/cmd/tools/vwhere/finder_utils.v +++ b/cmd/tools/vwhere/finder_utils.v @@ -127,12 +127,11 @@ fn collect_v_files(path string, recursive bool) ![]string { } mut all_files := []string{} mut entries := os.ls(path)! - mut local_path_separator := os.path_separator - if path.ends_with(os.path_separator) { - local_path_separator = '' - } for entry in entries { - file := path + local_path_separator + entry + if entry == '.git' { + continue + } + file := os.join_path_single(path, entry) if os.is_dir(file) && !os.is_link(file) && recursive { all_files << collect_v_files(file, recursive)! } else if os.exists(file) && (file.ends_with('.v') || file.ends_with('.vsh')) { diff --git a/cmd/tools/vwhere/test/file_common.v b/cmd/tools/vwhere/test/file_common.v new file mode 100644 index 000000000..087f70600 --- /dev/null +++ b/cmd/tools/vwhere/test/file_common.v @@ -0,0 +1,3 @@ +module test + +const common = 'abcdef' diff --git a/cmd/tools/vwhere/test/nested_mod/nested_file.v b/cmd/tools/vwhere/test/nested_mod/nested_file.v index 1d533ed7b..36d4ceca6 100644 --- a/cmd/tools/vwhere/test/nested_mod/nested_file.v +++ b/cmd/tools/vwhere/test/nested_mod/nested_file.v @@ -3,3 +3,4 @@ module nested_mod pub const a = 30 pub const b = 60 pub const c = 120 +pub const common = 42 diff --git a/cmd/tools/vwhere/vwhere_test.v b/cmd/tools/vwhere/vwhere_test.v index 9e3474701..e72b3c090 100644 --- a/cmd/tools/vwhere/vwhere_test.v +++ b/cmd/tools/vwhere/vwhere_test.v @@ -184,3 +184,24 @@ fn test_find_pub_const_with_mod() { }, ] } + +fn test_find_in_dir_recursive() { + args := ['const', 'common', '-dir', test_dir] + mut fdr := Finder{} + fdr.configure_from_arguments(args) + fdr.search_for_matches() + dump(fdr.matches) + + assert fdr.matches == [ + Match{ + path: os.join_path_single(test_dir, 'file_common.v') + line: 3 + text: "const common = 'abcdef'" + }, + Match{ + path: os.join_path_single(test_dir, 'nested_mod/nested_file.v') + line: 6 + text: 'pub const common = 42' + }, + ] +} -- 2.39.5