From 739730b9d84038dd88688c835f62c41ccfaa2a7c Mon Sep 17 00:00:00 2001 From: Turiiya <34311583+ttytm@users.noreply.github.com> Date: Fri, 12 Apr 2024 09:35:01 +0200 Subject: [PATCH] vdoc: fix sorting of docnodes on other than linux, enable tests (#21253) --- cmd/tools/vtest-self.v | 1 - vlib/v/doc/doc.v | 3 +-- vlib/v/doc/node.v | 42 +++++++++++++++++++++--------------------- 3 files changed, 22 insertions(+), 24 deletions(-) diff --git a/cmd/tools/vtest-self.v b/cmd/tools/vtest-self.v index e374b8c861eca3..f0fdb68cd870b9 100644 --- a/cmd/tools/vtest-self.v +++ b/cmd/tools/vtest-self.v @@ -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', diff --git a/vlib/v/doc/doc.v b/vlib/v/doc/doc.v index 652010775c6e9d..f478d04781102d 100644 --- a/vlib/v/doc/doc.v +++ b/vlib/v/doc/doc.v @@ -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() } } } diff --git a/vlib/v/doc/node.v b/vlib/v/doc/node.v index a6beb707af2d3d..c3f6a5d0597a60 100644 --- a/vlib/v/doc/node.v +++ b/vlib/v/doc/node.v @@ -13,23 +13,30 @@ 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 } @@ -37,17 +44,10 @@ fn compare_nodes_by_kind(a &DocNode, b &DocNode) int { } } -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 }