mirror of
https://github.com/vlang/v.git
synced 2025-09-13 22:42:26 +03:00
tools: cleanup vwhere (#21350)
This commit is contained in:
parent
32f7ec8cea
commit
903a585e9e
3 changed files with 80 additions and 117 deletions
|
@ -1,7 +1,7 @@
|
||||||
module main
|
module main
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import term
|
import term { blue, bright_cyan, bright_green, bright_magenta, bright_red, bright_yellow, colorize }
|
||||||
import regex
|
import regex
|
||||||
import os.cmdline
|
import os.cmdline
|
||||||
|
|
||||||
|
@ -19,41 +19,45 @@ mut:
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mut fdr Finder) configure_from_arguments(args []string) {
|
fn (mut fdr Finder) configure_from_arguments(args []string) {
|
||||||
match args.len {
|
if args.len == 1 {
|
||||||
1 {
|
fdr.name = args[0]
|
||||||
fdr.name = args[0]
|
return
|
||||||
}
|
|
||||||
else {
|
|
||||||
fdr.symbol.set_from_str(args[0])
|
|
||||||
if fdr.symbol == .method && !args[1].contains('.') {
|
|
||||||
make_and_print_error('method require a special notation:', [
|
|
||||||
'Receiver.method',
|
|
||||||
], '${args[1]}')
|
|
||||||
} else if fdr.symbol == .method {
|
|
||||||
temp_args := args[1].split('.')
|
|
||||||
fdr.receiver = temp_args[0]
|
|
||||||
fdr.name = temp_args[1]
|
|
||||||
} else {
|
|
||||||
fdr.name = args[1]
|
|
||||||
}
|
|
||||||
if fdr.name.contains('-') {
|
|
||||||
make_and_print_error('It seems you forgot positional arg name:', [], fdr.name)
|
|
||||||
}
|
|
||||||
fdr.visib.set_from_str(cmdline.option(args, '-vis', '${Visibility.all}'))
|
|
||||||
if fdr.symbol == .var && fdr.visib != .all {
|
|
||||||
make_and_print_error('-vis ${fdr.visib} just can be set with symbol_type:',
|
|
||||||
['fn', 'method', 'const', 'struct', 'enum', 'interface', 'regexp'],
|
|
||||||
'${fdr.symbol}')
|
|
||||||
}
|
|
||||||
fdr.mutab.set_from_str(cmdline.option(args, '-mut', '${Mutability.any}'))
|
|
||||||
if fdr.symbol != .var && fdr.mutab != .any {
|
|
||||||
make_and_print_error('-mut ${fdr.mutab} just can be set with symbol_type:',
|
|
||||||
['var'], '${fdr.symbol}')
|
|
||||||
}
|
|
||||||
fdr.modul = cmdline.option(args, '-mod', '')
|
|
||||||
fdr.dirs = cmdline.options(args, '-dir')
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
fdr.symbol.set_from_str(args[0])
|
||||||
|
if fdr.symbol == .method && !args[1].contains('.') {
|
||||||
|
make_and_print_error('method require a special notation:', [
|
||||||
|
'Receiver.method',
|
||||||
|
], '${args[1]}')
|
||||||
|
} else if fdr.symbol == .method {
|
||||||
|
temp_args := args[1].split('.')
|
||||||
|
fdr.receiver = temp_args[0]
|
||||||
|
fdr.name = temp_args[1]
|
||||||
|
} else {
|
||||||
|
fdr.name = args[1]
|
||||||
|
}
|
||||||
|
if fdr.name.contains('-') {
|
||||||
|
make_and_print_error('It seems you forgot positional arg name:', [], fdr.name)
|
||||||
|
}
|
||||||
|
fdr.visib.set_from_str(cmdline.option(args, '-vis', '${Visibility.all}'))
|
||||||
|
if fdr.symbol == .var && fdr.visib != .all {
|
||||||
|
make_and_print_error('-vis ${fdr.visib} just can be set with symbol_type:', [
|
||||||
|
'fn',
|
||||||
|
'method',
|
||||||
|
'const',
|
||||||
|
'struct',
|
||||||
|
'enum',
|
||||||
|
'interface',
|
||||||
|
'regexp',
|
||||||
|
], '${fdr.symbol}')
|
||||||
|
}
|
||||||
|
fdr.mutab.set_from_str(cmdline.option(args, '-mut', '${Mutability.any}'))
|
||||||
|
if fdr.symbol != .var && fdr.mutab != .any {
|
||||||
|
make_and_print_error('-mut ${fdr.mutab} just can be set with symbol_type:', [
|
||||||
|
'var',
|
||||||
|
], '${fdr.symbol}')
|
||||||
|
}
|
||||||
|
fdr.modul = cmdline.option(args, '-mod', '')
|
||||||
|
fdr.dirs = cmdline.options(args, '-dir')
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mut fdr Finder) search_for_matches() {
|
fn (mut fdr Finder) search_for_matches() {
|
||||||
|
@ -78,16 +82,13 @@ fn (mut fdr Finder) search_for_matches() {
|
||||||
panic(err)} }
|
panic(err)} }
|
||||||
paths_to_search << fdr.dirs.map(resolve_module(it) or { panic(err) })
|
paths_to_search << fdr.dirs.map(resolve_module(it) or { panic(err) })
|
||||||
}
|
}
|
||||||
// for p in paths_to_search {
|
|
||||||
// println(p)
|
// dump(paths_to_search)
|
||||||
// }
|
|
||||||
mut files_to_search := []string{}
|
mut files_to_search := []string{}
|
||||||
for path in paths_to_search {
|
for p in paths_to_search {
|
||||||
files_to_search << collect_v_files(path, recursive) or { panic(err) }
|
files_to_search << collect_v_files(p, recursive) or { panic(err) }
|
||||||
}
|
}
|
||||||
// for f in files_to_search {
|
// dump(files_to_search)
|
||||||
// println(f)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// Auxiliary rgx
|
// Auxiliary rgx
|
||||||
sp := r'\s*'
|
sp := r'\s*'
|
||||||
|
@ -104,26 +105,14 @@ fn (mut fdr Finder) search_for_matches() {
|
||||||
na := '${fdr.name}'
|
na := '${fdr.name}'
|
||||||
|
|
||||||
query := match fdr.symbol {
|
query := match fdr.symbol {
|
||||||
.@fn {
|
.@fn { '.*${sy}${sp}${na}${sp}${op}.*${cp}.*' }
|
||||||
'.*${sy}${sp}${na}${sp}${op}.*${cp}.*'
|
.method { '.*fn${st}${na}${sp}${op}.*${cp}.*' }
|
||||||
}
|
.var { '.*${na}${sp}:=.*' }
|
||||||
.method {
|
.@const { '.*${na}${sp} = .*' }
|
||||||
'.*fn${st}${na}${sp}${op}.*${cp}.*'
|
.regexp { '${na}' }
|
||||||
}
|
else { '.*${sy}${sp}${na}${sp}.*' } // struct, enum, interface
|
||||||
.var {
|
|
||||||
'.*${na}${sp}:=.*'
|
|
||||||
}
|
|
||||||
.@const {
|
|
||||||
'.*${na}${sp} = .*'
|
|
||||||
}
|
|
||||||
.regexp {
|
|
||||||
'${na}'
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
'.*${sy}${sp}${na}${sp}.*' // for struct, enum and interface
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// println(query)
|
// dump(query)
|
||||||
for file in files_to_search {
|
for file in files_to_search {
|
||||||
fdr.search_within_file(file, query)
|
fdr.search_within_file(file, query)
|
||||||
}
|
}
|
||||||
|
@ -192,10 +181,10 @@ fn (mut fdr Finder) search_within_file(file string, query string) {
|
||||||
fn (fdr Finder) show_results() {
|
fn (fdr Finder) show_results() {
|
||||||
if fdr.matches.len < 1 && (verbose || header) {
|
if fdr.matches.len < 1 && (verbose || header) {
|
||||||
print(fdr)
|
print(fdr)
|
||||||
println(maybe_color(term.bright_yellow, 'No Matches found'))
|
println(colorize(bright_yellow, 'No Matches found'))
|
||||||
} else if verbose || header {
|
} else if verbose || header {
|
||||||
print(fdr)
|
print(fdr)
|
||||||
println(maybe_color(term.bright_green, '${fdr.matches.len} matches Found\n'))
|
println(colorize(bright_green, '${fdr.matches.len} matches Found\n'))
|
||||||
for result in fdr.matches {
|
for result in fdr.matches {
|
||||||
result.show()
|
result.show()
|
||||||
}
|
}
|
||||||
|
@ -207,15 +196,15 @@ fn (fdr Finder) show_results() {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (fdr Finder) str() string {
|
fn (fdr Finder) str() string {
|
||||||
v := maybe_color(term.bright_red, '${fdr.visib}')
|
v := colorize(bright_red, '${fdr.visib}')
|
||||||
m := maybe_color(term.bright_red, '${fdr.mutab}')
|
m := colorize(bright_red, '${fdr.mutab}')
|
||||||
st := if fdr.receiver != '' { ' ( _ ${fdr.receiver})' } else { '' }
|
st := if fdr.receiver != '' { ' ( _ ${fdr.receiver})' } else { '' }
|
||||||
s := maybe_color(term.bright_magenta, '${fdr.symbol}')
|
s := colorize(bright_magenta, '${fdr.symbol}')
|
||||||
n := maybe_color(term.bright_cyan, '${fdr.name}')
|
n := colorize(bright_cyan, '${fdr.name}')
|
||||||
|
|
||||||
mm := if fdr.modul != '' { maybe_color(term.blue, '${fdr.modul}') } else { '' }
|
mm := if fdr.modul != '' { colorize(blue, '${fdr.modul}') } else { '' }
|
||||||
dd := if fdr.dirs.len != 0 {
|
dd := if fdr.dirs.len != 0 {
|
||||||
fdr.dirs.map(maybe_color(term.blue, it))
|
fdr.dirs.map(colorize(blue, it))
|
||||||
} else {
|
} else {
|
||||||
fdr.dirs
|
fdr.dirs
|
||||||
}
|
}
|
||||||
|
@ -241,9 +230,9 @@ struct Match {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mtc Match) show() {
|
fn (mtc Match) show() {
|
||||||
path := maybe_color(term.bright_magenta, mtc.path)
|
path := colorize(bright_magenta, mtc.path)
|
||||||
line := maybe_color(term.bright_yellow, '${mtc.line}')
|
line := colorize(bright_yellow, '${mtc.line}')
|
||||||
text := maybe_color(term.bright_green, '${mtc.text}')
|
text := colorize(bright_green, '${mtc.text}')
|
||||||
if verbose || format {
|
if verbose || format {
|
||||||
println('${path}\n${line} : [ ${text} ]\n')
|
println('${path}\n${line} : [ ${text} ]\n')
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
module main
|
module main
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import term
|
import term { bright_green, bright_red, bright_yellow, ecolorize }
|
||||||
import os.cmdline
|
import os.cmdline
|
||||||
|
|
||||||
// Symbol type to search
|
// Symbol type to search
|
||||||
|
@ -59,7 +59,6 @@ const vlib_dir = os.join_path(os.dir(vexe), 'vlib')
|
||||||
const vmod_dir = os.vmodules_dir()
|
const vmod_dir = os.vmodules_dir()
|
||||||
const vmod_paths = os.vmodules_paths()[1..]
|
const vmod_paths = os.vmodules_paths()[1..]
|
||||||
const current_dir = os.abs_path('.')
|
const current_dir = os.abs_path('.')
|
||||||
const color_out = term.can_show_color_on_stdout()
|
|
||||||
|
|
||||||
fn (mut cfg Symbol) set_from_str(str_in string) {
|
fn (mut cfg Symbol) set_from_str(str_in string) {
|
||||||
if str_in !in symbols {
|
if str_in !in symbols {
|
||||||
|
@ -105,49 +104,26 @@ fn invalid_option(invalid ParamOption, arg string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn valid_args_quantity_or_show_help(args []string) {
|
|
||||||
if true in [
|
|
||||||
(args.len < 1),
|
|
||||||
'-help' in args,
|
|
||||||
'--help' in args,
|
|
||||||
args == ['help'],
|
|
||||||
] {
|
|
||||||
os.system('${os.quoted_path(vexe)} help where')
|
|
||||||
exit(0)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn make_and_print_error(msg string, opts []string, arg string) {
|
fn make_and_print_error(msg string, opts []string, arg string) {
|
||||||
if verbose || format {
|
if verbose || format {
|
||||||
eprintln('\n' + maybe_color(term.bright_yellow, msg))
|
eprintln('\n' + ecolorize(bright_yellow, msg))
|
||||||
if opts.len > 0 {
|
if opts.len > 0 {
|
||||||
eprint(opts.map(maybe_color(term.bright_green, it)).join(' | '))
|
eprint(opts.map(ecolorize(bright_green, it)).join(' | '))
|
||||||
}
|
}
|
||||||
eprintln(' ...can not be ${maybe_color(term.bright_red, arg)}')
|
eprintln(' ...can not be ${ecolorize(bright_red, arg)}')
|
||||||
} else {
|
} else {
|
||||||
eprint(maybe_color(term.bright_yellow, msg) + ' ')
|
eprint(ecolorize(bright_yellow, msg) + ' ')
|
||||||
if opts.len > 0 {
|
if opts.len > 0 {
|
||||||
eprint(opts.map(maybe_color(term.bright_green, it)).join(' | '))
|
eprint(opts.map(ecolorize(bright_green, it)).join(' | '))
|
||||||
}
|
}
|
||||||
eprintln(' ...can not be ${maybe_color(term.bright_red, arg)}')
|
eprintln(' ...can not be ${ecolorize(bright_red, arg)}')
|
||||||
}
|
}
|
||||||
exit(1)
|
exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn maybe_color(term_color fn (string) string, str string) string {
|
|
||||||
if color_out {
|
|
||||||
return term_color(str)
|
|
||||||
} else {
|
|
||||||
return str
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn collect_v_files(path string, recursive bool) ![]string {
|
fn collect_v_files(path string, recursive bool) ![]string {
|
||||||
if path == '' {
|
if path == '' || !os.is_dir(path) {
|
||||||
return error('path cannot be empty')
|
return error('path `${path}` does not exist or is not a directory')
|
||||||
}
|
|
||||||
if !os.is_dir(path) {
|
|
||||||
return error('path does not exist or is not a directory')
|
|
||||||
}
|
}
|
||||||
mut all_files := []string{}
|
mut all_files := []string{}
|
||||||
mut entries := os.ls(path)!
|
mut entries := os.ls(path)!
|
||||||
|
@ -167,13 +143,10 @@ fn collect_v_files(path string, recursive bool) ![]string {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn resolve_module(path string) !string {
|
fn resolve_module(path string) !string {
|
||||||
if os.is_dir(path) {
|
return match true {
|
||||||
return path
|
os.is_dir(path) { path }
|
||||||
} else if os.is_dir(os.join_path(vmod_dir, path)) {
|
os.is_dir(os.join_path(vmod_dir, path)) { os.join_path(vmod_dir, path) }
|
||||||
return os.join_path(vmod_dir, path)
|
os.is_dir(os.join_path(vlib_dir, path)) { os.join_path(vlib_dir, path) }
|
||||||
} else if os.is_dir(os.join_path(vlib_dir, path)) {
|
else { error('Path: ${path} not found') }
|
||||||
return os.join_path(vlib_dir, path)
|
|
||||||
} else {
|
|
||||||
return error('Path: ${path} not found')
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,9 +4,10 @@ import os
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
args := os.args[2..]
|
args := os.args[2..]
|
||||||
|
if args.len == 0 || args == ['help'] || '-help' in args || '--help' in args {
|
||||||
valid_args_quantity_or_show_help(args)
|
os.system('${os.quoted_path(vexe)} help where')
|
||||||
|
exit(0)
|
||||||
|
}
|
||||||
mut fdr := Finder{}
|
mut fdr := Finder{}
|
||||||
fdr.configure_from_arguments(args)
|
fdr.configure_from_arguments(args)
|
||||||
fdr.search_for_matches()
|
fdr.search_for_matches()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue