1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cmd/tools: vwhere fixes (#15051)

This commit is contained in:
Sebastian Atlántico 2022-07-13 12:04:26 -03:00 committed by GitHub
parent 3f3742122f
commit 47b5d206a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 59 additions and 50 deletions

View File

@ -2,6 +2,7 @@ module main
import os import os
import term import term
import regex
import os.cmdline import os.cmdline
// Finder is entity that contains all the logic // Finder is entity that contains all the logic
@ -89,7 +90,7 @@ fn (mut fdr Finder) search_for_matches() {
'.*$na$sp:=.*' '.*$na$sp:=.*'
} }
.@const { .@const {
'.*$na$sp=.*' '.*$na$sp = .*'
} }
.regexp { .regexp {
'$na' '$na'
@ -99,12 +100,65 @@ fn (mut fdr Finder) search_for_matches() {
} }
} }
// println(query) // println(query)
is_const := fdr.symbol == .@const
for file in files_to_search { for file in files_to_search {
n_line, line := search_within_file(file, query, is_const, fdr.visib, fdr.mutab) fdr.search_within_file(file, query)
if n_line != 0 {
fdr.matches << Match{file, n_line, line}
} }
}
fn (mut fdr Finder) search_within_file(file string, query string) {
mut re := regex.regex_opt(query) or { panic(err) }
lines := os.read_lines(file) or { panic(err) }
mut const_found := if fdr.symbol == .@const { false } else { true }
mut n_line := 1
for line in lines {
match fdr.visib {
.all {
if line.contains('const (') {
const_found = true
}
}
.@pub {
if line.contains('pub const (') {
const_found = true
}
}
.pri {
if line.contains('const (') && !line.contains('pub') {
const_found = true
}
}
}
if re.matches_string(line) && (const_found || line.contains('const')) {
words := line.split(' ').map(it.trim('\t'))
match fdr.visib {
.all {}
.@pub {
if 'pub' !in words {
continue
}
}
.pri {
if 'pub' in words {
continue
}
}
}
match fdr.mutab {
.any {}
.yes {
if 'mut' !in words {
continue
}
}
.not {
if 'mut' in words {
continue
}
}
}
fdr.matches << Match{file, n_line, line.replace(' {', '').trim('\t')}
}
n_line++
} }
} }

View File

@ -2,7 +2,6 @@ module main
import os import os
import term import term
import regex
import v.pref import v.pref
import os.cmdline import os.cmdline
@ -159,47 +158,3 @@ fn collect_v_files(path string, recursive bool) ?[]string {
} }
return all_files return all_files
} }
fn search_within_file(file string, query string, is_const bool, v Visibility, m Mutability) (int, string) {
mut re := regex.regex_opt(query) or { panic(err) }
lines := os.read_lines(file) or { panic(err) }
mut const_found := if is_const { false } else { true }
mut n_line := 1
for line in lines {
if line.starts_with('const') {
const_found = true
}
if re.matches_string(line) && const_found {
words := line.split(' ').map(it.trim('\t'))
match v {
.all {}
.@pub {
if 'pub' !in words {
continue
}
}
.pri {
if 'pub' in words {
continue
}
}
}
match m {
.any {}
.yes {
if 'mut' !in words {
continue
}
}
.not {
if 'mut' in words {
continue
}
}
}
return n_line, line.replace(' {', '')
}
n_line++
}
return 0, ''
}