Skip to content

Commit

Permalink
vdoc: fix sorting of docnodes on other than linux, enable tests (#21253)
Browse files Browse the repository at this point in the history
  • Loading branch information
ttytm committed Apr 12, 2024
1 parent 313c3bc commit 739730b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 24 deletions.
1 change: 0 additions & 1 deletion cmd/tools/vtest-self.v
Expand Up @@ -278,7 +278,6 @@ const skip_on_linux = [
]
const skip_on_non_linux = [
'do_not_remove',
'cmd/tools/vdoc/tests/vdoc_file_test.v', // order of output is not as expected
]
const skip_on_windows_msvc = [
'do_not_remove',
Expand Down
3 changes: 1 addition & 2 deletions vlib/v/doc/doc.v
Expand Up @@ -514,8 +514,7 @@ pub fn (mut d Doc) file_asts(mut file_asts []ast.File) ! {
}
if d.contents[name].kind != .none_ || node.kind == .none_ {
d.contents[name].children << node.children
d.contents[name].children.sort_by_name()
d.contents[name].children.sort_by_kind()
d.contents[name].children.arrange()
}
}
}
Expand Down
42 changes: 21 additions & 21 deletions vlib/v/doc/node.v
Expand Up @@ -13,41 +13,41 @@ pub fn (nodes []DocNode) find(symname string) !DocNode {
return error('symbol not found')
}

// sort_by_name sorts the array based on the symbol names.
pub fn (mut nodes []DocNode) sort_by_name() {
if doc.should_sort {
nodes.sort_with_compare(compare_nodes_by_name)
// arrange sorts the DocNodes based on their symbols and names.
pub fn (mut nodes []DocNode) arrange() {
if !doc.should_sort {
return
}
}

// sort_by_kind sorts the array based on the symbol kind.
pub fn (mut nodes []DocNode) sort_by_kind() {
if doc.should_sort {
nodes.sort_with_compare(compare_nodes_by_kind)
mut kinds := []SymbolKind{}
for v in nodes {
if v.kind !in kinds {
kinds << v.kind
}
}
kinds.sort_with_compare(compare_sym_kinds)
mut res := []DocNode{}
for k in kinds {
mut kind_nodes := nodes.filter(it.kind == k)
kind_nodes.sort(a.name < b.name)
res << kind_nodes
}
nodes = res.clone()
}

fn compare_nodes_by_kind(a &DocNode, b &DocNode) int {
ak := int(a.kind)
bk := int(b.kind)
fn compare_sym_kinds(a &SymbolKind, b &SymbolKind) int {
ak := int(*a)
bk := int(*b)
return match true {
ak < bk { -1 }
ak > bk { 1 }
else { 0 }
}
}

fn compare_nodes_by_name(a &DocNode, b &DocNode) int {
al := a.name.to_lower()
bl := b.name.to_lower()
return compare_strings(al, bl)
}

// arr() converts the map into an array of `DocNode`.
pub fn (cnts map[string]DocNode) arr() []DocNode {
mut contents := cnts.values()
contents.sort_by_name()
contents.sort_by_kind()
contents.arrange()
return contents
}

Expand Down

0 comments on commit 739730b

Please sign in to comment.